forked from mchapman/mongoose-subkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (32 loc) · 1.16 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
/*!
* Mongoose Subkey Plugin
* Copyright(c) 2013 Mark Chapman
* MIT Licensed
*/
var mongoose = require('mongoose');
function subkeyPlugin(schema, options) {
schema.pre('save', function (next) {
for (var i = 0; i < options.subschemas.length; i++) {
var subSchema = options.subschemas[i]
, list = this[subSchema.list];
for (var j = 1; j < list.length; j++) {
for (var k = 0; k < j; k++) {
var matching = true;
for (var keyPart = 0; keyPart < subSchema.keyList.length ; keyPart++) {
var key = subSchema.keyList[keyPart];
// Not (currently) concerned with objects here - just simple types
if (list[j][key] !== list[k][key]) {
matching = false;
break;
}
}
if (matching) {
return next(new Error("Duplicate " + subSchema.list + " not allowed"))
}
}
}
}
next();
});
}
module.exports = subkeyPlugin;