🌾 A simple extension library for nestjs sequelize to perform seeding.
🌐 Description
Under the hood, nestjs-sequelize-seeder makes use of the nest framework, and you also need to install nestjs, and sequelize !
📦 Integration
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
;;
All options
SeederModule.forRoot;
The forRoot() method supports all the configuration properties exposed by the seeder constuctor . In addition, there are several extra configuration properties described below.
name | Description | type |
---|---|---|
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 is activated (default: id) |
string |
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 autoIdFieldName (default: true) |
boolean |
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 containsForeignKeys option enabled |
number |
Seeder
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.
The decorator 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
genSaltSync
andhashSync
are 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-many
, Many-to-many
, and One-to-one
, if you get an error just increase the delay time in the foreignDelay
option in the global configuration
⭐ Support for
nestjs-sequelize-seeder
is an open source project licensed by MIT. You can grow thanks to the sponsors and the support of the amazing sponsors. If you want to join them, contact me here.
🎩 Stay in touch
- Github @yonicalsin
- Twitter @yonicalsin
- Instagram @yoni_calsin
- Medium @yonicalsin
🚀 Contributors
Thanks to the wonderful people who collaborate with me !
📜 License
nestjs-sequelize-seeder
is MIT licensed.