ForgeTotal.js 5
it

Total.js 5

Aggiornare e rimuovere documenti NoSQL

Total.js 5 · total5@0.0.19 nosqldatabase Eseguita con Total.js 5

update(data) e remove() funzionano come find(): aggiungi le condizioni con .where() e termina con .callback(function(err, count) {}). Il callback riceve il numero di documenti modificati o rimossi. Aggiungi sempre una condizione: senza, l'operazione si applica a ogni documento.

update(data) unisce i campi di data ai documenti corrispondenti. Usa read() per ottenere un singolo documento.

controllers/tasks.js
exports.install = function() {
	ROUTE('POST /tasks/', create);
	ROUTE('PATCH /tasks/{id}/', done);
	ROUTE('DELETE /tasks/{id}/', remove);
	ROUTE('GET /tasks/{id}/', read);
};

function create($) {
	var task = { id: $.body.id, title: $.body.title, done: false };
	NOSQL('tasks').insert(task).callback(() => $.json(task));
}

function done($) {
	NOSQL('tasks').update({ done: true }).where('id', $.params.id).callback((err, count) => $.json({ updated: count }));
}

function remove($) {
	NOSQL('tasks').remove().where('id', $.params.id).callback((err, count) => $.json({ removed: count }));
}

function read($) {
	NOSQL('tasks').read().where('id', $.params.id).callback((err, response) => $.json(response || { missing: true }));
}