javascript - Storing objects with functions and other objects in mongoDB -
hi i'm having trouble deciding on correct way store objects in monogodb database, node.js.
a strip down example of classes shown below, in reality more complex:
function object1{ var variables; var object2: var object3: functions ... } function object2{ var variables; var object4; var object5; ... functions ... } ... etc
a typical function call in object1 result in several calls other objects, , objects belonging them objects.
if store object1 in mongodb, cannot store functions. research have 2 options:
change objects prototype when object loaded, per this answer
question: required, change prototypes every subsequent object, considering large amount of subsequent objects, not seem right thing do, there easy way this?
use mongoose schemas define objects allowing me use virtuals store functions in this example.
which result in following:
var objectschema = new schema({ variables: type object2: schema.objectid }); virtual functions ...
question: in case, each time wanted access variables, or call virtual functions in other objects objectid in schema, have call find() on database. potentially result in large amount of database calls compared having 1 database call loads object , passing information round subsequent functions, makes option seem invalid?
question: there better way these 2 methods?
i've managed solve problem (i think) removing function definitions objects , passing instance of object function.
object1.js
module.exports = { //create object object1: function(params, object2, object3){ this.variables = params; this.object2 = object2: this.object3 = object3: }, //object function myfunction: function(object1, params){ stuff object1. } }
in code use like:
app.js
var object1 = require('./object1.js'); var myobject = new object1.object1(); object1.myfunction(myobject, params);
instead of:
app.js
var object1 = require('./object1.js'); var myobject = new object1.object1(); object1.object1function(params);
instances of object1 can stored in database, , functions can accessed throughout application requiring module.
Comments
Post a Comment