Skip to content

Commit

Permalink
Attach sbaseConfig to model.
Browse files Browse the repository at this point in the history
  • Loading branch information
yz-ark7 committed May 30, 2020
1 parent e0094cf commit fda8776
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 22 additions & 12 deletions src/mongoose/discriminator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as _ from 'underscore';
import { Document, Model as MModel } from 'mongoose';

import { A7ModelType } from './a7-model';
import { sbaseMongooseConfig } from './model-config';
import { DocumentModel, Model, lazyFns, shareFns } from './model';
import { ConvertModel } from './types';
import { DocumentModel, Model, lazyFns, shareFns } from './model';
import { SBaseMongooseConfig } from './model-config';

export class Discriminator extends DocumentModel {
public static $discriminator<
Expand Down Expand Up @@ -38,32 +38,42 @@ function discriminatorMultiTenancy<
model: T,
dm: M,
): ConvertModel<T & InstanceType<M> & Document, T & InstanceType<M>> & T & M {
if (!sbaseMongooseConfig.multiTenancy.enabled) {
return model.discriminator(
const sbaseConfig: SBaseMongooseConfig = (model as any).sbaseConfig;

if (!sbaseConfig.multiTenancy.enabled) {
const m = model.discriminator(
dm.name,
dm.$mongooseOptions().mongooseSchema,
) as any;

m.sbaseConfig = sbaseConfig;

return m;
}

const tenants = ['default'].concat(sbaseMongooseConfig.multiTenancy.tenants);
const tenants = ['default'].concat(sbaseConfig.multiTenancy.tenants);
const tenantMap: {
[key: string]: MModel<Document> & T;
} = {};

const currentTenantMap = (model as any)._proxy.$tenantMap;

for (const tenancy of tenants) {
tenantMap[tenancy] = currentTenantMap[tenancy].discriminator(
const m = currentTenantMap[tenancy].discriminator(
dm.name,
dm.$mongooseOptions(tenancy).mongooseSchema,
dm.$mongooseOptions(sbaseConfig, tenancy).mongooseSchema,
);

m.sbaseConfig = sbaseConfig;

tenantMap[tenancy] = m;
}

const proxy: any = new Proxy<MModel<Document> & T>({} as any, {
get: (_obj: {}, prop: string) => {
if (lazyFns.indexOf(prop) >= 0) {
const ret = function() {
const t = sbaseMongooseConfig.multiTenancy.tenancyFn(prop);
const ret = function () {
const t = sbaseConfig.multiTenancy.tenancyFn(prop);
const m1: any = tenantMap[t];
const actualFn = m1[prop];

Expand All @@ -74,15 +84,15 @@ function discriminatorMultiTenancy<

if (shareFns.indexOf(prop) >= 0) {
const ret = () => {
return _.map(tenants, t => {
return _.map(tenants, (t) => {
const m2: any = tenantMap[t];
return m2[prop].apply(m2, arguments);
});
};
return ret;
}

const tenancy = sbaseMongooseConfig.multiTenancy.tenancyFn(prop);
const tenancy = sbaseConfig.multiTenancy.tenancyFn(prop);
const m: any = tenantMap[tenancy];
m._proxy = proxy;

Expand All @@ -94,7 +104,7 @@ function discriminatorMultiTenancy<
return _.isFunction(res) ? res.bind(m) : res;
},
set: (_obj: {}, prop: string, value: any) => {
const tenancy = sbaseMongooseConfig.multiTenancy.tenancyFn(prop);
const tenancy = sbaseConfig.multiTenancy.tenancyFn(prop);
const m: any = tenantMap[tenancy];
m[prop] = value;
return true;
Expand Down
29 changes: 19 additions & 10 deletions src/mongoose/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,17 @@ export class Model {
}
}

const collection = _.chain([
tenancy === 'default'
? sbaseConfig.multiTenancy.defaultCollectionNamespace
: tenancy,
mongooseOptions.config.collection,
])
.filter((x) => !!x)
.join('.')
.value();
const collection =
mongooseOptions.config.collection &&
_.chain([
tenancy === 'default' && sbaseConfig != null
? sbaseConfig.multiTenancy.defaultCollectionNamespace
: tenancy,
mongooseOptions.config.collection,
])
.filter((x) => !!x)
.join('.')
.value();

const mongooseSchema = new Schema(
mongooseOptions.schema,
Expand Down Expand Up @@ -408,10 +410,14 @@ function registerMultiTenancy<T extends ModelType>(
.sbaseConfig;

if (!sbaseConfig.multiTenancy.enabled) {
return mongooseInstance.model(
const m = mongooseInstance.model(
model.name,
model.$mongooseOptions().mongooseSchema,
) as any;

m.sbaseConfig = sbaseConfig;

return m;
}

const tenants = ['default'].concat(sbaseConfig.multiTenancy.tenants);
Expand Down Expand Up @@ -439,6 +445,9 @@ function registerMultiTenancy<T extends ModelType>(
model.name,
model.$mongooseOptions(sbaseConfig, tenancy).mongooseSchema,
) as any;

m.sbaseConfig = sbaseConfig;

tenantMap[tenancy] = m;
}

Expand Down
12 changes: 5 additions & 7 deletions test/benchmark/mongoose/mongoose.orig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Person, MongoosePerson } from './schemas/persons';
import { Property, MongooseProperty } from './schemas/properties';
import { MongoosePerson, Person } from './schemas/persons';
import { MongooseProperty, Property } from './schemas/properties';

describe('benchmark.mongoose', () => {
beforeEach(async () => {
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('benchmark.mongoose', () => {
firstName: `foo name ${i}`,
},
null,
{ populate: [{ path: 'home' }] },
{ populate: [{ path: 'home' }] as any },
);
}
});
Expand All @@ -82,7 +82,7 @@ describe('benchmark.mongoose', () => {
firstName: `foo name ${i}`,
},
null,
{ populate: [{ path: 'home' }] },
{ populate: [{ path: 'home' }] as any },
);
}
});
Expand Down Expand Up @@ -125,9 +125,7 @@ describe('benchmark.mongoose', () => {

it(`[sbase]\t\truns ${INSERT_BATCH} synchronize persons find limit 10 using lean`, async () => {
for (let i = 0; i < INSERT_BATCH; i++) {
await Person.find({}, null)
.skip(i)
.limit(10);
await Person.find({}, null).skip(i).limit(10);
}
});
});
4 changes: 1 addition & 3 deletions test/mongoose/data-level.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as _ from 'underscore';
import * as mongoose from 'mongoose';
import * as should from 'should';

import {
A7Model,
Expand Down Expand Up @@ -194,7 +192,7 @@ describe('NModel Data Level', () => {

it('should be able to populate refs within query', async () => {
const data = await DLRootRef.findOne({}, null, {
populate: [{ path: 'refs', options: { level: DataLevels.L1 } }],
populate: { path: 'refs', options: { level: DataLevels.L1 } },
});
data.refs[0]._id.toString().should.be.equal(m1._id.toString());
_.pick(data.refs[0].toJSON(), 'f1').should.be.deepEqual(e1);
Expand Down

0 comments on commit fda8776

Please sign in to comment.