I read from mongoose documentation that it is possible to create custom schema types to be added to the ones already existing.
As suggested I tried to look into the mongoose-long example: https://github.com/aheckmann/mongoose-long
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
}
}
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.
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.
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.
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?
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