GithubHelp home page GithubHelp logo

fullcube / loopback-ds-calculated-mixin Goto Github PK

View Code? Open in Web Editor NEW
13.0 12.0 3.0 128 KB

A mixin to enable calculated (persisted) properties for loopback Models

License: MIT License

JavaScript 100.00%
loopback lb2 fullcube mit loopback-mixin calculated-properties

loopback-ds-calculated-mixin's Introduction

CALCULATED MIXIN

Greenkeeper badge

CircleCI Coverage Status Dependencies semantic-release

This is a mixin for the LoopBack framework that adds calculated properties to a model.

A calculated property is a property of which the value is set once just before it is persisted to the data source.

The mixin enables you to define a callback that will be run in the before save operation hook to calculate and set the value for a given property.

INSTALL

npm install --save loopback-ds-calculated-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-calculated-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 calculate has to be defined in the model. The callback can be a promise too.

{
  "name": "Item",
  "properties": {
    "name": "String",
    "description": "String",
    "status": "String",
    "readonly": "boolean"
  },
  "mixins": {
    "Calculated": {
      "properties": {
        "readonly": "calculateReadonly",
        "isTest": {
          "recalculateOnUpdate": true,
          "callback": "calculateIsTest"  
        }
      }
    }
  }
}

On your model you have to define the callback methods.

// Set an item to readonly if status is 'archived'.
Item.calculateReadonly = function calculateReadonly(item) {
  return item.status === 'archived';
};
// Set an isTest if name is 'test'.
Item.calculateIsTest = function calculateIsTest(item) {
  return item.name === 'test'
};

USAGE

When saving a new model instance, your callback will be called automatically. The property value will be set to the value that your callback returns.

By default, the calculated mixin is only called when saving a new instance. You can make it run on updates in addition by setting recalculateOnUpdate in the mixin config for your property.

If you set skipCalculated to true when creating or updating a model instance, the mixin will not run:

Model.create({
  name: 'Bilbo',
  status: 'active'
  readonly: true,
  isTest: true
}, { skipCalculated: true })

CAVEATS

updateAll, is not currently supported as this could result in a large amount database queries and excessive memory usage. If you need to recalculate values en-mass our recommendation is to first apply your changes using updateAll, and then use the same where query to retrieve the result set in batches and apply your calculations manually.

TESTING

Run the tests in test.js

  npm test

DEBUGGING

Run with debugging output on:

  DEBUG='loopback:mixin:calculated' npm test

loopback-ds-calculated-mixin's People

Contributors

beeman avatar greenkeeper[bot] avatar isonet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

loopback-ds-calculated-mixin's Issues

TypeError: Promise.map is not a function

Hey guys,

I'm getting this error when I try to use the mixin:

loopback-computed-mixin-demo :: (master*) » node .                                                           ~/Coding/tests/loopback-computed-mixin-demo 127 ↵
/Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-ds-calculated-mixin/index.js:38
      Promise.map(Object.keys(options.properties), function(property) {
              ^

TypeError: Promise.map is not a function
    at /Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-ds-calculated-mixin/index.js:38:15
    at notifySingleObserver (/Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-datasource-juggler/lib/observer.js:98:22)
    at iterate (/Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-datasource-juggler/node_modules/async/lib/async.js:181:13)
    at Object.async.eachSeries (/Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-datasource-juggler/node_modules/async/lib/async.js:197:9)
    at doNotify (/Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-datasource-juggler/lib/observer.js:95:11)
    at doNotify (/Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-datasource-juggler/lib/observer.js:93:49)
    at doNotify (/Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-datasource-juggler/lib/observer.js:93:49)
    at doNotify (/Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-datasource-juggler/lib/observer.js:93:49)
    at Function.ObserverMixin._notifyBaseObservers (/Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-datasource-juggler/lib/observer.js:116:5)
    at Function.ObserverMixin.notifyObserversOf (/Users/hannes/Coding/tests/loopback-computed-mixin-demo/node_modules/loopback-datasource-juggler/lib/observer.js:91:8)

I used the loopback-computed-mixin-demo as a starting point and changed it to use the loopback-calculated-mixin instead.

Am I missing something here or is this a bug?

cheers
Hannes

Calculate method is not setup if method does not exist at the point the mixin runs

Mixins run very early in the boot process - before components have run. Components can add properties and methods to models. If you set up a calculate mixin that uses a Model function that is defined in a component (and therefore after the mixin initialization has run), the mixin will not be setup properly.

relevant code: https://github.com/fullcube/loopback-ds-calculated-mixin/blob/master/index.js#L8

I think this should probably just give the warning, and not prevent the mixin for actually being setup. Either that or there should be a force option to force the mixin to be configured regardless of wether this sanity check passes or not. Alternatively, this could be moved to a runtime check.

Ability to skip calculation when creating a model instance

Would be good to have the ability to skip the calculation when creating properties. Eg, for data imports/migrations where the property should be explicitly set.

This could be done using ctx.options

eg

  Model.observe('before save', function(ctx, next) {
    // Allow user to bypass calculation by setting `skipCalculated` option.
    if (ctx.options && ctx.options.skipCalculated) {
      return next();
    }
    // We only act on new instances
    if (ctx.instance !== undefined && ctx.isNewInstance !== undefined && ctx.isNewInstance === true) {
    ...

An in-range update of loopback is breaking the build 🚨

The devDependency loopback was updated from 3.24.2 to 3.25.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

loopback is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ci/circleci: Your tests failed on CircleCI (Details).

Commits

The new version differs by 3 commits.

  • 1e33ec5 3.25.0
  • c38900b Merge pull request #4119 from studykik/fix/appdynamics-proxy
  • edb8dbc Support middleware injected by AppDynamics.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.