https://github.com/fullcube/loopback-ds-computed-mixin
A mixin to enable computed (dynamic) properties for loopback Models
https://github.com/fullcube/loopback-ds-computed-mixin
fullcube lb2 loopback loopback-mixin mit
Last synced: 6 months ago
JSON representation
A mixin to enable computed (dynamic) properties for loopback Models
- Host: GitHub
- URL: https://github.com/fullcube/loopback-ds-computed-mixin
- Owner: fullcube
- License: mit
- Created: 2015-07-26T15:30:58.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-06-01T01:17:04.000Z (over 5 years ago)
- Last Synced: 2025-07-10T19:22:00.200Z (7 months ago)
- Topics: fullcube, lb2, loopback, loopback-mixin, mit
- Language: JavaScript
- Homepage:
- Size: 133 KB
- Stars: 32
- Watchers: 11
- Forks: 12
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
COMPUTED
================
[](https://greenkeeper.io/)
[](https://circleci.com/gh/fullcube/loopback-ds-computed-mixin) [](https://coveralls.io/github/fullcube/loopback-ds-computed-mixin?branch=master) [](https://david-dm.org/fullcube/loopback-ds-computed-mixin) [](https://github.com/semantic-release/semantic-release)
This is a mixin for the LoopBack framework that adds computed properties to a model.
A computed property is a property of which the value is set dynamically after reading model data from the data source.
- The mixin uses the `loaded` observer.
- It only runs when a single instance gets loaded, e.g. it checks `ctx.instance`.
- It only runs when it is a new instance, e.g. it checks `ctx.isNewInstance`.
- It overrides the configured property if one exists in the data source.
INSTALL
=============
```bash
npm install --save loopback-ds-computed-mixin
```
SERVER CONFIG
=============
Add the mixins property to your server/model-config.json:
```
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"../node_modules/loopback-ds-computed-mixin/lib",
"../common/mixins"
]
}
}
```
CONFIG
=============
To use with your Models add the `mixins` attribute to the definition object of your model config.
The property you want to compute has to be defined in the model. The callback can be a promise too.
```json
{
"name": "Item",
"properties": {
"name": "String",
"description": "String",
"status": "String",
"readonly": "boolean"
},
"mixins": {
"Computed": {
"properties": {
"readonly": "computeReadonly"
}
}
}
}
```
On your model you have to define the callback method.
```javascript
// Set an item to readonly if status is archived
Item.computeReadonly = function computeReadonly(item) {
return item.status === 'archived';
};
```
TESTING
=============
Run the tests in `test.js`
```bash
npm test
```
Run with debugging output on:
```bash
DEBUG='loopback:mixin:computed' npm test
```