From ff1acb823a3ac4b07b1b88cfedac77e6ef7bcf1e Mon Sep 17 00:00:00 2001 From: Beeno Tung Date: Wed, 10 Apr 2024 01:45:49 +0800 Subject: [PATCH] feat: auto setup synchronous pragma when establish connection to database --- index.d.ts | 2 ++ src/database.js | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index b18ee90..9cfb933 100644 --- a/index.d.ts +++ b/index.d.ts @@ -22,6 +22,8 @@ declare namespace BetterSqlite3Helper { fileMustExist?: boolean; /** Whether to automatically enable 'PRAGMA journal_mode = WAL'. Default: true */ WAL?: boolean; + /** Automatically enable 'PRAGMA synchronous = ?'. Default: 'NORMAL' for WAL, 'FULL' for non-WAL */ + synchronous?: 0 | 'OFF' | 1 | 'NORMAL' | 2 | 'FULL' | 3 | 'EXTRA'; /** Migration options. Disable completely by setting `migrate: false` */ migrate?: MigrationOptions | false; }; diff --git a/src/database.js b/src/database.js index 2972b5b..75e520e 100644 --- a/src/database.js +++ b/src/database.js @@ -31,13 +31,16 @@ function DB (options = {}) { instance = instance || new DB(...arguments) return instance } + let WAL = options.WAL || true + let synchronous = WAL ? 'NORMAL' : 'FULL' this.options = Object.assign( { path: dbFile, migrate: true, readonly: false, fileMustExist: false, - WAL: true + WAL, + synchronous, }, options ) @@ -98,6 +101,7 @@ DB.prototype.connection = function () { if (this.options.WAL) { this.db.pragma('journal_mode = WAL') } + this.db.pragma('synchronous = ' + this.options.synchronous) if (this.options.migrate) { this.migrate( typeof this.options.migrate === 'object' ? this.options.migrate : {}