/
schematype.ts
297 lines (268 loc) · 6.65 KB
/
schematype.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { setProp } from './util';
import ValidationError from './error/validation';
/**
* This is the basic schema type.
* All schema types should inherit from this class.
* For example:
*
* ``` js
* class SchemaTypeCustom extends SchemaType {};
* ```
*
* **Query operators**
*
* To add a query operator, defines a method whose name is started with `q$`.
* For example:
*
* ``` js
* SchemaTypeCustom.q$foo = function(value, query, data){
* // ...
* };
* ```
*
* The `value` parameter is the value of specified field; the `query` parameter
* is the value passed to the query operator; the `data` parameter is the
* complete data.
*
* The return value must be a boolean indicating whether the data passed.
*
* **Update operators**
*
* To add a update operator, defines a method whose name is started with `u$`.
* For example:
*
* ``` js
* SchemaTypeCustom.u$foo = function(value, update, data){
* // ...
* };
* ```
*
* The `value` parameter is the value of specified field; the `update` parameter
* is the value passed to the update operator; the `data` parameter is the
* complete data.
*
* The return value will replace the original data.
*/
class SchemaType<T> {
options: { required: boolean; default?: (() => T) | T; };
default: () => T;
/**
* SchemaType constructor.
*
* @param {String} name
* @param {Object} [options]
* @param {Boolean} [options.required=false]
* @param {*} [options.default]
*/
constructor(public name: string = '', options?: { required?: boolean; default?: (() => T) | T; }) {
this.options = Object.assign({
required: false
}, options);
const default_ = this.options.default;
if (typeof default_ === 'function') {
this.default = default_ as () => T;
} else {
this.default = () => default_;
}
}
/**
* Casts data. This function is used by getters to cast an object to document
* instances. If the value is null, the default value will be returned.
*
* @param {*} value
* @param {Object} data
* @return {*}
*/
cast(value?: unknown, data?: unknown): unknown {
if (value == null) {
return this.default();
}
return value;
}
/**
* Validates data. This function is used by setters.
*
* @param {*} value
* @param {Object} data
* @return {*|Error}
*/
validate(value: unknown, data?: unknown): unknown {
if (this.options.required && value == null) {
throw new ValidationError(`\`${this.name}\` is required!`);
}
return value;
}
/**
* Compares data. This function is used when sorting.
*
* @param {*} a
* @param {*} b
* @return {Number}
*/
compare(a: unknown, b: unknown): number {
if (a > b) {
return 1;
} else if (a < b) {
return -1;
}
return 0;
}
/**
* Parses data. This function is used when restoring data from database files.
*
* @param {*} value
* @param {Object} data
* @return {*}
*/
parse(value: unknown): any {
return value;
}
/**
* Transforms value. This function is used when saving data to database files.
*
* @param {*} value
* @param {Object} data
* @return {*}
*/
value(value: unknown, data?: unknown): any {
return value;
}
/**
* Checks the equality of data.
*
* @param {*} value
* @param {*} query
* @param {Object} data
* @return {Boolean}
*/
match(value: T, query: unknown, data?: unknown): boolean {
return value === query;
}
/**
* Checks the existance of data.
*
* @param {*} value
* @param {*} query
* @param {Object} data
* @return {Boolean}
*/
q$exist(value: unknown, query: unknown, data?: unknown): boolean {
return (value != null) === query;
}
/**
* Checks the equality of data. Returns true if the value doesn't match.
*
* @param {*} value
* @param {*} query
* @param {Object} data
* @return {boolean}
*/
q$ne(value: T, query: unknown, data?: unknown): boolean {
return !this.match(value, query, data);
}
/**
* Checks whether `value` is less than (i.e. <) the `query`.
*
* @param {*} value
* @param {*} query
* @param {Object} data
* @return {Boolean}
*/
q$lt(value: unknown, query: unknown, data?: unknown): boolean {
return value < query;
}
/**
* Checks whether `value` is less than or equal to (i.e. <=) the `query`.
*
* @param {*} value
* @param {*} query
* @param {Object} data
* @return {Boolean}
*/
q$lte(value: unknown, query: unknown, data?: unknown): boolean {
return value <= query;
}
/**
* Checks whether `value` is greater than (i.e. >) the `query`.
*
* @param {*} value
* @param {*} query
* @param {Object} data
* @return {Boolean}
*/
q$gt(value: unknown, query: unknown, data?: unknown): boolean {
return value > query;
}
/**
* Checks whether `value` is greater than or equal to (i.e. >=) the `query`.
*
* @param {*} value
* @param {*} query
* @param {Object} data
* @return {Boolean}
*/
q$gte(value: unknown, query: unknown, data?: unknown): boolean {
return value >= query;
}
/**
* Checks whether `value` is equal to one of elements in `query`.
*
* @param {*} value
* @param {Array} query
* @param {Object} data
* @return {Boolean}
*/
q$in(value: unknown, query: unknown[], data?: unknown): boolean {
return query.includes(value);
}
/**
* Checks whether `value` is not equal to any elements in `query`.
*
* @param {*} value
* @param {Array} query
* @param {Object} data
* @return {Boolean}
*/
q$nin(value: unknown, query: unknown[], data?: unknown): boolean {
return !query.includes(value);
}
/**
* Sets the value.
*
* @param {*} value
* @param {*} update
* @param {Object} data
* @return {*}
*/
u$set<T>(value: unknown, update: T, data?: unknown): T {
return update;
}
/**
* Unsets the value.
*
* @param {*} value
* @param {*} update
* @param {Object} data
* @return {*}
*/
u$unset<T>(value: T, update: boolean, data?: unknown): T | undefined { return update ? undefined : value; }
/**
* Renames a field.
*
* @param {*} value
* @param {*} update
* @param {Object} data
* @return {*}
*/
u$rename(value: unknown, update: unknown, data: unknown): void {
if (value !== undefined) setProp(data, update, value);
return undefined;
}
q$exists: SchemaType<T>['q$exist'];
q$max: SchemaType<T>['q$lte'];
q$min: SchemaType<T>['q$gte'];
}
SchemaType.prototype.q$exists = SchemaType.prototype.q$exist;
SchemaType.prototype.q$max = SchemaType.prototype.q$lte;
SchemaType.prototype.q$min = SchemaType.prototype.q$gte;
export default SchemaType;