Skip to content

Commit a5fe469

Browse files
committed
allow temporary databases and treat them like in-memory databases
1 parent 971c12d commit a5fe469

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lib/database.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ const util = require('./util');
55
const CPPDatabase = require('../build/better_sqlite3.node').Database;
66

77
function Database(filenameGiven, options) {
8+
if (filenameGiven == null) filenameGiven = '';
89
if (options == null) options = {};
910
if (typeof filenameGiven !== 'string') throw new TypeError('Expected first argument to be a string');
1011
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
1112

1213
let filename = filenameGiven.trim();
13-
if (!filename) throw new TypeError('Database filename cannot be an empty string');
1414
if (filename.toLowerCase().startsWith('file:')) throw new TypeError('URI filenames are reserved for internal use only');
1515
if ('readOnly' in options) throw new TypeError('Misspelled option "readOnly" should be "readonly"');
1616

17-
const anonymous = filename === ':memory:';
17+
const anonymous = filename === '' || filename === ':memory:';
1818
const memory = util.getBooleanOption(options, 'memory');
1919
const readonly = util.getBooleanOption(options, 'readonly');
2020
const fileMustExist = util.getBooleanOption(options, 'fileMustExist');
2121
const timeout = 'timeout' in options ? options.timeout : 5000;
2222

2323
if (readonly && (memory || anonymous)) throw new TypeError('In-memory databases cannot be readonly');
24-
if (anonymous && !memory && 'memory' in options) throw new TypeError('Option "memory" conflicts with :memory: filename');
24+
if (anonymous && !memory && 'memory' in options) throw new TypeError('Option "memory" conflicts with non-existent filename');
2525
if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer');
2626
if (timeout > 0x7fffffff) throw new RangeError('Option "timeout" cannot be greater than 2147483647');
2727

test/10.database.open.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ const { existsSync } = require('fs');
33
const Database = require('../.');
44

55
describe('new Database()', function () {
6-
it('should throw when file path is not a string', function () {
7-
expect(() => new Database()).to.throw(TypeError);
8-
expect(() => new Database(null)).to.throw(TypeError);
6+
it('should throw when given invalid argument types', function () {
7+
expect(() => new Database('', '')).to.throw(TypeError);
8+
expect(() => new Database({}, '')).to.throw(TypeError);
9+
expect(() => new Database({}, {})).to.throw(TypeError);
10+
expect(() => new Database({})).to.throw(TypeError);
911
expect(() => new Database(0)).to.throw(TypeError);
1012
expect(() => new Database(123)).to.throw(TypeError);
1113
expect(() => new Database(new String(util.next()))).to.throw(TypeError);
1214
expect(() => new Database(() => util.next())).to.throw(TypeError);
1315
expect(() => new Database([util.next()])).to.throw(TypeError);
1416
});
15-
it('should throw when file path is empty', function () {
16-
expect(() => new Database('')).to.throw(TypeError);
17-
});
1817
it('should throw when boolean options are provided as non-booleans', function () {
1918
expect(() => new Database(util.next(), { readOnly: false })).to.throw(TypeError);
2019
expect(() => new Database(util.next(), { readonly: undefined })).to.throw(TypeError);
@@ -26,6 +25,20 @@ describe('new Database()', function () {
2625
expect(() => new Database(`file:${util.next()}`)).to.throw(TypeError);
2726
expect(() => new Database(`file:${util.next()}?mode=memory&cache=shared`)).to.throw(TypeError);
2827
});
28+
it('should allow anonymous temporary databases to be created', function () {
29+
for (const args of [[''], [], [null], [undefined], ['', { timeout: 2000 }]]) {
30+
const db = new Database(...args);
31+
expect(db.name).to.equal('');
32+
expect(db.memory).to.be.true;
33+
expect(db.readonly).to.be.false;
34+
expect(db.open).to.be.true;
35+
expect(db.inTransaction).to.be.false;
36+
expect(existsSync('')).to.be.false;
37+
expect(existsSync('null')).to.be.false;
38+
expect(existsSync('undefined')).to.be.false;
39+
expect(existsSync('[object Object]')).to.be.false;
40+
}
41+
});
2942
it('should allow anonymous in-memory databases to be created', function () {
3043
const db = new Database(':memory:');
3144
expect(db.name).to.equal(':memory:');
@@ -57,7 +70,9 @@ describe('new Database()', function () {
5770
});
5871
it('should not allow conflicting in-memory options', function () {
5972
expect(() => new Database(':memory:', { memory: false })).to.throw(TypeError);
73+
expect(() => new Database('', { memory: false })).to.throw(TypeError);
6074
(new Database(':memory:', { memory: true })).close();
75+
(new Database('', { memory: true })).close();
6176
});
6277
it('should allow readonly database connections to be created', function () {
6378
expect(existsSync(util.next())).to.be.false;
@@ -76,6 +91,7 @@ describe('new Database()', function () {
7691
expect(existsSync(util.next())).to.be.false;
7792
expect(() => new Database(util.current(), { memory: true, readonly: true })).to.throw(TypeError);
7893
expect(() => new Database(':memory:', { readonly: true })).to.throw(TypeError);
94+
expect(() => new Database('', { readonly: true })).to.throw(TypeError);
7995
expect(existsSync(util.current())).to.be.false;
8096
});
8197
it('should accept the "fileMustExist" option', function () {

0 commit comments

Comments
 (0)