您如何访问环回模型 属性 类型? (model.definition.properties.type)
How do you access loopback model property types? (model.definition.properties.type)
如何从模型扩展文件 (model.js) 中访问模型 属性 类型?
如果我尝试访问 MODEL.definition.properties,我可以看到给定 属性 的以下内容:
{ type: [Function: String],
[1] required: false,
[1] description: 'deal stage' }
为什么类型被列为 [Function: String]
而不是 "String" 或类似的东西?
如果我运行typeof(property.type)
它returns"function",但是如果我运行property.type()
它returns一个空的字符串.
运行 typeof
关于函数 returns 的类型。
var type = typeof(property.type())
var type
将是字符串、数字等
不确定为什么环回不return类型本身,而不是函数。
免责声明:我是 LoopBack 的合著者和当前维护者。
TL;DR
使用以下表达式获取属性类型作为字符串名称。请注意,它仅适用于可能的 属性 定义的子集(见下文),最值得注意的是它不支持数组。
const type = typeof property.type === 'string'
? property.type
: property.type.modelName || property.type.name;
长版
LoopBack 允许以多种方式定义 属性 类型:
- 作为字符串名称,例如
{type: 'string', description: 'deal stage'}
。您也可以使用模型名称作为类型,例如{type: 'Customer'}
.
- 作为类型构造函数,例如
{type: String, description: 'deal stage'}
。您也可以使用模型构造函数作为类型,例如{type: Customer}
.
- 作为匿名模型的定义,例如
{type: {street: String, city: String, country: String}
- 作为数组类型。可以使用上述三种方式中的任何一种指定数组项的类型(作为字符串名称、类型构造函数或匿名模型定义)。
在我们的文档中阅读更多内容:LoopBack types
为了更好地理解如何处理不同类型的 属性 定义,您可以查看 loopback-swagger 中的代码,该代码正在将 LoopBack 模型模式转换为 Swagger 模式(类似于 JSON架构):
函数 getLdlTypeName
在输入上采用 属性 定义(由 buildFromLoopBackType
稍微规范化)和 returns 属性 类型作为字符串名称.
exports.getLdlTypeName = function(ldlType) {
// Value "array" is a shortcut for `['any']`
if (ldlType === 'array') {
return ['any'];
}
if (typeof ldlType === 'string') {
var arrayMatch = ldlType.match(/^\[(.*)\]$/);
return arrayMatch ? [arrayMatch[1]] : ldlType;
}
if (typeof ldlType === 'function') {
return ldlType.modelName || ldlType.name;
}
if (Array.isArray(ldlType)) {
return ldlType;
}
if (typeof ldlType === 'object') {
// Anonymous objects, they are allowed e.g. in accepts/returns definitions
// TODO(bajtos) Build a named schema for this anonymous object
return 'object';
}
if (ldlType === undefined) {
return 'any';
}
var msg = g.f('Warning: unknown LDL type %j, using "{{any}}" instead', ldlType);
console.error(msg);
return 'any';
};
如何从模型扩展文件 (model.js) 中访问模型 属性 类型?
如果我尝试访问 MODEL.definition.properties,我可以看到给定 属性 的以下内容:
{ type: [Function: String],
[1] required: false,
[1] description: 'deal stage' }
为什么类型被列为 [Function: String]
而不是 "String" 或类似的东西?
如果我运行typeof(property.type)
它returns"function",但是如果我运行property.type()
它returns一个空的字符串.
运行 typeof
关于函数 returns 的类型。
var type = typeof(property.type())
var type
将是字符串、数字等
不确定为什么环回不return类型本身,而不是函数。
免责声明:我是 LoopBack 的合著者和当前维护者。
TL;DR
使用以下表达式获取属性类型作为字符串名称。请注意,它仅适用于可能的 属性 定义的子集(见下文),最值得注意的是它不支持数组。
const type = typeof property.type === 'string'
? property.type
: property.type.modelName || property.type.name;
长版
LoopBack 允许以多种方式定义 属性 类型:
- 作为字符串名称,例如
{type: 'string', description: 'deal stage'}
。您也可以使用模型名称作为类型,例如{type: 'Customer'}
. - 作为类型构造函数,例如
{type: String, description: 'deal stage'}
。您也可以使用模型构造函数作为类型,例如{type: Customer}
. - 作为匿名模型的定义,例如
{type: {street: String, city: String, country: String}
- 作为数组类型。可以使用上述三种方式中的任何一种指定数组项的类型(作为字符串名称、类型构造函数或匿名模型定义)。
在我们的文档中阅读更多内容:LoopBack types
为了更好地理解如何处理不同类型的 属性 定义,您可以查看 loopback-swagger 中的代码,该代码正在将 LoopBack 模型模式转换为 Swagger 模式(类似于 JSON架构):
函数 getLdlTypeName
在输入上采用 属性 定义(由 buildFromLoopBackType
稍微规范化)和 returns 属性 类型作为字符串名称.
exports.getLdlTypeName = function(ldlType) {
// Value "array" is a shortcut for `['any']`
if (ldlType === 'array') {
return ['any'];
}
if (typeof ldlType === 'string') {
var arrayMatch = ldlType.match(/^\[(.*)\]$/);
return arrayMatch ? [arrayMatch[1]] : ldlType;
}
if (typeof ldlType === 'function') {
return ldlType.modelName || ldlType.name;
}
if (Array.isArray(ldlType)) {
return ldlType;
}
if (typeof ldlType === 'object') {
// Anonymous objects, they are allowed e.g. in accepts/returns definitions
// TODO(bajtos) Build a named schema for this anonymous object
return 'object';
}
if (ldlType === undefined) {
return 'any';
}
var msg = g.f('Warning: unknown LDL type %j, using "{{any}}" instead', ldlType);
console.error(msg);
return 'any';
};