Constructor
new Model()
Example
const Model = require('five-bells-shared/lib/model').Model
class Car extends Model {
constructor () {
super()
}
// Define filters for external data formats
static convertFromExternal (data) {
// Convert year to number
data.year = parseInt(data.year)
return data
}
static convertToExternal (data) {
// Year is presented as a string to the outside world
data.year = String(model.year)
return data
}
// Add a validator for external data
static validateExternal (data) {
if (typeof data.year !== 'string' || data.year.length > 4) {
throw new Error('Invalid year')
}
return true
}
// Add virtual properties via getters and setters
get description () {
return this.make + ' ' + this.model + ' ' + this.year
}
}
Methods
(static) convertFromExternal(data) → {Object}
Filter any data incoming from the outside world.
You can override this method to add data mutations anytime external data is passed in. This can include format conversions, sanitization and the like.
The data passed in is cloned, so feel free to modify the data object.
You must return the data object, or you will trigger an error.
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | External data |
Returns:
Internal data
- Type
- Object
(static) convertToExternal(data) → {Object}
Filter any data being sent to the outside world.
You can override this method to add filtering behavior anytime data is passed to the outside. This can include format conversions, converting local IDs to URIs and the like.
The data passed to this method is cloned, so feel free to modify the data object.
filterInput(filterOutput(data)) should be idempotent although it may normalize the data.
You must return the data object, or you will trigger an error.
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | Internal data |
Returns:
External data
- Type
- Object
(static) createBodyParser() → {bodyParserMiddleware}
Generate a middleware that creates an instance from the request body.
This method returns a middleware which will read the request body, parse it, validate it (if the model has set a schema) and create an instace of the model with that data.
The resulting model will be added to the Koa context as this.body
.
Returns:
Middleware for parsing model out of JSON body
- Type
- bodyParserMiddleware
(static) fromData(data) → {Model}
Creates instance from data.
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | Raw data. |
Returns:
Instance populated with data.
- Type
- Model
(static) fromDataExternal(data) → {Model}
Creates instance from raw data.
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | Raw data. |
Returns:
Instance populated with data.
- Type
- Model
(static) setData(data)
Set data to the model instance, bypassing filters.
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | Input data |
(static) validateExternal(data) → {ValidationResult}
Validate incoming external data.
Models may override this method to validate external data against a schema.
Parameters:
Name | Type | Description |
---|---|---|
data |
object | Data to be validated |
Returns:
Result of the validation
- Type
- ValidationResult
getData() → {Object}
Get model data as plain old JS object.
Bypasses output filters.
Returns:
Plain representation of model
- Type
- Object
getDataExternal() → {Object}
Get model data as plain old JS object.
Applies output filters.
Returns:
Plain, normalized representation of model
- Type
- Object
setDataExternal(data)
Apply a set of JSON data (in external format) to this instance.
Parameters:
Name | Type | Description |
---|---|---|
data |
Object | Input data |
setDataPersistent()
Set data from database.
Applies filters for conversion from database format to normal format.
- Source:
setDataPersistent()
Set data from database.
Applies filters for conversion from database format to normal format.
- Source: