|
|
@ -3,7 +3,6 @@ const { sleep, debug, isDev } = require("../src/util"); |
|
|
|
const { R } = require("redbean-node"); |
|
|
|
const { setSetting, setting } = require("./util-server"); |
|
|
|
const knex = require("knex"); |
|
|
|
const sqlite3 = require("@louislam/sqlite3"); |
|
|
|
|
|
|
|
class Database { |
|
|
|
|
|
|
@ -14,50 +13,23 @@ class Database { |
|
|
|
static sqliteInstance = null; |
|
|
|
|
|
|
|
static async connect() { |
|
|
|
|
|
|
|
if (! this.sqliteInstance) { |
|
|
|
this.sqliteInstance = new sqlite3.Database(Database.path); |
|
|
|
this.sqliteInstance.run("PRAGMA journal_mode = WAL"); |
|
|
|
} |
|
|
|
|
|
|
|
const Dialect = require("knex/lib/dialects/sqlite3/index.js"); |
|
|
|
Dialect.prototype._driver = () => sqlite3; |
|
|
|
|
|
|
|
if (isDev) { |
|
|
|
Dialect.prototype.acquireConnectionOrg = Dialect.prototype.acquireConnection; |
|
|
|
|
|
|
|
Dialect.prototype.acquireConnection = async function () { |
|
|
|
let a = await this.acquireConnectionOrg(); |
|
|
|
debug("acquired Connection"); |
|
|
|
return a; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
// Always return same connection.
|
|
|
|
Dialect.prototype.acquireRawConnection = async function () { |
|
|
|
return Database.sqliteInstance; |
|
|
|
}; |
|
|
|
|
|
|
|
Dialect.prototype.destroyRawConnection = async () => { } |
|
|
|
|
|
|
|
const acquireConnectionTimeout = 120 * 1000; |
|
|
|
|
|
|
|
const knexInstance = knex({ |
|
|
|
client: Dialect, |
|
|
|
connection: { }, // Do not remove, Leave it empty is ok
|
|
|
|
R.useBetterSQLite3 = true; |
|
|
|
R.betterSQLite3Options.timeout = acquireConnectionTimeout; |
|
|
|
|
|
|
|
R.setup("sqlite", { |
|
|
|
filename: Database.path, |
|
|
|
useNullAsDefault: true, |
|
|
|
acquireConnectionTimeout: acquireConnectionTimeout, |
|
|
|
pool: { |
|
|
|
min: 1, |
|
|
|
max: 1, |
|
|
|
idleTimeoutMillis: 120 * 1000, |
|
|
|
propagateCreateError: false, |
|
|
|
acquireTimeoutMillis: acquireConnectionTimeout, |
|
|
|
} |
|
|
|
}, { |
|
|
|
min: 1, |
|
|
|
max: 1, |
|
|
|
idleTimeoutMillis: 120 * 1000, |
|
|
|
propagateCreateError: false, |
|
|
|
acquireTimeoutMillis: acquireConnectionTimeout, |
|
|
|
}); |
|
|
|
|
|
|
|
R.setup(knexInstance); |
|
|
|
|
|
|
|
if (process.env.SQL_LOG === "1") { |
|
|
|
R.debug(true); |
|
|
|
} |
|
|
@ -65,6 +37,10 @@ class Database { |
|
|
|
// Auto map the model to a bean object
|
|
|
|
R.freeze(true) |
|
|
|
await R.autoloadModels("./server/model"); |
|
|
|
|
|
|
|
// Change to WAL
|
|
|
|
await R.exec("PRAGMA journal_mode = WAL"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
static async patch() { |
|
|
|