allow overriding ooye data directory

This commit is contained in:
bbedward 2025-09-08 22:16:17 -04:00
commit 42b331135d
11 changed files with 37 additions and 18 deletions

View file

@ -12,6 +12,7 @@ if (!channelID) {
const assert = require("assert/strict")
const sqlite = require("better-sqlite3")
const path = require("path")
const backfill = new sqlite("scripts/backfill.db")
backfill.prepare("CREATE TABLE IF NOT EXISTS backfill (channel_id TEXT NOT NULL, message_id INTEGER NOT NULL, PRIMARY KEY (channel_id, message_id))").run()
@ -19,9 +20,10 @@ const HeatSync = require("heatsync")
const {reg} = require("../src/matrix/read-registration")
const passthrough = require("../src/passthrough")
const {getDatabase} = require("../src/db/database")
const sync = new HeatSync({watchFS: false})
const db = new sqlite("ooye.db")
const db = getDatabase()
Object.assign(passthrough, {sync, db})
const DiscordClient = require("../src/d2m/discord-client")

View file

@ -4,10 +4,9 @@
// Trigger the database migration flow and exit after committing.
// You can use this to run migrations locally and check the result using sqlitebrowser.
const sqlite = require("better-sqlite3")
const passthrough = require("../src/passthrough")
const db = new sqlite("ooye.db")
const {getDatabase} = require("../src/db/database")
const db = getDatabase()
const migrate = require("../src/db/migrate")
Object.assign(passthrough, {db})

View file

@ -16,7 +16,8 @@ const oldAT = reg.old_bridge.as_token
const newAT = reg.as_token
const oldDB = new sqlite(reg.old_bridge.database)
const db = new sqlite("ooye.db")
const {getDatabase} = require("../src/db/database")
const db = getDatabase()
db.exec(`CREATE TABLE IF NOT EXISTS half_shot_migration (
discord_channel TEXT NOT NULL,

View file

@ -3,8 +3,8 @@
const HeatSync = require("heatsync")
const sync = new HeatSync({watchFS: false})
const sqlite = require("better-sqlite3")
const db = new sqlite("db/ooye.db")
const {getDatabase} = require("../src/db/database")
const db = getDatabase()
const passthrough = require("../src/passthrough")
Object.assign(passthrough, {db, sync})

View file

@ -1,12 +1,12 @@
#!/usr/bin/env node
// @ts-check
const sqlite = require("better-sqlite3")
const HeatSync = require("heatsync")
const {reg} = require("../src/matrix/read-registration")
const passthrough = require("../src/passthrough")
const db = new sqlite("ooye.db")
const {getDatabase} = require("../src/db/database")
const db = getDatabase()
const sync = new HeatSync({watchFS: false})

View file

@ -1,11 +1,11 @@
#!/usr/bin/env node
// @ts-check
const sqlite = require("better-sqlite3")
const HeatSync = require("heatsync")
const passthrough = require("../src/passthrough")
const db = new sqlite("ooye.db")
const {getDatabase} = require("../src/db/database")
const db = getDatabase()
const sync = new HeatSync({watchFS: false})

View file

@ -34,7 +34,8 @@ if (fs.existsSync("db")) {
}
const passthrough = require("../src/passthrough")
const db = new sqlite("ooye.db")
const {getDatabase} = require("../src/db/database")
const db = getDatabase()
const migrate = require("../src/db/migrate")
const sync = new HeatSync({watchFS: false})

View file

@ -4,13 +4,13 @@
const {createServer} = require("http")
const EventEmitter = require("events")
const {createApp, createRouter, toNodeListener} = require("h3")
const sqlite = require("better-sqlite3")
const migrate = require("../src/db/migrate")
const HeatSync = require("heatsync")
const {reg} = require("../src/matrix/read-registration")
const passthrough = require("../src/passthrough")
const db = new sqlite("ooye.db")
const {getDatabase} = require("../src/db/database")
const db = getDatabase()
const sync = new HeatSync()

View file

@ -1,7 +1,7 @@
#!/usr/bin/env node
// @ts-check
const sqlite = require("better-sqlite3")
const db = new sqlite("ooye.db", {fileMustExist: true})
const {getDatabase} = require("../src/db/database")
const db = getDatabase({fileMustExist: true})
db.pragma("journal_mode = wal")
db.close()

16
src/db/database.js Normal file
View file

@ -0,0 +1,16 @@
// @ts-check
const sqlite = require("better-sqlite3")
const path = require("path")
/**
* Create a new SQLite database instance
* @param {import("better-sqlite3").Options} [options] - SQLite options
* @returns {import("better-sqlite3").Database} Database instance
*/
function getDatabase(options = {}) {
const dataDir = process.env.OOYE_DATA_DIR || process.cwd()
return new sqlite(path.join(dataDir, "ooye.db"), options)
}
module.exports = {getDatabase}

View file

@ -2,13 +2,13 @@
// @ts-check
const fs = require("fs")
const sqlite = require("better-sqlite3")
const migrate = require("./src/db/migrate")
const HeatSync = require("heatsync")
const {reg} = require("./src/matrix/read-registration")
const passthrough = require("./src/passthrough")
const db = new sqlite("ooye.db")
const {getDatabase} = require("./src/db/database")
const db = getDatabase()
const sync = new HeatSync({watchFunction: fs.watchFile})