Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unset property if 'undefined' value provided on update #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,10 +867,14 @@ function route(name, model, resources, inflect, querytree) {
, path = update;

if (operation.op === 'replace') {
path.$set = path.$set || {};
if (field.length > 1 && field[0] !== "links") {
path.$set = path.$set || {};
path.$set[field.join(".")] = value;
} else if (value == undefined) {
path.$unset = path.$unset || {};
path.$unset[field[field.length-1]] = 1;
} else {
path.$set = path.$set || {};
path.$set[field[field.length-1]] = value;
}
}else if (operation.op === 'add'){
Expand Down
69 changes: 34 additions & 35 deletions test/fortune/direct.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,57 @@ module.exports = function(options){
ids = options.ids;
});

it("gets a resource collection", function(done){
app.direct.get("people").then(function(res){
it("gets a resource collection", function(){
return app.direct.get("people").then(function(res){
ids.people.forEach(function(id){
_.contains(_.pluck(res.body.people, 'id'), id).should.equal(true);
});
done();
});
});

it("gets a single resource", function(done){
app.direct.get("people", {params: { id:ids.people[0] }}).then(function(res){
it("gets a single resource", function(){
return app.direct.get("people", {params: { id:ids.people[0] }}).then(function(res){
res.body.people.length.should.be.equal(1);
res.body.people[0].id.should.equal(ids.people[0]);
done();
}).catch(function(err){ console.trace(err); });;
});
});

it("gets a number of resources by ids", function(done){
app.direct.get("people", {id:ids.people}).then(function(res){
it("gets a number of resources by ids", function(){
return app.direct.get("people", {id:ids.people}).then(function(res){
_.each(ids.people, function(id){
_.contains(_.pluck(res.body.people, "id"),id).should.be.true;
});
done();
});
});

it("deletes a collection", function(done){
app.direct.get("people").then(function(res){
it("deletes a collection", function(){
return app.direct.get("people").then(function(res){
res.body.people.length.should.be.above(1);
}).then(function(){
return app.direct.destroy("people");
}).then(function(){
return app.direct.get("people");
}).then(function(res){
res.body.people.length.should.be.equal(0);
done();
});
});

it("creates a resource", function(done){
it("creates a resource", function(){
var doc = {people: [{ name: "Director", email: "[email protected]" }]};

app.direct.create("people", {body:doc}).then(function(res){
return app.direct.create("people", {body:doc}).then(function(res){
res.body.people.length.should.be.equal(1);
res.body.people[0].id.should.be.equal(doc.people[0].email);
return app.direct.get("people", {params: {id:doc.people[0].email}});
}).then(function(res){
res.body.people[0].id.should.be.equal(doc.people[0].email);
done();
});
});

it("replaces a resource", function(done){
it("replaces a resource", function(){
var resource, id;

app.direct.get("people",{params: {id:id = ids.people[0]}}).then(function(res){
return app.direct.get("people",{params: {id:id = ids.people[0]}}).then(function(res){
resource = res.body.people[0];
resource.birthday = null;
resource.email = "[email protected]";
Expand All @@ -78,24 +73,22 @@ module.exports = function(options){
return app.direct.get("people", {params: {id: resource.email}});
}).then(function(res){
res.body.people[0].id.should.be.equal(resource.email);
done();
}).catch(function(err){ console.trace(err.stack || err); });
});
});

it("udpate can add a record to an array", function(done){
app.direct.update("people", {params: {id: ids.people[0]}, body:[{
it("update can add a record to an array", function(){
return app.direct.update("people", {params: {id: ids.people[0]}, body:[{
op: "add",
path: "/people/0/links/houses/-",
value: ids.houses[1]
}]}).then(function(res){
res.body.people[0].links.houses.length.should.equal(1);
res.body.people[0].links.houses[0].should.equal(ids.houses[1]);
done();
});
});

it("supports bulk update", function(done){
app.direct.update("people", {params: {id: ids.people[0]}, body:[{
it("supports bulk update", function(){
return app.direct.update("people", {params: {id: ids.people[0]}, body:[{
op: 'add',
path: '/people/0/links/houses/-',
value: ids.houses[0]
Expand All @@ -105,20 +98,28 @@ module.exports = function(options){
value: ids.houses[1]
}]}).then(function(res){
res.body.people[0].links.houses.length.should.equal(2);
done();
});
});

it("supports filters", function(done){
app.direct.get("people", {query: {filter: {name: "Robert"}}}).then(function(res){
it("update should properly match 'undefined' values", function(){
return app.direct.update("people", {params: {id: ids.people[0]}, body:[{
op: "replace",
path: "/people/0/email",
value: undefined
}]}).then(function(res){
should.not.exist(res.body.people[0].email);
});
});

it("supports filters", function(){
return app.direct.get("people", {query: {filter: {name: "Robert"}}}).then(function(res){
res.body.people.length.should.equal(1);
res.body.people[0].name.should.be.equal("Robert");
done();
});
});

it("supports includes", function(done){
app.direct.update("people", {params: {id: ids.people[0]}, body: [{
it("supports includes", function(){
return app.direct.update("people", {params: {id: ids.people[0]}, body: [{
op: "add",
path: "/people/0/links/houses/-",
value: ids.houses[1]
Expand All @@ -128,16 +129,14 @@ module.exports = function(options){
res.body.linked.should.be.an.Object;
res.body.linked.houses.length.should.equal(1);
res.body.linked.houses[0].id.should.equal(ids.houses[1]);
done();
});
});

it("should provide httpMethod for hooks that inspect it", function(done){
app.direct.update("people", {params: {id: ids.people[0]}, body: [{
it("should provide httpMethod for hooks that inspect it", function(){
return app.direct.update("people", {params: {id: ids.people[0]}, body: [{
op: "add", path: "/people/0/links/houses/-", value: ids.houses[0]
}]}).then(function(res){
res.headers["hookedmethod"].should.equal("PATCH");
done();
});
});
});
Expand Down