-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (39 loc) · 1.51 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
const _ = require('lodash');
const generators = require('./src/generators');
function cosmosDbPlugin(schema) {
const shardKey = schema.options.shardKey;
if (shardKey) {
const keys = _.keys(shardKey)
if (!_.isObject(shardKey) || keys.length > 1)
throw new Error("Cosmos DB expects a single shard key")
schema.$partition = {
key: keys[0],
checking: false,
postCheckCallbacks: [],
}
const shardKeySchemaType = schema.path(keys[0]);
// Either no key is specified or there is no default value
// and there are instructions for generating a key
if (!shardKeySchemaType
|| (!shardKeySchemaType.defaultValue
&& shardKeySchemaType.options.shardKey)) {
const shardKeyOptions = shardKeySchemaType ? shardKeySchemaType.options.shardKey : null;
const generatingFunction =
generators.processShardKeyOptions(shardKeyOptions);
if (shardKeySchemaType)
shardKeySchemaType.default(generatingFunction);
else {
const newShardKey = {}
props[shardKey] = {
type: String,
default: generatingFunction,
}
schema.add(newShardKey);
}
}
// The auto create option messes things up
schema.set('autoCreate', false);
schema.set('autoIndex', false);
}
}
module.exports = cosmosDbPlugin;