mirror of
https://github.com/hanzoai/better-sqlite3-multiple-ciphers.git
synced 2026-09-24 16:57:19 +00:00
fixed database subclassing
This commit is contained in:
+1
-1
@@ -9,7 +9,7 @@ const {
|
||||
} = require('bindings')('better_sqlite3.node');
|
||||
|
||||
function Database(filenameGiven, options) {
|
||||
if (new.target !== Database) {
|
||||
if (new.target == null) {
|
||||
return new Database(filenameGiven, options);
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ describe('new Database()', function () {
|
||||
});
|
||||
it('should allow disk-bound databases to be created', function () {
|
||||
expect(existsSync(util.next())).to.be.false;
|
||||
const db = this.db = Database(util.current());
|
||||
const db = this.db = new Database(util.current());
|
||||
expect(db.name).to.equal(util.current());
|
||||
expect(db.memory).to.be.false;
|
||||
expect(db.readonly).to.be.false;
|
||||
@@ -131,4 +131,29 @@ describe('new Database()', function () {
|
||||
expect(Database.prototype.close).to.equal(db.close);
|
||||
expect(Database.prototype).to.equal(Object.getPrototypeOf(db));
|
||||
});
|
||||
it('should work properly when called as a function', function () {
|
||||
const db = this.db = Database(util.next());
|
||||
expect(db).to.be.an.instanceof(Database);
|
||||
expect(db.constructor).to.equal(Database);
|
||||
expect(Database.prototype.close).to.equal(db.close);
|
||||
expect(Database.prototype).to.equal(Object.getPrototypeOf(db));
|
||||
});
|
||||
it('should work properly when subclassed', function () {
|
||||
class MyDatabase extends Database {
|
||||
foo() {
|
||||
return 999;
|
||||
}
|
||||
}
|
||||
const db = this.db = new MyDatabase(util.next());
|
||||
expect(db).to.be.an.instanceof(Database);
|
||||
expect(db).to.be.an.instanceof(MyDatabase);
|
||||
expect(db.constructor).to.equal(MyDatabase);
|
||||
expect(Database.prototype.close).to.equal(db.close);
|
||||
expect(MyDatabase.prototype.close).to.equal(db.close);
|
||||
expect(Database.prototype.foo).to.be.undefined;
|
||||
expect(MyDatabase.prototype.foo).to.equal(db.foo);
|
||||
expect(Database.prototype).to.equal(Object.getPrototypeOf(MyDatabase.prototype));
|
||||
expect(MyDatabase.prototype).to.equal(Object.getPrototypeOf(db));
|
||||
expect(db.foo()).to.equal(999);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user