1

我正在使用环回 4 并尝试使用属性配置模型注释以配置如何在 Mongo 中创建集合。

我有一个名为说客户端的模型,我希望将 Mongo 中的集合称为客户端。与文档的交叉令人困惑,因为它们在 v4 文档中引用了 v3 的属性。

我试过这个:

import {Entity, model, property} from '@loopback/repository';

@model({
  settings: {strict: false},
  name: 'client',
  plural: 'clients',
  options: {
    mongodb: {
      collection: 'clients',
    },
  },
})
export class Client extends Entity {
  @property({
    type: 'string',
    id: true,
    defaultFn: 'uuidv4',
    index: true,
  })
  id: string;

  @property({
    type: 'string',
    required: true,
  })
  name: string;

  @property({
    type: 'string',
  })
  code?: string;

  constructor(data?: Partial<Client>) {
    super(data);
  }
}

没有 Joy,仍然将集合创建为 Class name Client

4

2 回答 2

1

是从 2014 年开始的,但也许它仍然有效。尽量不要放mongodb钥匙options

  settings: {strict: false},
  name: 'client',
  plural: 'clients',
  mongodb: {
    collection: 'clients',
  },
于 2018-11-15T18:40:03.937 回答
0

请注意,所有模型设置必须嵌套在settings属性内,LB4 尚不支持顶级设置。

plural据我所知,LB4 也没有使用该选项。

我认为以下代码应该适合您:

@model({
  name: 'client',
  settings: {
    strict: false
    mongodb: {
      collection: 'clients',
    },
  },
})
export class Client extends Entity {
  // ...
}

更新:我打开了一个 GitHub 问题来讨论如何让@model来自 LB3 的用户更容易使用装饰器。见https://github.com/strongloop/loopback-next/issues/2142

于 2018-12-10T16:18:14.477 回答