Skip to content

Commit

Permalink
Finish 1.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alrik committed Aug 2, 2019
2 parents 9e83f71 + 01c5d74 commit 48d9d59
Show file tree
Hide file tree
Showing 7 changed files with 895 additions and 519 deletions.
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License

Copyright (c) 2016-2018 craftup
Copyright (c) 2019 RealMQ GmbH
Copyright (c) 2016-2018 Alrik Zachert & Henning Panke

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,15 @@ class MongoTenant {
next();
});


this.schema.pre('countDocuments', function(next) {
if (this.model.hasTenantContext) {
this._conditions[tenantIdKey] = this.model[tenantIdGetter]();
}

next();
});

this.schema.pre('findOne', function(next) {
if (this.model.hasTenantContext) {
this._conditions[tenantIdKey] = this.model[tenantIdGetter]();
Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "mongo-tenant",
"version": "1.6.0",
"version": "1.7.0",
"description": "The mongo-tenant is a multi-tenancy plugin for mongoose.",
"author": "Craftup Coding Company <moin@craftup.cc>",
"author": "RealMQ GmbH <service@realmq.com>",
"contributors": [
"Alrik Zachert <alrik@craftup.cc>",
"Henning Panke <henning@craftup.cc>"
"Alrik Zachert <alrik@realmq.com>",
"Henning Panke <henning@realmq.com>"
],
"keywords": [
"multi-tenancy",
Expand Down Expand Up @@ -44,5 +44,9 @@
},
"peerDependencies": {
"mongoose": ">=4.3.0 <=4.8.0 || >=4.8.3"
},
"resolutions": {
"braces": "2.3.1",
"js-yaml": "3.13.1"
}
}
5 changes: 5 additions & 0 deletions release-notes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ description: >
multi-tenancy environment (on premis vs. cloud hosted) is a question of a single line of config.
releases:
- version: 1.7.0
date: 2019-08-02
fixed:
- Missing tenant context in countDocuments ([#45](https://github.com/craftup/node-mongo-tenant/issues/45)).
- Audit and update dependencies, fixing 52 vulnerabilities.
- version: 1.6.0
date: 2019-03-21
fixed:
Expand Down
20 changes: 17 additions & 3 deletions test/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function createTestModel(schemaDefinition, options) {
mongoTenant: void 0,
withPlugin: true
}, options);

let schema = new Schema(schemaDefinition, options.schemaOptions);

if (typeof options.applyOnSchema === 'function') {
Expand All @@ -37,21 +37,35 @@ function createTestModel(schemaDefinition, options) {
return mongoose.model(`mongoTenantTestModel${++testModelUnifier}`, schema);
}

function isMongoose4() {
return mongoose.version[0] === '4';
}

function clearDatabase() {
mochaMongoose(MONGO_URI);

beforeEach(function(done) {
if (mongoose.connection.db) return done();

if (mongoose.version[0] === '4') {
if (isMongoose4()) {
mongoose.connect(MONGO_URI, { useMongoClient: true }, done);
} else {
mongoose.connect(MONGO_URI, done);
}
});
}

function skipIf(condition, title, test) {
if (condition) {
return xit(title, test);
}

return it(title, test);
}

module.exports = {
clearDatabase: clearDatabase,
createTestModel: createTestModel
createTestModel: createTestModel,
isMongoose4: isMongoose4,
skipIf: skipIf,
};
65 changes: 60 additions & 5 deletions test/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('MongoTenant', function() {
utils.clearDatabase();

it('should add tenant on all discriminators of a model', function (done) {
let
let
TestModel = utils.createTestModel({ kind: String }, {
schemaOptions : { discriminatorKey: 'kind' }
});
Expand All @@ -33,14 +33,14 @@ describe('MongoTenant', function() {
});

it('should inherit properties from Model when using discriminator', function (done) {
let
let
TestModel = utils.createTestModel({ kind: String });

let
let
DiscriminatorTest = utils.createTestModel({ inherit: Boolean });

DiscriminatorTest = TestModel.discriminator('DiscriminatorTest', DiscriminatorTest.schema);

DiscriminatorTest.byTenant(1).create({ inherit: true, kind: 'test' }, (err, doc) => {
assert.equal(doc.__t, 'DiscriminatorTest');
assert.equal(doc.tenantId, 1);
Expand All @@ -50,6 +50,26 @@ describe('MongoTenant', function() {
});
});

utils.skipIf(utils.isMongoose4(), 'should bind tenant context to Model.countDocuments().', function(done) {
let TestModel = utils.createTestModel({});

TestModel.byTenant(1).create({}, {}, {}, (err) => {
assert(!err, 'Expected creation of 3 test entities to work.');

TestModel.byTenant(1).countDocuments((err, count) => {
assert(!err, 'Expected entity counting to work.');
assert.equal(count, 3, 'Expected 3 entries for tenant `1`.');

TestModel.byTenant(2).countDocuments((err, count) => {
assert(!err, 'Expected entity counting to work.');
assert.equal(count, 0, 'Expected 0 entries for tenant `2`.');

done();
});
});
});
});

it('should bind tenant context to Model.count().', function(done) {
let
TestModel = utils.createTestModel({});
Expand All @@ -71,6 +91,26 @@ describe('MongoTenant', function() {
});
});

utils.skipIf(utils.isMongoose4(), 'should avoid tenant context jumping on Model.countDocuments().', function(done) {
let TestModel = utils.createTestModel({});

TestModel.byTenant(1).create({}, {}, {}, (err) => {
assert(!err, 'Expected creation of 3 test entities to work.');

TestModel.byTenant(2).countDocuments({tenantId: 1}, (err, count) => {
assert(!err, 'Expected entity counting to work.');
assert.equal(count, 0, 'Expected 0 entries for tenant `2`.');

TestModel.byTenant(1).countDocuments({tenantId: 2}, (err, count) => {
assert(!err, 'Expected entity counting to work.');
assert.equal(count, 3, 'Expected 3 entries for tenant `1`.');

done();
});
});
});
});

it('should avoid tenant context jumping on Model.count().', function(done) {
let
TestModel = utils.createTestModel({});
Expand All @@ -92,6 +132,21 @@ describe('MongoTenant', function() {
});
});

utils.skipIf(utils.isMongoose4(), 'should not affect Model.countDocuments() when not in tenant context.', function(done) {
let TestModel = utils.createTestModel({});

TestModel.create({tenantId: 1}, {tenantId: 2}, {tenantId: 3}, (err) => {
assert(!err, 'Expected creation of 3 test entities to work.');

TestModel.countDocuments((err, count) => {
assert(!err, 'Expected entity counting to work.');
assert.equal(count, 3, 'Expected 3 entries for all tenants.');

done();
});
});
});

it('should not affect Model.count() when not in tenant context.', function(done) {
let TestModel = utils.createTestModel({});

Expand Down Expand Up @@ -508,7 +563,7 @@ describe('MongoTenant', function() {
}, (err) => {
assert(!err, 'Expected model update to work.');

TestModel.byTenant('tenant1').find({}, (err, entities) => {
TestModel.byTenant('tenant1').find({}, (err, entities) => {
assert(!err, 'Expected entity search by Model.find to work.');
assert.equal(entities.length, 1, 'Expected to find exactly 1 entity.');
assert.equal(entities[0].someField, 'some-value', 'Expected updated value of someField to be `some-value`.');
Expand Down

0 comments on commit 48d9d59

Please sign in to comment.