@verdant-web/common 1.15.3-next.0 → 1.15.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/dist/cjs/EventSubscriber.d.ts +17 -17
  2. package/dist/cjs/EventSubscriber.js +66 -66
  3. package/dist/cjs/baseline.d.ts +5 -5
  4. package/dist/cjs/baseline.js +2 -2
  5. package/dist/cjs/batching.d.ts +42 -42
  6. package/dist/cjs/batching.js +91 -91
  7. package/dist/cjs/files.d.ts +19 -19
  8. package/dist/cjs/files.js +18 -18
  9. package/dist/cjs/index.d.ts +19 -19
  10. package/dist/cjs/index.js +41 -41
  11. package/dist/cjs/indexes.d.ts +17 -17
  12. package/dist/cjs/indexes.js +142 -142
  13. package/dist/cjs/indexes.test.d.ts +1 -1
  14. package/dist/cjs/indexes.test.js +127 -127
  15. package/dist/cjs/migration.d.ts +172 -172
  16. package/dist/cjs/migration.js +301 -301
  17. package/dist/cjs/oids.d.ts +100 -100
  18. package/dist/cjs/oids.js +457 -457
  19. package/dist/cjs/oids.test.d.ts +1 -1
  20. package/dist/cjs/oids.test.js +348 -348
  21. package/dist/cjs/operation.d.ts +139 -139
  22. package/dist/cjs/operation.js +545 -545
  23. package/dist/cjs/operation.test.d.ts +1 -1
  24. package/dist/cjs/operation.test.js +529 -529
  25. package/dist/cjs/patch.d.ts +26 -26
  26. package/dist/cjs/patch.js +226 -226
  27. package/dist/cjs/presence.d.ts +27 -27
  28. package/dist/cjs/presence.js +2 -2
  29. package/dist/cjs/protocol.d.ts +145 -145
  30. package/dist/cjs/protocol.js +3 -3
  31. package/dist/cjs/refs.d.ts +7 -7
  32. package/dist/cjs/refs.js +29 -29
  33. package/dist/cjs/replica.d.ts +28 -28
  34. package/dist/cjs/replica.js +28 -28
  35. package/dist/cjs/schema/defaults.test.d.ts +1 -1
  36. package/dist/cjs/schema/defaults.test.js +20 -20
  37. package/dist/cjs/schema/fields.d.ts +9 -9
  38. package/dist/cjs/schema/fields.js +144 -144
  39. package/dist/cjs/schema/index.d.ts +8 -8
  40. package/dist/cjs/schema/index.js +51 -51
  41. package/dist/cjs/schema/indexFilters.d.ts +6 -6
  42. package/dist/cjs/schema/indexFilters.js +30 -30
  43. package/dist/cjs/schema/types/collection.d.ts +23 -23
  44. package/dist/cjs/schema/types/collection.js +2 -2
  45. package/dist/cjs/schema/types/compounds.d.ts +11 -11
  46. package/dist/cjs/schema/types/compounds.js +2 -2
  47. package/dist/cjs/schema/types/fields.d.ts +75 -75
  48. package/dist/cjs/schema/types/fields.js +2 -2
  49. package/dist/cjs/schema/types/filters.d.ts +29 -29
  50. package/dist/cjs/schema/types/filters.js +2 -2
  51. package/dist/cjs/schema/types/shapes.d.ts +21 -21
  52. package/dist/cjs/schema/types/shapes.js +2 -2
  53. package/dist/cjs/schema/types/synthetics.d.ts +37 -37
  54. package/dist/cjs/schema/types/synthetics.js +2 -2
  55. package/dist/cjs/schema/types.d.ts +18 -18
  56. package/dist/cjs/schema/types.js +22 -22
  57. package/dist/cjs/schema/validation.d.ts +3 -3
  58. package/dist/cjs/schema/validation.js +86 -86
  59. package/dist/cjs/timestamp.d.ts +50 -50
  60. package/dist/cjs/timestamp.js +251 -251
  61. package/dist/cjs/timestamp.test.d.ts +1 -1
  62. package/dist/cjs/timestamp.test.js +104 -104
  63. package/dist/cjs/undo.d.ts +3 -3
  64. package/dist/cjs/undo.js +198 -198
  65. package/dist/cjs/utils.d.ts +20 -20
  66. package/dist/cjs/utils.js +145 -145
  67. package/dist/cjs/utils.test.d.ts +1 -1
  68. package/dist/cjs/utils.test.js +36 -36
  69. package/dist/esm/EventSubscriber.d.ts +17 -17
  70. package/dist/esm/EventSubscriber.js +62 -62
  71. package/dist/esm/baseline.d.ts +5 -5
  72. package/dist/esm/baseline.js +1 -1
  73. package/dist/esm/batching.d.ts +42 -42
  74. package/dist/esm/batching.js +86 -86
  75. package/dist/esm/files.d.ts +19 -19
  76. package/dist/esm/files.js +12 -12
  77. package/dist/esm/index.d.ts +19 -19
  78. package/dist/esm/index.js +16 -16
  79. package/dist/esm/indexes.d.ts +17 -17
  80. package/dist/esm/indexes.js +130 -130
  81. package/dist/esm/indexes.test.d.ts +1 -1
  82. package/dist/esm/indexes.test.js +125 -125
  83. package/dist/esm/migration.d.ts +172 -172
  84. package/dist/esm/migration.js +294 -294
  85. package/dist/esm/oids.d.ts +100 -100
  86. package/dist/esm/oids.js +422 -422
  87. package/dist/esm/oids.test.d.ts +1 -1
  88. package/dist/esm/oids.test.js +346 -346
  89. package/dist/esm/operation.d.ts +139 -139
  90. package/dist/esm/operation.js +530 -530
  91. package/dist/esm/operation.test.d.ts +1 -1
  92. package/dist/esm/operation.test.js +527 -527
  93. package/dist/esm/patch.d.ts +26 -26
  94. package/dist/esm/patch.js +222 -222
  95. package/dist/esm/presence.d.ts +27 -27
  96. package/dist/esm/presence.js +1 -1
  97. package/dist/esm/protocol.d.ts +145 -145
  98. package/dist/esm/protocol.js +2 -2
  99. package/dist/esm/refs.d.ts +7 -7
  100. package/dist/esm/refs.js +22 -22
  101. package/dist/esm/replica.d.ts +28 -28
  102. package/dist/esm/replica.js +25 -25
  103. package/dist/esm/schema/defaults.test.d.ts +1 -1
  104. package/dist/esm/schema/defaults.test.js +18 -18
  105. package/dist/esm/schema/fields.d.ts +9 -9
  106. package/dist/esm/schema/fields.js +133 -133
  107. package/dist/esm/schema/index.d.ts +8 -8
  108. package/dist/esm/schema/index.js +32 -32
  109. package/dist/esm/schema/indexFilters.d.ts +6 -6
  110. package/dist/esm/schema/indexFilters.js +22 -22
  111. package/dist/esm/schema/types/collection.d.ts +23 -23
  112. package/dist/esm/schema/types/collection.js +1 -1
  113. package/dist/esm/schema/types/compounds.d.ts +11 -11
  114. package/dist/esm/schema/types/compounds.js +1 -1
  115. package/dist/esm/schema/types/fields.d.ts +75 -75
  116. package/dist/esm/schema/types/fields.js +1 -1
  117. package/dist/esm/schema/types/filters.d.ts +29 -29
  118. package/dist/esm/schema/types/filters.js +1 -1
  119. package/dist/esm/schema/types/shapes.d.ts +21 -21
  120. package/dist/esm/schema/types/shapes.js +1 -1
  121. package/dist/esm/schema/types/synthetics.d.ts +37 -37
  122. package/dist/esm/schema/types/synthetics.js +1 -1
  123. package/dist/esm/schema/types.d.ts +18 -18
  124. package/dist/esm/schema/types.js +6 -6
  125. package/dist/esm/schema/validation.d.ts +3 -3
  126. package/dist/esm/schema/validation.js +81 -81
  127. package/dist/esm/timestamp.d.ts +50 -50
  128. package/dist/esm/timestamp.js +234 -234
  129. package/dist/esm/timestamp.test.d.ts +1 -1
  130. package/dist/esm/timestamp.test.js +102 -102
  131. package/dist/esm/undo.d.ts +3 -3
  132. package/dist/esm/undo.js +194 -194
  133. package/dist/esm/utils.d.ts +20 -20
  134. package/dist/esm/utils.js +127 -127
  135. package/dist/esm/utils.test.d.ts +1 -1
  136. package/dist/esm/utils.test.js +34 -34
  137. package/dist/tsconfig-cjs.tsbuildinfo +1 -1
  138. package/dist/tsconfig.tsbuildinfo +1 -1
  139. package/package.json +1 -1
  140. package/dist/cjs/files.test.d.ts +0 -1
  141. package/dist/cjs/files.test.js +0 -81
  142. package/dist/cjs/files.test.js.map +0 -1
  143. package/dist/cjs/schema/defaults.d.ts +0 -4
  144. package/dist/cjs/schema/defaults.js +0 -72
  145. package/dist/cjs/schema/defaults.js.map +0 -1
  146. package/dist/esm/files.test.d.ts +0 -1
  147. package/dist/esm/files.test.js +0 -79
  148. package/dist/esm/files.test.js.map +0 -1
  149. package/dist/esm/schema/defaults.d.ts +0 -4
  150. package/dist/esm/schema/defaults.js +0 -66
  151. package/dist/esm/schema/defaults.js.map +0 -1
