mirror of
https://github.com/karakeep-app/karakeep.git
synced 2026-10-10 20:49:25 +02:00
40 lines
790 B
TypeScript
40 lines
790 B
TypeScript
import Database from "better-sqlite3";
|
|
|
|
interface OpenSqliteOptions {
|
|
readOnly: boolean;
|
|
walMode: boolean;
|
|
}
|
|
|
|
export function openSqliteDatabase(
|
|
filename: string,
|
|
options: OpenSqliteOptions,
|
|
) {
|
|
const sqlite = new Database(
|
|
filename,
|
|
options.readOnly
|
|
? {
|
|
readonly: true,
|
|
fileMustExist: true,
|
|
}
|
|
: undefined,
|
|
);
|
|
|
|
if (!options.readOnly) {
|
|
if (options.walMode) {
|
|
sqlite.pragma("journal_mode = WAL");
|
|
sqlite.pragma("synchronous = NORMAL");
|
|
} else {
|
|
sqlite.pragma("journal_mode = DELETE");
|
|
}
|
|
}
|
|
sqlite.pragma("cache_size = -65536");
|
|
sqlite.pragma("foreign_keys = ON");
|
|
sqlite.pragma("temp_store = MEMORY");
|
|
|
|
if (options.readOnly) {
|
|
sqlite.pragma("query_only = ON");
|
|
}
|
|
|
|
return sqlite;
|
|
}
|