From af46a76a9cc8adc38bbd9f5d0964ea56c646224b Mon Sep 17 00:00:00 2001 From: liuyi Date: Sat, 21 Jun 2025 15:05:39 +0800 Subject: [PATCH] add db seed handler --- src/modules/database/commands/index.ts | 1 + .../database/commands/seeder.command.ts | 36 +++++++++++++++++++ .../database/commands/seeder.handler.ts | 29 +++++++++++++++ src/modules/database/commands/types.ts | 5 +++ 4 files changed, 71 insertions(+) create mode 100644 src/modules/database/commands/seeder.command.ts create mode 100644 src/modules/database/commands/seeder.handler.ts diff --git a/src/modules/database/commands/index.ts b/src/modules/database/commands/index.ts index 1bf6ce2..6345e2c 100644 --- a/src/modules/database/commands/index.ts +++ b/src/modules/database/commands/index.ts @@ -2,3 +2,4 @@ export * from './migration.create.command'; export * from './migration.revert.command'; export * from './migration.generate.command'; export * from './migration.run.command'; +export * from './seeder.command'; diff --git a/src/modules/database/commands/seeder.command.ts b/src/modules/database/commands/seeder.command.ts new file mode 100644 index 0000000..4286bfa --- /dev/null +++ b/src/modules/database/commands/seeder.command.ts @@ -0,0 +1,36 @@ +import { Arguments } from 'yargs'; + +import { CommandItem } from '@/modules/core/types'; +import { SeederHandler } from '@/modules/database/commands/seeder.handler'; +import { SeederArguments } from '@/modules/database/commands/types'; + +export const SeederCommand: CommandItem = async ({ configure }) => ({ + command: ['db:seed', 'dbs'], + describe: 'Runs all seeds data.', + builder: { + clear: { + type: 'boolean', + alias: 'r', + describe: 'Clear which tables will truncated specified by seeder class.', + default: true, + }, + connection: { + type: 'string', + alias: 'c', + describe: 'Connection name of typeorm to connect database.', + }, + transaction: { + type: 'boolean', + alias: 't', + describe: 'If is seed data in transaction,default is true', + default: true, + }, + ignorelock: { + type: 'boolean', + alias: 'i', + describe: 'Ignore seed lock and reset all seeds, not do it in production', + default: false, + }, + } as const, + handler: async (args: Arguments) => SeederHandler(configure, args), +}); diff --git a/src/modules/database/commands/seeder.handler.ts b/src/modules/database/commands/seeder.handler.ts new file mode 100644 index 0000000..0b304c2 --- /dev/null +++ b/src/modules/database/commands/seeder.handler.ts @@ -0,0 +1,29 @@ +import chalk from 'chalk'; +import { isNil } from 'lodash'; + +import ora from 'ora'; + +import { Configure } from '@/modules/config/configure'; +import { panic } from '@/modules/core/helpers'; +import { SeederOptions } from '@/modules/database/commands/types'; +import { DBOptions } from '@/modules/database/types'; +import { runSeeder } from '@/modules/database/utils'; + +export async function SeederHandler(configure: Configure, args: SeederOptions) { + const cname = args.connection ?? 'default'; + const { connections = [] }: DBOptions = await configure.get('database'); + const dbConfig = connections.find(({ name }) => name === cname); + if (isNil(dbConfig)) { + await panic(`Database connection named ${cname} not exists!`); + } + + const runner = dbConfig.seedRunner; + const spinner = ora('Start run seeder...'); + try { + spinner.start(); + await runSeeder(runner, args, spinner, configure, dbConfig); + spinner.succeed(`\n 👍 ${chalk.greenBright.underline(`Finished Seeding`)}`); + } catch (error) { + await panic({ spinner, message: `Run seeder failed`, error }); + } +} diff --git a/src/modules/database/commands/types.ts b/src/modules/database/commands/types.ts index cca887c..c5d9ca1 100644 --- a/src/modules/database/commands/types.ts +++ b/src/modules/database/commands/types.ts @@ -122,3 +122,8 @@ export interface SeederLoadParams { */ ignoreLock: boolean; } + +/** + * 数据填充命令参数 + */ +export type SeederArguments = TypeOrmArguments & SeederOptions;