猫鼬自定义架构类型 [英] mongoose custom schema types

查看:129
本文介绍了猫鼬自定义架构类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从猫鼬的文档中了解到,可以创建自定义架构类型以添加​​到现有的架构类型中.

I read from mongoose documentation that it is possible to create custom schema types to be added to the ones already existing.

根据建议,我尝试研究猫鼬长的示例: https://github.com/aheckmann /mongoose-long

As suggested I tried to look into the mongoose-long example: https://github.com/aheckmann/mongoose-long

我正在开发的应用程序中需要此配置文件,在该应用程序中,我的配置文件是猫鼬模型.该配置文件具有几个字段,例如namesurname等,它们是MultiValueField. MultiValueField位于具有以下结构的对象上:

I need this in an application I am developing in which I have a profile that is a mongoose model. The profile has several fields such as name, surname and so on that are MultiValueField. A MultiValueField is on object with the following structure:

{
    current : <string>
    providers : <Array>
    values : {
         provider1 : value1,
         provider2 : value2
    }
}

上面的结构有一个允许的提供程序列表(按优先级排序),可以从中检索字段的值. current属性跟踪当前选择了哪个提供程序作为字段值.最后,对象值包含每个提供程序的值.

The structure above has a list of allowed providers (ordered by priorities) from which the field's value can be retrieved. The current property keeps track of which provider is currently picked to use as value of the field. Finally the object values contains the values for each provider.

我已经在node.js中定义了一个对象,该对象的构造函数将上面的结构用作参数,并提供了许多有用的方法来设置新的提供程序,添加值等.

I have defined an object in node.js, whose constructor takes the structure above as argument and provides a lot of useful methods to set a new providers, adding values and so on.

我的个人资料的每个字段都应使用不同的提供程序列表进行初始化,但是我还没有找到一种方法.

Each field of my profile should be initialized with a different list of providers, but I haven't found a way yet to do so.

如果将MultiValueField定义为Embedded Schema,则只能将每个字段都设置为Array.同样,在创建带有提供者列表的配置文件时,我无法初始化每个字段.

If I define MultiValueField as an Embedded Schema, I can do so only having each field as Array. Also I cannot initialize every field at the moment a profile is created with the list of providers.

我认为最好的解决方案是定义一个SchemaType MultiValueFieldType,它具有一个返回MultiValueField object的强制转换函数.但是,如何定义这样的自定义架构类型?在配置文件的架构中定义自定义选项时,该如何使用它?

I think the best solution is to define a SchemaType MultiValueFieldType, that has a cast function that returns a MultiValueField object. However, how can I define such a custom schema type? And how can I use custom options when defining it in the schema of the profile?

我已经在猫鼬Google网上论坛上发布了一个问题,询问如何创建自定义架构类型: https://groups.google.com/forum/?fromgroups#!topic/mongoose-orm/fCpxtKSXFKc

I already posted a question on mongoose Google group to ask how to create Custom Schema Types: https://groups.google.com/forum/?fromgroups#!topic/mongoose-orm/fCpxtKSXFKc

推荐答案

我也只能将自定义模式类型创建为数组.您将能够初始化模型,但是此方法有缺点:

I was able to create custom schema types only as arrays too. You will be able to initialize the model, but this aproach has drawbacks:

  • 即使您不需要数组,您也将拥有它们
  • 如果在其他模型上使用ParameterSchema,则需要复制它

自定义架构类型代码:

// Location has inputs and outputs, which are Parameters

// file at /models/Location.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var ParameterSchema = new Schema({
  name: String,
  type: String,
  value: String,
});

var LocationSchema = new Schema({
    inputs: [ParameterSchema],
    outputs: [ParameterSchema],
});

module.exports = mongoose.model('Location', LocationSchema);

要初始化模型:

// file at /routes/locations.js
var Location = require('../models/Location');

var location = { ... } // javascript object usual initialization

location = new Location(location);
location.save();

此代码运行时,将使用其参数保存初始化的位置.

When this code runs, it will save the initialized location with its parameters.

我无法理解您所提问题的所有主题,但希望此答案对您有所帮助.

I didn't understand all topics of your question(s), but I hope that this answer helps you.

这篇关于猫鼬自定义架构类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