mongoose-multitenant
mongoose-multitenant copied to clipboard
multiple connections and models with dots
Not entirely sure this is the direction the base library should go in, but I thought I'd at least share changes I had to make during some recent projects.
multiple connections
The base library did not support non-default connections (eg, connections created using mongoose.createConnection
). To correct that, the bulk of the functionality has been wrapped up in setup
that takes the connection and delimiter, both optional.
This introduces breaking changes, as require(mongoose-multitenant)('delimiter')
will no longer work. Instead, the usage would look more like:
var multitenant = require('mongoose-multitenant');
var mongoose = require('mongoose');
mongoose.connect('...');
multitenant.setup('CUSTOM_DELIMITER');
I've updated the readme where appropriate to reflect this change.
model names with dots
The @watnotte changes work great with tenants that have dots in their names. However, mongo (and consequently, mongoose) allows for dots in the collection and model names, respectively. This changeset changes the behavior such that collection/model names with dots are also okay, so long as you either 1) add the tenant explicitly using connection.mtModel.addTenant(tenantId)
or 2) if the model is added to the tenant explicitly, eg var fooclass = mongoose.mtModel('tenant1.something', 'model.name')
. The documentation has not been updated to reflect this, as I think it should Just Work (tm). However, I added tests to ensure both behaviors are supported, as well as the original default behavior of the last dot part being used as the model name.
gruntfile changes
As a consequence of being able to add multiple connections, the dropTestDb
grunt task had to be modified to drop all of the test databases the multitenant tests now create.
I think that's everything. Feedback welcome.
Looks great - I'll take a look over the next few days and probably make this the next major release.