ForgeTotal.js 5
en

Total.js 5

Group actions in a schema and reuse input definitions

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

NEWSCHEMA(name, fn) groups the actions of one entity. The function receives the schema object and schema.action(name, meta) registers an action called Name/action, with the same options as NEWACTION() (input, output, params, user, permissions, action...). Everywhere else the action is used by its full name, exactly as if it had been declared with NEWACTION('Users/insert', ...): ROUTE('POST /users/ --> *Users/insert') or ACTION('Users/insert', payload).

A fragment of an input definition that several actions share, such as an address, goes in its own file in jsonschemas/. A .txt file holds the same name:Type list as input; the name of the file is the name of the schema. Reference it from an input with @Name, and its fields (and which of them are required) are added to the action's own fields.

jsonschemas/Address.txt
*street:String, *city:String, zip:String
schemas/users.js
NEWSCHEMA('Users', function(schema) {

	schema.action('insert', {
		name: 'Create a user',
		input: '*name:String, @Address',
		output: 'id:UID, name:String, city:String',
		action: function($, model) {
			$.callback({ id: UID(), name: model.name, city: model.city });
		}
	});

	schema.action('count', {
		name: 'Count users',
		action: function($) {
			$.callback({ count: 2 });
		}
	});

});
controllers/users.js
exports.install = function() {
	ROUTE('POST /users/ --> *Users/insert');
	ROUTE('GET /users/count/ --> *Users/count');
};