Playing Next Lesson In
seconds

Let's Learn AdonisJS 6 #4.5

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.

Created by
@tomgobich
Published

Join the Discussion 2 comments

Create a free account to join in on the discussion
  1. @n2zb

    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
    1. Responding to n2zb
      @tomgobich

      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