knex.js - 带有 Sqlite 的 Knex 是否需要销毁才能运行多个查询?
问题描述
我有一个脚本,我正在尝试使用 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
请注意,无论是还是then
永远都不会通过。catch
createSchemaVersion
如果我在.finally(() => knex.destroy())
每个 knex 操作中添加一个 - 那么一切正常,并且我得到了我所期望的完整日志,但是由于 knex 对象已被破坏,遗物创建显然会爆炸。所以,如果我在每个函数中重新制作 knex 对象 - 那么一切正常。
但这似乎是错误的。看来我应该能够重新使用 knex 对象。
这里发生了什么?您真的每次都必须销毁/重新创建 knex 对象吗?
解决方案
对于后来偶然发现的任何人,这是因为我是从电子渲染器进程而不是 main 调用这个脚本。我找不到任何错误或消息 - 但在另一位开发人员的建议下,我尝试从 Main.js 调用相同的脚本,它工作得很好。
这可能是一个新手 Electron 的错误——但这就是我所拥有的。
推荐阅读
- python-3.x - QMenu 仅在调用 QPushButton.menu() 时显示在 QPushButton
- php - 提交表单后变量未存储
- internet-explorer - Mat-icons 在 Internet Explorer 中不起作用
- javascript - MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型
- javascript - Python 请求模块在本地提供“请启用 JavaScript 以查看页面内容”但在 AMI 和 Heroku 服务器上工作
- c++ - 使用链表在 C++ 中实现堆栈
- javascript - Jquery在正常刷新时不加载
- java - 当映射包含 DTO 列表的对象成功时,为什么映射 DTO 列表会失败?
- python - urls.py 到不同的项目 django
- python - 脚本在 shell 中工作,但不能作为 IDLE 文件