Giter Site home page Giter Site logo

meteor-multitenancy's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.