Comparing version 2.4.1 to 3.0.0
@@ -10,2 +10,3 @@ 'use strict'; | ||
const Criteria = require('./criteria'); | ||
const Modifier = require('./modifier'); | ||
const Table = require('./table'); | ||
@@ -245,2 +246,22 @@ | ||
} | ||
increment(value) { | ||
return Modifier.type('increment', value); | ||
} | ||
append(value) { | ||
return Modifier.type('append', value); | ||
} | ||
unset() { | ||
return Modifier.type('unset'); | ||
} | ||
isModifier(value) { | ||
return (typeof value === 'function' && !!value.type); | ||
} | ||
}; | ||
@@ -247,0 +268,0 @@ |
@@ -10,2 +10,3 @@ 'use strict'; | ||
const Criteria = require('./criteria'); | ||
const Modifier = require('./modifier'); | ||
@@ -101,3 +102,5 @@ | ||
this._run(this._table.get(id).update(changes), 'update', { id: id, changes: changes }, callback, (ignore, result) => { | ||
const diag = { id, changes }; | ||
const wrapped = Modifier.wrap(changes); | ||
this._run(this._table.get(id)[typeof wrapped === 'object' ? 'update' : 'replace'](wrapped), 'update', diag, callback, (ignore, result) => { | ||
@@ -107,3 +110,3 @@ if (!result.replaced && | ||
return this._error('update', 'No item found to update', { id: id, changes: changes }, callback); | ||
return this._error('update', 'No item found to update', diag, callback); | ||
} | ||
@@ -115,10 +118,12 @@ | ||
increment(id, field, value, callback) { | ||
next(id, field, value, callback) { | ||
const changes = {}; | ||
changes[field] = RethinkDB.row(field).add(value); | ||
this._run(this._table.get(id).update(changes, { returnChanges: true }), 'increment', { id: id, field: field, value: value }, callback, (ignore, result) => { | ||
const diag = { id, field, value }; | ||
this._run(this._table.get(id).update(changes, { returnChanges: true }), 'next', diag, callback, (ignore, result) => { | ||
if (!result.replaced) { | ||
return this._error('increment', 'No item found to update', { id: id, field: field, value: value }, callback); | ||
return this._error('next', 'No item found to update', diag, callback); | ||
} | ||
@@ -131,32 +136,2 @@ | ||
append(id, field, value, callback) { | ||
const changes = {}; | ||
changes[field] = RethinkDB.row(field).append(value); | ||
this._run(this._table.get(id).update(changes), 'append', { id: id, field: field, value: value }, callback, (ignore, result) => { | ||
if (!result.replaced) { | ||
return this._error('append', 'No item found to update', { id: id, field: field, value: value }, callback); | ||
} | ||
return callback(null); | ||
}); | ||
} | ||
unset(id, fields, callback) { | ||
const changes = (item) => item.without(fields); | ||
this._run(this._table.get(id).replace(changes), 'unset', { id: id, fields: fields }, callback, (ignore, result) => { | ||
if (!result.replaced && | ||
!result.unchanged) { | ||
return this._error('unset', 'No item found to update', { id: id, fields: fields }, callback); | ||
} | ||
return callback(null); | ||
}); | ||
} | ||
remove(criteria, callback) { | ||
@@ -163,0 +138,0 @@ |
{ | ||
"name": "penseur", | ||
"description": "Lightweight RethinkDB wrapper", | ||
"version": "2.4.1", | ||
"version": "3.0.0", | ||
"author": "Eran Hammer <eran@hammer.io> (http://hueniverse.com)", | ||
@@ -6,0 +6,0 @@ "repository": "git://github.com/hueniverse/penseur", |
@@ -613,2 +613,14 @@ 'use strict'; | ||
}); | ||
describe('isModifier()', () => { | ||
it('indicates if a value is a modifier', (done) => { | ||
const db = new Penseur.Db('penseurtest'); | ||
expect(db.isModifier(1)).to.be.false(); | ||
expect(db.isModifier(() => { })).to.be.false(); | ||
expect(db.isModifier(db.increment(1))).to.be.true(); | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -392,8 +392,5 @@ 'use strict'; | ||
}); | ||
}); | ||
describe('increment()', () => { | ||
it('updates a record (increment modifier)', (done) => { | ||
it('updates a record', (done) => { | ||
const db = new Penseur.Db('penseurtest'); | ||
@@ -403,14 +400,40 @@ db.establish(['test'], (err) => { | ||
expect(err).to.not.exist(); | ||
db.test.insert({ id: 1, a: 1 }, (err, keys) => { | ||
const item = { | ||
id: 1, | ||
a: 1, | ||
b: { | ||
c: 2 | ||
} | ||
}; | ||
db.test.insert(item, (err, keys) => { | ||
expect(err).to.not.exist(); | ||
db.test.increment(1, 'a', 5, (err) => { | ||
const changes = { | ||
a: 2, | ||
b: { | ||
c: db.increment(10) | ||
} | ||
}; | ||
expect(changes.b.c).to.be.a.function(); | ||
db.test.update(1, changes, (err) => { | ||
expect(err).to.not.exist(); | ||
expect(changes.b.c).to.be.a.function(); | ||
db.test.get(1, (err, item) => { | ||
db.test.get(1, (err, updated) => { | ||
expect(err).to.not.exist(); | ||
expect(item.a).to.equal(6); | ||
expect(updated).to.deep.equal({ | ||
id: 1, | ||
a: 2, | ||
b: { | ||
c: 12 | ||
} | ||
}); | ||
done(); | ||
@@ -423,3 +446,3 @@ }); | ||
it('errors on unknown key', (done) => { | ||
it('updates a record (append modifier)', (done) => { | ||
@@ -431,31 +454,39 @@ const db = new Penseur.Db('penseurtest'); | ||
db.test.increment(1, 'a', 5, (err) => { | ||
const item = { | ||
id: 1, | ||
a: 1, | ||
b: { | ||
c: [2] | ||
} | ||
}; | ||
expect(err).to.exist(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
db.test.insert(item, (err, keys) => { | ||
describe('append()', () => { | ||
expect(err).to.not.exist(); | ||
it('updates a record', (done) => { | ||
const changes = { | ||
a: 2, | ||
b: { | ||
c: db.append(10) | ||
} | ||
}; | ||
const db = new Penseur.Db('penseurtest'); | ||
db.establish(['test'], (err) => { | ||
expect(changes.b.c).to.be.a.function(); | ||
expect(err).to.not.exist(); | ||
db.test.insert({ id: 1, a: [1] }, (err, keys) => { | ||
db.test.update(1, changes, (err) => { | ||
expect(err).to.not.exist(); | ||
db.test.append(1, 'a', 5, (err) => { | ||
expect(err).to.not.exist(); | ||
expect(changes.b.c).to.be.a.function(); | ||
db.test.get(1, (err, item) => { | ||
db.test.get(1, (err, updated) => { | ||
expect(err).to.not.exist(); | ||
expect(item.a).to.deep.equal([1, 5]); | ||
expect(updated).to.deep.equal({ | ||
id: 1, | ||
a: 2, | ||
b: { | ||
c: [2, 10] | ||
} | ||
}); | ||
done(); | ||
@@ -468,3 +499,3 @@ }); | ||
it('errors on unknown key', (done) => { | ||
it('updates a record (unset modifier)', (done) => { | ||
@@ -476,6 +507,40 @@ const db = new Penseur.Db('penseurtest'); | ||
db.test.append(1, 'a', 5, (err) => { | ||
const item = { | ||
id: 1, | ||
a: 1, | ||
b: { | ||
c: [2] | ||
} | ||
}; | ||
expect(err).to.exist(); | ||
done(); | ||
db.test.insert(item, (err, keys) => { | ||
expect(err).to.not.exist(); | ||
const changes = { | ||
a: 2, | ||
b: { | ||
c: db.unset() | ||
} | ||
}; | ||
expect(changes.b.c).to.be.a.function(); | ||
db.test.update(1, changes, (err) => { | ||
expect(err).to.not.exist(); | ||
expect(changes.b.c).to.be.a.function(); | ||
db.test.get(1, (err, updated) => { | ||
expect(err).to.not.exist(); | ||
expect(updated).to.deep.equal({ | ||
id: 1, | ||
a: 2, | ||
b: {} | ||
}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
@@ -486,3 +551,3 @@ }); | ||
describe('unset()', () => { | ||
describe('next()', () => { | ||
@@ -499,3 +564,3 @@ it('updates a record', (done) => { | ||
db.test.unset(1, 'a', (err) => { | ||
db.test.next(1, 'a', 5, (err) => { | ||
@@ -507,3 +572,3 @@ expect(err).to.not.exist(); | ||
expect(err).to.not.exist(); | ||
expect(item.a).to.not.exist(); | ||
expect(item.a).to.equal(6); | ||
done(); | ||
@@ -523,3 +588,3 @@ }); | ||
db.test.unset(1, 'a', (err) => { | ||
db.test.next(1, 'a', 5, (err) => { | ||
@@ -526,0 +591,0 @@ expect(err).to.exist(); |
78309
14
1869