Comparing version 5.1.1 to 5.1.2
104
lib/db.js
@@ -94,2 +94,41 @@ 'use strict'; | ||
this._connect((err) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
this._exists((err, exists) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (!exists) { | ||
this.close(); | ||
return callback(Boom.internal(`Missing database: ${this.name}`)); | ||
} | ||
this.verify((err) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
// Reconnect changes feeds | ||
const each = (feedId, next) => { | ||
const feed = this._feeds[feedId]; | ||
return feed.table.changes(feed.criteria, feed.options, next); | ||
}; | ||
Items.serial(Object.keys(this._feeds), each, callback); | ||
}); | ||
}); | ||
}); | ||
} | ||
_connect(callback) { | ||
const settings = this._connectionOptions || {}; | ||
@@ -125,18 +164,3 @@ if (!this._connectionOptions) { | ||
this._settings.onConnect(); | ||
this.verify((err) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
// Reconnect changes feeds | ||
const each = (feedId, next) => { | ||
const feed = this._feeds[feedId]; | ||
return feed.table.changes(feed.criteria, feed.options, next); | ||
}; | ||
Items.serial(Object.keys(this._feeds), each, callback); | ||
}); | ||
return callback(); | ||
}); | ||
@@ -173,4 +197,6 @@ } | ||
this._connection.removeAllListeners(); | ||
this._connection = null; | ||
if (this._connection) { | ||
this._connection.removeAllListeners(); | ||
this._connection = null; | ||
} | ||
@@ -265,6 +291,4 @@ return next(err); | ||
// Connect if not connected already | ||
if (!this._connection) { | ||
this.connect((err) => { | ||
this._connect((err) => { | ||
@@ -284,3 +308,3 @@ if (err) { | ||
RethinkDB.dbList().run(this._connection, (err, names) => { | ||
const finalize = (err) => { | ||
@@ -291,21 +315,33 @@ if (err) { | ||
if (names.indexOf(this.name) === -1) { | ||
return this._createTable(byName, (err) => { | ||
// Create new database | ||
if (err) { | ||
return callback(err); | ||
} | ||
RethinkDB.dbCreate(this.name).run(this._connection, (err, created) => { | ||
return this.verify(callback); | ||
}); | ||
}; | ||
if (err) { | ||
return callback(err); | ||
} | ||
this._exists((err, exists) => { | ||
return this._createTable(byName, callback); | ||
}); | ||
if (err || | ||
exists) { | ||
return finalize(err); | ||
} | ||
else { | ||
// Reuse existing | ||
RethinkDB.dbCreate(this.name).run(this._connection, finalize); | ||
}); | ||
} | ||
return this._createTable(byName, callback); | ||
_exists(callback) { | ||
RethinkDB.dbList().run(this._connection, (err, names) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
return callback(null, names.indexOf(this.name) !== -1); | ||
}); | ||
@@ -404,3 +440,3 @@ } | ||
const table = this.tables[name]; | ||
Id.verify(table, (err) => { | ||
Id.verify(table, { allocate: false }, (err) => { | ||
@@ -407,0 +443,0 @@ if (err) { |
@@ -124,3 +124,3 @@ 'use strict'; | ||
exports.verify(table, (err, allocated) => { | ||
exports.verify(table, { allocate: true }, (err, allocated) => { | ||
@@ -158,3 +158,3 @@ if (err) { | ||
exports.verify = function (table, callback) { | ||
exports.verify = function (table, options, callback) { | ||
@@ -185,2 +185,10 @@ if (!table._id || | ||
let initialId = table._id.initial - 1; | ||
let allocatedId = null; | ||
if (options.allocate) { | ||
++initialId; | ||
allocatedId = initialId; | ||
} | ||
if (record) { | ||
@@ -192,3 +200,3 @@ if (record[table._id.key] === undefined) { | ||
const changes = {}; | ||
changes[table._id.key] = table._id.initial; | ||
changes[table._id.key] = initialId; | ||
table._id.table.update(table._id.record, changes, (err) => { | ||
@@ -202,3 +210,3 @@ | ||
table._id.verified = true; | ||
return callback(null, table._id.initial); | ||
return callback(null, allocatedId); | ||
}); | ||
@@ -219,3 +227,3 @@ | ||
const item = { id: table._id.record }; | ||
item[table._id.key] = table._id.initial; | ||
item[table._id.key] = initialId; | ||
table._id.table.insert(item, (err, key) => { | ||
@@ -229,3 +237,3 @@ | ||
table._id.verified = true; | ||
return callback(null, table._id.initial); | ||
return callback(null, allocatedId); | ||
}); | ||
@@ -232,0 +240,0 @@ }); |
{ | ||
"name": "penseur", | ||
"description": "Lightweight RethinkDB wrapper", | ||
"version": "5.1.1", | ||
"version": "5.1.2", | ||
"author": "Eran Hammer <eran@hammer.io> (http://hueniverse.com)", | ||
@@ -6,0 +6,0 @@ "repository": "git://github.com/hueniverse/penseur", |
105
test/db.js
@@ -198,3 +198,3 @@ 'use strict'; | ||
expect(err).to.not.exist(); | ||
expect(keys).to.equal('2'); | ||
expect(keys).to.equal('1'); | ||
done(); | ||
@@ -205,2 +205,37 @@ }); | ||
}); | ||
it('errors on missing database', (done) => { | ||
const db = new Penseur.Db('penseurtest_no_such_db'); | ||
db.connect((err) => { | ||
expect(err).to.exist(); | ||
expect(err.message).to.equal('Missing database: penseurtest_no_such_db'); | ||
db.close(done); | ||
}); | ||
}); | ||
it('errors on database dbList() error', { parallel: false }, (done) => { | ||
const db = new Penseur.Db('penseurtest'); | ||
const orig = RethinkDB.dbList; | ||
RethinkDB.dbList = function () { | ||
RethinkDB.dbList = orig; | ||
return { | ||
run: function (connection, next) { | ||
return next(new Error('Bad database')); | ||
} | ||
}; | ||
}; | ||
db.connect((err) => { | ||
expect(err).to.exist(); | ||
db.close(done); | ||
}); | ||
}); | ||
}); | ||
@@ -289,2 +324,25 @@ | ||
it('creates new database (complex tables pre-loaded)', (done) => { | ||
const prep = new Penseur.Db('penseurtest'); | ||
prep.connect((err) => { | ||
expect(err).to.not.exist(); | ||
RethinkDB.dbDrop('penseurtest').run(prep._connection, (err, dropped) => { | ||
expect(err).to.not.exist(); | ||
prep.close(); | ||
const db = new Penseur.Db('penseurtest'); | ||
db.table({ test: { id: 'increment' } }); | ||
db.establish(['test'], (err) => { | ||
expect(err).to.not.exist(); | ||
db.close(done); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('customizes table options', (done) => { | ||
@@ -560,35 +618,2 @@ | ||
it('errors on database dbCreate() error', { parallel: false }, (done) => { | ||
const db = new Penseur.Db('penseur-test'); | ||
db.connect((err) => { | ||
expect(err).to.not.exist(); | ||
RethinkDB.dbDrop(db.name).run(db._connection, (err, dropped) => { | ||
expect(err).to.exist(); | ||
const orig = RethinkDB.dbCreate; | ||
RethinkDB.dbCreate = function () { | ||
RethinkDB.dbCreate = orig; | ||
return { | ||
run: function (connection, next) { | ||
return next(new Error('Bad database')); | ||
} | ||
}; | ||
}; | ||
db.establish(['test'], (err) => { | ||
expect(err).to.exist(); | ||
db.close(done); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('errors on database indexList() error', { parallel: false }, (done) => { | ||
@@ -719,7 +744,13 @@ | ||
expect(err).to.not.exist(); | ||
db.test.insert({ a: 1 }, (err, keys) => { | ||
db.allocate.get('test', (err, record) => { | ||
expect(err).to.not.exist(); | ||
expect(keys).to.equal('2'); | ||
done(); | ||
expect(record.value).to.equal(0); | ||
db.test.insert({ a: 1 }, (err, keys) => { | ||
expect(err).to.not.exist(); | ||
expect(keys).to.equal('1'); | ||
done(); | ||
}); | ||
}); | ||
@@ -726,0 +757,0 @@ }); |
@@ -139,3 +139,3 @@ 'use strict'; | ||
it('updates generate record', (done) => { | ||
it('completes an existing incomplete allocation record', (done) => { | ||
@@ -146,5 +146,6 @@ const db = new Penseur.Db('penseurtest'); | ||
expect(err).to.not.exist(); | ||
db.allocate.insert({ id: 'test' }, (err, key) => { | ||
db.allocate.update('test', { value: db.unset() }, (err) => { | ||
expect(err).to.not.exist(); | ||
db.test._id.verified = false; | ||
db.test.insert({ a: 1 }, (err, keys) => { | ||
@@ -166,3 +167,3 @@ | ||
expect(err).to.not.exist(); | ||
db.allocate.insert({ id: 'test', value: 33 }, (err, key) => { | ||
db.allocate.update('test', { value: 33 }, (err) => { | ||
@@ -186,3 +187,3 @@ expect(err).to.not.exist(); | ||
expect(err).to.not.exist(); | ||
db.allocate.insert({ id: 'test', value: 1324 }, (err, key) => { | ||
db.allocate.update('test', { value: 1324 }, (err) => { | ||
@@ -221,5 +222,6 @@ expect(err).to.not.exist(); | ||
expect(err).to.not.exist(); | ||
db.allocate.insert({ id: 'test', value: 'string' }, (err, key) => { | ||
db.allocate.update('test', { value: 'string' }, (err) => { | ||
expect(err).to.not.exist(); | ||
db.test._id.verified = false; | ||
db.test.insert({ a: 1 }, (err, keys) => { | ||
@@ -242,2 +244,3 @@ | ||
db.test._db._createTable = (options, callback) => callback(new Error('Failed')); | ||
db.test._id.verified = false; | ||
db.test.insert({ a: 1 }, (err, keys) => { | ||
@@ -259,2 +262,3 @@ | ||
db.test._id.table.get = (id, callback) => callback(new Error('Failed')); | ||
db.test._id.verified = false; | ||
db.test.insert({ a: 1 }, (err, keys) => { | ||
@@ -275,6 +279,7 @@ | ||
expect(err).to.not.exist(); | ||
db.allocate.insert({ id: 'test' }, (err, key) => { | ||
db.allocate.update('test', { value: db.unset() }, (err, key) => { | ||
expect(err).to.not.exist(); | ||
db.test._id.table.update = (id, changes, callback) => callback(new Error('Failed')); | ||
db.test._id.verified = false; | ||
db.test.insert({ a: 1 }, (err, keys) => { | ||
@@ -296,8 +301,13 @@ | ||
expect(err).to.not.exist(); | ||
db.test._id.table.insert = (item, callback) => callback(new Error('Failed')); | ||
db.test.insert({ a: 1 }, (err, keys) => { | ||
db.allocate.remove('test', (err) => { | ||
expect(err).to.exist(); | ||
expect(err.data.error.message).to.equal('Failed inserting increment id record: test'); | ||
done(); | ||
expect(err).to.not.exist(); | ||
db.test._id.table.insert = (item, callback) => callback(new Error('Failed')); | ||
db.test._id.verified = false; | ||
db.test.insert({ a: 1 }, (err, keys) => { | ||
expect(err).to.exist(); | ||
expect(err.data.error.message).to.equal('Failed inserting increment id record: test'); | ||
done(); | ||
}); | ||
}); | ||
@@ -314,2 +324,3 @@ }); | ||
db.test._id.table.next = (id, key, inc, callback) => callback(new Error('Failed')); | ||
db.test._id.verified = false; | ||
db.test.insert([{ a: 1 }, { a: 1 }], (err, keys) => { | ||
@@ -316,0 +327,0 @@ |
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
156764
3748