Last active
August 11, 2024 17:03
-
-
Save paulodutra/4a3d9f24c1abe5238dce048dfe02cda4 to your computer and use it in GitHub Desktop.
Faker js seed for task table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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