Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use with typeORM #37

Open
shinnthantminn opened this issue Nov 28, 2023 · 1 comment
Open

How to use with typeORM #37

shinnthantminn opened this issue Nov 28, 2023 · 1 comment

Comments

@shinnthantminn
Copy link

Use with typeOrm

i want to use seeding with typeOrm but stay db error how can i solve this

@petetnt
Copy link

petetnt commented May 7, 2024

Here's a small TypeORM example for future reference:

user.entity.ts

import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  Index,
} from 'typeorm';
import { Factory } from 'nestjs-seeder';

@Entity('user')
export class User {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

  @Factory((faker) => { 
    return faker!.string.alphanumeric(28);
  })
  @Index({ unique: true })
  @Column({ nullable: false })
  userId!: string;

  @Factory((faker) => faker.person.fullName())
  name!: string;
}

user.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import {
  DeepPartial,
  InsertResult,
  Repository,
 Not,
  IsNull
} from 'typeorm';
import { User } from './user.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private userRepository: Repository<User>,
  ) {}

  save(users: DeepPartial<User>[]) {
    return this.userRepository.save(users);
  }

  delete(criteria: Parameters<Repository<User>['delete']>[0]) {
    return this.userRepository.delete({
      id: Not(IsNull())
    });
  }
}

user.seeder.ts

import { Injectable } from '@nestjs/common';
import { Seeder, DataFactory } from 'nestjs-seeder';
import { User } from '../users/user.entity';
import { DeepPartial, In } from 'typeorm';
import { UserService } from './user.service';

@Injectable()
export class UsersSeeder implements Seeder {
  users: DeepPartial<User>[];
  constructor(private readonly userService: UserService) {
    // Generate 10 users.
    this.users = DataFactory.createForClass(User).generate(
      10,
    ) as DeepPartial<User>[];
  }

  async seed() {
    return this.userService.save(this.users);
  }

  async drop() {
    return this.userService.deleteAll()
  }
}

user.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import { UserService } from './user.service';
import { User } from './user.entity';

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  providers: [UserService],
  exports: [UserService],
})
export class UserModule {}

user.seeder.ts

import { seeder } from 'nestjs-seeder';
import { UsersSeeder } from './user.seeder';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TypeOrmConfigService } from '../typeorm-config/typeorm-config.service';
import { UserModule } from '../user.module';

seeder({
  imports: [
    TypeOrmModule.forFeature([User])
    UserModule,
  ],
}).run([UsersSeeder]);

Install ts-node and add to package.json:

  "seed": "ts-node -r tsconfig-paths/register ./src/user.seeder.ts"
  "seed:refresh": "ts-node -r tsconfig-paths/register ./src/user.seeder.ts --refresh"

and run

npm run seed

or

npm run seed:refresh

@edwardanthony Would you want a PR to README for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants