Build a complete REST resource with actions
A REST resource is six operations: list (query), read, insert, update (replace), patch (change a few fields) and remove. In Total.js each one is an action with its own validation, so the route table stays a plain map from URL to action. params validates the id of the URL, query the filters of the list, input the body. Inside an action $.params, $.query and the validated body (second argument of action) are already clean values.
Report a missing record with $.invalid(404): it answers with the status code and a JSON error, the same way for every action. $.success() answers { "success": true } and $.callback(value) answers with a value, which is also filtered by the output of the action. UID() creates the id of a new record.
patch needs partial: true. Without it a field that was not sent is still filled with the default of its type ('' for a String), and update() would overwrite the stored value with that empty text. With partial: true only the fields present in the request reach the action.
NEWACTION('Products/query', {
name: 'List products',
query: 'page:Number, search:String',
output: 'items:[id:UID, name:String, price:Number], page:Number',
action: function($) {
var page = $.query.page || 1;
var builder = NOSQL('products').find().sort('name').take(10).skip((page - 1) * 10);
if ($.query.search)
builder.search('name', $.query.search);
builder.callback((err, items) => $.callback({ items: items, page: page }));
}
});
NEWACTION('Products/read', {
name: 'Read a product',
params: '*id:UID',
output: 'id:UID, name:String, price:Number',
action: function($) {
NOSQL('products').read().where('id', $.params.id).callback(function(err, item) {
if (item)
$.callback(item);
else
$.invalid(404);
});
}
});
NEWACTION('Products/insert', {
name: 'Create a product',
input: '*name:String, *price:Number',
action: function($, model) {
model.id = UID();
NOSQL('products').insert(model).callback(() => $.callback({ id: model.id }));
}
});
NEWACTION('Products/update', {
name: 'Replace a product',
params: '*id:UID',
input: '*name:String, *price:Number',
action: function($, model) {
NOSQL('products').update(model).where('id', $.params.id).callback(function(err, count) {
if (count)
$.success();
else
$.invalid(404);
});
}
});
NEWACTION('Products/patch', {
name: 'Change some fields of a product',
partial: true,
params: '*id:UID',
input: 'name:String, price:Number',
action: function($, model) {
NOSQL('products').update(model).where('id', $.params.id).callback(function(err, count) {
if (count)
$.success();
else
$.invalid(404);
});
}
});
NEWACTION('Products/remove', {
name: 'Remove a product',
params: '*id:UID',
action: function($) {
NOSQL('products').remove().where('id', $.params.id).callback(function(err, count) {
if (count)
$.success();
else
$.invalid(404);
});
}
});exports.install = function() {
ROUTE('GET /products/ --> *Products/query');
ROUTE('GET /products/{id}/ --> *Products/read');
ROUTE('POST /products/ --> *Products/insert');
ROUTE('PUT /products/{id}/ --> *Products/update');
ROUTE('PATCH /products/{id}/ --> *Products/patch');
ROUTE('DELETE /products/{id}/ --> *Products/remove');
};