11/**
22 * @module FrameworkBuilders
3- * @version 1.7.0
3+ * @version 1.7.2
44 */
55
66'use strict' ;
@@ -92,7 +92,9 @@ function SchemaBuilderEntity(parent, name, obj, validator, properties) {
9292 this . properties = properties === undefined ? Object . keys ( obj ) : properties ;
9393 this . transforms ;
9494 this . composes ;
95+ this . operations ;
9596 this . rules ;
97+ this . constants ;
9698 this . onDefault ;
9799 this . onValidation = validator ;
98100 this . onSave ;
@@ -118,6 +120,9 @@ SchemaBuilderEntity.prototype.define = function(name, type, required, primary) {
118120 return self ;
119121 }
120122
123+ if ( type instanceof SchemaBuilderEntity )
124+ type = type . name ;
125+
121126 if ( primary )
122127 self . primary = primary ;
123128 self . schema [ name ] = type ;
@@ -232,7 +237,7 @@ SchemaBuilderEntity.prototype.setQuery = function(fn) {
232237
233238/**
234239 * Set remove handler
235- * @param {Function(error, model, helper, next(value)) } fn
240+ * @param {Function(error, helper, next(value)) } fn
236241 * @return {SchemaBuilderEntity }
237242 */
238243SchemaBuilderEntity . prototype . setRemove = function ( fn ) {
@@ -273,6 +278,25 @@ SchemaBuilderEntity.prototype.addRule = function(name, value) {
273278 return self ;
274279} ;
275280
281+ /**
282+ * Add a new constant for the schema
283+ * @param {String } name Constant name, optional.
284+ * @param {Object } value
285+ * @return {SchemaBuilderEntity }
286+ */
287+ SchemaBuilderEntity . prototype . constant = function ( name , value ) {
288+ var self = this ;
289+
290+ if ( value === undefined )
291+ return self . constants ? self . constants [ name ] : undefined ;
292+
293+ if ( ! self . constants )
294+ self . constants = { } ;
295+
296+ self . constants [ name ] = value ;
297+ return self ;
298+ } ;
299+
276300/**
277301 * Add a new transformation for the entity
278302 * @param {String } name Transform name, optional.
@@ -294,6 +318,27 @@ SchemaBuilderEntity.prototype.addTransform = function(name, fn) {
294318 return self ;
295319} ;
296320
321+ /**
322+ * Add a new operation for the entity
323+ * @param {String } name Operation name, optional.
324+ * @param {Function(errorBuilder, [model], helper, next([output]), entityName) } fn
325+ * @return {SchemaBuilderEntity }
326+ */
327+ SchemaBuilderEntity . prototype . addOperation = function ( name , fn ) {
328+ var self = this ;
329+
330+ if ( typeof ( name ) === FUNCTION ) {
331+ fn = name ;
332+ name = 'default' ;
333+ }
334+
335+ if ( ! self . operations )
336+ self . operations = { } ;
337+
338+ self . operations [ name ] = fn ;
339+ return self ;
340+ } ;
341+
297342/**
298343 * Add a new workflow for the entity
299344 * @param {String } name Workflow name, optional.
@@ -689,6 +734,30 @@ SchemaBuilderEntity.prototype.$make = function(obj) {
689734 return obj ;
690735 } ;
691736
737+ obj . $operation = function ( name , helper , callback ) {
738+
739+ if ( ! obj . $$async ) {
740+ self . operation ( name , obj , helper , callback ) ;
741+ return obj ;
742+ }
743+
744+ obj . $$async . push ( function ( next ) {
745+ self . operation ( name , obj , helper , function ( err , result ) {
746+
747+ if ( obj . $$result )
748+ obj . $$result . push ( err ? null : result ) ;
749+
750+ if ( ! err )
751+ return next ( ) ;
752+ obj . $$async = null ;
753+ next = null ;
754+ obj . $callback ( err , obj . $$result ) ;
755+ } ) ;
756+ } ) ;
757+
758+ return obj ;
759+ } ;
760+
692761 obj . $clean = function ( ) {
693762 return self . clean ( obj ) ;
694763 } ;
@@ -713,6 +782,10 @@ SchemaBuilderEntity.prototype.$make = function(obj) {
713782 return self . rule ( name ) ;
714783 } ;
715784
785+ obj . $constant = function ( name ) {
786+ return self . constant ( name ) ;
787+ } ;
788+
716789 return obj ;
717790} ;
718791
@@ -1327,6 +1400,61 @@ SchemaBuilderEntity.prototype.workflow = function(name, model, helper, callback)
13271400 return self ;
13281401} ;
13291402
1403+ /**
1404+ * Run an operation
1405+ * @param {String } name
1406+ * @param {Object } model A model object, optional, priority: 2.
1407+ * @param {Object } helper A helper object, optional, priority: 1.
1408+ * @param {Function(errorBuilder, output) } callback
1409+ * @return {SchemaBuilderEntity }
1410+ */
1411+ SchemaBuilderEntity . prototype . operation = function ( name , model , helper , callback ) {
1412+
1413+ var self = this ;
1414+
1415+ var tm = typeof ( model ) ;
1416+ var th = typeof ( helper ) ;
1417+ var tc = typeof ( callback ) ;
1418+
1419+ if ( tc === UNDEFINED ) {
1420+ if ( th === FUNCTION ) {
1421+ callback = helper ;
1422+ helper = model ;
1423+ model = undefined ;
1424+ } else if ( th === UNDEFINED ) {
1425+ helper = model ;
1426+ model = undefined ;
1427+ }
1428+ } else if ( th === UNDEFINED ) {
1429+ helper = model ;
1430+ model = undefined ;
1431+ }
1432+
1433+ if ( typeof ( helper ) === FUNCTION ) {
1434+ callback = helper ;
1435+ helper = undefined ;
1436+ }
1437+
1438+ if ( typeof ( callback ) !== 'function' )
1439+ callback = undefined ;
1440+
1441+ var operation = self . operations ? self . operations [ name ] : undefined ;
1442+
1443+ if ( ! operation ) {
1444+ callback ( new ErrorBuilder ( ) . add ( '' , 'Operation not found.' ) ) ;
1445+ return ;
1446+ }
1447+
1448+ var builder = new ErrorBuilder ( ) ;
1449+
1450+ operation . call ( self , builder , model , helper , function ( result ) {
1451+ if ( callback )
1452+ callback ( builder . hasError ( ) ? builder : null , result ) ;
1453+ } , self . name ) ;
1454+
1455+ return self ;
1456+ } ;
1457+
13301458/**
13311459 * Clean model (remove state of all schemas in model).
13321460 * @param {Object } model
@@ -1354,6 +1482,7 @@ SchemaBuilderEntity.prototype.clean = function(m, isCopied) {
13541482 delete model [ '$callback' ] ;
13551483 delete model [ '$transform' ] ;
13561484 delete model [ '$workflow' ] ;
1485+ delete model [ '$operation' ] ;
13571486 delete model [ '$destroy' ] ;
13581487 delete model [ '$save' ] ;
13591488 delete model [ '$remove' ] ;
@@ -1364,6 +1493,7 @@ SchemaBuilderEntity.prototype.clean = function(m, isCopied) {
13641493 delete model [ '$validate' ] ;
13651494 delete model [ '$compose' ] ;
13661495 delete model [ '$rule' ] ;
1496+ delete model [ '$constant' ] ;
13671497
13681498 var keys = Object . keys ( model ) ;
13691499
@@ -1414,6 +1544,7 @@ function ErrorBuilder(onResource) {
14141544 this . onResource = onResource ;
14151545 this . resourceName = framework . config [ 'default-errorbuilder-resource-name' ] || 'default' ;
14161546 this . resourcePrefix = framework . config [ 'default-errorbuilder-resource-prefix' ] || '' ;
1547+ this . isResourceCustom = false ;
14171548 this . count = 0 ;
14181549 this . replacer = [ ] ;
14191550 this . isPrepared = false ;
@@ -1700,6 +1831,7 @@ ErrorBuilder.prototype = {
17001831 */
17011832ErrorBuilder . prototype . resource = function ( name , prefix ) {
17021833 var self = this ;
1834+ self . isResourceCustom = true ;
17031835 self . resourceName = name || 'default' ;
17041836 self . resourcePrefix = prefix || '' ;
17051837 return self . _resource ( ) ;
0 commit comments