代码之家  ›  专栏  ›  技术社区  ›  Hayk Safaryan

将已存在的模型传递到环回中的下一个

  •  0
  • Hayk Safaryan  · 技术社区  · 6 年前

    Project 模型

    {
      "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": {}
    }
    

    而且它 hasAndBelongsToMany 标签

    这里是 Tag 模型

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

    现在,当创建关系时,环回API给出了这个API端点。

    POST /Projects/{id}/tags
    

    这将在标记集合中创建一个新标记,并将其添加到项目中。 但是如何向项目中添加已经存在的标记呢?

    所以我想也许我会加上 before save 钩子到 标签 在这里,我将检查标记是否存在,然后传递关系的现有标记。

    像这样。

    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'});
    };
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   bipin    6 年前

    @Hayksafarian只是演示如何在项目中使用标签

    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()
     }    
     });
    };
    

    这只是一个示例代码,演示如何使用更新、创建、查找、upsertwithwhere等标记进行验证。您必须在此处设置条件,它不会接受您在标记模型中定义的验证。