Wrapper for Mongoose that allows for easy horizontal multitenancy (collection prefix per tenant)
This package facilitates horizontal multitenancy in one MongoDB database, obviously using Mongoose as the interaction layer.
With this package, you can use one schema per model, as you would normally with Mongoose, and then use special methods and syntax apply that schema to different tenant collections.
Instead of using
mongoose.model(name, schema) to compile your model, you would now use
mongoose.mtModel(name, schema). This still creates the Mongoose model as normal, but adds some additional functionality. Specifically, you can retrieve a model for a specific tenant using this syntax:
When that happens, the package will check if that model for that tenant has already been compiled. If not, it creates a copy of the base model's schema, updates any
refs to other collections, and then compiles a new model with the new schema, with a collection name of
tenantId__originalCollectionName. All per-tenant models are lazy-loaded, meaning that they won't take up memory until they are needed.
var mongoose = require'mongoose';require'mongoose-multitenant';mongooseconnect'mongodb://localhost/multitenant';
Thanks to @watnotte for this - if you want to change the delimiter from the default
__ you can do the following:
With mongoose-multitenant you use all the same syntax for schema creation as you normally do with Mongoose, with the addition of the
$tenant property on document references. This tells the system whether it is a reference to a document for the same tenant (
true) or a root-level document without a tenant (
var barSchema =title:String_foos:type:mongooseSchemaTypesObjectIdref:'Foo'$tenant:true;var fooSchema =title:Stringdate:Date;
Instead of using
mongoose.model to compile, you use
mongoosemtModel'Bar' barSchema;mongoosemtModel'Foo' fooSchema;
// This returns a new Foo model for tenant "tenant1"var fooConstructor = mongoosemtModel'tenant1.Foo';var myFoo =title:'My Foo'date:;myFoosave// This saved it to the collection named "tenant1__foos";
And make use of refs/populate:
var barConstructor = mongoosemtModel'tenant1.Bar';var myBar =title:'My Bar'_foos:myFoo_id;myBarsave// Saved to the collection named "tenant1__bars"barConstructorfindpopulate'foos'execconsole.logresults0_foos0title; // "My Foo";;
But you can't populate across tenancies:
var tenant2Bar = mongoosemtModel'tenant2.Bar';var newBar =title:'New Bar'_foos:myFoo_id;newBarsavetenant2Barfindpopulate'foos'execconsole.logresults0_foos0; // "undefined";;
In addition to this base functionality, each per-tenant model gets two new schema methods:
This does what you think it does - returns the tenant ID for the model.
This can be used in your mongoose middleware methods to get the related model class. E.g.
barSchemapre'save'// This gets Foos in the same tenancy as this BarthisgetModel'Foo'find_id:$in:this_foos// Do something to the related Foos.next;;
- Brian Kirchoff for his great mongoose-schema-extend package