-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
142 lines (129 loc) · 4.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Multi-tenancy for Mongoose
See readme for examples and info
@author Jason Raede <[email protected]>
*/
var collectionDelimiter, dot, mongoose, owl, _;
mongoose = require('mongoose');
require('mongoose-schema-extend');
dot = require('dot-component');
_ = require('underscore');
owl = require('owl-deepcopy');
/*
Added by @watnotte
*/
collectionDelimiter = '__';
module.exports = function(delimiter) {
if (delimiter) {
return collectionDelimiter = delimiter;
}
};
mongoose.mtModel = function(name, schema, collectionName) {
var extendPathWithTenantId, extendSchemaWithTenantId, model, modelName, multitenantSchemaPlugin, newSchema, origSchema, parts, pre, preModelName, precompile, split, tenantCollectionName, tenantId, tenantModelName, uniq, _i, _len;
precompile = [];
extendPathWithTenantId = function(tenantId, path) {
var key, newPath, val, _ref;
if (path.instance !== 'ObjectID' && path.instance !== mongoose.Schema.Types.ObjectId) {
return false;
}
if ((path.options.$tenant == null) || path.options.$tenant !== true) {
return false;
}
newPath = {
type: mongoose.Schema.Types.ObjectId
};
_ref = path.options;
for (key in _ref) {
val = _ref[key];
if (key !== 'type') {
newPath[key] = _.clone(val, true);
}
}
newPath.ref = tenantId + collectionDelimiter + path.options.ref;
precompile.push(tenantId + '.' + path.options.ref);
return newPath;
};
extendSchemaWithTenantId = function(tenantId, schema) {
var config, extension, newPath, newSchema, newSubSchema, prop, _ref;
extension = {};
newSchema = owl.deepCopy(schema);
newSchema.callQueue.forEach(function(k) {
var args, key, val, _ref;
args = [];
_ref = k[1];
for (key in _ref) {
val = _ref[key];
args.push(val);
}
return k[1] = args;
});
_ref = schema.paths;
for (prop in _ref) {
config = _ref[prop];
if (config.options.type instanceof Array) {
if (config.schema != null) {
newSubSchema = extendSchemaWithTenantId(tenantId, config.schema);
newSubSchema = extendSchemaWithTenantId(tenantId, config.schema);
newSchema.path(prop, [newSubSchema]);
} else {
newPath = extendPathWithTenantId(tenantId, config.caster);
if (newPath) {
newSchema.path(prop, [newPath]);
}
}
} else {
if (config.schema != null) {
newSubSchema = extendSchemaWithTenantId(tenantId, config.schema);
newSchema.path(prop, newSubSchema);
} else {
newPath = extendPathWithTenantId(tenantId, config);
if (newPath) {
newSchema.path(prop, newPath);
}
}
}
}
return newSchema;
};
multitenantSchemaPlugin = function(schema, options) {
schema.statics.getTenantId = schema.methods.getTenantId = function() {
return this.schema.$tenantId;
};
return schema.statics.getModel = schema.methods.getModel = function(name) {
return mongoose.mtModel(this.getTenantId() + '.' + name);
};
};
if (name.indexOf('.') >= 0) {
parts = name.split('.');
modelName = parts.pop();
tenantId = parts.join('.');
tenantModelName = tenantId + collectionDelimiter + modelName;
if (mongoose.models[tenantModelName] != null) {
return mongoose.models[tenantModelName];
}
model = this.model(modelName);
tenantCollectionName = tenantId + collectionDelimiter + model.collection.name;
origSchema = model.schema;
newSchema = extendSchemaWithTenantId(tenantId, origSchema);
newSchema.$tenantId = tenantId;
newSchema.plugin(multitenantSchemaPlugin);
if (mongoose.mtModel.goingToCompile.indexOf(tenantModelName) < 0) {
mongoose.mtModel.goingToCompile.push(tenantModelName);
}
if (precompile.length) {
uniq = _.uniq(precompile);
for (_i = 0, _len = uniq.length; _i < _len; _i++) {
pre = uniq[_i];
split = pre.split('.');
preModelName = split[0] + collectionDelimiter + split[1];
if ((mongoose.models[preModelName] == null) && mongoose.mtModel.goingToCompile.indexOf(preModelName) < 0) {
mongoose.mtModel(pre, null, tenantCollectionName);
}
}
}
return this.model(tenantModelName, newSchema, tenantCollectionName);
} else {
return this.model(name, schema, collectionName);
}
};
mongoose.mtModel.goingToCompile = [];