1.0.2 • Published 5 years ago

loopback-author-mixin v1.0.2

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

loopback-author-mixin

Mixin that allows to set content author based on the current logged in user.

Installation

npm install loopback-author-mixin --save

Config

Edit server/model-config.js file.

...
"_meta": {
  "sources": [
    ...
  ],
  "mixins": [
    "loopback/common/mixins",
    "loopback/server/mixins",
    "../node_modules/loopback-author-mixin"     // add this line
  ]
}
...

Then edit your model.json file (eg. common/models/article.json);

{
  "name": "Article",
  "base": "PersistedModel",
  "idInjection": true,
  "mixins": {
    "Author": true              // add this line; default field
  },
  "properties": { ... },
  "relations": {
    "author": {                 // add relation if necessary
      "type": "belongsTo",
      "model": "User",
      "foreignKey": ""
    }  
}

By default authorId will be populated with current user ID.

If you want to use different field name, you need to pass options to the mixin:

  "mixins": {
    "Author": {
      "authorField": "userId"
    }
  },

Now every time that you create new instance of the Article model it will have author field populated.