简体   繁体   English

如何将属性定义为 map(字典)

[英]How to define a property as a map (dictionary)

I have a loopback model.我有一个环回 model。 I want a certain property to have a map type.我希望某个属性具有 map 类型。 I tried something like this:我试过这样的事情:

  @property({
    postgresql: {
      dataType: 'json',
    }
  })
  nodes: Map<string, string>;

or something like this:或类似的东西:

  @property({
    postgresql: {
      dataType: 'json',
    }
  })
  nodes: {[key: string]: string};

It works, but there is no type check and no schema shows up in Swagger api explorer.它可以工作,但没有类型检查,并且 Swagger api 资源管理器中没有显示模式。 nodes property can have any object or map there, and it's not limited to <string, string> nodes属性可以有任何 object 或 map 那里,它不限于<string, string>

Is there a way for this to work?有没有办法让它工作?

This should do it.这应该这样做。

import {getJsonSchema} from '@loopback/repository-json-schema'

@model()
export class MapEntry {
  @property()
  id: string;

}

@model( {
  jsonSchema: {
    additionalProperties: getJsonSchema(MapEntry)
  }
})
export class MyMap {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM