fixed database subclassing

This commit is contained in:
Joshua Wise
2021-04-14 17:53:42 -05:00
parent 594d261e1e
commit acfda67fd7
2 changed files with 27 additions and 2 deletions
+1 -1
View File
@@ -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);
}
+26 -1
View File
@@ -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);
});
});