forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrx-schema-helper.ts
366 lines (322 loc) · 10.3 KB
/
rx-schema-helper.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { newRxError } from './rx-error.ts';
import type {
CompositePrimaryKey,
DeepReadonly,
JsonSchema,
PrimaryKey,
RxDocumentData,
RxJsonSchema,
RxStorageDefaultCheckpoint,
StringKeys
} from './types/index.d.ts';
import {
appendToArray,
ensureNotFalsy,
flatClone,
getProperty,
isMaybeReadonlyArray,
REGEX_ALL_DOTS,
RX_META_LWT_MINIMUM,
sortObject,
trimDots
} from './plugins/utils/index.ts';
import type { RxSchema } from './rx-schema.ts';
/**
* Helper function to create a valid RxJsonSchema
* with a given version.
*/
export function getPseudoSchemaForVersion<T = any>(
version: number,
primaryKey: StringKeys<T>
): RxJsonSchema<RxDocumentData<T>> {
const pseudoSchema: RxJsonSchema<RxDocumentData<T>> = fillWithDefaultSettings({
version,
type: 'object',
primaryKey: primaryKey as any,
properties: {
[primaryKey]: {
type: 'string',
maxLength: 100
}
} as any,
indexes: [
[primaryKey]
],
required: [primaryKey]
});
return pseudoSchema;
}
/**
* Returns the sub-schema for a given path
*/
export function getSchemaByObjectPath<T = any>(
rxJsonSchema: RxJsonSchema<T>,
path: keyof T | string
): JsonSchema {
let usePath: string = path as string;
usePath = usePath.replace(REGEX_ALL_DOTS, '.properties.');
usePath = 'properties.' + usePath;
usePath = trimDots(usePath);
const ret = getProperty(rxJsonSchema, usePath);
return ret;
}
export function fillPrimaryKey<T>(
primaryPath: keyof T,
jsonSchema: RxJsonSchema<T>,
documentData: RxDocumentData<T>
): RxDocumentData<T> {
// optimization shortcut.
if (typeof jsonSchema.primaryKey === 'string') {
return documentData;
}
const newPrimary = getComposedPrimaryKeyOfDocumentData<T>(
jsonSchema,
documentData
);
const existingPrimary: string | undefined = documentData[primaryPath] as any;
if (
existingPrimary &&
existingPrimary !== newPrimary
) {
throw newRxError(
'DOC19',
{
args: {
documentData,
existingPrimary,
newPrimary,
},
schema: jsonSchema
});
}
(documentData as any)[primaryPath] = newPrimary;
return documentData;
}
export function getPrimaryFieldOfPrimaryKey<RxDocType>(
primaryKey: PrimaryKey<RxDocType>
): StringKeys<RxDocType> {
if (typeof primaryKey === 'string') {
return primaryKey as any;
} else {
return (primaryKey as CompositePrimaryKey<RxDocType>).key;
}
}
export function getLengthOfPrimaryKey<RxDocType>(
schema: RxJsonSchema<RxDocumentData<RxDocType>>
): number {
const primaryPath = getPrimaryFieldOfPrimaryKey(schema.primaryKey);
const schemaPart = getSchemaByObjectPath(schema, primaryPath);
return ensureNotFalsy(schemaPart.maxLength);
}
/**
* Returns the composed primaryKey of a document by its data.
*/
export function getComposedPrimaryKeyOfDocumentData<RxDocType>(
jsonSchema: RxJsonSchema<RxDocType> | RxJsonSchema<RxDocumentData<RxDocType>>,
documentData: Partial<RxDocType>
): string {
if (typeof jsonSchema.primaryKey === 'string') {
return (documentData as any)[jsonSchema.primaryKey];
}
const compositePrimary: CompositePrimaryKey<RxDocType> = jsonSchema.primaryKey as any;
return compositePrimary.fields.map(field => {
const value = getProperty(documentData as any, field as string);
if (typeof value === 'undefined') {
throw newRxError('DOC18', { args: { field, documentData } });
}
return value;
}).join(compositePrimary.separator);
}
/**
* Normalize the RxJsonSchema.
* We need this to ensure everything is set up properly
* and we have the same hash on schemas that represent the same value but
* have different json.
*
* - Orders the schemas attributes by alphabetical order
* - Adds the primaryKey to all indexes that do not contain the primaryKey
* - We need this for deterministic sort order on all queries, which is required for event-reduce to work.
*
* @return RxJsonSchema - ordered and filled
*/
export function normalizeRxJsonSchema<T>(jsonSchema: RxJsonSchema<T>): RxJsonSchema<T> {
const normalizedSchema: RxJsonSchema<T> = sortObject(jsonSchema, true);
return normalizedSchema;
}
/**
* If the schema does not specify any index,
* we add this index so we at least can run RxQuery()
* and only select non-deleted fields.
*/
export function getDefaultIndex(primaryPath: string) {
return ['_deleted', primaryPath];
}
/**
* fills the schema-json with default-settings
* @return cloned schemaObj
*/
export function fillWithDefaultSettings<T = any>(
schemaObj: RxJsonSchema<T>
): RxJsonSchema<RxDocumentData<T>> {
schemaObj = flatClone(schemaObj);
const primaryPath: string = getPrimaryFieldOfPrimaryKey(schemaObj.primaryKey);
schemaObj.properties = flatClone(schemaObj.properties);
// additionalProperties is always false
schemaObj.additionalProperties = false;
// fill with key-compression-state ()
if (!Object.prototype.hasOwnProperty.call(schemaObj, 'keyCompression')) {
schemaObj.keyCompression = false;
}
// indexes must be array
schemaObj.indexes = schemaObj.indexes ? schemaObj.indexes.slice(0) : [];
// required must be array
schemaObj.required = schemaObj.required ? schemaObj.required.slice(0) : [];
// encrypted must be array
schemaObj.encrypted = schemaObj.encrypted ? schemaObj.encrypted.slice(0) : [];
// add _rev
(schemaObj.properties as any)._rev = {
type: 'string',
minLength: 1
};
// add attachments
(schemaObj.properties as any)._attachments = {
type: 'object'
};
// add deleted flag
(schemaObj.properties as any)._deleted = {
type: 'boolean'
};
// add meta property
(schemaObj.properties as any)._meta = RX_META_SCHEMA;
/**
* meta fields are all required
*/
schemaObj.required = schemaObj.required ? schemaObj.required.slice(0) : [];
(schemaObj.required as string[]).push('_deleted');
(schemaObj.required as string[]).push('_rev');
(schemaObj.required as string[]).push('_meta');
(schemaObj.required as string[]).push('_attachments');
// final fields are always required
const finalFields = getFinalFields(schemaObj);
appendToArray(schemaObj.required as any, finalFields);
schemaObj.required = schemaObj.required
.filter((field: string) => !field.includes('.'))
.filter((elem: any, pos: any, arr: any) => arr.indexOf(elem) === pos); // unique;
// version is 0 by default
schemaObj.version = schemaObj.version || 0;
const useIndexes: string[][] = schemaObj.indexes.map(index => {
const arIndex = isMaybeReadonlyArray(index) ? index.slice(0) : [index];
/**
* Append primary key to indexes that do not contain the primaryKey.
* All indexes must have the primaryKey to ensure a deterministic sort order.
*/
if (!arIndex.includes(primaryPath)) {
arIndex.push(primaryPath);
}
// add _deleted flag to all indexes so we can query only non-deleted fields
// in RxDB itself
if (arIndex[0] !== '_deleted') {
arIndex.unshift('_deleted');
}
return arIndex;
});
if (useIndexes.length === 0) {
useIndexes.push(getDefaultIndex(primaryPath));
}
// we need this index for the getChangedDocumentsSince() method
useIndexes.push(['_meta.lwt', primaryPath]);
// also add the internalIndexes
if (schemaObj.internalIndexes) {
schemaObj.internalIndexes.map(idx => {
useIndexes.push(idx);
});
}
// make indexes unique
const hasIndex = new Set<string>();
useIndexes.filter(index => {
const indexStr = index.join(',');
if (hasIndex.has(indexStr)) {
return false;
} else {
hasIndex.add(indexStr);
return true;
}
});
schemaObj.indexes = useIndexes;
return schemaObj as any;
}
export const RX_META_SCHEMA: JsonSchema = {
type: 'object',
properties: {
/**
* The last-write time.
* Unix time in milliseconds.
*/
lwt: {
type: 'number',
/**
* We use 1 as minimum so that the value is never falsy.
*/
minimum: RX_META_LWT_MINIMUM,
maximum: 1000000000000000,
multipleOf: 0.01
}
},
/**
* Additional properties are allowed
* and can be used by plugins to set various flags.
*/
additionalProperties: true as any,
required: [
'lwt'
]
};
/**
* returns the final-fields of the schema
* @return field-names of the final-fields
*/
export function getFinalFields<T = any>(
jsonSchema: RxJsonSchema<T>
): string[] {
const ret = Object.keys(jsonSchema.properties)
.filter(key => (jsonSchema as any).properties[key].final);
// primary is also final
const primaryPath = getPrimaryFieldOfPrimaryKey(jsonSchema.primaryKey);
ret.push(primaryPath);
// fields of composite primary are final
if (typeof jsonSchema.primaryKey !== 'string') {
(jsonSchema.primaryKey as CompositePrimaryKey<T>).fields
.forEach(field => ret.push(field as string));
}
return ret;
}
/**
* fills all unset fields with default-values if set
* @hotPath
*/
export function fillObjectWithDefaults(rxSchema: RxSchema<any>, obj: any): any {
const defaultKeys = Object.keys(rxSchema.defaultValues);
for (let i = 0; i < defaultKeys.length; ++i) {
const key = defaultKeys[i];
if (!Object.prototype.hasOwnProperty.call(obj, key) || typeof obj[key] === 'undefined') {
obj[key] = rxSchema.defaultValues[key];
}
}
return obj;
}
export const DEFAULT_CHECKPOINT_SCHEMA: DeepReadonly<JsonSchema<RxStorageDefaultCheckpoint>> = {
type: 'object',
properties: {
id: {
type: 'string'
},
lwt: {
type: 'number'
}
},
required: [
'id',
'lwt'
],
additionalProperties: false
} as const;