better-sqlite3
Advanced tools
Comparing version 1.4.0 to 2.0.0
@@ -7,18 +7,15 @@ 'use strict'; | ||
module.exports = function (sql, name, callback) { | ||
var opened = 0; | ||
var ourDb = new ours(path.join('temp', name + '.ours.db')).on('open', open); | ||
var theirDb = new theirs.Database(path.join('temp', name + '.theirs.db'), open); | ||
var ourDb = new ours(path.join('temp', name + '.ours.db')); | ||
var theirDb = new theirs.Database(path.join('temp', name + '.theirs.db'), opened); | ||
function open() { | ||
if (++opened === 2) { | ||
ourDb.prepare(sql).run(); | ||
theirDb.run(sql, function (err) { | ||
if (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
callback(ourDb, theirDb); | ||
}); | ||
} | ||
function opened() { | ||
ourDb.prepare(sql).run(); | ||
theirDb.run(sql, function (err) { | ||
if (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
callback(ourDb, theirDb); | ||
}); | ||
} | ||
}; |
@@ -7,9 +7,5 @@ 'use strict'; | ||
module.exports = function (name, callback) { | ||
var opened = 0; | ||
ours = new ours(path.join('temp', name + '.ours.db')).on('open', open); | ||
theirs = new theirs.Database(path.join('temp', name + '.theirs.db'), open); | ||
function open() { | ||
++opened === 2 && pragma(); | ||
} | ||
function pragma() { | ||
ours = new ours(path.join('temp', name + '.ours.db')); | ||
theirs = new theirs.Database(path.join('temp', name + '.theirs.db'), opened); | ||
function opened() { | ||
ours.pragma('cache_size = -16000;'); | ||
@@ -16,0 +12,0 @@ theirs.run('PRAGMA cache_size = -16000;', function () { |
@@ -13,8 +13,4 @@ 'use strict'; | ||
function callback2() { | ||
var closedCount = 0; | ||
ourDb.on('close', closed).close(); | ||
theirDb.close(closed); | ||
function closed() { | ||
++closedCount === 2 && process.exit(); | ||
} | ||
ourDb.close(); | ||
theirDb.close(process.exit); | ||
} | ||
@@ -21,0 +17,0 @@ setTimeout(callback0, 100); |
@@ -13,8 +13,4 @@ 'use strict'; | ||
function callback2() { | ||
var closedCount = 0; | ||
ourDb.on('close', closed).close(); | ||
theirDb.close(closed); | ||
function closed() { | ||
++closedCount === 2 && process.exit(); | ||
} | ||
ourDb.close(); | ||
theirDb.close(process.exit); | ||
} | ||
@@ -21,0 +17,0 @@ setTimeout(callback0, 100); |
@@ -28,8 +28,4 @@ 'use strict'; | ||
function callback2() { | ||
var closedCount = 0; | ||
ourDb.on('close', closed).close(); | ||
theirDb.close(closed); | ||
function closed() { | ||
++closedCount === 2 && process.exit(); | ||
} | ||
ourDb.close(); | ||
theirDb.close(process.exit); | ||
} | ||
@@ -36,0 +32,0 @@ setTimeout(callback0, 100); |
@@ -24,8 +24,4 @@ 'use strict'; | ||
function callback2() { | ||
var closedCount = 0; | ||
ourDb.on('close', closed).close(); | ||
theirDb.close(closed); | ||
function closed() { | ||
++closedCount === 2 && process.exit(); | ||
} | ||
ourDb.close(); | ||
theirDb.close(process.exit); | ||
} | ||
@@ -32,0 +28,0 @@ setTimeout(callback0, 100); |
@@ -24,8 +24,4 @@ 'use strict'; | ||
function callback2() { | ||
var closedCount = 0; | ||
ourDb.on('close', closed).close(); | ||
theirDb.close(closed); | ||
function closed() { | ||
++closedCount === 2 && process.exit(); | ||
} | ||
ourDb.close(); | ||
theirDb.close(process.exit); | ||
} | ||
@@ -32,0 +28,0 @@ setTimeout(callback0, 100); |
@@ -15,8 +15,4 @@ 'use strict'; | ||
function callback2() { | ||
var closedCount = 0; | ||
ourDb.on('close', closed).close(); | ||
theirDb.close(closed); | ||
function closed() { | ||
++closedCount === 2 && process.exit(); | ||
} | ||
ourDb.close(); | ||
theirDb.close(process.exit); | ||
} | ||
@@ -23,0 +19,0 @@ setTimeout(callback0, 100); |
@@ -13,8 +13,4 @@ 'use strict'; | ||
function callback2() { | ||
var closedCount = 0; | ||
ourDb.on('close', closed).close(); | ||
theirDb.close(closed); | ||
function closed() { | ||
++closedCount === 2 && process.exit(); | ||
} | ||
ourDb.close(); | ||
theirDb.close(process.exit); | ||
} | ||
@@ -21,0 +17,0 @@ setTimeout(callback0, 100); |
'use strict'; | ||
var path = require('path'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var toDescriptor = require('to-descriptor'); | ||
@@ -46,4 +45,3 @@ var CPPDatabase = require('bindings')({ | ||
Database.prototype = Object.create(Object.prototype, toDescriptor(CPPDatabase.prototype)); | ||
Object.setPrototypeOf(Database.prototype, EventEmitter.prototype); | ||
Object.setPrototypeOf(CPPDatabase.prototype, Database.prototype); | ||
module.exports = Database; |
{ | ||
"name": "better-sqlite3", | ||
"version": "1.4.0", | ||
"version": "2.0.0", | ||
"description": "The fastest and simplest library for SQLite3 in Node.js.", | ||
@@ -5,0 +5,0 @@ "homepage": "http://github.com/JoshuaWise/better-sqlite3", |
@@ -22,6 +22,4 @@ # better-sqlite3 [![Build Status](https://travis-ci.org/JoshuaWise/better-sqlite3.svg?branch=master)](https://travis-ci.org/JoshuaWise/better-sqlite3) | ||
db.on('open', function () { | ||
var row = db.prepare('SELECT * FROM users WHERE id=?').get(userId); | ||
console.log(row.firstName, row.lastName, row.email); | ||
}); | ||
var row = db.prepare('SELECT * FROM users WHERE id=?').get(userId); | ||
console.log(row.firstName, row.lastName, row.email); | ||
``` | ||
@@ -28,0 +26,0 @@ |
@@ -36,3 +36,3 @@ var expect = require('chai').expect; | ||
}); | ||
it('should allow disk-based databases to be created', function (done) { | ||
it('should allow disk-based databases to be created', function () { | ||
expect(function () {fs.accessSync(util.next());}).to.throw(Error); | ||
@@ -42,10 +42,6 @@ var db = new Database(util.current()); | ||
expect(db.memory).to.be.false; | ||
expect(db.open).to.be.false; | ||
db.on('open', function () { | ||
fs.accessSync(util.current()); | ||
expect(db.open).to.be.true; | ||
done(); | ||
}); | ||
expect(db.open).to.be.true; | ||
fs.accessSync(util.current()); | ||
}); | ||
it('should allow in-memory databases to be created', function (done) { | ||
it('should allow in-memory databases to be created', function () { | ||
expect(function () {fs.accessSync(util.next());}).to.throw(Error); | ||
@@ -55,21 +51,10 @@ var db = new Database(util.current(), {memory: true}); | ||
expect(db.memory).to.be.true; | ||
expect(db.open).to.be.false; | ||
db.on('open', function () { | ||
expect(function () {fs.accessSync(util.current());}).to.throw(Error); | ||
expect(db.open).to.be.true; | ||
done(); | ||
}); | ||
expect(db.open).to.be.true; | ||
expect(function () {fs.accessSync(util.current());}).to.throw(Error); | ||
}); | ||
it('should not allow the database to be used before it opens', function (done) { | ||
var db = new Database(util.next()); | ||
expect(db.open).to.be.false; | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT)');}).to.throw(Error); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)']);}).to.throw(Error); | ||
expect(function () {db.pragma('cache_size');}).to.throw(Error); | ||
expect(function () {db.checkpoint();}).to.throw(Error); | ||
db.on('open', function () { | ||
expect(db.open).to.be.true; | ||
done(); | ||
}); | ||
}); | ||
it('should throw an Error if opening the database failed', function () { | ||
expect(function () {fs.accessSync(util.next());}).to.throw(Error); | ||
expect(function () {new Database('temp/nonexistent/abcfoobar123/' + util.current());}) | ||
expect(function () {fs.accessSync(util.current());}).to.throw(Error); | ||
}) | ||
it('should have a proper prototype chain', function () { | ||
@@ -76,0 +61,0 @@ var db = new Database(util.next()); |
@@ -16,64 +16,24 @@ var expect = require('chai').expect; | ||
describe('Database#close()', function () { | ||
describe('before opening it', function () { | ||
it('should never emit the "open" event', function (done) { | ||
var db = new Database(util.next()); | ||
expect(db.open).to.be.false; | ||
db.on('open', function () { | ||
done(new Error('This event should not have been fired.')); | ||
}); | ||
db.on('close', function (err) { | ||
expect(err).to.be.null; | ||
expect(db.open).to.be.false; | ||
done(); | ||
}); | ||
expect(db.close()).to.equal(db); | ||
expect(db.open).to.be.false; | ||
}); | ||
it('should cause db.open to return false', function () { | ||
var db = new Database(util.next()); | ||
expect(db.open).to.be.true; | ||
db.close(); | ||
expect(db.open).to.be.false; | ||
}); | ||
describe('because of a failed open attempt', function () { | ||
it('should have an Error object as its first parameter', function (done) { | ||
var db = new Database('temp/nonexistent/abcfoobar123/' + util.next()); | ||
expect(db.open).to.be.false; | ||
db.on('open', function () { | ||
done(new Error('This event should not have been fired.')); | ||
}); | ||
db.on('close', function (err) { | ||
expect(err).to.be.an.instanceof(Error); | ||
expect(db.open).to.be.false; | ||
done(); | ||
}); | ||
}) | ||
it('should return the database object', function () { | ||
var db = new Database(util.next()); | ||
expect(db.open).to.be.true; | ||
expect(db.close()).to.equal(db); | ||
expect(db.open).to.be.false; | ||
expect(db.close()).to.equal(db); | ||
expect(db.open).to.be.false; | ||
}); | ||
describe('after opening it', function () { | ||
it('should emit the close event as normal', function (done) { | ||
var db = new Database(util.next()); | ||
expect(db.open).to.be.false; | ||
db.on('open', function () { | ||
expect(db.open).to.be.true; | ||
db.close(); | ||
expect(db.open).to.be.false; | ||
db.on('close', function (err) { | ||
expect(err).to.be.null; | ||
expect(db.open).to.be.false; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should prevent any further database operations', function (done) { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(db.open).to.be.true; | ||
db.close(); | ||
db.on('close', function (err) { | ||
expect(err).to.be.null; | ||
expect(db.open).to.be.false; | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT)');}).to.throw(Error); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)']);}).to.throw(Error); | ||
expect(function () {db.pragma('cache_size');}).to.throw(Error); | ||
expect(function () {db.checkpoint();}).to.throw(Error); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should prevent any further database operations', function () { | ||
var db = new Database(util.next()); | ||
db.close(); | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT)');}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)']);}).to.throw(TypeError); | ||
expect(function () {db.pragma('cache_size');}).to.throw(TypeError); | ||
expect(function () {db.checkpoint();}).to.throw(TypeError); | ||
}); | ||
}); |
@@ -16,64 +16,43 @@ var expect = require('chai').expect; | ||
describe('Database#pragma()', function () { | ||
it('should throw an exception if a string is not provided', function (done) { | ||
it('should throw an exception if a string is not provided', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.pragma(123);}).to.throw(TypeError); | ||
expect(function () {db.pragma(0);}).to.throw(TypeError); | ||
expect(function () {db.pragma(null);}).to.throw(TypeError); | ||
expect(function () {db.pragma();}).to.throw(TypeError); | ||
expect(function () {db.pragma(new String('cache_size'));}).to.throw(TypeError); | ||
done(); | ||
}); | ||
expect(function () {db.pragma(123);}).to.throw(TypeError); | ||
expect(function () {db.pragma(0);}).to.throw(TypeError); | ||
expect(function () {db.pragma(null);}).to.throw(TypeError); | ||
expect(function () {db.pragma();}).to.throw(TypeError); | ||
expect(function () {db.pragma(new String('cache_size'));}).to.throw(TypeError); | ||
}); | ||
it('should throw an exception if invalid/redundant SQL is provided', function (done) { | ||
it('should throw an exception if invalid/redundant SQL is provided', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.pragma('PRAGMA cache_size');}).to.throw(Error); | ||
done(); | ||
}); | ||
expect(function () {db.pragma('PRAGMA cache_size');}).to.throw(Error); | ||
}); | ||
it('should execute the pragma, returning rows of strings', function (done) { | ||
it('should execute the pragma, returning rows of strings', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
var rows = db.pragma('cache_size'); | ||
expect(rows[0].cache_size).to.be.a('string'); | ||
expect(rows[0].cache_size).to.equal('-16000'); | ||
done(); | ||
}); | ||
var rows = db.pragma('cache_size'); | ||
expect(rows[0].cache_size).to.be.a('string'); | ||
expect(rows[0].cache_size).to.equal('-16000'); | ||
}); | ||
it('should optionally return simpler results', function (done) { | ||
it('should optionally return simpler results', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
var cache_size = db.pragma('cache_size', true); | ||
expect(cache_size).to.be.a('string'); | ||
expect(cache_size).to.equal('-16000'); | ||
done(); | ||
}); | ||
var cache_size = db.pragma('cache_size', true); | ||
expect(cache_size).to.be.a('string'); | ||
expect(cache_size).to.equal('-16000'); | ||
}); | ||
it('should accept any truthy value to simplify results', function (done) { | ||
it('should accept any truthy value to simplify results', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(db.pragma('cache_size', {})).to.equal('-16000'); | ||
expect(db.pragma('cache_size', 123)).to.equal('-16000'); | ||
expect(db.pragma('cache_size', function () {})).to.equal('-16000'); | ||
expect(db.pragma('cache_size', NaN)).to.deep.equal([{cache_size: '-16000'}]); | ||
done(); | ||
}); | ||
expect(db.pragma('cache_size', {})).to.equal('-16000'); | ||
expect(db.pragma('cache_size', 123)).to.equal('-16000'); | ||
expect(db.pragma('cache_size', function () {})).to.equal('-16000'); | ||
expect(db.pragma('cache_size', NaN)).to.deep.equal([{cache_size: '-16000'}]); | ||
}); | ||
it('should obey PRAGMA changes', function (done) { | ||
it('should obey PRAGMA changes', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(db.pragma('cache_size', true)).to.equal('-16000'); | ||
db.pragma('cache_size = -8000'); | ||
expect(db.pragma('cache_size', true)).to.equal('-8000'); | ||
done(); | ||
}); | ||
expect(db.pragma('cache_size', true)).to.equal('-16000'); | ||
db.pragma('cache_size = -8000'); | ||
expect(db.pragma('cache_size', true)).to.equal('-8000'); | ||
}); | ||
it('should return undefined if no rows exist and simpler results are desired', function (done) { | ||
it('should return undefined if no rows exist and simpler results are desired', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(db.pragma('table_info', true)).to.be.undefined; | ||
done(); | ||
}); | ||
expect(db.pragma('table_info', true)).to.be.undefined; | ||
}); | ||
}); |
@@ -16,39 +16,27 @@ var expect = require('chai').expect; | ||
describe('Database#prepare()', function () { | ||
it('should throw an exception if a string is not provided', function (done) { | ||
it('should throw an exception if a string is not provided', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.prepare(123);}).to.throw(TypeError); | ||
expect(function () {db.prepare(0);}).to.throw(TypeError); | ||
expect(function () {db.prepare(null);}).to.throw(TypeError); | ||
expect(function () {db.prepare();}).to.throw(TypeError); | ||
expect(function () {db.prepare(new String('CREATE TABLE people (name TEXT)'));}).to.throw(TypeError); | ||
done(); | ||
}); | ||
expect(function () {db.prepare(123);}).to.throw(TypeError); | ||
expect(function () {db.prepare(0);}).to.throw(TypeError); | ||
expect(function () {db.prepare(null);}).to.throw(TypeError); | ||
expect(function () {db.prepare();}).to.throw(TypeError); | ||
expect(function () {db.prepare(new String('CREATE TABLE people (name TEXT)'));}).to.throw(TypeError); | ||
}); | ||
it('should throw an exception if invalid SQL is provided', function (done) { | ||
it('should throw an exception if invalid SQL is provided', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT');}).to.throw(Error); | ||
expect(function () {db.prepare('INSERT INTO people VALUES (?)');}).to.throw(Error); | ||
done(); | ||
}); | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT');}).to.throw(Error); | ||
expect(function () {db.prepare('INSERT INTO people VALUES (?)');}).to.throw(Error); | ||
}); | ||
it('should throw an exception if no statements are provided', function (done) { | ||
it('should throw an exception if no statements are provided', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.prepare('');}).to.throw(TypeError); | ||
expect(function () {db.prepare(';');}).to.throw(TypeError); | ||
done(); | ||
}); | ||
expect(function () {db.prepare('');}).to.throw(TypeError); | ||
expect(function () {db.prepare(';');}).to.throw(TypeError); | ||
}); | ||
it('should throw an exception if more than one statement is provided', function (done) { | ||
it('should throw an exception if more than one statement is provided', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT);CREATE TABLE animals (name TEXT)');}).to.throw(TypeError); | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT); ');}).to.throw(TypeError); | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT);;');}).to.throw(TypeError); | ||
done(); | ||
}); | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT);CREATE TABLE animals (name TEXT)');}).to.throw(TypeError); | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT); ');}).to.throw(TypeError); | ||
expect(function () {db.prepare('CREATE TABLE people (name TEXT);;');}).to.throw(TypeError); | ||
}); | ||
it('should create a prepared Statement object', function (done) { | ||
it('should create a prepared Statement object', function () { | ||
function assertStmt(stmt, source) { | ||
@@ -64,26 +52,20 @@ expect(stmt.source).to.equal(source); | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
var stmt1 = db.prepare('CREATE TABLE people (name TEXT)'); | ||
var stmt2 = db.prepare('CREATE TABLE people (name TEXT);'); | ||
assertStmt(stmt1, 'CREATE TABLE people (name TEXT)'); | ||
assertStmt(stmt2, 'CREATE TABLE people (name TEXT);'); | ||
expect(stmt1).to.not.equal(stmt2); | ||
expect(stmt1).to.not.equal(db.prepare('CREATE TABLE people (name TEXT)')); | ||
done(); | ||
}); | ||
var stmt1 = db.prepare('CREATE TABLE people (name TEXT)'); | ||
var stmt2 = db.prepare('CREATE TABLE people (name TEXT);'); | ||
assertStmt(stmt1, 'CREATE TABLE people (name TEXT)'); | ||
assertStmt(stmt2, 'CREATE TABLE people (name TEXT);'); | ||
expect(stmt1).to.not.equal(stmt2); | ||
expect(stmt1).to.not.equal(db.prepare('CREATE TABLE people (name TEXT)')); | ||
}); | ||
it('should create a prepared Statement object with just an expression', function (done) { | ||
it('should create a prepared Statement object with just an expression', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
var stmt = db.prepare('SELECT 555'); | ||
expect(stmt.source).to.equal('SELECT 555'); | ||
expect(stmt.constructor.name).to.equal('Statement'); | ||
expect(stmt.database).to.equal(db); | ||
expect(stmt.returnsData).to.equal(true); | ||
expect(function () { | ||
new stmt.constructor('SELECT 555'); | ||
}).to.throw(TypeError); | ||
done(); | ||
}); | ||
var stmt = db.prepare('SELECT 555'); | ||
expect(stmt.source).to.equal('SELECT 555'); | ||
expect(stmt.constructor.name).to.equal('Statement'); | ||
expect(stmt.database).to.equal(db); | ||
expect(stmt.returnsData).to.equal(true); | ||
expect(function () { | ||
new stmt.constructor('SELECT 555'); | ||
}).to.throw(TypeError); | ||
}); | ||
}); |
@@ -16,26 +16,20 @@ var expect = require('chai').expect; | ||
describe('Database#transaction()', function () { | ||
it('should throw an exception if an array of strings is not provided', function (done) { | ||
it('should throw an exception if an array of strings is not provided', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.transaction(123);}).to.throw(TypeError); | ||
expect(function () {db.transaction(0);}).to.throw(TypeError); | ||
expect(function () {db.transaction(null);}).to.throw(TypeError); | ||
expect(function () {db.transaction();}).to.throw(TypeError); | ||
expect(function () {db.transaction(new String('CREATE TABLE people (name TEXT)'));}).to.throw(TypeError); | ||
expect(function () {db.transaction({0: 'CREATE TABLE people (name TEXT)', length: 1});}).to.throw(TypeError); | ||
expect(function () {db.transaction([123]);}).to.throw(TypeError); | ||
expect(function () {db.transaction([0]);}).to.throw(TypeError); | ||
expect(function () {db.transaction([null]);}).to.throw(TypeError); | ||
expect(function () {db.transaction([new String('CREATE TABLE people (name TEXT)')]);}).to.throw(TypeError); | ||
done(); | ||
}); | ||
expect(function () {db.transaction(123);}).to.throw(TypeError); | ||
expect(function () {db.transaction(0);}).to.throw(TypeError); | ||
expect(function () {db.transaction(null);}).to.throw(TypeError); | ||
expect(function () {db.transaction();}).to.throw(TypeError); | ||
expect(function () {db.transaction(new String('CREATE TABLE people (name TEXT)'));}).to.throw(TypeError); | ||
expect(function () {db.transaction({0: 'CREATE TABLE people (name TEXT)', length: 1});}).to.throw(TypeError); | ||
expect(function () {db.transaction([123]);}).to.throw(TypeError); | ||
expect(function () {db.transaction([0]);}).to.throw(TypeError); | ||
expect(function () {db.transaction([null]);}).to.throw(TypeError); | ||
expect(function () {db.transaction([new String('CREATE TABLE people (name TEXT)')]);}).to.throw(TypeError); | ||
}); | ||
it('should throw an exception if no strings are provided', function (done) { | ||
it('should throw an exception if no strings are provided', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.transaction([]);}).to.throw(TypeError); | ||
done(); | ||
}); | ||
expect(function () {db.transaction([]);}).to.throw(TypeError); | ||
}); | ||
it('should propagate exceptions thrown from array accessors', function (done) { | ||
it('should propagate exceptions thrown from array accessors', function () { | ||
var db = new Database(util.next()); | ||
@@ -45,74 +39,56 @@ var err = new Error('foobar'); | ||
Object.defineProperty(arr, '0', {get: function () {throw err;}}); | ||
db.on('open', function () { | ||
expect(function () {db.transaction(arr);}).to.throw(err); | ||
done(); | ||
}); | ||
expect(function () {db.transaction(arr);}).to.throw(err); | ||
}); | ||
it('should throw an exception if invalid SQL is provided', function (done) { | ||
it('should throw an exception if invalid SQL is provided', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT']);}).to.throw(Error); | ||
expect(function () {db.transaction(['INSERT INTO people VALUES (?)']);}).to.throw(Error); | ||
done(); | ||
}); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT']);}).to.throw(Error); | ||
expect(function () {db.transaction(['INSERT INTO people VALUES (?)']);}).to.throw(Error); | ||
}); | ||
it('should throw an exception if a string contains no statements', function (done) { | ||
it('should throw an exception if a string contains no statements', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.transaction(['']);}).to.throw(TypeError); | ||
expect(function () {db.transaction([';']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', '']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', ';']);}).to.throw(TypeError); | ||
done(); | ||
}); | ||
expect(function () {db.transaction(['']);}).to.throw(TypeError); | ||
expect(function () {db.transaction([';']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', '']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', ';']);}).to.throw(TypeError); | ||
}); | ||
it('should throw an exception if multiple statements exist in one string', function (done) { | ||
it('should throw an exception if multiple statements exist in one string', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT);CREATE TABLE animals (name TEXT)']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT); ']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT);;']);}).to.throw(TypeError); | ||
done(); | ||
}); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT);CREATE TABLE animals (name TEXT)']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT); ']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT);;']);}).to.throw(TypeError); | ||
}); | ||
it('should throw an exception if any read-only statements are provided', function (done) { | ||
it('should throw an exception if any read-only statements are provided', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
expect(function () {db.transaction(['SELECT 555']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['BEGIN TRANSACTION']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['COMMIT TRANSACTION']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['ROLLBACK TRANSACTION']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'SELECT 555']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['SELECT 555', 'CREATE TABLE people (name TEXT)']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'BEGIN TRANSACTION']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'COMMIT TRANSACTION']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'ROLLBACK TRANSACTION']);}).to.throw(TypeError); | ||
done(); | ||
}); | ||
expect(function () {db.transaction(['SELECT 555']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['BEGIN TRANSACTION']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['COMMIT TRANSACTION']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['ROLLBACK TRANSACTION']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'SELECT 555']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['SELECT 555', 'CREATE TABLE people (name TEXT)']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'BEGIN TRANSACTION']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'COMMIT TRANSACTION']);}).to.throw(TypeError); | ||
expect(function () {db.transaction(['CREATE TABLE people (name TEXT)', 'ROLLBACK TRANSACTION']);}).to.throw(TypeError); | ||
}); | ||
it('should create a prepared Transaction object', function (done) { | ||
it('should create a prepared Transaction object', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
var trans1 = db.transaction(['CREATE TABLE people (name TEXT)']); | ||
var trans2 = db.transaction(['CREATE TABLE people (name TEXT)', 'CREATE TABLE animals (name TEXT);']); | ||
var trans3 = db.transaction(['CREATE TABLE people (name TEXT);', 'CREATE TABLE animals (name TEXT)']); | ||
expect(trans1.source).to.equal('CREATE TABLE people (name TEXT);'); | ||
expect(trans2.source).to.equal('CREATE TABLE people (name TEXT);\nCREATE TABLE animals (name TEXT);'); | ||
expect(trans2.source).to.equal(trans3.source); | ||
expect(trans1.constructor.name).to.equal('Transaction'); | ||
expect(trans2.constructor.name).to.equal('Transaction'); | ||
expect(trans3.constructor.name).to.equal('Transaction'); | ||
expect(trans1.database).to.equal(db); | ||
expect(trans2.database).to.equal(db); | ||
expect(trans3.database).to.equal(db); | ||
expect(function () { | ||
new trans1.constructor(['CREATE TABLE people (name TEXT)']); | ||
}).to.throw(TypeError); | ||
expect(trans1).to.not.equal(trans2); | ||
expect(trans1).to.not.equal(trans3); | ||
expect(trans2).to.not.equal(trans3); | ||
expect(trans1).to.not.equal(db.transaction(['CREATE TABLE people (name TEXT)'])); | ||
done(); | ||
}); | ||
var trans1 = db.transaction(['CREATE TABLE people (name TEXT)']); | ||
var trans2 = db.transaction(['CREATE TABLE people (name TEXT)', 'CREATE TABLE animals (name TEXT);']); | ||
var trans3 = db.transaction(['CREATE TABLE people (name TEXT);', 'CREATE TABLE animals (name TEXT)']); | ||
expect(trans1.source).to.equal('CREATE TABLE people (name TEXT);'); | ||
expect(trans2.source).to.equal('CREATE TABLE people (name TEXT);\nCREATE TABLE animals (name TEXT);'); | ||
expect(trans2.source).to.equal(trans3.source); | ||
expect(trans1.constructor.name).to.equal('Transaction'); | ||
expect(trans2.constructor.name).to.equal('Transaction'); | ||
expect(trans3.constructor.name).to.equal('Transaction'); | ||
expect(trans1.database).to.equal(db); | ||
expect(trans2.database).to.equal(db); | ||
expect(trans3.database).to.equal(db); | ||
expect(function () { | ||
new trans1.constructor(['CREATE TABLE people (name TEXT)']); | ||
}).to.throw(TypeError); | ||
expect(trans1).to.not.equal(trans2); | ||
expect(trans1).to.not.equal(trans3); | ||
expect(trans2).to.not.equal(trans3); | ||
expect(trans1).to.not.equal(db.transaction(['CREATE TABLE people (name TEXT)'])); | ||
}); | ||
}); |
@@ -6,5 +6,4 @@ 'use strict'; | ||
before(function (done) { | ||
before(function () { | ||
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db'); | ||
db.on('open', done); | ||
}); | ||
@@ -11,0 +10,0 @@ |
@@ -6,5 +6,4 @@ 'use strict'; | ||
before(function (done) { | ||
before(function () { | ||
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db'); | ||
db.on('open', done); | ||
}); | ||
@@ -11,0 +10,0 @@ |
@@ -6,5 +6,4 @@ 'use strict'; | ||
before(function (done) { | ||
before(function () { | ||
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db'); | ||
db.on('open', done); | ||
}); | ||
@@ -11,0 +10,0 @@ |
@@ -6,5 +6,4 @@ 'use strict'; | ||
before(function (done) { | ||
before(function () { | ||
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db'); | ||
db.on('open', done); | ||
}); | ||
@@ -11,0 +10,0 @@ |
@@ -6,5 +6,4 @@ 'use strict'; | ||
before(function (done) { | ||
before(function () { | ||
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db'); | ||
db.on('open', done); | ||
}); | ||
@@ -11,0 +10,0 @@ |
@@ -6,5 +6,4 @@ 'use strict'; | ||
before(function (done) { | ||
before(function () { | ||
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db'); | ||
db.on('open', done); | ||
}); | ||
@@ -11,0 +10,0 @@ |
@@ -6,5 +6,4 @@ 'use strict'; | ||
before(function (done) { | ||
before(function () { | ||
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db'); | ||
db.on('open', done); | ||
}); | ||
@@ -11,0 +10,0 @@ |
@@ -6,9 +6,6 @@ var expect = require('chai').expect; | ||
before(function (done) { | ||
before(function () { | ||
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.db'); | ||
db.on('open', function () { | ||
db.pragma('journal_mode = WAL'); | ||
db.prepare('CREATE TABLE entries (a TEXT, b INTEGER)').run(); | ||
done(); | ||
}); | ||
db.pragma('journal_mode = WAL'); | ||
db.prepare('CREATE TABLE entries (a TEXT, b INTEGER)').run(); | ||
}); | ||
@@ -15,0 +12,0 @@ |
@@ -17,61 +17,44 @@ var expect = require('chai').expect; | ||
describe('Database#close()', function () { | ||
it('should prevent statements and transactions from operating', function (done) { | ||
it('should prevent statements and transactions from operating', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
db.prepare('CREATE TABLE people (name TEXT)').run(); | ||
var stmt1 = db.prepare('SELECT * FROM people'); | ||
var stmt2 = db.prepare("INSERT INTO people VALUES ('foobar')"); | ||
var trans = db.transaction(["INSERT INTO people VALUES ('foobar')"]); | ||
db.prepare('SELECT * FROM people').bind(); | ||
db.prepare("INSERT INTO people VALUES ('foobar')").bind(); | ||
db.transaction(["INSERT INTO people VALUES ('foobar')"]).bind(); | ||
db.prepare('SELECT * FROM people').get(); | ||
db.prepare('SELECT * FROM people').all(); | ||
db.prepare('SELECT * FROM people').each(function () {}); | ||
db.prepare("INSERT INTO people VALUES ('foobar')").run(); | ||
db.transaction(["INSERT INTO people VALUES ('foobar')"]).run(); | ||
db.close(); | ||
expect(function () {stmt1.bind();}).to.throw(Error); | ||
expect(function () {stmt2.bind();}).to.throw(Error); | ||
expect(function () {trans.bind();}).to.throw(Error); | ||
expect(function () {stmt1.get();}).to.throw(Error); | ||
expect(function () {stmt1.all();}).to.throw(Error); | ||
expect(function () {stmt1.each(function () {});}).to.throw(Error); | ||
expect(function () {stmt2.run();}).to.throw(Error); | ||
expect(function () {trans.run();}).to.throw(Error); | ||
db.on('close', function () { | ||
expect(function () {stmt1.bind();}).to.throw(Error); | ||
expect(function () {stmt2.bind();}).to.throw(Error); | ||
expect(function () {trans.bind();}).to.throw(Error); | ||
expect(function () {stmt1.get();}).to.throw(Error); | ||
expect(function () {stmt1.all();}).to.throw(Error); | ||
expect(function () {stmt1.each(function () {});}).to.throw(Error); | ||
expect(function () {stmt2.run();}).to.throw(Error); | ||
expect(function () {trans.run();}).to.throw(Error); | ||
done(); | ||
}); | ||
}); | ||
db.prepare('CREATE TABLE people (name TEXT)').run(); | ||
var stmt1 = db.prepare('SELECT * FROM people'); | ||
var stmt2 = db.prepare("INSERT INTO people VALUES ('foobar')"); | ||
var trans = db.transaction(["INSERT INTO people VALUES ('foobar')"]); | ||
db.prepare('SELECT * FROM people').bind(); | ||
db.prepare("INSERT INTO people VALUES ('foobar')").bind(); | ||
db.transaction(["INSERT INTO people VALUES ('foobar')"]).bind(); | ||
db.prepare('SELECT * FROM people').get(); | ||
db.prepare('SELECT * FROM people').all(); | ||
db.prepare('SELECT * FROM people').each(function () {}); | ||
db.prepare("INSERT INTO people VALUES ('foobar')").run(); | ||
db.transaction(["INSERT INTO people VALUES ('foobar')"]).run(); | ||
db.close(); | ||
expect(function () {stmt1.bind();}).to.throw(TypeError); | ||
expect(function () {stmt2.bind();}).to.throw(TypeError); | ||
expect(function () {trans.bind();}).to.throw(TypeError); | ||
expect(function () {stmt1.get();}).to.throw(TypeError); | ||
expect(function () {stmt1.all();}).to.throw(TypeError); | ||
expect(function () {stmt1.each(function () {});}).to.throw(TypeError); | ||
expect(function () {stmt2.run();}).to.throw(TypeError); | ||
expect(function () {trans.run();}).to.throw(TypeError); | ||
}); | ||
it('should delete the database\'s associated temporary files', function (done) { | ||
it('should delete the database\'s associated temporary files', function () { | ||
var db = new Database(util.next()); | ||
db.on('open', function () { | ||
fs.accessSync(util.current()); | ||
db.pragma('journal_mode = WAL'); | ||
db.prepare('CREATE TABLE people (name TEXT)').run(); | ||
db.prepare('INSERT INTO people VALUES (?)').run('foobar'); | ||
fs.accessSync(util.current()); | ||
db.pragma('journal_mode = WAL'); | ||
db.prepare('CREATE TABLE people (name TEXT)').run(); | ||
db.prepare('INSERT INTO people VALUES (?)').run('foobar'); | ||
fs.accessSync(util.current() + '-wal'); | ||
db.close(); | ||
fs.accessSync(util.current()); | ||
expect(function () { | ||
fs.accessSync(util.current() + '-wal'); | ||
db.close(); | ||
db.on('close', function (err) { | ||
fs.accessSync(util.current()); | ||
expect(function () { | ||
fs.accessSync(util.current() + '-wal'); | ||
}).to.throw(); | ||
done(); | ||
}); | ||
}); | ||
}).to.throw(); | ||
}); | ||
}); |
@@ -7,15 +7,7 @@ var expect = require('chai').expect; | ||
before(function (done) { | ||
before(function () { | ||
db = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.1.db'); | ||
db2 = new Database('temp/' + require('path').basename(__filename).split('.')[0] + '.2.db'); | ||
var openCount = 0; | ||
db.on('open', opened); | ||
db2.on('open', opened); | ||
function opened() { | ||
if (++openCount === 2) { | ||
db.prepare('CREATE TABLE entries (a INTEGER, b REAL, c TEXT)').run(); | ||
db2.prepare('CREATE TABLE entries (a INTEGER, b REAL, c TEXT)').run(); | ||
done(); | ||
} | ||
} | ||
db.prepare('CREATE TABLE entries (a INTEGER, b REAL, c TEXT)').run(); | ||
db2.prepare('CREATE TABLE entries (a INTEGER, b REAL, c TEXT)').run(); | ||
}); | ||
@@ -165,6 +157,11 @@ | ||
db.defaultSafeIntegers(true); | ||
db.defaultSafeIntegers(); | ||
expect(stmt.get()).to.equal(1006028374637854700); | ||
expect(stmt2.get()).to.deep.equal(int); | ||
var stmt3 = db.prepare('SELECT a FROM entries').pluck(); | ||
expect(stmt3.get()).to.deep.equal(int); | ||
expect(stmt3.safeIntegers(false).get()).to.equal(1006028374637854700); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2681283
152
2400
43