【问题标题】:Loopback4 Model Definition options for mongodb collection namemongodb 集合名称的 Loopback4 模型定义选项
【发布时间】:2018-12-11 00:24:35
【问题描述】:

我正在使用 loopback 4 并尝试使用属性配置 Model 注释以配置如何在 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

【问题讨论】:

    标签: loopbackjs loopback v4l2loopback


    【解决方案1】:

    This 来自 2014 年,但也许它仍然有效。尽量不要放mongodboptions

      settings: {strict: false},
      name: 'client',
      plural: 'clients',
      mongodb: {
        collection: 'clients',
      },
    

    【讨论】:

    • 试过了,我想他们可能还没有实现它。当我有机会时,我会检查回购。感谢您的回复!
    【解决方案2】:

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

    据我所知,LB4 不使用选项plural

    我认为下面的代码应该适合你:

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

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

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 2019-08-23
      • 2019-06-06
      • 2014-09-02
      相关资源
      最近更新 更多