This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CustomTypeCollection.js
96 lines (86 loc) · 3.02 KB
/
CustomTypeCollection.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
import { Mongo } from 'meteor/mongo';
import { EJSON } from 'meteor/ejson';
import { getSettings } from 'meteor/quave:settings';
const PACKAGE_NAME = 'quave:collections';
const settings = getSettings({ packageName: PACKAGE_NAME });
const { isVerbose } = settings;
const lookForTypesAndApply = (definition, obj, consumeType) => {
if (obj) {
Object.entries(definition.fields).forEach(([key, value]) => {
if (!(key in obj)) {
return;
}
if (!EJSON._getTypes()[value.typeName]) {
return;
}
// is not a subtype
if (!value.fields) {
if (Array.isArray(value)) {
// eslint-disable-next-line no-param-reassign
obj[key] = obj[key].map(v => consumeType(value[0].customType, v));
} else {
// eslint-disable-next-line no-param-reassign
obj[key] = consumeType(value.customType, obj[key]);
}
} else {
const subtype = value;
const arr = Array.isArray(obj[key]) ? obj[key] : [obj[key]];
const newArr = [];
for (let i = 0; i < arr.length; i++) {
const v = { ...arr[i] };
newArr.push(v);
lookForTypesAndApply(subtype, v, consumeType);
}
// eslint-disable-next-line no-param-reassign
obj[key] = Array.isArray(obj[key]) ? newArr : newArr[0];
}
});
}
return obj;
};
const onPersistCollection = (definition, obj) => {
return lookForTypesAndApply(definition, obj, (parser, value) => {
return parser.toPersist(value);
});
};
const onLoadFromCollection = (definition, obj) =>
lookForTypesAndApply(definition, obj, (parser, value) =>
parser.fromPersisted(value)
);
export const CustomTypeCollection = {
createTypedCollection: (name, definition, opts) => {
if (!definition) {
throw new Error(`"definition" option was not found for "${name}"`);
}
const collection = new Mongo.Collection(name, {
transform: obj => onLoadFromCollection(definition, obj),
});
if (!collection.before) {
console.warn(
'If you want to automatically convert your types before persisting on MongoDB please add this package https://github.com/Meteor-Community-Packages/meteor-collection-hooks'
);
} else {
// registerHooks
collection.before.insert((_, obj) => {
onPersistCollection(definition, obj);
});
collection.before.update((userId, doc, fields, set) => {
onPersistCollection(definition, set.$set);
});
}
if (opts && opts.helpers && Object.keys(opts.helpers).length) {
// :-( private access
const transformType = collection._transform;
collection._transform = null;
if (!collection.helpers) {
throw new Error(
"You need to add this package https://github.com/dburles/meteor-collection-helpers to use 'helpers'"
);
}
collection.helpers(opts.helpers);
const transformHelpers = collection._transform;
collection._transform = doc => transformType(transformHelpers(doc));
}
return collection;
},
};