Unread Notifications

Latest Notifications

No Notifications

You're all set! Start a discussion by leaving a comment on a lesson or replying to an existing comment.

Let's Learn AdonisJS 5

Database Migrations To Lucid Models

04:59 Watch
6:08 Read

In this lesson, we'll walk through examples of transferring our database migrations into our Lucid Models. This will pave the way for us to begin CRUD operations in later lessons.

Watch on YouTube
GitHub
View Repository

🧳 In-Video References:

AdonisJS Attachment Lite - Used on Avatar & Thumbnail columns
VSCode Change-Case - Used to convert columns from snake_case to camelCase


🕑 Chapters:

00:00 - Intro
00:15 - The Process / Profile Model
01:10 - Defining Attachment Lite Column
02:05 - State Model
02:20 - Post Model
03:40 - Topic Model
04:00 - Nullable Columns
04:20 - Outro


📜 Transcript:

0:00

now we're going to go ahead and move the

0:01

columns that we defined within our

0:02

migrations in the last lesson over into

0:04

column definitions within our models so

0:06

we can actually start using them for

0:07

crud operations within our application

0:09

so we're going to follow the same

0:10

process that we covered within our model

0:11

introduction lesson to get all of these

0:13

ported over so we're just going to copy

0:14

the column definitions as they are go

0:17

within our model go to where we want

0:19

those to be inserted paste them in

0:20

remember that's Commander control option

0:22

and then down arrow to get us an

0:24

additional cursor then we can do option

0:26

shift and then right arrow to highlight

0:28

whole words so we'll want to just go

0:30

over one whole word set plus some

0:32

additional character to get that period

0:34

cut that out go over another word there

0:35

to get the actual column type we can cut

0:37

that out using command or control X and

0:40

then go over two additional characters

0:41

delete that go over one more word and

0:43

then here let's just go to the end of

0:44

the string and delete the rest of that

0:46

out we can do colon and then paste in

0:48

those types go to the start of the

0:49

string using command or control left

0:51

Arrow type at column hit enter public go

0:54

to the end of the string using command

0:55

or control right arrow and then just hit

0:57

enter to do an additional line break

0:59

loading that last line break out at the

1:01

end there and then we're going to just

1:03

want to convert those database types to

1:05

typescript Safe types so again integer

1:07

is going to be number now for our actual

1:09

Avatar column here we're going to be

1:10

using attachment light which it does

1:12

specify to use Json as the actual

1:14

database column type but whenever it

1:15

comes to the actual model it's going to

1:16

have a specific decorator and column

1:17

type that we need to use if we were to

1:19

dive into the documentation you can see

1:20

that is at attachment and attachment

1:22

contract so what to do is install the

1:24

package so that we have access to this

1:25

decorator and type and then we can

1:26

actually go ahead and utilize them so

1:28

it's going to be npmi at adonisjs slash

1:31

attachment hyphen light and enter on

1:33

that all right cool let's go ahead and

1:34

do node Ace configure at adonisjs slash

1:37

attachment light so now it should be

1:39

configured with our project let's go

1:40

ahead and copy this import list here and

1:43

paste that within this particular model

1:44

and then we can specify the column

1:46

decorator here as attachment and the

1:49

type as attachment contract and the

1:52

attachment light project is actually

1:53

super neat because the way that it's

1:54

going to work is anytime that we query

1:55

our profile the actual Avatar column is

1:58

going to be the reference to the image

1:59

in itself so for an image source all

2:01

that we need to provide is our

2:02

profile.avatar and it will render out

2:04

just fine so our profile should now be

2:05

all set let's go ahead and move on to

2:07

our States now our states just has a

2:09

name I think I can remember that much so

2:10

let's just dive into our state model go

2:12

underneath our ID type at column public

2:14

name of type string and there we go that

2:16

one's all set let's do our post next now

2:18

this one does have a little bit more and

2:20

it also has another attachment light

2:22

column on it so let's go ahead and copy

2:24

all of these and run through that same

2:25

flow so let's dive within our post model

2:27

here go to where we want those inserted

2:29

paste it in command or control option

2:30

down to get all of our cursors there

2:32

option shift right arrow let go of

2:35

option right arrow once more to get that

2:37

dot delete that out highlight that word

2:38

set using option shift right arrow cut

2:40

that so that we have those types

2:42

accessible go over two more characters

2:43

holding down shift delete option right

2:45

arrow option shift right arrow to get

2:47

those word sets there let's go ahead and

2:49

convert those to camelcase oh you know

2:51

what we forgot to do that within our

2:52

profile so we'll need to go back and do

2:54

that then we can just go ahead and note

2:55

that the not nullables stop at the

2:57

description from there on it becomes

2:59

nullable Commander control shift and

3:00

right arrow to highlight the rest of the

3:02

string there hit delete colon paste go

3:05

to the start of the string and column

3:07

enter public enter the Line enter go

3:09

down to the end delete that extra line

3:11

break and so here we have two integers

3:13

we want to just change both of those

3:14

numbers we have text within typescript

3:16

Lane there's no real max length there

3:18

for string so we can just set that to

3:20

string and then Json here we'll want

3:21

that to again just be an attachment of

3:24

type attachment contract and we should

3:25

be all good with our post model here so

3:27

let's jump back into our profile here

3:28

and Yep looks like the only one that

3:30

needs to change here is our user ID just

3:32

change that to camel case instead of uh

3:34

snake case and let's double check our

3:36

state there should be no State's good

3:37

all right cool so next we can move on

3:40

from our post to our topics so let's

3:42

jump down to our topics here copy these

3:44

jump into our topic model paste those in

3:46

and we're just going to follow the exact

3:47

same flow so get our multi-cursors go

3:49

over one word plus one character cut out

3:51

the type over two delete we don't have

3:53

to change any of those to camel case

3:54

again description becomes nullable so we

3:56

can delete those out paste in our types

3:58

go to the start of string at call column

4:00

public and hit enter get rid of the

4:02

additional and we do need or null there

4:04

we also forgot to do that for our post

4:06

so let's jump up to there everything

4:08

after description was nullable so we'll

4:10

specify that or null or null and or null

4:13

in terms of our profile we also have a

4:15

couple of nullables there as well the

4:17

biography or null and our Avatar and I

4:20

should do it now we have all of the

4:21

column definitions defined within our

4:23

models as they're needed to be able to

4:25

work with the actual database structure

4:26

that we've created if you enjoyed this

4:27

lesson or you learned something new

4:28

please consider hitting the like button

4:29

down below and subscribing future

4:30

lessons just like this one we're going

4:32

to be extending on to our relationships

4:34

next and crud work after that and

4:36

hopefully you'll stick around and learn

4:37

a bit with us so thanks for watching

4:38

Everybody I'll see you in the next one

4:40

[Music]

4:44

Comment

  1. Anonymous (MoccasinReba925)
    Commented 3 weeks ago

    Praying for the core team create the wizard soon 🙏

    1. tomgobich
      Commented 3 weeks ago

      If I had to hazard a guess, I'd expect this after the AdonisJS 6 release. They're migrating to ESM in version 6, so releasing something new prior to that change would just add to their to-do list to get version 6 out.

Prepared By

Tom Gobich

Burlington, KY

Owner of Adocasts, JavaScript developer, educator, PlayStation gamer, burrito eater.

Visit Website