npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-class-seeder

v1.2.2

Published

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

Downloads

264

Readme

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.