GithubHelp home page GithubHelp logo

nvdnkpr / loopback-ds-changed-mixin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fullcube/loopback-ds-changed-mixin

0.0 2.0 0.0 27 KB

A mixin to provide change notifications for loopback Model properties

License: MIT License

JavaScript 100.00%

loopback-ds-changed-mixin's Introduction

CHANGED

This module is designed for the Strongloop Loopback framework. It provides a mixin that makes it possible to trigger a function if selected model properties change.

The property value is an object that details model properties to be watched as well as the callback function to be trigged.

INSTALL

npm install --save loopback-ds-changed-mixin

SERVER.JS

In your server/server.js file add the following line before the boot(app, __dirname); line.

...
var app = module.exports = loopback();
...
// Add Readonly Mixin to loopback
require('loopback-ds-changed-mixin')(app);

boot(app, __dirname, function(err) {
  'use strict';
  if (err) throw err;

  // start the server if `$ node server.js`
  if (require.main === module)
    app.start();
});

CONFIG

To use with your Models add the mixins attribute to the definition object of your model config.

{
    "name": "Widget",
    "properties": {
        "name": "String",
        "description": "String",
        "status": "String"
    },
    "mixins": {
        "Changed": {
            "properties": {
                "name": "changeName",
                "status": "changeStatus",
            }
        }
    }
}

In the above, if the status property is modified on one of more model instances, the function that is defined at the property will be executed with a ChangeSet as a parameter.

// The ChangeSet object passed here has several helper methods    
function changeName(changeSet) {

    // Return an array of all the ID's in this change set
    changeSet.getIdList();

    // Return an object with all the ID's and their new values
    changeSet.getIds();

    // Return the value for a given ID
    changeSet.getId(id);

    // Return an array of all the unique values in this change set
    changeSet.getValueList();

    // Return an object with all the Values and an array of the ID's changed to this value
    changeSet.getValues();

    // Return an array of all the ID's changed to a given value
    changeSet.getValue(value);

    // The raw data is available as well
    changeSet.ids;
    changeSet.values;

}

OPTIONS

The specific fields that are to be marked as changed can be set by passing an object to the mixin options.

In this example we mark the status and productId properties for change notifications. The value of each property defined here is the name of the callback method that is invoked when changes on that property is detected.

{
    "name": "Widget",
    "properties": {
        "name": "String",
        "description": "String",
        "status": "String"
    },
    "mixins": {
        "Changed": {
            "properties": {
                "name": "changeName",
                "status": "changeStatus",
            }
        }
    }
}

You can selectively skip the changed mixin behavior in calls to loopback update methods by setting the skipChange option. This can be a boolean to skip the behavior on all properties, a string to skip the behavior for a single property, or an array or object to skip the behavior for multiple properties.

instance.save({skipChanged: true}); // skip behavior
instance.save({skipChanged: 'name'}); // skip behavior for the properties.
instance.save({skipChanged: ['name', 'status']}); // skip behavior for name and status properties.
instance.save({skipChanged: {name: true, status: true}}); // skip behavior for name and status properties.

TESTING

Run the tests in test.js

  npm test

Run with debugging output on:

  DEBUG='loopback-ds-changed-mixin' npm test

Run the test with a mongodb datasource

  MONGODB_URL=mongodb://localhost/ds_changed_mixin npm test

loopback-ds-changed-mixin's People

Contributors

beeman avatar

Watchers

Navid Nikpour avatar  avatar

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.