Generate Lucid Models from an existing database
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:
- It'll use AdonisJS to connect to your database
- Once connected, it collects your tables, columns, and foreign keys
- Then, it'll map out what your models should look like for the tables & columns
- Finally, it'll then use your foreign keys to work backwards and build out your relationships
Here's what's required:
- Your tables, columns, and foreign keys must be defined in your database
- Your database must be well structured, deviations will cause anomolies in the generated relationships
- 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.
You can easily install and configure via the Ace CLI's add
command.
node ace add @adocasts.com/generate-models
You can also manually install and configure if you'd prefer
npm install @adocasts.com/generate-models
node ace configure @adocasts.com/generate-models
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.