【发布时间】:2020-06-10 01:56:14
【问题描述】:
我有一个脚本,我正在尝试使用 Knex 和 Sqlite3 生成我的数据库模式。如下所示:
schema.js
const log = require('electron-log');
const app = require('electron').remote.app;
let knex = require('knex')({
client: 'sqlite3',
connection: {
filename: app.getPath('userData') + '/warframeData.db',
},
});
async function create() {
log.debug('Creating schema.');
await createSchemaVersion()
.then(createRelicTable)
.catch((err) => log.debug(err));
}
function createSchemaVersion() {
log.debug('calling Create Schema Version');
return knex.schema
.createTableIfNotExists('schemaVersion', function (table) {
table.string('name').unique().notNullable();
table.integer('major').notNullable();
table.integer('minor').notNullable();
table.integer('patch').notNullable();
})
.then(() => {
log.debug('Table schemaVersion created');
})
.catch((err) => {
log.error('Error creating table schemaVersion', err);
})
}
function createRelicTable() {
log.debug('calling Create Relics');
return knex.schema
.createTableIfNotExists('relics', function (table) {
table.string('name').unique().notNullable().primary();
table.integer('tradable').notNullable();
table.string('url').notNullable();
table.string('image').notNullable();
table.integer('vaulted').notNullable();
})
.then(() => {
log.debug('Table relics created');
})
.catch((err) => {
log.error('Error creating table relics', err);
});
当我运行它时,数据库被创建,schemaVersion 表被创建,但随后它挂起。日志只产生这个:
[2020-06-09 10:56:17.073] [debug] Creating schema.
[2020-06-09 10:56:17.074] [debug] calling Create Schema Version
请注意,createSchemaVersion 的 then 和 catch 都不会出现。
如果我在每个 knex 操作中添加 .finally(() => knex.destroy()) - 那么一切正常并且我得到了我所期望的完整日志,但是由于 knex 对象已被破坏,遗物创建显然会爆炸。所以,如果我在每个函数中重新制作 knex 对象 - 那么一切正常。
但这似乎是错误的。看来我应该能够重新使用 knex 对象。
这里发生了什么?您真的每次都必须销毁/重新创建 knex 对象吗?
【问题讨论】:
标签: knex.js node-sqlite3