用于mongodb集合名称的Loopback4模型定义选项

问题描述 投票:1回答:2

我正在使用loopback 4并尝试使用属性配置Model注释来配置如何在Mongo中创建集合。

我有一个名为say Client的模型,我希望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,仍会将集合创建为类名称Client

loopbackjs loopback v4l2loopback
2个回答
1
投票

This是从2014年开始的,但也许它仍然有效。尽量不要把mongodboptions

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

0
投票

请注意,所有模型设置必须嵌套在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

© www.soinside.com 2019 - 2024. All rights reserved.