Search code examples
expressmodelforeign-keysloopbackjs

Pass an already existing Model to next in Loopback


There is Project model

{
  "name": "Project",
  "plural": "Projects",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string"
    },
    "code": {
      "type": "string"
    },
    "startDate": {
      "type": "date",
      "required": true
    },
    "endDate": {
      "type": "date"
    },
    "value": {
      "type": "number"
    },
    "infoEN": {
      "type": "string"
    },
    "infoRU": {
      "type": "string"
    },
    "infoAM": {
      "type": "string"
    },
    "externalLinks": {
      "type": [
        "string"
      ]
    }
  },
  "validations": [],
  "relations": {
    "industry": {
      "type": "belongsTo",
      "model": "Industry",
      "foreignKey": "",
      "options": {
        "nestRemoting": true
      }
    },
    "service": {
      "type": "belongsTo",
      "model": "Service",
      "foreignKey": "",
      "options": {
        "nestRemoting": true
      }
    },
    "tags": {
      "type": "hasAndBelongsToMany",
      "model": "Tag",
      "foreignKey": "",
      "options": {
        "nestRemoting": true
      }
    }
  },
  "acls": [],
  "methods": {}
}

And it hasAndBelongsToMany tags

here is Tag model

{
  "name": "Tag",
  "plural": "Tags",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

Now when the relation is created loopback api gives this api endpoint.

POST /Projects/{id}/tags

This creates a new tag into the tags collection and adds it to the project. But what about adding an already existing tag to the project?

So I figured maybe I add before save hook to the Tag Here I'll check if the tag exists and then pass the existing one for the relation.

Something like this.

tag.js

'use strict';

module.exports = function(Tag) {
  Tag.observe('before save', function(ctx, next) {
    console.log(ctx.instance);
    Tag.find({name: ctx.instance.name})
    next();
  });
  // Tag.validatesUniquenessOf('name', {message: 'name is not unique'});
};

Solution

  • @HaykSafaryan it just demo to show you how to use tag inside project

    var app = require('../../server/server');
    module.exports = function(project) {
     var tag=app.models.tags
     //afterremote it just demo. you can use any method
     project.afterRemote('create', function(ctx, next) { 
     tag.find({name: ctx.instance.name},function(err,result)){
     if(err) throw err; 
      next()
     }    
     });
    };
    

    this is just example code to show you how to use update,create ,find ,upsertwithwhere etc. tag for validation you have to setup condition over here it will not take validation which you defined in tags models