Chris
Chris

Reputation: 14260

meteor: use different database for each user

I currently assign a mongodb to my meteor app using the env variable "MONGO_URL": "mongodb://localhost:27017/dbName" when I start the meteor instance.

So all data gets written to the mongo database with the name "dbName". I am looking for a way to individually set the dbName for each custumer upon login in order to seperate their data into different databases.

Upvotes: 2

Views: 846

Answers (2)

Michel Floyd
Michel Floyd

Reputation: 20256

Here's another approach that will make your life eternally easier:

  1. Create a generic site with no accounts at mysite.com
  2. When they login at mysite.com, figure out what site they actually belong to and redirect them to customerName.mysite.com and log them in there
  3. Run a separate instance of Meteor configured for a different mongo at each site

nginx might help you with the above.

It is generally good practice to run separate DBs when offering a B2B solution.

That's a matter of opinion that depends heavily on the platform. Many SaaS providers would argue that point.

Upvotes: 0

Brett McLain
Brett McLain

Reputation: 2010

This generally unsupported as this is defined at startup. However, this thread offers a possible solution:

https://forums.meteor.com/t/switch-database-while-meteor-is-running/4361/6

var database = new MongoInternals.RemoteCollectionDriver("<mongo url>"); 

MyCollection = new Mongo.Collection("collection_name", { _driver: database });

This would allow you to define the database name in the mongo url but would require a fair bit of extra work to redefine your collections on a customer by customer basis.

Upvotes: 1

Related Questions