0

I am creating an app for employees using Meteor and MongoDB. This app will be used by multiple organizations. So I will make a separate DB for each organization. I am facing an issue in Meteor about how to keep database name and collection name as variable. Database name will be decided on login. Then I will keep DB name in Session.
Collection name can also be a variable.

For example:

var dbName = Session.get("dbName"); //for eg dbName="redex"
var collectionName = Session.get("collectionName"); // for ex collectionName="employees"
Employees = new Mongo.Collection(collectionName);

How to manage the variables in this case?

Kyll
  • 7,036
  • 7
  • 41
  • 64
Rohit Bansal
  • 1,039
  • 2
  • 12
  • 21

1 Answers1

0

You will have to create a server method that creates a given database according to dbName and collectionName parameters:

'newDatabase': function (dbName, collectionName) {
  var d = new MongoInternals.RemoteCollectionDriver(process.env.MONGO_URL.replace("originDB", dbName));
  Employees = new Mongo.Collection(collectionName, { _driver: d });
}

You will also have to declare that new collection on your client side:

Meteor.call('newDatabase', Session.get("dbName"), Session.get("collectionName"), function (err, res) {
  if (!err)
    Employees = new Mongo.Collection(Session.get("collectionName"));
});
Community
  • 1
  • 1
SylvainB
  • 4,765
  • 2
  • 26
  • 39
  • Thanks. Its working. But one issue. I can not use same collection name with different database. Exception while invoking method 'connectNewDatabase' Error: A method named '/employees/insert' is already defined at packages/ddp/livedata_server.js:1461:1 at Function._.each._.forEach (packages/underscore/underscore.js:113:1) – Rohit Bansal May 04 '15 at 13:28
  • According to [this answer](http://stackoverflow.com/a/25915025/1439597), there is not much you can do but work around the problem... – SylvainB May 04 '15 at 13:33
  • this means we need to have unique collection name for eg if database name is redix then collection name will be redix.employees and if database name is apl then use apl.employees – Rohit Bansal May 04 '15 at 13:45
  • That is one option, yes. Although make sure security against your users is not critical, otherwise I would not recommend putting those infos in `Session`. One redix user could easily switch their collection using `Session.set('dbName', 'apl')` and `Session.set('collectionName', 'apl.employees')`. Then again you can always verify users during publications. – SylvainB May 04 '15 at 13:55
  • Yes we can authenticate the user at server. also we will not keep the database name in client sessions, we will keep it at server while authorizing the user. But you said "That is one option." Is there any other option ? – Rohit Bansal May 04 '15 at 14:03
  • Oh I just meant "that is one way to concatenate", not implying I had other tricks up my sleeve... Although if you feel bold, you can have a look at [this solution](http://stackoverflow.com/a/25926060/1439597) using your concatenation on the first server collection declaration, and just `collectionName` in the two others (server and client)... but you're on your own from there. – SylvainB May 04 '15 at 14:09