Skip to content

Commit

Permalink
Fixed tenantPlugin to let plugin get all fields of baseSchema.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecuesta committed May 26, 2017
1 parent 2bd8dfc commit b64135b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-multitenancy",
"version": "0.1.17",
"version": "0.1.18",
"description": "The best of both worlds Mongoose.discriminator & mongoose-multitenant",
"main": "src/index.js",
"scripts": {
Expand Down
22 changes: 21 additions & 1 deletion src/discriminator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* Created by jorgecuesta on 6/6/16.
*/
let mongoose = require('mongoose'),
utils = require('mongoose/lib/utils');
utils = require('mongoose/lib/utils'),
_ = require('lodash');

mongoose.Model.discriminator = function discriminator(name, schema) {
// Using this because if you want register tenant plugins we need know
Expand Down Expand Up @@ -90,6 +91,25 @@ mongoose.Model.discriminator = function discriminator(name, schema) {
// merges base schema into new discriminator schema and sets new type field.
merge(schema, baseSchema);

if (this.schema.options.tenantPlugins &&
_.isArray(this.schema.options.tenantPlugins)) {
this.schema.options.tenantPlugins.forEach(function(plugin) {
if (!_.isFunction(plugin.register)) {
throw new Error([
'tenantPlugins should be an array with object. Those objects',
' should have register key with plugin function and optionally',
' option key with options to send plugin.',
].join());
}

schema.plugin(plugin.register, plugin.options);
});
} else if (this.schema.options.tenantPlugins) {
throw new Error([
'tenantPlugins should be an array.',
].join());
}

if (!this.discriminators) {
this.discriminators = {};
}
Expand Down
21 changes: 1 addition & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = {
this.connection = options.connection;
} else this.connection = mongoose;

console.log('model delimiter', this.modelDelimiter);
debug('model delimiter %o', this.modelDelimiter);

if (this.connection.version !== mongoose.version) {
debug(
Expand Down Expand Up @@ -185,25 +185,6 @@ module.exports = {
newSchema.$tenantId = tenantId;
newSchema.plugin(multitenantSchemaPlugin);

if (model.schema.options.tenantPlugins &&
_.isArray(model.schema.options.tenantPlugins)) {
model.schema.options.tenantPlugins.forEach(function(plugin) {
if (!_.isFunction(plugin.register)) {
throw new Error([
'tenantPlugins should be an array with object. Those objects',
' should have register key with plugin function and optionally',
' option key with options to send plugin.',
].join());
}

newSchema.plugin(plugin.register, plugin.options);
});
} else if (model.schema.options.tenantPlugins) {
throw new Error([
'tenantPlugins should be an array.',
].join());
}

newModel = model.discriminator(tenantModelName, newSchema);

// Prevent duplicated hooks.
Expand Down

0 comments on commit b64135b

Please sign in to comment.