Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
nestjs-class-seeder
Advanced tools
An extension library for NestJS to perform seeding. Forked from nestjs-seeder
A class-decorator based seeder for NestJS projects.
npm install nestjs-class-seeder --save-dev
Create a seeding script called seeder.ts
under the src
folder of your NestJS project:
import { seeder, createClassSeeder } from "nestjs-class-seeder";
import { TypeOrmModule } from "@nestjs/typeorm";
seeder({
imports: [
TypeOrmModule.forRoot(yourConfig),
TypeOrmModule.forFeature([Duck]),
],
providers: [],
}).run([createClassSeeder(Duck)]);
For multiple entities, use the plural createClassSeeders
function instead:
import { seeder, createClassSeeders } from "nestjs-class-seeder";
import { TypeOrmModule } from "@nestjs/typeorm";
const entities = [Duck, Beaver, Gecko];
seeder({
imports: [
TypeOrmModule.forRoot(yourConfig),
TypeOrmModule.forFeature(entities),
],
providers: [],
}).run(createClassSeeders(entities));
Note that you can pass additional imports and providers for dependency injection.
Add these two scripts (seed
and seed:refresh
) under the scripts
property in your
package.json
file:
"scripts": {
"seed": "ts-node -r tsconfig-paths/register src/seeder.ts",
"seed:refresh": "ts-node -r tsconfig-paths/register src/seeder.ts -- --refresh"
}
You can now run either npm run seed
. If you run npm run seed:refresh
you drop all
data in the seeded tables before generating new data.
To generate values for columns, decorate them with the Seed
decorator, the decorator
takes simple static values, or a generator function. Generator functions are handed a
Faker
instance to help you produce fake values, and a context
(more on that later):
import { Seed } from "nestjs-class-seeder";
import { Entity, Column } from "typeorm";
@Entity()
export class Duck {
@Seed(faker => faker.name.firstName())
@Column()
name: string;
@Seed("yellow")
@Column()
billColor: string;
}
You can seed relationships using the SeedRelation
decorator:
import { SeedRelation } from 'nestjs-class-seeder';
import { Entity, JoinTable, ManyToMany, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Duck } from './duck.entity';
@Entity()
export class Beaver {
@PrimaryGeneratedColumn()
id: number;
// Selects a random Duck
@SeedRelation(() => Duck)
@ManyToOne(() => Duck, {onDelete: 'SET NULL'})
random: Duck;
// Selects the 4th Duck created this seeding batch
@SeedRelation(() => Duck, 3)
@ManyToOne(() => Duck, {onDelete: 'SET NULL'})
reallyHatesThis: Duck;
// Selects multiple Ducks
@SeedRelation(() => Duck, [6, 4, 1])
@ManyToMany(() => Duck, {onDelete: 'SET NULL'})
@JoinTable()
butHasTheirEyesOn: Duck[];
}
Note the use of the () => MyClass
pattern to avoid circular dependencies.
nestjs-class-seeder
uses sift.js to let you use
MongoDB queries to find generated relationship objects. Relationship queries only apply to
objects created during the same seeding batch, the database entries are not queried.
import { SeedRelation } from 'nestjs-class-seeder';
import { Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from 'typeorm';
import { Duck } from './duck.entity';
@Entity()
export class Beaver {
@PrimaryGeneratedColumn()
id: number;
// Selects a duck named Tucker
@SeedRelation(() => Duck, {name: "Tucker"})
@ManyToOne(() => Duck, {onDelete: 'SET NULL'})
bestFriend: Duck;
// Selects all ducks with more than 11350 feathers.
@SeedRelation(() => Duck, {feathers: {$gt: 11350}}, {many: true})
@ManyToMany(() => Duck, {onDelete: 'SET NULL'})
@JoinTable()
extraFluffyOnes: Duck[];
}
Clearly, Tucker is not all beavers' best friend (see previous example if you're confused). Some outcomes depend on the current context. The seeding context is passed as the second argument to generator functions:
import { SeedEnum, SeedRelation } from 'nestjs-class-seeder';
import { Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from 'typeorm';
import { Duck } from './duck.entity';
const enum BeaverNames = {
BuckBean,
Sawyer,
Bonnie,
}
@Entity()
export class Beaver {
@PrimaryGeneratedColumn()
id: number;
@SeedEnum(BeaverNames)
name: BeaverNames;
// Tucker is only BuckBean's best friend
@SeedRelation(() => Duck, (faker, ctx) =>
ctx.currentRecord.name === BeaverNames.BuckBean ? {name: "Tucker"} : null
)
@ManyToOne(() => Duck, {onDelete: 'SET NULL'})
bestFriend: Duck;
}
It also gives you access to:
currentRecord
: A Record
of the generated values so far.currentIndex
: The index of the current record in the batch.currentRecords
: The other records generated so far.currentBatchRecords
: A class-to-records Map
generated so far.savedEntities
: A class-to-entity Map
with the entities saved to the database so far.dataSource
: Your TypeORM connection to the database.nestjs-class-seeder
is MIT licensed. This project was forked from
nestjs-seeder
.
FAQs
An extension library for NestJS to perform seeding. Forked from nestjs-seeder
The npm package nestjs-class-seeder receives a total of 40 weekly downloads. As such, nestjs-class-seeder popularity was classified as not popular.
We found that nestjs-class-seeder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.