package/dist/cjs/index.js CHANGED
@@ -1,42 +1,42 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.compareRefs = exports.isRef = exports.makeFileRef = exports.makeObjectRef = exports.createMigration = exports.createDefaultMigration = exports.migrationRange = exports.migrate = void 0;
18
- __exportStar(require("./protocol.js"), exports);
19
- __exportStar(require("./timestamp.js"), exports);
20
- __exportStar(require("./operation.js"), exports);
21
- __exportStar(require("./baseline.js"), exports);
22
- __exportStar(require("./replica.js"), exports);
23
- __exportStar(require("./schema/index.js"), exports);
24
- __exportStar(require("./utils.js"), exports);
25
- __exportStar(require("./indexes.js"), exports);
26
- var migration_js_1 = require("./migration.js");
27
- Object.defineProperty(exports, "migrate", { enumerable: true, get: function () { return migration_js_1.migrate; } });
28
- Object.defineProperty(exports, "migrationRange", { enumerable: true, get: function () { return migration_js_1.migrationRange; } });
29
- Object.defineProperty(exports, "createDefaultMigration", { enumerable: true, get: function () { return migration_js_1.createDefaultMigration; } });
30
- Object.defineProperty(exports, "createMigration", { enumerable: true, get: function () { return migration_js_1.createMigration; } });
31
- __exportStar(require("./patch.js"), exports);
32
- __exportStar(require("./oids.js"), exports);
33
- __exportStar(require("./EventSubscriber.js"), exports);
34
- __exportStar(require("./undo.js"), exports);
35
- __exportStar(require("./batching.js"), exports);
36
- __exportStar(require("./files.js"), exports);
37
- var refs_js_1 = require("./refs.js");
38
- Object.defineProperty(exports, "makeObjectRef", { enumerable: true, get: function () { return refs_js_1.makeObjectRef; } });
39
- Object.defineProperty(exports, "makeFileRef", { enumerable: true, get: function () { return refs_js_1.makeFileRef; } });
40
- Object.defineProperty(exports, "isRef", { enumerable: true, get: function () { return refs_js_1.isRef; } });
41
- Object.defineProperty(exports, "compareRefs", { enumerable: true, get: function () { return refs_js_1.compareRefs; } });
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.compareRefs = exports.isRef = exports.makeFileRef = exports.makeObjectRef = exports.createMigration = exports.createDefaultMigration = exports.migrationRange = exports.migrate = void 0;
18
+ __exportStar(require("./protocol.js"), exports);
19
+ __exportStar(require("./timestamp.js"), exports);
20
+ __exportStar(require("./operation.js"), exports);
21
+ __exportStar(require("./baseline.js"), exports);
22
+ __exportStar(require("./replica.js"), exports);
23
+ __exportStar(require("./schema/index.js"), exports);
24
+ __exportStar(require("./utils.js"), exports);
25
+ __exportStar(require("./indexes.js"), exports);
26
+ var migration_js_1 = require("./migration.js");
27
+ Object.defineProperty(exports, "migrate", { enumerable: true, get: function () { return migration_js_1.migrate; } });
28
+ Object.defineProperty(exports, "migrationRange", { enumerable: true, get: function () { return migration_js_1.migrationRange; } });
29
+ Object.defineProperty(exports, "createDefaultMigration", { enumerable: true, get: function () { return migration_js_1.createDefaultMigration; } });
30
+ Object.defineProperty(exports, "createMigration", { enumerable: true, get: function () { return migration_js_1.createMigration; } });
31
+ __exportStar(require("./patch.js"), exports);
32
+ __exportStar(require("./oids.js"), exports);
33
+ __exportStar(require("./EventSubscriber.js"), exports);
34
+ __exportStar(require("./undo.js"), exports);
35
+ __exportStar(require("./batching.js"), exports);
36
+ __exportStar(require("./files.js"), exports);
37
+ var refs_js_1 = require("./refs.js");
38
+ Object.defineProperty(exports, "makeObjectRef", { enumerable: true, get: function () { return refs_js_1.makeObjectRef; } });
39
+ Object.defineProperty(exports, "makeFileRef", { enumerable: true, get: function () { return refs_js_1.makeFileRef; } });
40
+ Object.defineProperty(exports, "isRef", { enumerable: true, get: function () { return refs_js_1.isRef; } });
41
+ Object.defineProperty(exports, "compareRefs", { enumerable: true, get: function () { return refs_js_1.compareRefs; } });
42
42
  //# sourceMappingURL=index.js.map
