Introducing Lucid Models

In this lesson, we'll introduce models using the Lucid ORM. We'll learn how we can map database columns to our model properties and specify special behavior for our date time columns.

Published
Feb 28
Duration
5m 43s

Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.

Adocasts

Burlington, KY

Join The Discussion! (6 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 3 months ago

    Hi Tom,

    When I initially created the adonis project, it didn't create the “User” model for me. Is there a “node ace” command to specifically create the “User” class? At first glance I don't seem to see one in the group “maker" command.

    1

    Please sign in or sign up for free to reply

    1. Commented 3 months ago

      Hi Noctisy! You most likely skipped installing an auth guard during your project's creation (this is a new question during project creation that was added after we released our lesson). You can get this easily added in by installing and configuring the AdonisJS Auth package!

      node ace add @adonisjs/auth --guard=session
      Copied!

      https://docs.adonisjs.com/guides/authentication/introduction#installation

      0

      Please sign in or sign up for free to reply

  2. Commented 2 months ago

    I just have a question about defining the model. I'm coming from NestJS/TypeORM background, and it's weird for me why you defined the migration first and then update the model second, could you please explain why.

    In TypeORM, I used to define the DB models, and it'll take care of generating the tables in the DB automatically, by just setting synchronize to true in the connection config. Is there something similar in Lucid.

    1

    Please sign in or sign up for free to reply

    1. Commented 2 months ago

      Hey Hexacker!

      In AdonisJS, migrations are what create our tables, foreign keys, indexes, etc. Migrations are great because they give us control to incrementally build or update our database as we need in the order we need. Most of the time, in starting an application, it'll seem tedious. But, as your application ages, they're fantastic for maintaining flexibility.

      For example, if I have a pre-existing database with a histories table that holds two types of histories, view history and progression history. As the app ages, if I decide it'd behoove me to have this one table split into two, I can easily use migrations to create a view_histories table and a progression_histories table. Then, in those same migrations, I can fill them directly with data from the histories table. Then, delete the histories table. These changes would take 3 migrations, which are run one after another at the same time my application with the needed code updates are deploying.

      1. create_view_histories_table
        Creates the view_histories table, then uses the defer method to populate it with pre-existing view histories from the histories table.

      2. create_progression_histories_table
        Creates the progression_histories table, then uses the defer method to populate it with pre-existing progression histories from the histories table.

      3. delete_histories_table
        Drops the no longer needed histories table

      Models then, are strictly used to aide in CRUD operations inside our code. At present, there is no official way to generate migrations nor models. However, I do have a package in dev release to generate models from a database.

      Fun fact, the migration example above is based on a refactor I did on the Adocasts site.

      1

      Please sign in or sign up for free to reply

      1. Commented 2 months ago

        Thank you Tom for the very detailed answer, it just looks really weird for me the process especially that I'm used to the flow on TypeORM, which scan the models, create the schema on the DB and you're good to go from there.

        Your package looks very promising and it do a great job, I will check it and see if I can help you improve it.

        1

        Please sign in or sign up for free to reply

        1. Commented 2 months ago

          Anytime! The grass can be a different shade of green depending on the ecosystem you're in, but just because it is different doesn't mean it's a bad thing. The migration approach is a common and popular choice, used in the like of .NET, Laravel, and likely others but those are two I know and have personally used.

          Thank you, Hexacker! The one thing those two (.NET & Laravel) offer that AdonisJS doesn't is model generation, which is why I wanted to make a package to at least offer something in that regard. An approach for model/migration generation has been something planned by the core team, but it is likely still a ways out.

          With model generation in play, the flow would go:
          Define migrations → run migrations → generate models

          1

          Please sign in or sign up for free to reply

Playing Next Lesson In
seconds