Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yonycalsin/nestjs-sequelize-seeder
🌾 A simple extension library for nestjs sequelize to perform seeding.
https://github.com/yonycalsin/nestjs-sequelize-seeder
nestjs nestjs-seeder seeder seeding sequelize sequelize-seeder
Last synced: about 2 months ago
JSON representation
🌾 A simple extension library for nestjs sequelize to perform seeding.
- Host: GitHub
- URL: https://github.com/yonycalsin/nestjs-sequelize-seeder
- Owner: yonycalsin
- License: mit
- Created: 2020-03-04T05:48:20.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-26T04:16:59.000Z (2 months ago)
- Last Synced: 2024-11-07T17:10:01.002Z (about 2 months ago)
- Topics: nestjs, nestjs-seeder, seeder, seeding, sequelize, sequelize-seeder
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/nestjs-sequelize-seeder
- Size: 75.2 KB
- Stars: 4
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A simple extension library for nestjs sequelize to perform seeding.
## Description
Under the hood, nestjs-sequelize-seeder makes use of the [nest framework](https://nestjs.com/), and you also need to install [nestjs](https://nestjs.com/), and [sequelize](https://docs.nestjs.com/techniques/database#sequelize-integration) !
## 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 !
```ts
// 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**
```ts
import { Module } from '@nestjs/common';
import { SeederModule } from 'nestjs-sequelize-seeder';@Module({
imports: [
SeederModule.forRoot({
// Activate this if you want to run the seeders if the table is empty in the database
runOnlyIfTableIsEmpty: true,
}),
],
})
export class AppModule {}
```All options
```ts
SeederModule.forRoot({
isGlobal: true,
logging: true,
disabled: false,
runOnlyIfTableIsEmpty: false,
connection: 'default',
autoIdFieldName: 'id',
disableEveryOne: false,
enableAutoId: true,
foreignDelay: 2000, // 2 seconds
});
```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
```ts
@Seeder({
model: ModelUser,
unique: ['name'], // You can add more !
// Here you can also add the following options, but those options only work for this seeder !
disabled: false,
logging: true,
runOnlyIfTableIsEmpty: false,
connection: 'default',
disableEveryOne: false,
enableAutoId: true,// Enables this function if it uses a relationship management model (foreignKeys)
containsForeignKeys: false,// This option add run time delay, if you still have errors just increase the delay time
foreignDelay: 2000,
})```
> `genSaltSync` and `hashSync` are imported from **bcryptjs**, you will have to install it independently !
```ts
import { Seeder, OnSeederInit } from 'nestjs-sequelize-seeder';
import { ModelUser } from 'src/models/user';
import { genSaltSync, hashSync } from 'bcryptjs';@Seeder({
model: ModelUser,
unique: ['name', 'username'],
})
export class SeedUser implements OnSeederInit {
run() {
const data = [
{
name: 'Admin',
username: 'admin',
age: 34,
password: 'admin_password',
},
{
name: 'Editor',
username: 'editor',
age: 25,
password: 'editor_password',
},
];
return data;
}// This function is optional!
everyone(data) {
// Encrypting the password for each user !
if (data.password) {
const salt = genSaltSync(10);
data.password = hashSync(data.password, salt);
data.salt = salt;
}// Aggregated timestamps
data.created_at = new Date().toISOString();
data.updated_at = new Date().toISOString();return data;
}
}
```Next, let's look at the **UserModule:**
```ts
import { Module } from '@nestjs/common';
import { SeederModule } from 'nestjs-sequelize-seeder';
import { SeedUser } from 'src/seeds/user.seed';@Module({
imports: [
// Within an array!
SeederModule.forFeature([SeedUser]),
],
})
export class 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```ts
@Table
export class Cat extends Model {
@PrimaryKey
@AutoIncrement
@Column
id: number;@Column
name: string;@BelongsToMany(
() => Breed,
() => CatBreed,
)
breeds: Breed[];
}@Table
export class Breed extends Model {
@PrimaryKey
@AutoIncrement
@Column
id: number;@Column
name: string;@BelongsToMany(
() => Cat,
() => CatBreed,
)
Cats: Cat[];
}@Table
export class CatBreed extends Model {
@ForeignKey(() => Cat)
@Column
cat_id: number;@ForeignKey(() => Breed)
@Column
breed_id: number;
}
```And as a consequence we will create the sowers
```ts
@Seeder({
model: Cat,
})
export class SeedCat implements OnSeederInit {
run() {
const data = [
{
name: 'First Cat',
},
{
name: 'Second Cat',
},
];
return data;
}
}
@Seeder({
model: Breed,
})
export class SeedCatBreed implements OnSeederInit {
run() {
const data = [
{
name: 'First Breed',
},
{
name: 'Second Breed',
},
];
return data;
}
}
```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
```ts
@Seeder({
model: CatBreed,
containsForeignKeys: true,
})
export class SeedCatBreedUse implements OnSeederInit {
run() {
const data = [
{
cat_id: 1,
breed_id: 2,
},
{
cat_id: 1,
breed_id: 1,
},
{
cat_id: 2,
breed_id: 1,
},
{
cat_id: 2,
breed_id: 2,
},
];
return data;
}
}
```