🌾 A simple extension library for nestjs sequelize to perform seeding.
To start using it, we first install the required dependencies. In this chapter we will demonstrate the use of the seeder for nestjs.
You simply need to install the package !
// We install with npm, but you could use the package manager you prefer !npm install --save nestjs-sequelize-seeder
▶️ Getting started
Once the installation process is complete, we can import the SeederModule into the root AppModule
The forRoot() method supports all the configuration properties exposed by the seeder constuctor . In addition, there are several extra configuration properties described below.
|isGlobal||If you want the module globally (default: true )||boolean|
|logging||Option to display or not, the log of each creation (default: true)||boolean|
|disabled||This option allows you to disable the whole module, it is very useful for production mode (default: false)||boolean|
|runOnlyIfTableIsEmpty||This option allows you to disable if the table is empty (default: false)||boolean|
|connection||This option is to add the name of the connection, this is very important if you use several connections to different databases (default: default)||string|
|autoIdFieldName||This option is the id field, it works if the option
|enableAutoId||This option adds the id automatically to each item, saving you the work, and solving some errors, the name of the id field is customized with the option
|foreignDelay||This option adds the timeout for tables that have relationships with other tables for each element, and this works if a seed has the
Sequelize implements the Active Record pattern. With this pattern, you use model classes directly to interact with the database. To continue the example, we need at least one seed. Let's define the User seed.
Seeder receives as parameter the unique values, this has to be added if you have in the table any column as unique !
The following options will be applied individually to the seeders, and will be compared and operated with the global configuration
hashSyncare imported from bcryptjs, you will have to install it independently !
Next, let's look at the UserModule:
🎉 Associations, ForeignKeys
You were probably wondering how I handle seeders with associations, well I'm anxious to tell you that it's like this
- First create three models
- We'll make some connections
- Creation of seeders for each model
And as a consequence we will create the sowers
As you can see we already created the sembredores free of relations, but the next one has relations, therefore we have to activate the option
containsForeignKeys, this works with
One-to-one, if you get an error just increase the delay time in the
foreignDelay option in the global configuration
⭐ Support for
🎩 Stay in touch
Thanks to the wonderful people who collaborate with me !
nestjs-sequelize-seeder is MIT licensed.