new RelationBuilderApi()
Builder DSL extension to build model relations
Adds the following property modifiers:
hasMany
sets a one to many hierarchical relation under the attribute name, maps to RelationBuilderApi#attrAsCollection
hasOne
sets a one to one hierarchical relation under the attribute name, maps to RelationBuilderApi#attrAsResource
belongsTo
sets a one to one reference relation under the attribute name, maps to RelationBuilderApi#attrAsReference
belongsToMany
sets a one to many reference relation under the attribute name, maps to RelationBuilderApi#attrAsReferenceToMany
- Source:
- module/extended/builder-relations.js, line 27
Methods
-
attrAsCollection(_name, _model, _url, _source, _inverseOf, _params, _hooks) → {BuilderApi}
-
Registers a model resources relation
Parameters:
Name Type Description _name
string Attribute name
_model
string | object Other model, supports a model name or a direct reference.
_url
string Partial url
_source
string Inline resource alias (optional)
_inverseOf
string Inverse property name (optional)
_params
object Generated collection default parameters
_hooks
object Hooks to be applied just to the generated collection
- Source:
- module/extended/builder-relations.js, line 56
Returns:
self
- Type
- BuilderApi
-
attrAsReference(_name, _model, _key, _prefetch) → {BuilderApi}
-
Registers a model reference relation.
A reference relation expects the host object to provide the primary key of the referenced object or the referenced object itself (including its key).
For example, given the following resource structure with a foreign key:
{ user_id: 20 }
Or this other structure with inlined data:
{ user: { id: 30, name: 'Steve' } }
You should define the following model:
restmod.model('/bikes', { user: { belongsTo: 'User' } // works for both cases detailed above })
The object generated by the relation is not scoped to the host object, but to it's base class instead (not like hasOne), so the type should not be nested.
Its also posible to override the foreign key name.
When a object containing a belongsTo reference is encoded for a server request, only the primary key value is sent using the same foreign key name that was using on decoding. (
user_id
in the above example).Parameters:
Name Type Description _name
string Attribute name
_model
string | object Other model, supports a model name or a direct reference.
_key
string foreign key property name (optional, defaults to _attr + '_id').
_prefetch
bool if set to true, $fetch will be automatically called on relation object load.
- Source:
- module/extended/builder-relations.js, line 217
Returns:
self
- Type
- BuilderApi
-
attrAsReferenceToMany(_name, _model, _keys) → {BuilderApi}
-
Registers a model reference relation.
A reference relation expects the host object to provide the primary key of the referenced objects or the referenced objects themselves (including its key).
For example, given the following resource structure with a foreign key array:
{ users_ids: [20, 30] }
Or this other structure with inlined data:
{ users: [{ id: 20, name: 'Steve' },{ id: 30, name: 'Pili' }] }
You should define the following model:
restmod.model('/bikes', { users: { belongsToMany: 'User' } // works for both cases detailed above })
The object generated by the relation is not scoped to the host object, but to it's base class instead (unlike hasMany), so the referenced type should not be nested.
When a object containing a belongsToMany reference is encoded for a server request, only the primary key value is sent for each object.
Parameters:
Name Type Description _name
string Attribute name
_model
string | object Other model, supports a model name or a direct reference.
_keys
string Server name for the property that holds the referenced keys in response and request.
- Source:
- module/extended/builder-relations.js, line 312
Returns:
self
- Type
- BuilderApi
-
attrAsResource(_name, _model, _url, _source, _inverseOf, _hooks) → {BuilderApi}
-
Registers a model resource relation
Parameters:
Name Type Description _name
string Attribute name
_model
string | object Other model, supports a model name or a direct reference.
_url
string Partial url (optional)
_source
string Inline resource alias (optional)
_inverseOf
string Inverse property name (optional)
_hooks
object Hooks to be applied just to the instantiated record
- Source:
- module/extended/builder-relations.js, line 121
Returns:
self
- Type
- BuilderApi