new ModelBaseClass(data, options) → {ModelBaseClass}
Model class: base class for all persistent objects.
`ModelBaseClass` mixes `Validatable` and `Hookable` classes methods
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | Initial object data |
options |
Object | An object to control the instantiation |
- Source:
Returns:
an instance of the ModelBaseClass
- Type
- ModelBaseClass
Methods
getPropertyType(propName) → {String}
Get model property type.
Parameters:
Name | Type | Description |
---|---|---|
propName |
String | Property name |
- Source:
Returns:
Name of property type
- Type
- String
reset()
Reset dirty attributes.
This method does not perform any database operations; it just resets the object to its
initial state.
- Source:
toObject(onlySchema, removeHidden, removeProtected) → {Object}
Convert model instance to a plain JSON object.
Returns a canonical object representation (no getters and setters).
Parameters:
Name | Type | Description |
---|---|---|
onlySchema |
Boolean | Restrict properties to dataSource only. Default is false. If true, the function returns only properties defined in the schema; Otherwise it returns all enumerable properties. |
removeHidden |
Boolean | Boolean flag as part of the transformation. If true, then hidden properties should not be brought out. |
removeProtected |
Boolean | Boolean flag as part of the transformation. If true, then protected properties should not be brought out. |
- Source:
Returns:
returns Plain JSON object
- Type
- Object
(static) defineProperty(prop, params)
Define a property on the model.
Parameters:
Name | Type | Description |
---|---|---|
prop |
String | Property name |
params |
Object | Various property configuration |
- Source:
(static) getMergePolicy(options) → {Object}
`getMergePolicy()` provides model merge policies to apply when extending
a child model from a base model. Such a policy drives the way parent/child model
properties/settings are merged/mixed-in together.
Below is presented the expected merge behaviour for each option.
NOTE: This applies to top-level settings properties
- Any
- `{replace: true}` (default): child replaces the value from parent
- assignin `null` on child setting deletes the inherited setting
- Arrays:
- `{replace: false}`: unique elements of parent and child cumulate
- `{rank: true}` adds the model inheritance rank to array
elements of type Object {} as internal property `__rank`
- Object {}:
- `{replace: false}`: deep merges parent and child objects
- `{patch: true}`: child replaces inner properties from parent
The recommended built-in merge policy is as follows. It is returned by getMergePolicy()
when calling the method with option `{configureModelMerge: true}`.
```
{
description: {replace: true}, // string or array
options: {patch: true}, // object
hidden: {replace: false}, // array
protected: {replace: false}, // array
indexes: {patch: true}, // object
methods: {patch: true}, // object
mixins: {patch: true}, // object
relations: {patch: true}, // object
scope: {replace: true}, // object
scopes: {patch: true}, // object
acls: {rank: true}, // array
// this setting controls which child model property's value allows deleting
// a base model's property
__delete: null,
// this setting controls the default merge behaviour for settings not defined
// in the mergePolicy specification
__default: {replace: true},
}
```
The legacy built-in merge policy is as follows, it is retuned by `getMergePolicy()`
when avoiding option `configureModelMerge`.
NOTE: it also provides the ACLs ranking in addition to the legacy behaviour, as well
as fixes for settings 'description' and 'relations': matching relations from child
replace relations from parents.
```
{
description: {replace: true}, // string or array
properties: {patch: true}, // object
hidden: {replace: false}, // array
protected: {replace: false}, // array
relations: {acls: true}, // object
acls: {rank: true}, // array
}
```
`getMergePolicy()` can be customized using model's setting `configureModelMerge` as follows:
``` json
{
// ..
options: {
configureModelMerge: {
// merge options
}
}
// ..
}
```
NOTE: mergePolicy parameter can also defined at JSON model definition root
`getMergePolicy()` method can also be extended programmatically as follows:
``` js
myModel.getMergePolicy = function(options) {
const origin = myModel.base.getMergePolicy(options);
return Object.assign({}, origin, {
// new/overriding options
});
};
```
Parameters:
Name | Type | Description |
---|---|---|
options |
Object | option `configureModelMerge` can be used to alter the returned merge policy: - `configureModelMerge: true` will have the method return the recommended merge policy. - `configureModelMerge: {..}` will actually have the method return the provided object. - not providing this options will have the method return a merge policy emulating the the model merge behaviour up to datasource-juggler v3.6.1, as well as the ACLs ranking. |
- Source:
Returns:
mergePolicy The model merge policy to apply when using the
current model as base class for a child model
- Type
- Object
(static) getPropertyType(propName) → {String}
Get model property type.
Parameters:
Name | Type | Description |
---|---|---|
propName |
String | Property name |
- Source:
Returns:
Name of property type
- Type
- String
(static) getUpdateOnlyProperties() → {updateOnlyProps}
Gets properties defined with 'updateOnly' flag set to true from the model. This flag is also set to true
internally for the id property, if this property is generated and IdInjection is true.
- Source:
Returns:
List of properties with updateOnly set to true.
- Type
- updateOnlyProps
(static) isHiddenProperty(propertyName) → {Boolean}
Checks if property is hidden.
Parameters:
Name | Type | Description |
---|---|---|
propertyName |
String | Property name |
- Source:
Returns:
true or false if hidden or not.
- Type
- Boolean
(static) isProtectedProperty(propertyName) → {Boolean}
Checks if property is protected.
Parameters:
Name | Type | Description |
---|---|---|
propertyName |
String | Property name |
- Source:
Returns:
true or false if protected or not.
- Type
- Boolean
(static) mixin(anotherClass, options) → {ModelClass}
Parameters:
Name | Type | Description |
---|---|---|
anotherClass |
String | could be string or class. Name of the class or the class itself |
options |
Object | An object to control the instantiation |
- Source:
Returns:
- Type
- ModelClass
(static) toString()
Return string representation of class
This overrides the default `toString()` method
- Source: