Defining Our Models

In this lesson, we'll walk through the process of defining all our database tables and columns as models and properties within our application.

Published
Feb 28, 24
Duration
6m 49s

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

Get the Code

Download or explore the source code for this lesson on GitHub

Repository

Join The Discussion! (2 Comments)

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

  1. Commented 1 month ago

    Hello, Tom,

    I see in the LucidModel interface that there is a table property which allows you to tell the model which table to use. For my project (Bible application) I have several tables (different Bibles) with exactly the same structure. A single model is therefore sufficient, as it is suitable for all these tables. Is it good practice to change the value of table in my controller/services? For example, by writing Verse.table = ... when it's needed.

    1

    Please sign in or sign up for free to reply

    1. Commented 1 month ago

      Hi n2zb!

      I have never tried changing the table property at runtime like you're asking about. I imagine it would work, but even if it does, it would be susceptible to accidentally entering data into the wrong table.

      Instead, I'd recommend defining those shared columns & relationships via a Model Mixin and then composing the mixin into a separate model per table. I thought I had a free lesson on this, but it looks like there is currently only this one from the Building with AdonisJS & Inertia series. I'll have to make a free one on this as well, as it's quite handy.

      In the meantime, you can get the gist of it via the code from this series.

      0

      Please sign in or sign up for free to reply

Playing Next Lesson In
seconds