mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
simple seed is working
This commit is contained in:
parent
15d1e6545b
commit
0b3ead8369
@ -4,4 +4,6 @@ DB_USER=root
|
|||||||
DB_PASSWORD=
|
DB_PASSWORD=
|
||||||
DB_DATABASE=gradido_community
|
DB_DATABASE=gradido_community
|
||||||
MIGRATIONS_TABLE=migrations
|
MIGRATIONS_TABLE=migrations
|
||||||
MIGRATIONS_DIRECTORY=./migrations/
|
MIGRATIONS_DIRECTORY=./migrations/
|
||||||
|
|
||||||
|
TYPEORM_SEEDING_FACTORIES=src/factories/**/*{.ts,.js}
|
||||||
|
|||||||
15
database/ormconfig.js
Normal file
15
database/ormconfig.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||||
|
|
||||||
|
const CONFIG = require('./src/config')
|
||||||
|
|
||||||
|
module.export = {
|
||||||
|
name: 'default',
|
||||||
|
type: 'mysql',
|
||||||
|
host: CONFIG.DB_HOST,
|
||||||
|
port: CONFIG.DB_PORT,
|
||||||
|
username: CONFIG.DB_USER,
|
||||||
|
password: CONFIG.DB_PASSWORD,
|
||||||
|
database: CONFIG.DB_DATABASE,
|
||||||
|
seeds: ['src/seeds/**/*{.ts,.js}'],
|
||||||
|
factories: ['src/factories/**/*{.ts,.js}'],
|
||||||
|
}
|
||||||
@ -16,7 +16,9 @@
|
|||||||
"dev_up": "nodemon -w ./ --ext ts --exec ts-node src/index.ts up",
|
"dev_up": "nodemon -w ./ --ext ts --exec ts-node src/index.ts up",
|
||||||
"dev_down": "nodemon -w ./ --ext ts --exec ts-node src/index.ts down",
|
"dev_down": "nodemon -w ./ --ext ts --exec ts-node src/index.ts down",
|
||||||
"dev_reset": "nodemon -w ./ --ext ts --exec ts-node src/index.ts reset",
|
"dev_reset": "nodemon -w ./ --ext ts --exec ts-node src/index.ts reset",
|
||||||
"lint": "eslint . --ext .js,.ts"
|
"lint": "eslint . --ext .js,.ts",
|
||||||
|
"seed:config": "ts-node ./node_modules/typeorm-seeding/dist/cli.js config",
|
||||||
|
"seed": "nodemon -w ./ --ext ts --exec ts-node src/index.ts seed"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/faker": "^5.5.9",
|
"@types/faker": "^5.5.9",
|
||||||
|
|||||||
@ -12,9 +12,10 @@ interface UserContext {
|
|||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
define(User, (faker: typeof Faker, context: UserContext) => {
|
define(User, (faker: typeof Faker, context?: UserContext) => {
|
||||||
const user = new User()
|
if (!context) context = {}
|
||||||
|
|
||||||
|
const user = new User()
|
||||||
user.pubkey = context.pubkey ? context.pubkey : randomBytes(32)
|
user.pubkey = context.pubkey ? context.pubkey : randomBytes(32)
|
||||||
user.email = context.email ? context.email : faker.internet.email()
|
user.email = context.email ? context.email : faker.internet.email()
|
||||||
user.firstName = context.firstName ? context.firstName : faker.name.firstName()
|
user.firstName = context.firstName ? context.firstName : faker.name.firstName()
|
||||||
|
|||||||
@ -4,6 +4,8 @@ import { Migration } from 'ts-mysql-migrate'
|
|||||||
import CONFIG from './config'
|
import CONFIG from './config'
|
||||||
import prepare from './prepare'
|
import prepare from './prepare'
|
||||||
import connection from './typeorm/connection'
|
import connection from './typeorm/connection'
|
||||||
|
import { useSeeding, runSeeder } from 'typeorm-seeding'
|
||||||
|
import { CreateUserSeed } from './seeds/create-user.seed'
|
||||||
|
|
||||||
const run = async (command: string) => {
|
const run = async (command: string) => {
|
||||||
// Database actions not supported by our migration library
|
// Database actions not supported by our migration library
|
||||||
@ -47,6 +49,13 @@ const run = async (command: string) => {
|
|||||||
case 'reset':
|
case 'reset':
|
||||||
await migration.reset() // use for resetting database
|
await migration.reset() // use for resetting database
|
||||||
break
|
break
|
||||||
|
case 'seed':
|
||||||
|
await useSeeding({
|
||||||
|
root: process.cwd(),
|
||||||
|
configName: 'ormconfig.js',
|
||||||
|
})
|
||||||
|
await runSeeder(CreateUserSeed)
|
||||||
|
break
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unsupported command ${command}`)
|
throw new Error(`Unsupported command ${command}`)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { Factory, Seeder } from 'typeorm-seeding'
|
import { Factory, Seeder } from 'typeorm-seeding'
|
||||||
import { User } from '../../entity/User'
|
import { User } from '../../entity/User'
|
||||||
|
|
||||||
export class CreateUser implements Seeder {
|
export class CreateUserSeed implements Seeder {
|
||||||
public async run(factory: Factory): Promise<void> {
|
public async run(factory: Factory): Promise<void> {
|
||||||
await factory(User)().create()
|
await factory(User)().create()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user