ForgeTotal.js 5
en

Total.js 5

Use the DATA API to switch databases

Total.js 5 · total5@0.0.19 databasenosqlquerybuilder Executed against Total.js 5

DATA is a database-agnostic entry point to the query builder. The first argument is driver/table; nosql/people means "the embedded NoSQL database, table people". The builder methods (where, sort, take, callback) are the same you use with NOSQL(), so the code of your actions stays the same if you later replace the driver.

CallDoes
DATA.insert(table, doc)inserts a document
DATA.find(table)returns a builder for a list
DATA.read(table)returns a builder for one document
DATA.update(table, data)updates documents
DATA.remove(table)removes documents
definitions/people.js
NEWACTION('People/insert', {
	name: 'Insert a person',
	input: '*name:String, age:Number',
	action: function($, model) {
		model.id = UID();
		DATA.insert('nosql/people', model).callback(() => $.success(model.id));
	}
});

NEWACTION('People/query', {
	name: 'List people',
	query: 'sort:Lowercase',
	action: function($) {
		DATA.find('nosql/people').sort($.query.sort || 'name_asc').callback((err, response) => $.callback(response));
	}
});

NEWACTION('People/rename', {
	name: 'Rename a person',
	params: '*id:UID',
	input: '*name:String',
	action: function($, model) {
		DATA.update('nosql/people', { name: model.name }).where('id', $.params.id).callback((err, count) => $.success(count));
	}
});
controllers/people.js
exports.install = function() {
	ROUTE('POST /people/ --> *People/insert');
	ROUTE('GET /people/ --> *People/query');
	ROUTE('PATCH /people/{id}/ --> *People/rename');
};