Skip to content

Commit

Permalink
fix: mysql init bug
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyzak committed Aug 14, 2023
1 parent 3709e45 commit 6a3823b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/db/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@ class MySqlClient {
Object.assign(options, {})
)

this.testConnection = this.testConnection.bind(this)
this.initialize = this.initialize.bind(this)

this.instance = this.initialize()
this.testConnection()
}

async testConnection() {
try {
// 测试数据库连接
await this.sequelize.authenticate()
logger.info(
'Database connection has been established successfully.'
)
} catch (error) {
logger.error('Unable to connect to the database:', error)
}
}

initialize() {
Expand All @@ -48,6 +64,8 @@ class MySqlClient {
force: false
})
db[model.name] = model

logger.info('Loaded DB model: ' + model.name)
} else {
db[file] = {}
fs.readdirSync(path.join(this.modelDir, file))
Expand All @@ -60,6 +78,7 @@ class MySqlClient {
file,
f
))(this.sequelize)

//如果表不存在,则创建表
model.sync({
force: false
Expand Down
2 changes: 1 addition & 1 deletion models/mysql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let client

if (config.app.mysql) {
const MySqlDbClient = require('../../lib/db/mysql')
const client = new MySqlDbClient(config.db.mysql, __dirname)
client = new MySqlDbClient(config.db.mysql, __dirname)
}

module.exports.client = client
Expand Down

0 comments on commit 6a3823b

Please sign in to comment.