Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nestjs-class-seeder

Package Overview
Dependencies
Maintainers
0
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-class-seeder

An extension library for NestJS to perform seeding. Forked from nestjs-seeder

  • 1.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
46
increased by84%
Maintainers
0
Weekly downloads
 
Created
Source

A class-decorator based seeder for NestJS projects.

Installation and setup

Install the dependency

npm install nestjs-class-seeder --save-dev

Create a seeding script

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.

Integrate your seeder into the command line

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.

Usage

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;
}

Relationships

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.

Relationship queries

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[];
}

Context

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.

📜 License

nestjs-class-seeder is MIT licensed. This project was forked from nestjs-seeder.

Keywords

FAQs

Package last updated on 23 Oct 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc