10BC0 Add ability to create, update, patch and remove many by daffl · Pull Request #179 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
Merged
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/mixins/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ module.exports = function (service) {
emitter.on(eventName, function (results, args) {
if (!results[0]) { // callback without error
var hook = hookObject(method, 'after', args);
service.emit(event, results[1], hook);
var data = Array.isArray(results[1]) ? results[1] : [ results[1] ];

data.forEach(function(current) {
service.emit(event, current, hook);
});
} else {
service.emit('serviceError', results[0]);
}
Expand Down
8 changes: 7 additions & 1 deletion lib/providers/rest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ module.exports = function (config) {

// GET / -> service.find(cb, params)
baseRoute.get.apply(baseRoute, before.concat(app.rest.find(service), after, handler));
// POST -> service.create(data, params, cb)
// POST / -> service.create(data, params, cb)
baseRoute.post.apply(baseRoute, before.concat(app.rest.create(service), after, handler));
// PATCH / -> service.patch(null, data, params)
baseRoute.patch.apply(baseRoute, before.concat(app.rest.patch(service), after, handler));
// PUT / -> service.update(null, data, params)
baseRoute.put.apply(baseRoute, before.concat(app.rest.update(service), after, handler));
// DELETE / -> service.remove(null, data, params)
baseRoute.delete.apply(baseRoute, before.concat(app.rest.remove(service), after, handler));

// GET /:id -> service.get(id, params, cb)
idRoute.get.apply(idRoute, before.concat(app.rest.get(service), after, handler));
Expand Down
4 changes: 2 additions & 2 deletions lib/providers/rest/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ function reqNone () {

// Returns the leading parameters for a `get` or `remove` request (the id)
function reqId (req) {
return [ req.params.id ];
return [ req.params.id || null ];
}

// Returns the leading parameters for an `update` or `patch` request (id, data)
function reqUpdate (req) {
return [ req.params.id, req.body ];
return [ req.params.id || null, req.body ];
}

// Returns the leading parameters for a `create` request (data)
Expand Down
30 changes: 30 additions & 0 deletions test/mixins/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,36 @@ describe('Event mixin', function () {
});
});

it('array event data emits multiple event', function (done) {
var fixture = [
{ id: 0 },
{ id: 1 },
{ id: 2 },
];
var FixtureService = Proto.extend({
create: function (data, params, cb) {
_.defer(function () {
cb(null, fixture);
});
}
});

var instance = create.call(FixtureService);
var counter = 0;

mixinEvent(instance);

instance.on('created', function (data) {
assert.equal(data.id, counter);
counter++;
if(counter === fixture.length) {
done();
}
});

instance.create({}, {}, function () {});
});

it('does not punch when service has an events list (#118)', function(done) {
var FixtureService = Proto.extend({
events: [ 'created' ],
Expand Down
34 changes: 34 additions & 0 deletions test/providers/primus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ describe('Primus provider', function () {
});
});

it('::update many', function (done) {
var original = {
name: 'updating',
many: true
};

socket.send('todo::update', null, original, {}, function (error, data) {
verify.update(null, original, data);

done(error);
});
});

it('::patch', function (done) {
var original = {
name: 'patching'
Expand All @@ -193,13 +206,34 @@ describe('Primus provider', function () {
});
});

it('::patch many', function (done) {
var original = {
name: 'patching',
many: true
};

socket.send('todo::patch', null, original, {}, function (error, data) {
verify.patch(null, original, data);

done(error);
});
});

it('::remove', function (done) {
socket.send('todo::remove', 11, {}, function (error, data) {
verify.remove(11, data);

done(error);
});
});

it('::remove many', function (done) {
socket.send('todo::remove', null, {}, function (error, data) {
verify.remove(null, data);

done(error);
});
});
});

describe('Events', function () {
Expand Down
65 changes: 60 additions & 5 deletions test/providers/rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ describe('REST provider', function () {
});
});

it('PUT .update many', function (done) {
var original = {
description: 'PUT .update',
many: true
};

request({
url: 'http://localhost:4777/todo',
method: 'put',
body: JSON.stringify(original),
headers: {
'Content-Type': 'application/json'
}
}, function (error, response, body) {
var data = JSON.parse(body);
assert.ok(response.statusCode === 200, 'Got OK status code');
verify.update(null, original, data);

done(error);
});
});

it('PATCH .patch', function (done) {
var original = {
description: 'PATCH .patch'
Expand All @@ -113,12 +135,31 @@ describe('REST provider', function () {
done(error);
});
});
});

describe('Dynamic Services', function() {
it('.remove', function (done) {
it('PATCH .patch many', function (done) {
var original = {
description: 'PATCH .patch',
many: true
};

request({
url: 'http://localhost:4777/todo/233',
url: 'http://localhost:4777/todo',
method: 'patch',
body: JSON.stringify(original),
headers: {
'Content-Type': 'application/json'
}
}, function (error, response, body) {
assert.ok(response.statusCode === 200, 'Got OK status code');
verify.patch(null, original, JSON.parse(body));

done(error);
});
});

it('DELETE .remove', function (done) {
request({
url: 'http://localhost:4777/tasks/233',
method: 'delete'
}, function (error, response, body) {
assert.ok(response.statusCode === 200, 'Got OK status code');
Expand All @@ -128,6 +169,20 @@ describe('REST provider', function () {
});
});

it('DELETE .remove many', function (done) {
request({
url: 'http://localhost:4777/tasks',
method: 'delete'
}, function (error, response, body) {
assert.ok(response.statusCode === 200, 'Got OK status code');
verify.remove(null, JSON.parse(body));

done(error);
});
});
});

describe('Dynamic Services', function() {
it('GET .find', function (done) {
request('http://localhost:4777/tasks', function (error, response, body) {
assert.ok(response.statusCode === 200, 'Got OK status code');
Expand Down Expand Up @@ -270,7 +325,7 @@ describe('REST provider', function () {
done(error);
});
});

it('empty response sets 204 status codes', function(done) {
request('http://localhost:4780/todo', function (error, response) {
assert.ok(response.statusCode === 204, 'Got empty status code');
Expand Down
21 changes: 19 additions & 2 deletions test/providers/service-fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,44 @@ exports.Service = {
var result = _.clone(data);
result.id = 42;
result.status = 'created';

if(Array.isArray(data)) {
result.many = true;
}

callback(null, result);
},

update: function (id, data, params, callback) {
var result = _.clone(data);
result.id = id;
result.status = 'updated';

if(id === null) {
result.many = true;
}

callback(null, result);
},

patch: function (id, data, params, callback) {
var result = _.clone(data);
result.id = id;
result.status = 'patched';

if(id === null) {
result.many = true;
}

callback(null, result);
},

remove: function (id, params, callback) {
callback(null, {
var result = {
id: id
});
};

callback(null, result);
}
};

Expand Down
34 changes: 34 additions & 0 deletions test/providers/socketio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ describe('SocketIO provider', function () {
});
});

it('::update many', function (done) {
var original = {
name: 'updating',
many: true
};

socket.emit('todo::update', null, original, {}, function (error, data) {
verify.update(null, original, data);

done(error);
});
});

it('::patch', function (done) {
var original = {
name: 'patching'
Expand All @@ -189,13 +202,34 @@ describe('SocketIO provider', function () {
});
});

it('::patch many', function (done) {
var original = {
name: 'patching',
many: true
};

socket.emit('todo::patch', null, original, {}, function (error, data) {
verify.patch(null, original, data);

done(error);
});
});

it('::remove', function (done) {
socket.emit('todo::remove', 11, {}, function (error, data) {
verify.remove(11, data);

done(error);
});
});

it('::remove many', function (done) {
socket.emit('todo::remove', null, {}, function (error, data) {
verify.remove(null, data);

done(error);
});
});
});

describe('Events', function () {
Expand Down
0