-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.cjs
More file actions
55 lines (48 loc) · 1.46 KB
/
db.cjs
File metadata and controls
55 lines (48 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// db.cjs - データベース操作モジュール
// ネイティブモジュールはasar外に展開されるため、パスを補正
const path = require('path');
let betterSqlitePath;
try {
betterSqlitePath = require.resolve('better-sqlite3');
} catch {
// asar環境ではapp.asar内のパスが解決できないため、app.asar.unpackedを参照
betterSqlitePath = path.join(__dirname.replace('app.asar', 'app.asar.unpacked'), 'node_modules', 'better-sqlite3');
}
const Database = require(betterSqlitePath);
let db;
/**
* データベースを初期化する
* @param {string} dbPath - データベースファイルのパス
*/
function initialize(dbPath) {
db = new Database(dbPath);
db.pragma('journal_mode = WAL');
db.exec(`CREATE TABLE IF NOT EXISTS videos (
path TEXT PRIMARY KEY,
name TEXT,
thumbnail TEXT,
preview TEXT,
codec TEXT,
width INTEGER,
height INTEGER,
fps TEXT,
tags TEXT
)`);
}
/** SQL実行(INSERT/UPDATE/DELETE用) */
const run = (sql, params = []) => {
return db.prepare(sql).run(...params);
};
/** 単一行取得 */
const get = (sql, params = []) => {
return db.prepare(sql).get(...params);
};
/** 複数行取得 */
const all = (sql, params = []) => {
return db.prepare(sql).all(...params);
};
/** データベースを閉じる */
function close() {
if (db) db.close();
}
module.exports = { initialize, run, get, all, close };