gpt4 book ai didi

javascript - 如何配置 Sequelize ORM 以执行序列化查询

转载 作者:行者123 更新时间:2023-12-03 22:30:12 26 4
gpt4 key购买 nike

我正在开发一个 Electron 应用程序,它同时执行诸如插入、删除、更新等写入查询。我使用的是单一威胁数据库 SQLite,因此如果应用程序同时触发多个写入查询,则 SQLite 数据库会显示 Database is locked 错误。那么如何配置 sequelize 来执行序列化查询呢?

const { app } = require('electron')
const { Sequelize } = require('sequelize')
const path = require('path')
const cls = require('cls-hooked')

const namespace = cls.createNamespace('P&C.Beejak')
Sequelize.useCLS(namespace)

const db = new Sequelize({
dialect: 'sqlite',
storage: path.join(app.getPath('userData') + '/database', 'database.db'),
})

module.exports = db

最佳答案

使用具有/正确隔离方法的事务:https://sequelize.org/master/manual/transactions.html

// You can overwrite the isolationLevel setting globally with an option in the Sequelize constructor:


const { Sequelize, Transaction } = require('sequelize');

const sequelize = new Sequelize('sqlite::memory:', {
isolationLevel: Transaction.ISOLATION_LEVELS.SERIALIZABLE
});

关于javascript - 如何配置 Sequelize ORM 以执行序列化查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67705511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com