npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-plugin-multitenancy

v0.0.4

Published

Mongoose plugin to help with isolating documents of multiple tenants

Downloads

134

Readme

Build status

Mongoose Multitenancy plugin

Simple plugin for Mongoose which adds an account attribute on your model and overrides query methods to scope queries to the specified account.

Disclaimer

As opposed to some other mongoose plugins attempting to provide multitenancy, this plugin doesn't do any fancy stuff with collections or databases.

Instead, this plugin provides syntactic sugar for model methods like find, findOneAndUpdate, count, update, etc...

How it works

This plugin adds a account property to the schema of the model on which you want to enable multitenancy.

Then, it provides syntactic sugar on top of the most used query methods to prevent forgetting about the { account: ObjectId("507f191e810c19729de860ea") } where clause.

Installation

npm install mongoose-plugin-multitenancy

Usage

Defining the tenant

If you want to provide multitenancy to one of your Models, you first need to define the mongoose model that will act as your tenant. For instance Account or User.

There is nothing special about this.

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const accountSchema = new Schema({
    name: String
});
const Account = mongoose.model('Account', accountSchema);

Enabling multitenancy on one of your models

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const multitenancy = require('mongoose-plugin-multitenancy');

const schema = new Schema({
    color: String,
    brand: String
});
schema.plugin(multitenancy);

const Car = mongoose.model('Car', schema);

By default, the plugin expects a model named Account to be defined first. And it adds an account property on your model.

These defaults can be overriden by passing options like:

schema.plugin(multitenancy, {
    path: 'owner',
    ref: 'User'
});

Queries

This plugin overrides the following methods to optionnally scope the query to the correct account:

  • find
  • findOne
  • findOneAndUpdate,
  • count,
  • update,
  • remove,
  • findOneAndRemove
// Insert multiple accounts
const account1 = new Account({ name: 'Account 1' });
const account2 = new Account({ name: 'Account 2' });

// Insert a few cars on both accounts
const car11 = new Car({ account: account1, color: 'red', brand: 'Renault' });
const car12 = new Car({ account: account1, color: 'blue', brand: 'Renault' });
const car13 = new Car({ account: account1, color: 'blue', brand: 'Peugeot' });

const car21 = new Car({ account: account2, color: 'black', brand: 'Peugeot' });
const car22 = new Car({ account: account2, color: 'red', brand: 'Mercedes' });

// ... Save the documents...

// Query methods work as before when no account is specified
Car.find({ color: 'red' }, function(err, cars) {
    // Here we get the 2 red cars, across all accounts
    // ...
});

// Query methods scope the query to the correct account when an account or an _id (ObjectId or string) is passed as first argument
Car.find(account1, { color: 'red' }, function(err, cars) {
    // Here we only get the cars of the first account, here only one
    // ...
});

// You can also only provide the account _id
Car.count(account1._id, { color: 'red' }, function(err, count) {
    // Here count = 1
    // ...
});

License

Copyright (c) 2017 Matthieu Balmes [email protected]

Licensed under the MIT license.