Skip to content

Commit

Permalink
Rename mode to flags
Browse files Browse the repository at this point in the history
Signed-off-by: Boelensman1 <me@wiggerboelens.com>
  • Loading branch information
Boelensman1 committed Apr 19, 2021
1 parent 28cb666 commit 99c72a1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
18 changes: 9 additions & 9 deletions lib/dialects/sqlite3/index.js
Expand Up @@ -63,23 +63,23 @@ class Client_SQLite3 extends Client {
acquireRawConnection() {
return new Promise((resolve, reject) => {
// the default mode for sqlite3
let mode = this.driver.OPEN_READWRITE | this.driver.OPEN_CREATE;
let flags = this.driver.OPEN_READWRITE | this.driver.OPEN_CREATE;

if (this.connectionSettings.mode) {
if (!Array.isArray(this.connectionSettings.mode)) {
throw new Error(`Mode must be an array of strings`);
if (this.connectionSettings.flags) {
if (!Array.isArray(this.connectionSettings.flags)) {
throw new Error(`flags must be an array of strings`);
}
this.connectionSettings.mode.forEach((_mode) => {
if (!_mode.startsWith('OPEN_') || !this.driver[_mode]) {
throw new Error(`Mode ${_mode} not supported by node-sqlite3`);
this.connectionSettings.flags.forEach((_flags) => {
if (!_flags.startsWith('OPEN_') || !this.driver[_flags]) {
throw new Error(`flags ${_flags} not supported by node-sqlite3`);
}
mode = mode | this.driver[_mode];
flags = flags | this.driver[_flags];
});
}

const db = new this.driver.Database(
this.connectionSettings.filename,
mode,
flags,
(err) => {
if (err) {
return reject(err);
Expand Down
Empty file added memdb-test
Empty file.
16 changes: 8 additions & 8 deletions test/tape/knex.js
Expand Up @@ -67,14 +67,14 @@ test('it should use knex supported dialect', function (t) {
knexObj.destroy();
});

test('it should support mode selection for sqlite3', function (t) {
test('it should support open flags selection for sqlite3', function (t) {
t.plan(1);
const knexObj = knex({
client: 'sqlite3',
connection: {
filename: 'file:memdb-test?mode=memory',
// allow the filename to be interpreted as a URI
mode: ['OPEN_URI'],
flags: ['OPEN_URI'],
},
});
// run a query so a connection is created
Expand All @@ -89,15 +89,15 @@ test('it should support mode selection for sqlite3', function (t) {
});
});

test('it should error when invalid mode is selected for sqlite3', function (t) {
test('it should error when invalid open flags are selected for sqlite3', function (t) {
t.plan(2);

// Test invalid mode
// Test invalid flags
let knexObj = knex({
client: 'sqlite3',
connection: {
filename: ':memory:',
mode: ['NON_EXISTING'],
flags: ['NON_EXISTING'],
},
});
// run a query so a connection is created
Expand All @@ -107,7 +107,7 @@ test('it should error when invalid mode is selected for sqlite3', function (t) {
t.fail('Should not get here');
})
.catch((err) => {
t.equal(err.message, 'Mode NON_EXISTING not supported by node-sqlite3');
t.equal(err.message, 'flags NON_EXISTING not supported by node-sqlite3');
})
.finally(() => {
knexObj.destroy();
Expand All @@ -118,7 +118,7 @@ test('it should error when invalid mode is selected for sqlite3', function (t) {
client: 'sqlite3',
connection: {
filename: ':memory:',
mode: 'OPEN_URI',
flags: 'OPEN_URI',
},
});
// run a query so a connection is created
Expand All @@ -128,7 +128,7 @@ test('it should error when invalid mode is selected for sqlite3', function (t) {
t.fail('Should not get here');
})
.catch((err) => {
t.equal(err.message, 'Mode must be an array of strings');
t.equal(err.message, 'flags must be an array of strings');
})
.finally(() => {
knexObj.destroy();
Expand Down

0 comments on commit 99c72a1

Please sign in to comment.