Divides the DB in groups for meteor apps.
This package is under development, it is not recommended to use in production
This package doesn't use the standard multi-tenancy approach as others DBs do, it will only divide your data in differents groups
After creating a user, you have to provide a group id for that user. It can be a companyId, teamId or any unique indentifier that you want.
import { Tenancy } from 'meteor/cinn:multitenancy';
const companyId = Companies.insert({ name: 'New Company Name' });
const newUserId = Accounts.createUser({ email: '[email protected]', profile: { name: 'John Doe' } });
Tenancy.setUserTenant(newUserId, companyId);
After creating a collection, just call the Tenancy.prepareCollection
, and the collection will be ready.
import { Tenancy } from 'meteor/cinn:multitenancy';
const Posts = new Mongo.Collection('posts');
Tenancy.prepareCollection(Posts);
Every time that you insert a collection to the DB, this collection will be automatically in the current group.
Every time that you search a collection data, the result will only be from the current group of the current user.
// Only returns the posts on the same current user's group, every query will be intercepted
Posts.find({});
import { Tenancy } from 'meteor/cinn:multitenancy';
const groupId = Tenancy.currentGroupId(Meteor.userId());
It's pretty simples, it sets a _groupId
field on every collection that is using the Tenancy.prepareCollection
.
// Basic setup
import { Tenancy } from 'meteor/cinn:multitenancy';
const Posts = new Mongo.Collection('posts');
Tenancy.prepareCollection(Posts);
// With a user setup and logged in
const postId = Posts.insert({ name: 'Name Test' });
const post = Posts.findOne({ _id: postId });
post._groupId === Tenancy.currentGroupId(Meteor.userId()); // Same group as the logged user
Every query that you run will be intercepted by the tenancy
// With a user setup and logged in
const postId = Posts.insert({ name: 'Name Test' });
const post = Posts.find({});
// Only returns the posts in the same group as the current user's group
// Its doing the same thins as this:
Posts.find({ _groupId: Tenancy.currentGroupId(Meteor.userId()) });
By default the group field name is _groupId
but you can set your own custom name:
import { Tenancy } from 'meteor/cinn:multitenancy';
Tenancy.setGroupName('companyId');