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

FEATURE: update book seeding #27

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/seed/book-seed.service.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
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()
export class BookSeedService {
constructor(
@InjectRepository(BookInfo)
private bookInfosRepository: Repository<BookInfo>,
@InjectRepository(Book)
private bookRepository: Repository<Book>,
) {}

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,
});
}
}
}
21 changes: 0 additions & 21 deletions src/seed/book.service.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/seed/seed.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}