Skip to content

sabativi/meteor-multitenancy

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Meteor Multitenancy

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

How to use it?

Setting up the user

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);

Setting up the collection

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({});

Getting user's group

import { Tenancy } from 'meteor/cinn:multitenancy';

const groupId = Tenancy.currentGroupId(Meteor.userId());

How it works?

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()) });

Configurations

Changing the group field name

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');

About

Multi tenancy for meteor apps

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%