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
/
Copy pathCustomType.js
88 lines (73 loc) · 1.72 KB
/
CustomType.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
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;
export const CustomTypes = {
scalarAndEjson(type) {
return {
name: type.name(),
description: type.description(),
serialize: obj => obj,
parseValue: obj => obj,
};
},
};
/* eslint-disable class-methods-use-this */
export class CustomType {
constructor() {
this.register();
}
register() {
// Type is already present
if (!EJSON._getTypes()[this.name()]) {
if (isVerbose) {
console.log(
`${PACKAGE_NAME} EJSON.addType ${this.name()} from TypeDef class`
);
}
EJSON.addType(this.name(), json => this.fromJSONValue(json));
}
}
name() {
throw new Error(
`name() needs to be implemented in ${this.constructor.name}`
);
}
toSimpleSchema() {
throw new Error(
`toSimpleSchema() needs to be implemented in ${this.constructor.name}`
);
}
description() {
return '';
}
fromJSONValue(json) {
return json;
}
toPersist(obj) {
if (obj !== undefined && obj !== null) {
return this.doToPersist(obj);
}
return obj;
}
fromPersisted(obj) {
if (obj !== undefined && obj !== null) {
return this.doFromPersisted(obj);
}
return obj;
}
doToPersist(obj) {
throw new Error(obj);
}
doFromPersisted(obj) {
return obj;
}
doParseLiteral(ast) {
if (ast.kind === 'IntValue') {
const result = parseInt(ast.value, 10);
return this.doFromPersisted(result); // ast value is always in string format
}
return null;
}
}