This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongojs-hooks.js
155 lines (117 loc) · 3.93 KB
/
mongojs-hooks.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
143
144
145
146
147
148
149
150
151
152
153
154
155
var mongojs = require("mongojs");
var fnhooks = require("fn-hooks");
var util = require("util");
var _ = require("lodash");
// DB connection helper
var dbConns = { };
mongojs.db = function (key, dbConnectionString) {
if (dbConnectionString !== undefined)
dbConns[key] = mongojs(dbConnectionString);
return dbConns[key];
};
// Tenancy helper
mongojs.Database.prototype.tenant = function (tenantName) {
var db = this;
var tenant = mongojs.Database.prototype.collection.apply(this, arguments);
tenant.collection = function (collName) {
// 'this' currently refers to the collection returned as 'tenant' and not the db
return mongojs.Database.prototype.collection.call(db, tenantName + "." + collName, collName);
};
return tenant;
};
// Define collection hooks
var colls = { };
mongojs.reset = function () {
colls = { };
};
mongojs.collection = function (name) {
// Use already existing collection if previously created
if (colls[name] !== undefined)
return colls[name].prototype;
// Create retrievable collection
colls[name] = function () {
colls[name].super_.apply(this, arguments);
};
// Inherit from parent mongojs collection and add pre() and post() hooks
util.inherits(colls[name], mongojs.Collection);
fnhooks(colls[name].prototype);
return colls[name].prototype;
};
mongojs.Database.prototype.collection = _.wrap(mongojs.Database.prototype.collection, function (fn, name, collName) {
// Create a parent mongojs collection to setup _name and _get fields
var collection = fn.call(this, name);
// Now if a pre-defined collection exists (use tenant-less name if it exists), call it with the internal fields retrieved above
if (colls[collName || name])
collection = new colls[collName || name](collection._name, collection._get);
return collection;
});
mongojs.util = {
// Converts any . and $ characters in key names with the full-length unicode characters.
// This is necessary since these two characters are illegal in Mongo key names.
sanitise: function (object) {
if (!object || object._bsontype)
return object;
if (_.isString(object))
return object.replace(/\./g, "U+FF0E").replace(/\$/g, "U+FF04");
var newObject = _.isArray(object) ? [ ] : { };
Object.keys(object).forEach(function (key) {
var sKey = key;
if (/[.|$]/.test(key)) {
sKey = mongojs.util.sanitise(key);
newObject[sKey] = object[key];
} else {
newObject[key] = object[key];
}
// Recurse
if (isObject(newObject[sKey]))
newObject[sKey] = this.sanitise(newObject[sKey]);
}.bind(this));
return newObject;
},
// Will convert full-length unicode chatacters back to their ASCII form.
unsanitise: function (object) {
if (!object || object._bsontype)
return object;
if (_.isString(object))
return object.replace(/U\+FF0E/g, ".").replace(/U\+FF04/g, "$");
var newObject = _.isArray(object) ? [ ] : { };
Object.keys(object).forEach(function (sKey) {
var key = sKey;
if (/(U\+FF0E|U\+FF04)/.test(sKey)) {
key = mongojs.util.unsanitise(sKey);
newObject[key] = object[sKey];
} else {
newObject[key] = object[key];
}
// Recurse
if (isObject(newObject[key]))
newObject[key] = this.unsanitise(newObject[key]);
}.bind(this));
return newObject;
},
// Flattens an object into dot-notation
flatten: function (object) {
var result = { };
for (var i in object) {
if (!object.hasOwnProperty(i)) continue;
if (isObject(object[i])) {
var flatObject = this.flatten(object[i]);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
result[i + '.' + x] = flatObject[x];
}
} else {
result[i] = object[i];
}
}
return result;
}
};
var isObject = function (obj) {
if (typeof obj !== "object" || obj === null)
return false;
var ObjProto = obj;
while (Object.getPrototypeOf(ObjProto = Object.getPrototypeOf(ObjProto)) !== null) ;
return Object.getPrototypeOf(obj) === ObjProto;
};
module.exports = mongojs;