3

I need a way to store lots and lots of different functions, each with a unique ID and perhaps some other properties that I can then query for and bring back to the client or server for use.

The only way to save functions is with MongoDB's system.js collection, but I am unable to access it via Meteor.

When I try to implement the solution here I can seemingly save a function but I don't know how to bring it back and run it:

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()
Community
  • 1
  • 1
fuzzybabybunny
  • 5,146
  • 6
  • 32
  • 58

1 Answers1

2

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

I put this into /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 it ugly. I'm seriously hoping that there is a better way to do this.

I'm also concerned about the fact that if I implement this obvious hack, when an update rolls around it could seriously compromise my system on update.

Also take note that the function definitions still have to be a string. If they aren't a string, they won't be saved (only the _id gets saved).

fuzzybabybunny
  • 5,146
  • 6
  • 32
  • 58