-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- auth 시딩파일 설정 - `yarn seed` 으로 시딩 - `yarn seed:reset` 으로 시딩할 디비 리셋
- Loading branch information
Showing
8 changed files
with
203 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//import dotenv | ||
import { config } from 'dotenv'; | ||
|
||
import { DataSource, DataSourceOptions } from 'typeorm'; | ||
import { runSeeders, SeederOptions } from 'typeorm-extension'; | ||
import { Auth } from '../src/entity/auth.entity'; | ||
import AuthSeeder from './seeder/auth.seeder'; | ||
import AuthFactory from './factory/auth.factory'; | ||
|
||
config(); | ||
|
||
(async () => { | ||
const options: DataSourceOptions & SeederOptions = { | ||
type: 'postgres', | ||
host: process.env.DB_HOST, | ||
port: Number(process.env.DB_PORT), | ||
database: 'testdb', | ||
username: process.env.DB_USER, | ||
password: process.env.DB_PASSWORD, | ||
synchronize: true, | ||
entities: [Auth], | ||
seeds: [AuthSeeder], | ||
factories: [AuthFactory], | ||
}; | ||
console.log('options: ', options); | ||
|
||
const dataSource = new DataSource(options); | ||
await dataSource.initialize(); | ||
|
||
await runSeeders(dataSource); | ||
})(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// file name should be <entityName>.factory.ts | ||
|
||
import { Faker } from '@faker-js/faker'; | ||
import { Auth, AuthStatus } from '../../src/entity/auth.entity'; | ||
import { setSeederFactory } from 'typeorm-extension'; | ||
|
||
export default setSeederFactory(Auth, (faker: Faker) => { | ||
const auth = new Auth(); | ||
|
||
//auth.id is auto generated | ||
//auth.status = faker.datatype.boolean() ? AuthStatus.REGISTERD : AuthStatus.UNREGISTERD; | ||
auth.status = AuthStatus.REGISTERD; | ||
auth.email = faker.internet.email(); | ||
|
||
return auth; | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
DB=$1; | ||
if [ $1 -z ] ; then | ||
echo "Please provide a database name"; | ||
read -p "Database name: " DB; | ||
fi | ||
dropdb $DB 2>/dev/null; createdb $DB; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { DataSource, MoreThan, Repository } from 'typeorm'; | ||
import { Seeder, SeederFactoryManager } from 'typeorm-extension'; | ||
|
||
import { Auth } from '../../src/entity/auth.entity'; | ||
|
||
export default class AuthSeeder implements Seeder { | ||
public async run(dataSource: DataSource, factoryManager: SeederFactoryManager) { | ||
const repository: Repository<Auth> = dataSource.getRepository(Auth); | ||
repository.delete({ id: MoreThan(0) }); | ||
//await repository.insert([ | ||
// { | ||
// firstName: 'Caleb', | ||
// lastName: 'Barrows', | ||
// email: 'caleb.barrows@gmail.com', | ||
// }, | ||
//]); | ||
|
||
// --------------------------------------------------- | ||
|
||
const authFactory = await factoryManager.get(Auth); | ||
// save 1 factory generated entity, to the database | ||
//await authFactory.save(); | ||
|
||
// save 5 factory generated entities, to the database | ||
await authFactory.saveMany(1000); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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