Where and how to store functions in Meteor? Or how to use system.js?

I need a way to store many and many different functions, each with a unique ID and possibly some other properties that I can query and return to the client or server for use.

The only way to save the features is by collecting MongoDB system.js

, but I can't access it through Meteor.

When I try to implement the solution here, I can apparently save the function, but I don't know how to get it back and run:

How do I access mongos db.system.js in meteor?

// on /server

var myDB = MongoInternals.defaultRemoteCollectionDriver().mongo.db;

systemJS = myDB.collection('system.js');

systemJS.save({
    _id: "echoFunction",
    value : function(x) { return x; }
  },
  function (err, inserted) {
    console.log("error: ", err);
    console.log("number of docs inserted: ", inserted);
  }
);

systemJS.save({
    _id : "myAddFunction" ,
    value : function (x, y){ return x + y; }
  },
  function (err, inserted) {
    console.log("error: ", err);
    console.log("number of docs inserted: ", inserted);
  }
);

// always causes the server to crash
// systemJS.findOne()

// returns undefined for db
// db.LoadServerScripts()

      

+3


source to share


1 answer


Well, I found a very ugly way to make it work:

I put this in /server/fixtures.js

myDB = MongoInternals.defaultRemoteCollectionDriver().mongo.db;

systemJS = myDB.collection('system.js');

systemJS.save({
    _id: 'echoFunction',
    value : 'function(x) { return x; }'
  },
  function (err, inserted) {
    console.log("error: ", err);
    console.log("number of docs inserted: ", inserted);
  }
);

systemJS.save({
    _id : "myAddFunction" ,
    value : 'function (x, y){ return x + y; }'
  },
  function (err, inserted) {
    console.log("error: ", err);
    console.log("number of docs inserted: ", inserted);
  }
);

var arg = "10, 5";

var mongoFuncName='myAddFunction';

var string = 'db.loadServerScripts(); return ' + mongoFuncName+ '(' + arg + ');';

console.log(string);

myDB.eval(string, function (err, mongoFuncReturnData) {
  console.log("error: ", err);
  console.log("mongoFuncReturnData: ", mongoFuncReturnData);
});

// counting the number of functions in system.js
myDB.eval("db.system.js.count();", function (err, mongoFuncReturnData) {
  console.log("error: ", err);
  console.log("mongoFuncReturnData: ", mongoFuncReturnData);
});

// finding and retrieving a stored function via the _id
myDB.eval("db.system.js.findOne({_id: 'myAddFunction'});", function (err, mongoFuncReturnData) {
  console.log("error: ", err);
  console.log("mongoFuncReturnData: ", mongoFuncReturnData);
});

      



Holy hell is ugly. I seriously hope there is a better way to do this.

I am also concerned about the fact that if I implement this obvious hack when an update pops up, it could seriously disrupt my system when I update.

Also note that function definitions must still be string. If they are not a string, they will not be saved (only _id is saved).

+2


source







All Articles