2025-05-24 14:26:20 +08:00
#!/usr/bin/env node
/ * *
* 数据库模型生成脚本
* 功能等同于gen _models . ps1
* /
const { execSync } = require ( 'child_process' ) ;
const fs = require ( 'fs' ) ;
const path = require ( 'path' ) ;
// 配置信息
const config = {
// 数据库连接信息
dbUrl : 'qnc:5vg67b3UNHu8@tcp(127.0.0.1:21001)/qnc' ,
// 输出目录
outputDir : './model' ,
// 模板目录
templateDir : '../template' ,
// 目标目录
targetDir : '../../app/user/model' ,
// 表名列表
tables : [
2025-06-03 12:24:15 +08:00
// 'agent',
2025-05-24 14:26:20 +08:00
// 'agent_active_stat',
2025-06-03 12:24:15 +08:00
// 'agent_audit',
// 'agent_real_name'
2025-05-24 14:26:20 +08:00
// 'agent_closure',
// 'agent_commission',
// 'agent_commission_deduction',
// 'agent_link',
// 'agent_membership_config',
// 'agent_membership_recharge_order',
// 'agent_membership_user_config',
// 'agent_order',
// 'agent_platform_deduction',
// 'agent_product_config',
// 'agent_rewards',
// 'agent_wallet',
// 'agent_withdrawal',
// 'feature',
// 'global_notifications',
// 'order',
// 'product',
// 'product_feature',
// 'query',
// 'user',
// 'user_auth',
// 'example',
2025-06-03 12:24:15 +08:00
'authorization' ,
2025-05-24 14:26:20 +08:00
// 'authorization_face'
]
} ;
/ * *
* 将表名转换为驼峰命名法
* @ param { string } tableName 表名
* @ returns { string } 驼峰命名的表名
* /
function convertToCamelCase ( tableName ) {
try {
// 将表名按_分割, 并将每个部分首字母大写
const parts = tableName . split ( '_' ) ;
let camelCase = '' ;
for ( const part of parts ) {
if ( part . length > 0 ) {
camelCase += part . charAt ( 0 ) . toUpperCase ( ) + part . slice ( 1 ) . toLowerCase ( ) ;
}
}
return camelCase ;
} catch ( error ) {
console . error ( ` Error in convertToCamelCase for table: ${ tableName } ` ) ;
console . error ( error . message ) ;
return tableName ; // 出错时返回原表名
}
}
/ * *
* 确保目录存在
* @ param { string } dirPath 目录路径
* /
function ensureDirectoryExists ( dirPath ) {
if ( ! fs . existsSync ( dirPath ) ) {
console . log ( ` Creating directory: ${ dirPath } ` ) ;
fs . mkdirSync ( dirPath , { recursive : true } ) ;
}
}
/ * *
* 为单个表生成模型
* @ param { string } table 表名
* /
function generateModelForTable ( table ) {
try {
console . log ( '=========================================' ) ;
console . log ( ` Processing table: ${ table } ` ) ;
// 生成模型
console . log ( 'Generating model...' ) ;
const command = ` goctl model mysql datasource -url=" ${ config . dbUrl } " -table=" ${ table } " -dir=" ${ config . outputDir } " --home=" ${ config . templateDir } " -cache=true --style=goZero ` ;
console . log ( ` Running command: ${ command } ` ) ;
execSync ( command , { stdio : 'inherit' } ) ;
// 将表名转换为驼峰命名法
const camelCaseName = convertToCamelCase ( table ) ;
console . log ( ` Table name converted to: ${ camelCaseName } ` ) ;
// 定义源文件和目标文件路径
const sourceModelFile = path . join ( config . outputDir , ` ${ camelCaseName } Model.go ` ) ;
const sourceModelGenFile = path . join ( config . outputDir , ` ${ camelCaseName } Model_gen.go ` ) ;
const targetModelFile = path . join ( config . targetDir , ` ${ camelCaseName } Model.go ` ) ;
const targetModelGenFile = path . join ( config . targetDir , ` ${ camelCaseName } Model_gen.go ` ) ;
console . log ( 'Source files:' ) ;
console . log ( ` - ${ sourceModelFile } ` ) ;
console . log ( ` - ${ sourceModelGenFile } ` ) ;
console . log ( 'Target files:' ) ;
console . log ( ` - ${ targetModelFile } ` ) ;
console . log ( ` - ${ targetModelGenFile } ` ) ;
// 检查源文件是否存在并移动
if ( fs . existsSync ( sourceModelFile ) ) {
console . log ( ` Moving ${ sourceModelFile } to ${ targetModelFile } ` ) ;
fs . copyFileSync ( sourceModelFile , targetModelFile ) ;
fs . unlinkSync ( sourceModelFile ) ;
} else {
console . log ( ` WARNING: Source file not found: ${ sourceModelFile } ` ) ;
}
if ( fs . existsSync ( sourceModelGenFile ) ) {
console . log ( ` Moving ${ sourceModelGenFile } to ${ targetModelGenFile } ` ) ;
fs . copyFileSync ( sourceModelGenFile , targetModelGenFile ) ;
fs . unlinkSync ( sourceModelGenFile ) ;
} else {
console . log ( ` WARNING: Source file not found: ${ sourceModelGenFile } ` ) ;
}
console . log ( ` Processing completed for table: ${ table } ` ) ;
} catch ( error ) {
console . error ( ` ERROR processing table: ${ table } ` ) ;
console . error ( error . message ) ;
console . error ( error . stack ) ;
}
}
/ * *
* 主函数
* /
function main ( ) {
try {
// 确保目录存在
ensureDirectoryExists ( config . outputDir ) ;
ensureDirectoryExists ( config . targetDir ) ;
// 为每个表生成模型
for ( const table of config . tables ) {
generateModelForTable ( table ) ;
}
console . log ( '=========================================' ) ;
console . log ( 'Script execution completed.' ) ;
} catch ( error ) {
console . error ( 'ERROR in main execution:' ) ;
console . error ( error . message ) ;
console . error ( error . stack ) ;
process . exit ( 1 ) ;
}
}
// 执行主函数
main ( ) ;