@@ -1,17 +1,17 @@
1
- import { StorageCollectionSchema, StorageDirectSyntheticSchema } from './index.js';
2
- export declare const COMPOUND_INDEX_SEPARATOR = "\uFFFFFE";
3
- export declare const COMPOUND_INDEX_LOWER_BOUND_SEPARATOR = "\0";
4
- export declare const COMPOUND_INDEX_UPPER_BOUND_SEPARATOR = "\uFFFFFF";
5
- declare type IndexableFieldValue = string | number | boolean | any[];
6
- export declare type CompoundIndexValue = string | string[];
7
- export declare function createCompoundIndexValue(...fields: IndexableFieldValue[]): CompoundIndexValue;
8
- export declare function createUpperBoundIndexValue(...fields: IndexableFieldValue[]): string;
9
- export declare function createLowerBoundIndexValue(...fields: IndexableFieldValue[]): string;
10
- export declare function isDirectSynthetic(index: any): index is StorageDirectSyntheticSchema<any>;
11
- export declare function computeSynthetics(schema: StorageCollectionSchema, obj: any): Record<string, any>;
12
- export declare function computeCompoundIndices(schema: StorageCollectionSchema<any, any, any>, doc: any): any;
13
- export declare function getIndexValues(schema: StorageCollectionSchema<any, any, any>, doc: any): any;
14
- export declare function assignIndexValues(schema: StorageCollectionSchema<any, any, any>, doc: any): any;
15
- export declare const NULL_INDEX_VALUE = "null";
16
- export declare function sanitizeIndexValue(value: unknown): string | number | (string | number)[];
17
- export {};
1
+ import { StorageCollectionSchema, StorageDirectSyntheticSchema } from './index.js';
2
+ export declare const COMPOUND_INDEX_SEPARATOR = "\uFFFFFE";
3
+ export declare const COMPOUND_INDEX_LOWER_BOUND_SEPARATOR = "\0";
4
+ export declare const COMPOUND_INDEX_UPPER_BOUND_SEPARATOR = "\uFFFFFF";
5
+ declare type IndexableFieldValue = string | number | boolean | any[];
6
+ export declare type CompoundIndexValue = string | string[];
7
+ export declare function createCompoundIndexValue(...fields: IndexableFieldValue[]): CompoundIndexValue;
8
+ export declare function createUpperBoundIndexValue(...fields: IndexableFieldValue[]): string;
9
+ export declare function createLowerBoundIndexValue(...fields: IndexableFieldValue[]): string;
10
+ export declare function isDirectSynthetic(index: any): index is StorageDirectSyntheticSchema<any>;
11
+ export declare function computeSynthetics(schema: StorageCollectionSchema, obj: any): Record<string, any>;
12
+ export declare function computeCompoundIndices(schema: StorageCollectionSchema<any, any, any>, doc: any): any;
13
+ export declare function getIndexValues(schema: StorageCollectionSchema<any, any, any>, doc: any): any;
14
+ export declare function assignIndexValues(schema: StorageCollectionSchema<any, any, any>, doc: any): any;
15
+ export declare const NULL_INDEX_VALUE = "null";
16
+ export declare function sanitizeIndexValue(value: unknown): string | number | (string | number)[];
17
+ export {};
@@ -1,143 +1,143 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sanitizeIndexValue = exports.NULL_INDEX_VALUE = exports.assignIndexValues = exports.getIndexValues = exports.computeCompoundIndices = exports.computeSynthetics = exports.isDirectSynthetic = exports.createLowerBoundIndexValue = exports.createUpperBoundIndexValue = exports.createCompoundIndexValue = exports.COMPOUND_INDEX_UPPER_BOUND_SEPARATOR = exports.COMPOUND_INDEX_LOWER_BOUND_SEPARATOR = exports.COMPOUND_INDEX_SEPARATOR = void 0;
4
- const index_js_1 = require("./index.js");
5
- // unlikely to be used unicode character
6
- exports.COMPOUND_INDEX_SEPARATOR = '\uFFFFFE';
7
- // 1 lower than separator
8
- exports.COMPOUND_INDEX_LOWER_BOUND_SEPARATOR = '\u0000';
9
- // 1 higher than separator
10
- exports.COMPOUND_INDEX_UPPER_BOUND_SEPARATOR = '\uFFFFFF';
11
- function createCompoundIndexValue(...fields) {
12
- const value = expandArrayIndex(fields);
13
- if (value.length === 1) {
14
- return value[0];
15
- }
16
- return value;
17
- }
18
- exports.createCompoundIndexValue = createCompoundIndexValue;
19
- function createUpperBoundIndexValue(...fields) {
20
- return (fields.join(exports.COMPOUND_INDEX_SEPARATOR) +
21
- `${exports.COMPOUND_INDEX_UPPER_BOUND_SEPARATOR}`);
22
- }
23
- exports.createUpperBoundIndexValue = createUpperBoundIndexValue;
24
- function createLowerBoundIndexValue(...fields) {
25
- return (fields.join(exports.COMPOUND_INDEX_SEPARATOR) +
26
- `${exports.COMPOUND_INDEX_SEPARATOR}${exports.COMPOUND_INDEX_LOWER_BOUND_SEPARATOR}`);
27
- }
28
- exports.createLowerBoundIndexValue = createLowerBoundIndexValue;
29
- /**
30
- * Whenever an array value is included in a compound index, we have to expand
31
- * the value to include all permutations of values in the array.
32
- * For example if we had an index id + tags on a document
33
- *
34
- * {
35
- * id: '1',
36
- * tags: ['a', 'b']
37
- * }
38
- *
39
- * we want to create an index:
40
- *
41
- * id_tags: ['1#a', '1#b']
42
- *
43
- * If multiple arrays are indexed we have to exponentially expand...
44
- *
45
- * id_tags_tags2: ['1#a#a', '1#a#b', '1#b#a', '1#b#b']
46
- *
47
- * To generalize this we construct a 2-level array of strings.
48
- * Iterating over indexed values, we expand each item into N items if
49
- * the current value is an array.
50
- *
51
- * Then we combine all nested arrays into compound index values.
52
- * This will produce an array of 1 element if none of the indexed values
53
- * were arrays. The caller should unwrap that.
54
- *
55
- * This function also deduplicates values.
56
- */
57
- function expandArrayIndex(fields) {
58
- let value = [[]];
59
- for (const field of fields) {
60
- if (Array.isArray(field)) {
61
- const newValue = [];
62
- for (const previousValue of value) {
63
- for (const fieldValue of field) {
64
- newValue.push(previousValue.concat(fieldValue));
65
- }
66
- }
67
- value = newValue;
68
- }
69
- else {
70
- for (const item of value) {
71
- item.push(`${field}`);
72
- }
73
- }
74
- }
75
- return Array.from(new Set(value.map((item) => {
76
- return item.join(exports.COMPOUND_INDEX_SEPARATOR);
77
- })));
78
- }
79
- function isDirectSynthetic(index) {
80
- return !!index.field;
81
- }
82
- exports.isDirectSynthetic = isDirectSynthetic;
83
- function computeSynthetics(schema, obj) {
84
- const result = {};
85
- for (const [name, property] of Object.entries(schema.indexes || {})) {
86
- const index = property;
87
- if (isDirectSynthetic(index)) {
88
- result[name] = sanitizeIndexValue(obj[index.field]);
89
- }
90
- else {
91
- result[name] = sanitizeIndexValue(index.compute(obj));
92
- }
93
- }
94
- return result;
95
- }
96
- exports.computeSynthetics = computeSynthetics;
97
- function computeCompoundIndices(schema, doc) {
98
- return Object.entries(schema.compounds || {}).reduce((acc, [indexKey, index]) => {
99
- acc[indexKey] = createCompoundIndexValue(...index.of.map((key) => doc[key]));
100
- return acc;
101
- }, {});
102
- }
103
- exports.computeCompoundIndices = computeCompoundIndices;
104
- function computeIndexedFields(schema, doc) {
105
- return Object.entries(schema.fields).reduce((acc, [key, field]) => {
106
- if ((0, index_js_1.isIndexed)(field)) {
107
- acc[key] = sanitizeIndexValue(doc[key]);
108
- }
109
- return acc;
110
- }, {});
111
- }
112
- function getIndexValues(schema, doc) {
113
- return Object.assign(Object.assign(Object.assign(Object.assign({ [schema.primaryKey]: doc[schema.primaryKey] }, computeIndexedFields(schema, doc)), computeSynthetics(schema, doc)), computeCompoundIndices(schema, doc)), { '@@@snapshot': doc });
114
- }
115
- exports.getIndexValues = getIndexValues;
116
- function assignIndexValues(schema, doc) {
117
- Object.assign(doc, computeSynthetics(schema, doc));
118
- Object.assign(doc, computeCompoundIndices(schema, doc));
119
- return doc;
120
- }
121
- exports.assignIndexValues = assignIndexValues;
122
- exports.NULL_INDEX_VALUE = 'null';
123
- function sanitizeIndexValue(value) {
124
- if (value === null) {
125
- return exports.NULL_INDEX_VALUE;
126
- }
127
- if (typeof value === 'string' || typeof value === 'number') {
128
- return value;
129
- }
130
- if (typeof value === 'boolean' || value === null) {
131
- return `${value}`;
132
- }
133
- if (value === undefined) {
134
- // this shouldn't happen ,but for resiliency...
135
- return 'undefined';
136
- }
137
- if (Array.isArray(value)) {
138
- return value.map(sanitizeIndexValue);
139
- }
140
- throw new Error(`Unsupported index value: ${value}`);
141
- }
142
- exports.sanitizeIndexValue = sanitizeIndexValue;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sanitizeIndexValue = exports.NULL_INDEX_VALUE = exports.assignIndexValues = exports.getIndexValues = exports.computeCompoundIndices = exports.computeSynthetics = exports.isDirectSynthetic = exports.createLowerBoundIndexValue = exports.createUpperBoundIndexValue = exports.createCompoundIndexValue = exports.COMPOUND_INDEX_UPPER_BOUND_SEPARATOR = exports.COMPOUND_INDEX_LOWER_BOUND_SEPARATOR = exports.COMPOUND_INDEX_SEPARATOR = void 0;
4
+ const index_js_1 = require("./index.js");
5
+ // unlikely to be used unicode character
6
+ exports.COMPOUND_INDEX_SEPARATOR = '\uFFFFFE';
7
+ // 1 lower than separator
8
+ exports.COMPOUND_INDEX_LOWER_BOUND_SEPARATOR = '\u0000';
9
+ // 1 higher than separator
10
+ exports.COMPOUND_INDEX_UPPER_BOUND_SEPARATOR = '\uFFFFFF';
11
+ function createCompoundIndexValue(...fields) {
12
+ const value = expandArrayIndex(fields);
13
+ if (value.length === 1) {
14
+ return value[0];
15
+ }
16
+ return value;
17
+ }
18
+ exports.createCompoundIndexValue = createCompoundIndexValue;
19
+ function createUpperBoundIndexValue(...fields) {
20
+ return (fields.join(exports.COMPOUND_INDEX_SEPARATOR) +
21
+ `${exports.COMPOUND_INDEX_UPPER_BOUND_SEPARATOR}`);
22
+ }
23
+ exports.createUpperBoundIndexValue = createUpperBoundIndexValue;
24
+ function createLowerBoundIndexValue(...fields) {
25
+ return (fields.join(exports.COMPOUND_INDEX_SEPARATOR) +
26
+ `${exports.COMPOUND_INDEX_SEPARATOR}${exports.COMPOUND_INDEX_LOWER_BOUND_SEPARATOR}`);
27
+ }
28
+ exports.createLowerBoundIndexValue = createLowerBoundIndexValue;
29
+ /**
30
+ * Whenever an array value is included in a compound index, we have to expand
31
+ * the value to include all permutations of values in the array.
32
+ * For example if we had an index id + tags on a document
33
+ *
34
+ * {
35
+ * id: '1',
36
+ * tags: ['a', 'b']
37
+ * }
38
+ *
39
+ * we want to create an index:
40
+ *
41
+ * id_tags: ['1#a', '1#b']
42
+ *
43
+ * If multiple arrays are indexed we have to exponentially expand...
44
+ *
45
+ * id_tags_tags2: ['1#a#a', '1#a#b', '1#b#a', '1#b#b']
46
+ *
47
+ * To generalize this we construct a 2-level array of strings.
48
+ * Iterating over indexed values, we expand each item into N items if
49
+ * the current value is an array.
50
+ *
51
+ * Then we combine all nested arrays into compound index values.
52
+ * This will produce an array of 1 element if none of the indexed values
53
+ * were arrays. The caller should unwrap that.
54
+ *
55
+ * This function also deduplicates values.
56
+ */
57
+ function expandArrayIndex(fields) {
58
+ let value = [[]];
59
+ for (const field of fields) {
60
+ if (Array.isArray(field)) {
61
+ const newValue = [];
62
+ for (const previousValue of value) {
63
+ for (const fieldValue of field) {
64
+ newValue.push(previousValue.concat(fieldValue));
65
+ }
66
+ }
67
+ value = newValue;
68
+ }
69
+ else {
70
+ for (const item of value) {
71
+ item.push(`${field}`);
72
+ }
73
+ }
74
+ }
75
+ return Array.from(new Set(value.map((item) => {
76
+ return item.join(exports.COMPOUND_INDEX_SEPARATOR);
77
+ })));
78
+ }
79
+ function isDirectSynthetic(index) {
80
+ return !!index.field;
81
+ }
82
+ exports.isDirectSynthetic = isDirectSynthetic;
83
+ function computeSynthetics(schema, obj) {
84
+ const result = {};
85
+ for (const [name, property] of Object.entries(schema.indexes || {})) {
86
+ const index = property;
87
+ if (isDirectSynthetic(index)) {
88
+ result[name] = sanitizeIndexValue(obj[index.field]);
89
+ }
90
+ else {
91
+ result[name] = sanitizeIndexValue(index.compute(obj));
92
+ }
93
+ }
94
+ return result;
95
+ }
96
+ exports.computeSynthetics = computeSynthetics;
97
+ function computeCompoundIndices(schema, doc) {
98
+ return Object.entries(schema.compounds || {}).reduce((acc, [indexKey, index]) => {
99
+ acc[indexKey] = createCompoundIndexValue(...index.of.map((key) => doc[key]));
100
+ return acc;
101
+ }, {});
102
+ }
103
+ exports.computeCompoundIndices = computeCompoundIndices;
104
+ function computeIndexedFields(schema, doc) {
105
+ return Object.entries(schema.fields).reduce((acc, [key, field]) => {
106
+ if ((0, index_js_1.isIndexed)(field)) {
107
+ acc[key] = sanitizeIndexValue(doc[key]);
108
+ }
109
+ return acc;
110
+ }, {});
111
+ }
112
+ function getIndexValues(schema, doc) {
113
+ return Object.assign(Object.assign(Object.assign(Object.assign({ [schema.primaryKey]: doc[schema.primaryKey] }, computeIndexedFields(schema, doc)), computeSynthetics(schema, doc)), computeCompoundIndices(schema, doc)), { '@@@snapshot': doc });
114
+ }
115
+ exports.getIndexValues = getIndexValues;
116
+ function assignIndexValues(schema, doc) {
117
+ Object.assign(doc, computeSynthetics(schema, doc));
118
+ Object.assign(doc, computeCompoundIndices(schema, doc));
119
+ return doc;
120
+ }
121
+ exports.assignIndexValues = assignIndexValues;
122
+ exports.NULL_INDEX_VALUE = 'null';
123
+ function sanitizeIndexValue(value) {
124
+ if (value === null) {
125
+ return exports.NULL_INDEX_VALUE;
126
+ }
127
+ if (typeof value === 'string' || typeof value === 'number') {
128
+ return value;
129
+ }
130
+ if (typeof value === 'boolean' || value === null) {
131
+ return `${value}`;
132
+ }
133
+ if (value === undefined) {
134
+ // this shouldn't happen ,but for resiliency...
135
+ return 'undefined';
136
+ }
137
+ if (Array.isArray(value)) {
138
+ return value.map(sanitizeIndexValue);
139
+ }
140
+ throw new Error(`Unsupported index value: ${value}`);
141
+ }
142
+ exports.sanitizeIndexValue = sanitizeIndexValue;
143
143
  //# sourceMappingURL=indexes.js.map
@@ -1 +1 @@
1
- export {};
1
+ export {};