-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectionPlugin.js
47 lines (42 loc) · 1.43 KB
/
collectionPlugin.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
const ObjectID = require('bson').ObjectID;
const _ = require('lodash');
const traverse = require("traverse");
const {convertNameToTestFunction} = require("./utils");
module.exports = function (orm) {
orm.collectionOptions = orm.collectionOptions || [];
orm.registerCollectionOptions = function (collectionName, dbName, options) {
if (orm.mode === 'single') {
options = dbName;
dbName = null;
} else if (!options && orm.dbName) {
[dbName, options] = [orm.dbName, dbName]
}
orm.collectionOptions.push({
testCollection: convertNameToTestFunction(collectionName),
options,
...(orm.mode !== 'single' && {
testDb: convertNameToTestFunction(dbName)
})
})
}
orm.getOptions = function (collectionName, dbName) {
let matches = orm.collectionOptions.filter(match => {
if (orm.mode === 'single') {
if (match.testCollection(collectionName)) return true
} else {
if (!dbName) {
dbName = orm.dbName;
}
if (match.testCollection(collectionName) && match.testDb(dbName)) return true;
}
}).map(m => m.options);
return _.merge({}, ...matches)
}
/*const _idIndexArr = [];
orm.on('pre:execChain', async (query) => {
if (_idIndexArr.includes(query.name)) return;
_idIndexArr.push(query.name);
let cursor = orm._getCollection(...query.name.split('@'));
await cursor.createIndex('_id', {unique: true});
})*/
}