Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto setup synchronous pragma when establish connection to database #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
6 changes: 5 additions & 1 deletion src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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 : {}
Expand Down