Comparing version 0.6.5 to 0.6.6
@@ -67,3 +67,2 @@ /* -*- Mode: Javascript; js-indent-level: 2 -*- */ | ||
var untouchedObj = _.clone(obj); | ||
@@ -106,6 +105,11 @@ if (untouchedObj[this.options.id] != null) { | ||
*/ | ||
read: function(id, callback) { | ||
read: function(id, property, callback) { | ||
if (typeof property == 'function') { | ||
callback = property; | ||
property = null; | ||
} | ||
if (Array.isArray(id)) { | ||
var txn = this._safeBatch(); | ||
async.map(id, txn.read, callback); | ||
async.map(id, naan.ncurry(txn.read, property, 1), callback); | ||
return this._safeBatchCommit(txn); | ||
@@ -119,3 +123,7 @@ } | ||
var endpoint = util.format('%s/properties', this._nodeRoot(id)); | ||
var endpoint; | ||
if (!property) | ||
endpoint = util.format('%s/properties', this._nodeRoot(id)); | ||
else | ||
endpoint = util.format('%s/properties/%s', this._nodeRoot(id), property); | ||
var op = this.operation(endpoint); | ||
@@ -126,3 +134,3 @@ this.call(op, function(err, body) { | ||
} else { | ||
body[this.options.id] = id; | ||
if (!property) body[this.options.id] = id; | ||
return callback(null, body); | ||
@@ -138,5 +146,9 @@ } | ||
delete: function(id, force, callback) { | ||
if (typeof force === 'function') { | ||
var property = null; | ||
if (typeof force == 'function') { | ||
callback = force; | ||
force = false; | ||
} else if (typeof force == 'string') { | ||
property = force; | ||
force = false; | ||
} | ||
@@ -146,6 +158,7 @@ | ||
var txn = this._safeBatch(); | ||
async.map(id, txn.node.delete, callback); | ||
async.map(id, naan.ncurry(txn.node.delete, force || property, 1), callback); | ||
return this._safeBatchCommit(txn); | ||
} | ||
var object = id; | ||
id = this._getId(id); | ||
@@ -157,5 +170,14 @@ if (!this._isValidId(id)) { | ||
if (!force) { | ||
var endpoint = this._nodeRoot(id); | ||
var endpoint; | ||
if (!property) endpoint = this._nodeRoot(id); | ||
else | ||
endpoint = util.format('%s/properties/%s', this._nodeRoot(id), property); | ||
var op = this.operation(endpoint, 'DELETE'); | ||
this.call(op, function(err) { callback(err) }); | ||
this.call(op, function(err) { | ||
if (!property || err || typeof object != 'object') callback(err); | ||
else { | ||
delete object[property]; | ||
callback(null, object); | ||
} | ||
}); | ||
} else { | ||
@@ -257,3 +279,3 @@ this.query([ | ||
var txn = this._safeBatch(); | ||
var finder = naan.ncurry(txn.node.find, any, 2); | ||
var finder = naan.ecurry(txn.node.find, [any, start], [1, 2]); | ||
async.map(predicate, finder, callback); | ||
@@ -260,0 +282,0 @@ return this._safeBatchCommit(txn); |
@@ -8,3 +8,3 @@ { | ||
"description": "A thin and familiar layer between node and neo4j's REST api.", | ||
"version": "0.6.5", | ||
"version": "0.6.6", | ||
"repository": { | ||
@@ -11,0 +11,0 @@ "url": "https://github.com/brikteknologier/seraph" |
@@ -470,3 +470,3 @@ # Seraph.js | ||
<a name="node.read" /> | ||
### read(id|object, callback) | ||
### read(id|object, [property,] callback) | ||
*Aliases: __node.read__* | ||
@@ -486,2 +486,4 @@ | ||
property of the node to read. | ||
* `property` (optional) - the name of the property to read. if this is specified, | ||
only the value of this property on the object is returned. | ||
* `callback` - function(err, node). `node` is an object containing the properties | ||
@@ -503,3 +505,3 @@ of the node with the given id. | ||
<a name="node.delete" /> | ||
### delete(id|object, [force], [callback]) | ||
### delete(id|object, [force | property], [callback]) | ||
*Aliases: __node.delete__* | ||
@@ -513,3 +515,7 @@ | ||
property of the node to delete. | ||
* `force` - if truthy, will delete all the node's relations prior to deleting the node. | ||
* `force` (optional - default = false) - if truthy, will delete all the node's | ||
relations prior to deleting the node. | ||
* `property` (optional) - if specified, delete only the property with this name | ||
on the object. **note that you can either specify `property` or `force`, not | ||
both, as force is meaningless when deleting a property** | ||
* `callback` - function(err). if `err` is falsy, the node has been deleted. | ||
@@ -516,0 +522,0 @@ |
@@ -125,2 +125,64 @@ /* -*- Mode: Javascript; js-indent-level: 2 -*- */ | ||
it('should delete a single property of an object', function(done) { | ||
db.save({name: 'bob', age: 46}, function(err, node) { | ||
assert(!err) | ||
assert.equal(node.name, 'bob'); | ||
assert.equal(node.age, 46); | ||
node.name = 'hidden name man'; | ||
db.save(node, 'age', 47, function(err, node) { | ||
assert(!err); | ||
db.delete(node, 'age', function(err, node) { | ||
assert(!err); | ||
assert(node.age == null); | ||
db.read(node, function(err, node) { | ||
assert(!err); | ||
assert(node.age == null); | ||
assert(node.name == 'bob'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should read a single property of an object', function(done) { | ||
db.save({name: 'bob', age: 47}, function(err, node) { | ||
assert(!err); | ||
db.read(node, 'name', function(err, name) { | ||
assert(!err); | ||
assert.equal(name, 'bob'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should read a single property from an array of objects', function(done) { | ||
db.save([{name:'bob'}, {name:'james'}], function(err, nodes) { | ||
assert(!err); | ||
db.read(nodes, 'name', function(err, names) { | ||
assert(!err); | ||
assert(names.indexOf('bob') != -1); | ||
assert(names.indexOf('james') != -1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should delete a single property from an array of objects', function(done) { | ||
db.save([{name:'bob', b:5}, {name:'james', b:2}], function(err, nodes) { | ||
assert(!err); | ||
db.delete(nodes, 'b', function(err) { | ||
assert(!err); | ||
db.read(nodes, function(err, nodes) { | ||
assert(!err); | ||
nodes.forEach(function(node) { | ||
assert(node.name); | ||
assert(node.b == null); | ||
}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should handle an empty array of objects to save', function(done) { | ||
@@ -127,0 +189,0 @@ db.save([], done); |
@@ -292,2 +292,42 @@ /* -*- Mode: Javascript; js-indent-level: 2 -*- */ | ||
it('should find some items based on an array of predicates', function(done) { | ||
var uniqueKey = 'seraph_find_test' + uniqn(); | ||
function createObjs(done) { | ||
var objs = [ {name: 'Jon', age: 23}, | ||
{name: 'Neil', age: 60}, | ||
{name: 'Katie', age: 29} ]; | ||
for (var index in objs) { | ||
objs[index][uniqueKey] = true; | ||
} | ||
objs[3] = {name: 'Belinda', age: 26}; | ||
objs[3][uniqueKey] = false; | ||
db.save(objs, function(err, users) { | ||
done(); | ||
}); | ||
} | ||
function findObjs(done) { | ||
var predicate = {}; | ||
predicate[uniqueKey] = true; | ||
var predicate2 = {}; | ||
predicate2[uniqueKey] = false; | ||
db.find([predicate, predicate2], true, 'node(*)', function(err, objs) { | ||
assert.ok(!err); | ||
assert.equal(objs[0].length, 3); | ||
assert.equal(objs[1].length, 1); | ||
var names = objs[0].map(function(o) { return o.name }); | ||
assert.ok(names.indexOf('Jon') >= 0); | ||
assert.ok(names.indexOf('Neil') >= 0); | ||
assert.ok(names.indexOf('Katie') >= 0); | ||
assert.ok(names.indexOf('Belinda') === -1); | ||
var names2 = objs[1].map(function(o) { return o.name }); | ||
assert.ok(names2.indexOf('Belinda') >= 0); | ||
done(); | ||
}); | ||
} | ||
async.series([createObjs, findObjs], done); | ||
}) | ||
it('should find some items based on an OR predicate', function(done) { | ||
@@ -294,0 +334,0 @@ var uniqueKey = 'seraph_find_test' + uniqn(); |
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
147617
3245
1095