node.js - Mongoose Plugin - schema.add to Subschema -
i writing mongoose plugin involves adding elements subschema. thinking quite straightforward, i've been trying unsuccessfully few days now.
below simplification of schema:
var inheritables = require('./inheritables.server.module.js'); var oufieldtypeschema = new schema({ name: string, desc: string }); var oufieldtype = mongoose.model('oufieldtype', oufieldtypeschema); var ouschema = new schema({ name: string, fieldtypes: [oufieldtypeschema], }); ouschema.plugin(inheritables, {inherit: ['fieldtypes']}); mongoose.model('ou', ouschema);
within body of mongoose plugin, i'd iterate on "inherit" array , add following schemas represent each of these elements in ou schema;
a simplification of plugin follows:
var _ = require('lodash'); module.exports = exports = function(schema, options) { _.each(options.inherit, function(curritemschema){ var addtoschema = {inheritedfrom: {name: string}}; //really want add oufieldtypeschema represented fieldtypes element array. schema.add(addtoschema, curritemschema + "."); }); });
note second parameter of add() adds prefix, doesn't work.
i've tried:
_.each(options.inherit, function(curritemschema){ var addtoschema = {}; addtoschema[curritemschema] = {inheritedfrom: {name: string}}; schema.add(addtoschema); });
the expected behaviour here elsewhere in plugin, have method sets inheritedfrom schema. no error received when item set, resulting output never sees inheritedfrom elements.
i adding inheritedfrom each subschema used oufieldtype, above simplification , not dry.
i'm new node.js - pointers in right direction appreciated.
Comments
Post a Comment