Seperate Databases for Seperate Users


#1

Hello, so I am trying to create an app with an infrastructure somewhat like Shopify. Someone should be able to logon and create their own instance of a ‘store’. However I’m having some trouble with figuring out how to separate the MongoDB databases for each different store.

I am happy to store the contents of the store on a global database, however I want each store to have separate users as you shouldn’t be able to create an account on one store and then be able to login with that account on the other stores.

I have seen people changing the database using:

Meteor.users = new Mongo.Collection('users_new');

however that didn’t work for me. If anyone has any ideas or a better way to approach the issue it would be much appreciated. I have never done anything like this before and really do not know the best way to approach this, thanks for any help!


#2

How abount adding a new field into the user account collection or creating a new collection to connect user accounts to stores.

In this case, you have to extend the Accounts package to add the checking if the user is allowed on a particular store to login


#3

Hi thanks for the reply, I’ll give this a go in the next day or two and tell you what I came up with with your idea and see if it worked


#4

Using a package like alanning:roles allows this by separating the users and their roles into “groups”, just make sure the group name is unique and stored with the other information about each store


#5

Hi so I’m not sure that that would work. Basically, I want it so that you can signup on separate stores and be logged in on one store and not on the other. This is why I think I’ll need a different database for each store because I can see no easy way of handling that.

I looked into doing it this way, however the way I was doing it is a mess and in no way would it have worked.
Can you maybe elaborate a bit more because I’m not sure if I understand the way your thinking of doing it.

If a user registers themselves on one website then if they tried to signup on another website their password would have to be the same (I think) because it would always end up referencing the same user object no matter which way I did it. However, if the store had it’s own database of users that is completely separate from every other store then it would be fine.


#6

Actually that’s a fair point. All stores would share the same accounts in that case, just with permissions partitioning

I don’t think the default accounts system is well suited to this, but you can probably use it as a base to fork into your own multi-tennent accounts system


#7

Yeah this is a bit past my expertise when it comes to Meteor so I might just have to leave this project until another day when there’s an easier way to do it as I have no clue how I would change the accounts system.


#8

#9

Try the package mizzao:partitioner.

Then you would partition the user, product, category, and order (etc) collections based on the storeId. Then every product, customer, category, order, etc, that the user creates will automatically be partitioned and only be visible to users assigned to the same storeId.

Having users registered on multiple stores is a bit trickier. I guess you would have each store running on its own subdomain? In that case when a user logs in you would have to change their ‘groupId/storeId’ to match the store subdomain they logged in from.

The alanning:roles package could also be helpful for managing different roles within stores. For example, a user might be an admin of their own store but also just a customer of another store. I’m not sure how well alanning:roles and mizzao:partitioner play well together though - I use mizzao:partitioner but found that alanning:roles wasn’t flexible enough for me so I haven’t used it for a long time.