0.0.1 • Published 7 years ago

loopback-shortid-mixin v0.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

loopback-shortId-mixin

A mixin to set id to shortid for a loopback Model.

Build Status Coverage Status

INSTALL

npm i --save loopback-shortid-mixin

There are 2 ways to enable mixin:

1) server.js

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

...
var app = module.exports = loopback();
...
// Add shortid Mixin to loopback
require('loopback-shortid-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();
});

2) mixin sources

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",
      "loopback/server/mixins",
      "../node_modules/loopback-shortid-mixin",
      "../common/mixins",
      "./mixins"
    ]
  }
}

config

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

{
  "name": "Widget",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "string",
      "id": true
    },
    "name": {
      "type": "string",
      "required": false
    },
    "type": {
      "type": "string",
      "required": false
    }
  },
  "mixins": {
    "ShortId" : true
  }
}

example

const loopback = require('loopback');

const app = loopback();
const Widget = app.models.Widget;
Widget.create({name: 'foo', type: 'bar'});

will return

{
  "name": "foo",
  "type": "bar",
  "id": "rJ9ZE74AW"
}

LICENSE

@MIT