Skip to content

Generate Lucid Models from an existing database schema

License

Notifications You must be signed in to change notification settings

adocasts/generate-models

Repository files navigation

@adocasts.com/generate-models

Generate Lucid Models from an existing database

npm-image license-image

Whether you're looking to start a new AdonisJS project for an existing database or you've define your migrations and ran them, this package can help you get up and running by generating models to match your database schema!

Note that this likely won't be perfect, always be sure to double-check the files after they're generated.

Here's how it works:

  1. It'll use AdonisJS to connect to your database
  2. Once connected, it collects your tables, columns, and foreign keys
  3. Then, it'll map out what your models should look like for the tables & columns
  4. Finally, it'll then use your foreign keys to work backwards and build out your relationships

Here's what's required:

  1. Your tables, columns, and foreign keys must be defined in your database
  2. Your database must be well structured, deviations will cause anomolies in the generated relationships
  3. Your AdonisJS application must have its database connection defined

Right now, we only have database type to TypeScript type mappings defined for MySQL and PostgreSQL. Unknown column types will default to unknown if they cannot be mapped.

Installation

You can easily install and configure via the Ace CLI's add command.

node ace add @adocasts.com/generate-models
Manual Install & Configure

You can also manually install and configure if you'd prefer

npm install @adocasts.com/generate-models
node ace configure @adocasts.com/generate-models

Generate Models Command

Right now we only offer one command, and that is the generate:models command.

node ace generate:models

This will generate your models and plop them at the location defined within your adonisrc.ts file, which is app/models by default. If any of the models already exist in your project, those models will be skipped and will not be replaced.