Skip to content

Instantly share code, notes, and snippets.

@paulodutra
Last active August 11, 2024 17:03
Show Gist options
  • Save paulodutra/4a3d9f24c1abe5238dce048dfe02cda4 to your computer and use it in GitHub Desktop.
Save paulodutra/4a3d9f24c1abe5238dce048dfe02cda4 to your computer and use it in GitHub Desktop.
Faker js seed for task table
import { Injectable } from '@nestjs/common';
import { EntityManager } from 'typeorm';
import { faker } from '@faker-js/faker';
import { Task } from '@/infrastructure/database/entities/task.entity';
@Injectable()
export class TaskSeedService {
private done = [0, 1];
constructor(private readonly entityManager: EntityManager) {}
async seed() {
const tasks = Array.from({ length: 10 }).map(() => {
const randomIndex = Math.floor(Math.random() * this.done.length);
const task = new Task();
task.name = faker.word.verb();
task.done = this.done[randomIndex];
return task;
});
await this.entityManager.save(tasks);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment