diff --git a/src/seed/book-seed.service.ts b/src/seed/book-seed.service.ts index e1d0ed5..93903b8 100644 --- a/src/seed/book-seed.service.ts +++ b/src/seed/book-seed.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { BookInfo, BookCategory } from 'src/books/entities/bookInfo.entity'; +import { Book } from 'src/books/entities/book.entity'; import { Repository } from 'typeorm'; @Injectable() @@ -8,19 +9,40 @@ export class BookSeedService { constructor( @InjectRepository(BookInfo) private bookInfosRepository: Repository, + @InjectRepository(Book) + private bookRepository: Repository, ) {} async seed() { const bookInfos: any[] = require('./book-info.json'); for (const bookInfo of bookInfos) { - const categoryIdx: number = Math.floor(Math.random() * 10); + const categoryIdx: number = Math.floor( + Math.random() * Object.values(BookCategory).length, + ); bookInfo.category = Object.values(BookCategory)[categoryIdx]; } - await this.bookInfosRepository + const result = await this.bookInfosRepository .createQueryBuilder() .insert() .into(BookInfo) .values(bookInfos) .execute(); + for (const identifier of result.identifiers) { + await this.bookRepository.insert({ + donator: [ + 'hyekim', + 'jwoo', + 'tkim', + 'sujikim', + 'chanykim', + 'minkykim', + 'gilee', + '', + ][Math.floor(Math.random() * 8)], + callSign: `AA.2021.${identifier.id}`, + status: 1, + info: identifier, + }); + } } } diff --git a/src/seed/book.service.ts b/src/seed/book.service.ts deleted file mode 100644 index de4766d..0000000 --- a/src/seed/book.service.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Factory, Seeder } from 'typeorm-seeding'; -import { Connection } from 'typeorm'; -import { BookInfo } from 'src/books/entities/bookInfo.entity'; -import { BookCategory } from 'src/books/entities/bookInfo.entity'; -import { Book } from 'src/books/entities/book.entity'; - -export default class CreateUsers implements Seeder { - public async run(factory: Factory, connection: Connection): Promise { - const bookInfos: any[] = require('./bookInfos.json'); - for (const bookInfo of bookInfos) { - const categoryIdx: number = Math.floor(Math.random() * 10); - bookInfo.category = Object.values(BookCategory)[categoryIdx]; - } - await connection - .createQueryBuilder() - .insert() - .into(BookInfo) - .values(bookInfos) - .execute(); - } -} diff --git a/src/seed/seed.module.ts b/src/seed/seed.module.ts index 2bfbdd3..627029f 100644 --- a/src/seed/seed.module.ts +++ b/src/seed/seed.module.ts @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common'; import { BookSeedService } from './book-seed.service'; import { TypeOrmModule } from '@nestjs/typeorm'; import { BookInfo } from 'src/books/entities/bookInfo.entity'; +import { Book } from 'src/books/entities/book.entity'; @Module({ providers: [BookSeedService], - imports: [TypeOrmModule.forFeature([BookInfo])], + imports: [TypeOrmModule.forFeature([BookInfo, Book])], }) export class SeedModule {}