🧳 In-Video References:
AdonisJS Attachment Lite - Used on Avatar & Thumbnail columns
VSCode Change-Case - Used to convert columns from snake_case to camelCase
🕑 Chapters:
- The Process / Profile Model
- Defining Attachment Lite Column
- State Model
- Post Model
- Topic Model
- Nullable Columns
- Outro
📜 Transcript:
now we're going to go ahead and move the
columns that we defined within our
migrations in the last lesson over into
column definitions within our models so
we can actually start using them for
crud operations within our application
so we're going to follow the same
process that we covered within our model
introduction lesson to get all of these
ported over so we're just going to copy
the column definitions as they are go
within our model go to where we want
those to be inserted paste them in
remember that's Commander control option
and then down arrow to get us an
additional cursor then we can do option
shift and then right arrow to highlight
whole words so we'll want to just go
over one whole word set plus some
additional character to get that period
cut that out go over another word there
to get the actual column type we can cut
that out using command or control X and
then go over two additional characters
delete that go over one more word and
then here let's just go to the end of
the string and delete the rest of that
out we can do colon and then paste in
those types go to the start of the
string using command or control left
Arrow type at column hit enter public go
to the end of the string using command
or control right arrow and then just hit
enter to do an additional line break
loading that last line break out at the
end there and then we're going to just
want to convert those database types to
typescript Safe types so again integer
is going to be number now for our actual
Avatar column here we're going to be
using attachment light which it does
specify to use Json as the actual
database column type but whenever it
comes to the actual model it's going to
have a specific decorator and column
type that we need to use if we were to
dive into the documentation you can see
that is at attachment and attachment
contract so what to do is install the
package so that we have access to this
decorator and type and then we can
actually go ahead and utilize them so
it's going to be npmi at adonisjs slash
attachment hyphen light and enter on
that all right cool let's go ahead and
do node Ace configure at adonisjs slash
attachment light so now it should be
configured with our project let's go
ahead and copy this import list here and
paste that within this particular model
and then we can specify the column
decorator here as attachment and the
type as attachment contract and the
attachment light project is actually
super neat because the way that it's
going to work is anytime that we query
our profile the actual Avatar column is
going to be the reference to the image
in itself so for an image source all
that we need to provide is our
profile.avatar and it will render out
just fine so our profile should now be
all set let's go ahead and move on to
our States now our states just has a
name I think I can remember that much so
let's just dive into our state model go
underneath our ID type at column public
name of type string and there we go that
one's all set let's do our post next now
this one does have a little bit more and
it also has another attachment light
column on it so let's go ahead and copy
all of these and run through that same
flow so let's dive within our post model
here go to where we want those inserted
paste it in command or control option
down to get all of our cursors there
option shift right arrow let go of
option right arrow once more to get that
dot delete that out highlight that word
set using option shift right arrow cut
that so that we have those types
accessible go over two more characters
holding down shift delete option right
arrow option shift right arrow to get
those word sets there let's go ahead and
convert those to camelcase oh you know
what we forgot to do that within our
profile so we'll need to go back and do
that then we can just go ahead and note
that the not nullables stop at the
description from there on it becomes
nullable Commander control shift and
right arrow to highlight the rest of the
string there hit delete colon paste go
to the start of the string and column
enter public enter the Line enter go
down to the end delete that extra line
break and so here we have two integers
we want to just change both of those
numbers we have text within typescript
Lane there's no real max length there
for string so we can just set that to
string and then Json here we'll want
that to again just be an attachment of
type attachment contract and we should
be all good with our post model here so
let's jump back into our profile here
and Yep looks like the only one that
needs to change here is our user ID just
change that to camel case instead of uh
snake case and let's double check our
state there should be no State's good
all right cool so next we can move on
from our post to our topics so let's
jump down to our topics here copy these
jump into our topic model paste those in
and we're just going to follow the exact
same flow so get our multi-cursors go
over one word plus one character cut out
the type over two delete we don't have
to change any of those to camel case
again description becomes nullable so we
can delete those out paste in our types
go to the start of string at call column
public and hit enter get rid of the
additional and we do need or null there
we also forgot to do that for our post
so let's jump up to there everything
after description was nullable so we'll
specify that or null or null and or null
in terms of our profile we also have a
couple of nullables there as well the
biography or null and our Avatar and I
should do it now we have all of the
column definitions defined within our
models as they're needed to be able to
work with the actual database structure
that we've created if you enjoyed this
lesson or you learned something new
please consider hitting the like button
down below and subscribing future
lessons just like this one we're going
to be extending on to our relationships
next and crud work after that and
hopefully you'll stick around and learn
a bit with us so thanks for watching
Everybody I'll see you in the next one
Join The Discussion! (4 Comments)
Please sign in or sign up for free to join in on the dicussion.
Anonymous (MoccasinReba925)
Praying for the core team create the wizard soon 🙏
Please sign in or sign up for free to reply
tomgobich
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.
Please sign in or sign up for free to reply
pat_toast
Really happy to see you do the model and migration stuff separately. I find that the "magic" of some frameworks make it hard to piece together how these parts really work together. Thanks!
Please sign in or sign up for free to reply
tomgobich
Thank you, pat_toast!! Yeah, for sure, it can definitely be tricky to grasp where migrations stop and models start and what the separation of concern is between them.
Please sign in or sign up for free to reply