如何使环回 4 中的模型在其模式中具有唯一值?

How to make a model in loopback 4 to have unique values in its schema?

我正在尝试找到一种方法来实现与我放置时相同的功能 unique:true 在表达模式中。我如何在环回 4 中完成此操作。我尝试将 unique true 放入 属性 装饰器中,但它不起作用。

@property({
    type: 'string',
    id: true,
    required: false,
    unique: true,
  })
  id: string;

这行不通

LB4 中的

@property 装饰器借用了 LB3 中相同的属性。假设我已经理解您的要求,您可以使用 index 属性 来强制整个集合中字段的唯一性。 对于像 'id' 这样的字段,属性 装饰器将采用以下参数:

@property({
    type: 'string',
    id: true,
    required: false,
    index: {
        unique: true
    }
  })
  id: string;

此外,如果您正在使用由 MongoDB 生成的 'id',您不需要明确强制唯一性,但以上内容应该适用于其他字段,如电子邮件、用户名等。