确保回送模型中的唯一字段值 [英] Ensure unique field value in loopback model

查看:114
本文介绍了确保回送模型中的唯一字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确保环回模型中特定字段的唯一性. 就像下面的模型Post一样,我在其中有一个字段genericId,我希望它在数据库中是唯一的,并且在重复键插入时会通过错误回送.

How to ensure uniqueness of a particular field in loopback model. Like below is the model Post, I have a field genericId in it, I want it to be unique in the database, and loopback to through an error, on duplicate key insertion.

{
  "name": "Post",
  "plural": "Post",
  "base": "PersistedModel",
  "properties": {
    "genericId": {
      "type": "string",
      "required":True 
    },
    "moderatedAt": {
      "type": "date"
    }
  },
  "validations": [],
  "acls": [],
  "methods": []
}

我尝试搜索那里的文档和其他示例,但没有成功. 我能想到的一种解决方案是,为create函数创建一个remoteHook,并在插入之前验证此字段,但是正在寻找其他方法.

I have tried searching there documentation, and other examples but no success. One solution which I can think of is, to create a remoteHook for the create function, and validate this field before inserting, but looking for some other way.

推荐答案

设置验证中的common/models/post.js

Post.validatesUniquenessOf('genericId');

这篇关于确保回送模型中的唯一字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