'What's the ideal way to add set data to a database in Nest.JS when starting the app for the first time?
I have a database with quite a few tables, and some of those tables need to have default information in them - stuff like categories, permissions, roles, tags, etc. and I want to make sure that exact information has predictable IDs whenever the app is launched fresh on a new setup (In production or even development) - ideally when, or shortly after, the tables are created.
Right now I'm using NestJS, sequelize-typescript, and MariaDB, but I may change the ORM or DB in the future. Sequelize has the 'seed' ability, but:
- I'd prefer to only use that for adding test data. Having to separate necessary data from test data is potentially a lot of extra work.
- There's some extra magic in my service files that may need to happen before adding data - like determining what a category's parent id is. It can technically be recreated in a seed file, but it'd be extra work and require assumptions like the parent's inserted ID (Which would be easier if I used a service).
- Again, I may change to something like TypeORM or even completely change everything to Mongoose.
- I would just prefer to use Nest for this - in case I forget to call the seed command, so I don't have to install sequelize-cli on production, and a few other minor reasons.
So I'm hoping there's something I'm missing in the docs, or some NestJS friendly, database/ORM agnostic way of adding starter data to my tables. Ideally I'd want to do this per-module and use that module's service file(s) to keep everything separated and guarantee the data is being added correctly without having to rewrite a lot of logic.
So, is there a database / ORM agnostic way to pull this off without using sequelize-cli?
It doesn't even need to be something that's only called when the tables are first created - it could be something called every time the app is launched, and have it check to see if the data already exists.
Solution 1:[1]
I am not convinced seeding your data through your API is necessarily the best idea, but setting my opinion aside, here is what I would probably do.
NestJS does not expose a lifecycle hooks for after the application starts listening ref. That means you cannot encapsulate that logic in a service. Instead, you would have to execute that logic in the bootstrapping sequence after the app starts listening. If your seeding logic needs information from services you can access their instances with app.get(<injectable-class-or-token>).
When it comes to the data itself, if it really needs to be generated dynamically, you might as well just script it with TypeScript. In that case, if you want deterministic data, you might want to seed your RNG (or whatever lib you use to generate data). Otherwise, you could hardcode your data in the script or read it from other files in whatever format you prefer (JSON, CSV, etc.).
In essence, you have a .ts script that is reponsible for generating (or reading) your seed data and sending it to your API. That script is called after your app is listening in your bootstrap sequence and it can access information gro your services if needed.
Does that answer your question?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Antoine Viscardi |
