//
// child base model
function abstractSchema() {
Schema.apply(this, arguments);
this.add({
name: String
});
};
util.inherits(abstractSchema, Schema);
var mySchema = new abstractSchema();
//
// Inherited Types
var textPropertySchema = new abstractSchema({
length: Number
});
var numberPropertySchema = new abstractSchema({
dp: Number
});
//
// Parent Model
var myModelSchema = mongoose.Schema({
name: String,
properties : [mySchema]
});
When i save each an instance of numberPropertySchema or textPropertySchema, the _t (type is written) and is able to deserialise properly.
When however added as a sub doc array, they're all persisted with the base object properties only.
Is there any way round this? any extensions that could be used?
Coming from C#, the MongoDB driver lets you override (provide alternative) the serialisation/deserialisation of objects to and from the database. Is this something that can easily be achieved using mongoose?
Thanks
Sam
{
name : 'parent',
properties: [
{ name:'sub type 1', __t : 'SubTypeOne', propertyOnlyInSubTypeOne: '1234' },
{ name: 'sub type 2', __t:'SubTypeTwo' , propertyOnlyInSubTypeTwo: 'abc'
]
}
{
name: "Sam",
job: "Carpenter",
tools : [
{ __t : "Hammer", length: 30, desc: "Normal hammer", weightKg: 0.5 },
{ __t: "Screwdriver", type: "Flat", desc: "Flat headed screwdriver", weightKg: 0.15}
]
}
//
// child base model
function baseToolSchema() {
Schema.apply(this, arguments);
this.add({
weightKg: Number,
desc: String
}
}
util.inherits(baseToolSchema, Schema);
var myBaseToolSchema = new baseToolSchema();
//
// Tool (inherited) Types
var screwdriverType= new baseToolSchema({
type: String
});
var hammerType= new baseToolSchema({
length: Number
});
//
// Worker Model
var workerSchema = mongoose.Schema({
name: String,
job: String,
tools: [myBaseToolSchema]
});
tools : [ hammerType, screwdriverType]
tools : [baseToolSchema,baseToolSchema]