0

Hi I'm having trouble deciding on the correct way to store my objects in the monogoDB database, from node.js.

A Strip down example of my classes are shown below, in reality it is much 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 would result in several calls to other objects, and objects belonging to them objects.

If I store object1 in mongoDB, I cannot store the functions. From my research I have two options:

  1. Change objects prototype when the object is loaded, as per this answer

    Question: I would then be required, to change the prototypes for every subsequent object, but considering the large amount of subsequent objects, this does not seem like the right thing to do, is there an easy way to do this?

  2. Use mongoose schemas to define the objects allowing me to use virtuals to store the functions as in this example.

    Which would result in the following:

    var objectSchema = new Schema({
        variables: Type
        object2: Schema.ObjectId
    });
    
    virtual functions
    ...
    

    Question: In this case, each time I wanted to access variables, or call virtual functions in other objects whose ObjectId is in the schema, I would have to call find() on the database. This would potentially result in a large amount of database calls compared to having one database call that loads the object and passing the information round to the subsequent functions, which makes this option also seem invalid?

Question: Is there a better way than these two methods?

1 Answer 1

0

I've managed to solve my problem (i think) by removing the function definitions from the objects and then passing an instance of the object to the function.

object1.js

module.exports = {
    //Create an object
    Object1: function(params, object2, object3){
            this.variables = params;
            this.object2 = object2:
            this.object3 = object3:
    },
    //object function
    myFunction: function(object1, params){
            do stuff with object1.
    }
}

In my code I then use something 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 then be stored in the database, and the functions can be accessed throughout the application by requiring the module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.