Giter Site home page Giter Site logo

cloudever / loopback-ds-readonly-mixin Goto Github PK

View Code? Open in Web Editor NEW

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

1.0 1.0 0.0 44 KB

A mixin to enable readonly properties for loopback Models

License: MIT License

JavaScript 100.00%

loopback-ds-readonly-mixin's Introduction

READONLY

Greenkeeper badge

Circle CI Coverage Status Dependencies semantic-release

This module is designed for the Strongloop Loopback framework. It provides a mixin that makes it possible to mark models or model properties as read only. A read only property may not be written to directly when creating or updating models using remote REST methods.

The property value is an object that details model properties to be treated as readonly properties. Each key in the object must match a property name defined for the model.

A feature requests exists against the Loopback project for similar functionality in core: strongloop/loopback#531

INSTALL

npm install --save loopback-ds-readonly-mixin

MIXINSOURCES

With [email protected] mixinSources have been implemented in a way which allows for loading this mixin without changes to the server.js file previously required.

Add the mixins property to your server/model-config.json like the following:

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "../node_modules/loopback-ds-readonly-mixin/lib",
      "../common/mixins"
    ]
  }
}

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-readonly-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": {
        "type": "string",
      }
    },
    "mixins": {
      "ReadOnly" : true
    }
  }

Attempting to update a ReadOnly model will reult in a 403 error.

OPTIONS

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

In this example we mark the status and role fields as readonly.

  {
    "name": "Widget",
    "properties": {
      "name": {
        "type": "string",
      },
      "status": {
        "type": "string",
      },
      "role": {
        "type": "string",
      }
    },
    "mixins": {
      "ReadOnly" : {
        "status" : true,
        "role" : true
      }
    }
  }

Any data set by a REST client in ReadOnly properties will be stripped out on the way to the server and will not be saved on the updated model instance.

TESTING

Run the tests in test.js

  npm test

Run with debugging output on:

  DEBUG='loopback:mixin:readonly' npm test

loopback-ds-readonly-mixin's People

Contributors

cloudever avatar greenkeeper[bot] avatar mrbatista avatar

Stargazers

 avatar

Watchers

 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.