@verdant-web/common 1.11.0
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.
- package/LICENSE +21 -0
- package/dist/cjs/EventSubscriber.d.ts +17 -0
- package/dist/cjs/EventSubscriber.js +67 -0
- package/dist/cjs/EventSubscriber.js.map +1 -0
- package/dist/cjs/baseline.d.ts +5 -0
- package/dist/cjs/baseline.js +3 -0
- package/dist/cjs/baseline.js.map +1 -0
- package/dist/cjs/batching.d.ts +41 -0
- package/dist/cjs/batching.js +89 -0
- package/dist/cjs/batching.js.map +1 -0
- package/dist/cjs/files.d.ts +19 -0
- package/dist/cjs/files.js +19 -0
- package/dist/cjs/files.js.map +1 -0
- package/dist/cjs/files.test.d.ts +1 -0
- package/dist/cjs/files.test.js +81 -0
- package/dist/cjs/files.test.js.map +1 -0
- package/dist/cjs/index.d.ts +18 -0
- package/dist/cjs/index.js +36 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/indexes.d.ts +14 -0
- package/dist/cjs/indexes.js +116 -0
- package/dist/cjs/indexes.js.map +1 -0
- package/dist/cjs/indexes.test.d.ts +1 -0
- package/dist/cjs/indexes.test.js +128 -0
- package/dist/cjs/indexes.test.js.map +1 -0
- package/dist/cjs/migration.d.ts +98 -0
- package/dist/cjs/migration.js +128 -0
- package/dist/cjs/migration.js.map +1 -0
- package/dist/cjs/oids.d.ts +87 -0
- package/dist/cjs/oids.js +375 -0
- package/dist/cjs/oids.js.map +1 -0
- package/dist/cjs/oids.test.d.ts +1 -0
- package/dist/cjs/oids.test.js +282 -0
- package/dist/cjs/oids.test.js.map +1 -0
- package/dist/cjs/operation.d.ts +139 -0
- package/dist/cjs/operation.js +533 -0
- package/dist/cjs/operation.js.map +1 -0
- package/dist/cjs/operation.test.d.ts +1 -0
- package/dist/cjs/operation.test.js +827 -0
- package/dist/cjs/operation.test.js.map +1 -0
- package/dist/cjs/patch.d.ts +26 -0
- package/dist/cjs/patch.js +227 -0
- package/dist/cjs/patch.js.map +1 -0
- package/dist/cjs/presence.d.ts +27 -0
- package/dist/cjs/presence.js +3 -0
- package/dist/cjs/presence.js.map +1 -0
- package/dist/cjs/protocol.d.ts +145 -0
- package/dist/cjs/protocol.js +4 -0
- package/dist/cjs/protocol.js.map +1 -0
- package/dist/cjs/refs.d.ts +5 -0
- package/dist/cjs/refs.js +22 -0
- package/dist/cjs/refs.js.map +1 -0
- package/dist/cjs/replica.d.ts +28 -0
- package/dist/cjs/replica.js +29 -0
- package/dist/cjs/replica.js.map +1 -0
- package/dist/cjs/schema/defaults.d.ts +4 -0
- package/dist/cjs/schema/defaults.js +72 -0
- package/dist/cjs/schema/defaults.js.map +1 -0
- package/dist/cjs/schema/defaults.test.d.ts +1 -0
- package/dist/cjs/schema/defaults.test.js +21 -0
- package/dist/cjs/schema/defaults.test.js.map +1 -0
- package/dist/cjs/schema/index.d.ts +8 -0
- package/dist/cjs/schema/index.js +29 -0
- package/dist/cjs/schema/index.js.map +1 -0
- package/dist/cjs/schema/indexFilters.d.ts +6 -0
- package/dist/cjs/schema/indexFilters.js +31 -0
- package/dist/cjs/schema/indexFilters.js.map +1 -0
- package/dist/cjs/schema/types/collection.d.ts +51 -0
- package/dist/cjs/schema/types/collection.js +3 -0
- package/dist/cjs/schema/types/collection.js.map +1 -0
- package/dist/cjs/schema/types/compounds.d.ts +12 -0
- package/dist/cjs/schema/types/compounds.js +3 -0
- package/dist/cjs/schema/types/compounds.js.map +1 -0
- package/dist/cjs/schema/types/fields.d.ts +59 -0
- package/dist/cjs/schema/types/fields.js +3 -0
- package/dist/cjs/schema/types/fields.js.map +1 -0
- package/dist/cjs/schema/types/filters.d.ts +29 -0
- package/dist/cjs/schema/types/filters.js +3 -0
- package/dist/cjs/schema/types/filters.js.map +1 -0
- package/dist/cjs/schema/types/shapes.d.ts +21 -0
- package/dist/cjs/schema/types/shapes.js +3 -0
- package/dist/cjs/schema/types/shapes.js.map +1 -0
- package/dist/cjs/schema/types/synthetics.d.ts +28 -0
- package/dist/cjs/schema/types/synthetics.js +3 -0
- package/dist/cjs/schema/types/synthetics.js.map +1 -0
- package/dist/cjs/schema/types.d.ts +18 -0
- package/dist/cjs/schema/types.js +23 -0
- package/dist/cjs/schema/types.js.map +1 -0
- package/dist/cjs/timestamp.d.ts +48 -0
- package/dist/cjs/timestamp.js +244 -0
- package/dist/cjs/timestamp.js.map +1 -0
- package/dist/cjs/timestamp.test.d.ts +1 -0
- package/dist/cjs/timestamp.test.js +94 -0
- package/dist/cjs/timestamp.test.js.map +1 -0
- package/dist/cjs/undo.d.ts +3 -0
- package/dist/cjs/undo.js +199 -0
- package/dist/cjs/undo.js.map +1 -0
- package/dist/cjs/utils.d.ts +15 -0
- package/dist/cjs/utils.js +128 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/cjs/utils.test.d.ts +1 -0
- package/dist/cjs/utils.test.js +37 -0
- package/dist/cjs/utils.test.js.map +1 -0
- package/dist/esm/EventSubscriber.d.ts +17 -0
- package/dist/esm/EventSubscriber.js +63 -0
- package/dist/esm/EventSubscriber.js.map +1 -0
- package/dist/esm/baseline.d.ts +5 -0
- package/dist/esm/baseline.js +2 -0
- package/dist/esm/baseline.js.map +1 -0
- package/dist/esm/batching.d.ts +41 -0
- package/dist/esm/batching.js +84 -0
- package/dist/esm/batching.js.map +1 -0
- package/dist/esm/files.d.ts +19 -0
- package/dist/esm/files.js +13 -0
- package/dist/esm/files.js.map +1 -0
- package/dist/esm/files.test.d.ts +1 -0
- package/dist/esm/files.test.js +79 -0
- package/dist/esm/files.test.js.map +1 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.js +16 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/indexes.d.ts +14 -0
- package/dist/esm/indexes.js +106 -0
- package/dist/esm/indexes.js.map +1 -0
- package/dist/esm/indexes.test.d.ts +1 -0
- package/dist/esm/indexes.test.js +126 -0
- package/dist/esm/indexes.test.js.map +1 -0
- package/dist/esm/migration.d.ts +98 -0
- package/dist/esm/migration.js +122 -0
- package/dist/esm/migration.js.map +1 -0
- package/dist/esm/oids.d.ts +87 -0
- package/dist/esm/oids.js +348 -0
- package/dist/esm/oids.js.map +1 -0
- package/dist/esm/oids.test.d.ts +1 -0
- package/dist/esm/oids.test.js +280 -0
- package/dist/esm/oids.test.js.map +1 -0
- package/dist/esm/operation.d.ts +139 -0
- package/dist/esm/operation.js +518 -0
- package/dist/esm/operation.js.map +1 -0
- package/dist/esm/operation.test.d.ts +1 -0
- package/dist/esm/operation.test.js +825 -0
- package/dist/esm/operation.test.js.map +1 -0
- package/dist/esm/patch.d.ts +26 -0
- package/dist/esm/patch.js +223 -0
- package/dist/esm/patch.js.map +1 -0
- package/dist/esm/presence.d.ts +27 -0
- package/dist/esm/presence.js +2 -0
- package/dist/esm/presence.js.map +1 -0
- package/dist/esm/protocol.d.ts +145 -0
- package/dist/esm/protocol.js +3 -0
- package/dist/esm/protocol.js.map +1 -0
- package/dist/esm/refs.d.ts +5 -0
- package/dist/esm/refs.js +17 -0
- package/dist/esm/refs.js.map +1 -0
- package/dist/esm/replica.d.ts +28 -0
- package/dist/esm/replica.js +26 -0
- package/dist/esm/replica.js.map +1 -0
- package/dist/esm/schema/defaults.d.ts +4 -0
- package/dist/esm/schema/defaults.js +66 -0
- package/dist/esm/schema/defaults.js.map +1 -0
- package/dist/esm/schema/defaults.test.d.ts +1 -0
- package/dist/esm/schema/defaults.test.js +19 -0
- package/dist/esm/schema/defaults.test.js.map +1 -0
- package/dist/esm/schema/index.d.ts +8 -0
- package/dist/esm/schema/index.js +10 -0
- package/dist/esm/schema/index.js.map +1 -0
- package/dist/esm/schema/indexFilters.d.ts +6 -0
- package/dist/esm/schema/indexFilters.js +23 -0
- package/dist/esm/schema/indexFilters.js.map +1 -0
- package/dist/esm/schema/types/collection.d.ts +51 -0
- package/dist/esm/schema/types/collection.js +2 -0
- package/dist/esm/schema/types/collection.js.map +1 -0
- package/dist/esm/schema/types/compounds.d.ts +12 -0
- package/dist/esm/schema/types/compounds.js +2 -0
- package/dist/esm/schema/types/compounds.js.map +1 -0
- package/dist/esm/schema/types/fields.d.ts +59 -0
- package/dist/esm/schema/types/fields.js +2 -0
- package/dist/esm/schema/types/fields.js.map +1 -0
- package/dist/esm/schema/types/filters.d.ts +29 -0
- package/dist/esm/schema/types/filters.js +2 -0
- package/dist/esm/schema/types/filters.js.map +1 -0
- package/dist/esm/schema/types/shapes.d.ts +21 -0
- package/dist/esm/schema/types/shapes.js +2 -0
- package/dist/esm/schema/types/shapes.js.map +1 -0
- package/dist/esm/schema/types/synthetics.d.ts +28 -0
- package/dist/esm/schema/types/synthetics.js +2 -0
- package/dist/esm/schema/types/synthetics.js.map +1 -0
- package/dist/esm/schema/types.d.ts +18 -0
- package/dist/esm/schema/types.js +7 -0
- package/dist/esm/schema/types.js.map +1 -0
- package/dist/esm/timestamp.d.ts +48 -0
- package/dist/esm/timestamp.js +229 -0
- package/dist/esm/timestamp.js.map +1 -0
- package/dist/esm/timestamp.test.d.ts +1 -0
- package/dist/esm/timestamp.test.js +92 -0
- package/dist/esm/timestamp.test.js.map +1 -0
- package/dist/esm/undo.d.ts +3 -0
- package/dist/esm/undo.js +195 -0
- package/dist/esm/undo.js.map +1 -0
- package/dist/esm/utils.d.ts +15 -0
- package/dist/esm/utils.js +114 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/esm/utils.test.d.ts +1 -0
- package/dist/esm/utils.test.js +35 -0
- package/dist/esm/utils.test.js.map +1 -0
- package/dist/tsconfig-cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +38 -0
- package/src/EventSubscriber.ts +81 -0
- package/src/baseline.ts +9 -0
- package/src/batching.ts +135 -0
- package/src/files.ts +34 -0
- package/src/index.ts +26 -0
- package/src/indexes.test.ts +158 -0
- package/src/indexes.ts +151 -0
- package/src/migration.ts +391 -0
- package/src/oids.test.ts +395 -0
- package/src/oids.ts +442 -0
- package/src/operation.test.ts +966 -0
- package/src/operation.ts +755 -0
- package/src/patch.ts +277 -0
- package/src/presence.ts +29 -0
- package/src/protocol.ts +184 -0
- package/src/refs.ts +16 -0
- package/src/replica.ts +29 -0
- package/src/schema/defaults.test.ts +19 -0
- package/src/schema/defaults.ts +72 -0
- package/src/schema/index.ts +28 -0
- package/src/schema/indexFilters.ts +49 -0
- package/src/schema/types/collection.ts +133 -0
- package/src/schema/types/compounds.ts +33 -0
- package/src/schema/types/fields.ts +85 -0
- package/src/schema/types/filters.ts +39 -0
- package/src/schema/types/shapes.ts +83 -0
- package/src/schema/types/synthetics.ts +45 -0
- package/src/schema/types.ts +28 -0
- package/src/timestamp.test.ts +113 -0
- package/src/timestamp.ts +284 -0
- package/src/undo.ts +205 -0
- package/src/utils.test.ts +38 -0
- package/src/utils.ts +137 -0
package/src/batching.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
export class Batcher<T, UserData = any> {
|
|
2
|
+
private batches: Map<string, Batch<T, UserData>> = new Map();
|
|
3
|
+
|
|
4
|
+
constructor(
|
|
5
|
+
private flusher: (items: T[], batchKey: string, userData: UserData) => any,
|
|
6
|
+
) {}
|
|
7
|
+
|
|
8
|
+
add({
|
|
9
|
+
key,
|
|
10
|
+
userData,
|
|
11
|
+
items,
|
|
12
|
+
max,
|
|
13
|
+
timeout,
|
|
14
|
+
}: {
|
|
15
|
+
key: string;
|
|
16
|
+
userData?: UserData;
|
|
17
|
+
items: T[];
|
|
18
|
+
max?: number | null;
|
|
19
|
+
timeout?: number | null;
|
|
20
|
+
}) {
|
|
21
|
+
let batch = this.batches.get(key);
|
|
22
|
+
if (!batch) {
|
|
23
|
+
batch = new Batch({
|
|
24
|
+
max: max || null,
|
|
25
|
+
startedAt: Date.now(),
|
|
26
|
+
userData,
|
|
27
|
+
timeout: timeout || null,
|
|
28
|
+
flusher: this.flusher,
|
|
29
|
+
key,
|
|
30
|
+
});
|
|
31
|
+
this.batches.set(key, batch);
|
|
32
|
+
}
|
|
33
|
+
batch.update({
|
|
34
|
+
items,
|
|
35
|
+
max,
|
|
36
|
+
timeout,
|
|
37
|
+
userData,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return batch;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
flush = (key: string) => {
|
|
44
|
+
const batch = this.batches.get(key);
|
|
45
|
+
if (!batch) return;
|
|
46
|
+
|
|
47
|
+
return batch.flush();
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
discard = (key: string) => {
|
|
51
|
+
const batch = this.batches.get(key);
|
|
52
|
+
if (!batch) return;
|
|
53
|
+
|
|
54
|
+
batch.discard();
|
|
55
|
+
this.batches.delete(key);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class Batch<T, UserData = any> {
|
|
60
|
+
items: Array<T> = [];
|
|
61
|
+
max: number | null;
|
|
62
|
+
startedAt: number;
|
|
63
|
+
timeout: number | null;
|
|
64
|
+
flushTimeout?: NodeJS.Timeout;
|
|
65
|
+
userData?: any;
|
|
66
|
+
flusher: (items: T[], batchKey: string, userData: UserData) => any;
|
|
67
|
+
key: string;
|
|
68
|
+
|
|
69
|
+
constructor({
|
|
70
|
+
max,
|
|
71
|
+
startedAt,
|
|
72
|
+
timeout,
|
|
73
|
+
userData,
|
|
74
|
+
flusher,
|
|
75
|
+
key,
|
|
76
|
+
}: {
|
|
77
|
+
key: string;
|
|
78
|
+
max: number | null;
|
|
79
|
+
startedAt: number;
|
|
80
|
+
timeout: number | null;
|
|
81
|
+
userData?: UserData;
|
|
82
|
+
flusher: (items: T[], batchKey: string, userData: UserData) => any;
|
|
83
|
+
}) {
|
|
84
|
+
this.max = max;
|
|
85
|
+
this.startedAt = startedAt;
|
|
86
|
+
this.timeout = timeout;
|
|
87
|
+
this.userData = userData;
|
|
88
|
+
this.flusher = flusher;
|
|
89
|
+
this.key = key;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
update = ({
|
|
93
|
+
items,
|
|
94
|
+
max,
|
|
95
|
+
timeout,
|
|
96
|
+
userData,
|
|
97
|
+
}: {
|
|
98
|
+
items: Array<T>;
|
|
99
|
+
max?: number | null;
|
|
100
|
+
timeout?: number | null;
|
|
101
|
+
userData?: UserData;
|
|
102
|
+
}) => {
|
|
103
|
+
this.items.push(...items);
|
|
104
|
+
if (max !== undefined) this.max = max;
|
|
105
|
+
if (timeout !== undefined) this.timeout = timeout;
|
|
106
|
+
if (userData) this.userData = userData;
|
|
107
|
+
|
|
108
|
+
// if the batch has items and a timeout but has not
|
|
109
|
+
// scheduled yet, schedule it
|
|
110
|
+
const needsSchedule =
|
|
111
|
+
this.items.length !== 0 && this.timeout !== null && !this.flushTimeout;
|
|
112
|
+
|
|
113
|
+
// if the batch has already reached its max, skip scheduling
|
|
114
|
+
// and flush immediately
|
|
115
|
+
if (this.max !== null && this.items.length >= this.max) {
|
|
116
|
+
this.flush();
|
|
117
|
+
} else if (needsSchedule && this.timeout !== null) {
|
|
118
|
+
this.flushTimeout = setTimeout(this.flush, this.timeout);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
flush = () => {
|
|
123
|
+
this.flushTimeout && clearTimeout(this.flushTimeout);
|
|
124
|
+
this.flushTimeout = undefined;
|
|
125
|
+
const items = this.items;
|
|
126
|
+
this.items = [];
|
|
127
|
+
return this.flusher(items, this.key, this.userData);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
discard = () => {
|
|
131
|
+
this.flushTimeout && clearTimeout(this.flushTimeout);
|
|
132
|
+
this.flushTimeout = undefined;
|
|
133
|
+
this.items = [];
|
|
134
|
+
};
|
|
135
|
+
}
|
package/src/files.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type FileRef = {
|
|
2
|
+
'@@type': 'file';
|
|
3
|
+
id: string;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export function isFileRef(value: any): value is FileRef {
|
|
7
|
+
return value && value['@@type'] === 'file';
|
|
8
|
+
}
|
|
9
|
+
export function createFileRef(id: string): FileRef {
|
|
10
|
+
return {
|
|
11
|
+
'@@type': 'file',
|
|
12
|
+
id,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type FileData = {
|
|
17
|
+
id: string;
|
|
18
|
+
/**
|
|
19
|
+
* For locally created files, this starts false, until it's uploaded
|
|
20
|
+
* Remote files this is always true
|
|
21
|
+
*/
|
|
22
|
+
remote: boolean;
|
|
23
|
+
name: string;
|
|
24
|
+
type: string;
|
|
25
|
+
file?: Blob;
|
|
26
|
+
url?: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export function getAllFileFields(snapshot: any): [string, FileRef][] {
|
|
30
|
+
return Object.entries(snapshot).filter((entry) => isFileRef(entry[1])) as [
|
|
31
|
+
string,
|
|
32
|
+
FileRef,
|
|
33
|
+
][];
|
|
34
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export * from './protocol.js';
|
|
2
|
+
export * from './timestamp.js';
|
|
3
|
+
export * from './operation.js';
|
|
4
|
+
export * from './baseline.js';
|
|
5
|
+
export * from './replica.js';
|
|
6
|
+
export * from './schema/index.js';
|
|
7
|
+
export * from './utils.js';
|
|
8
|
+
export * from './indexes.js';
|
|
9
|
+
export {
|
|
10
|
+
migrate,
|
|
11
|
+
migrationRange,
|
|
12
|
+
createDefaultMigration,
|
|
13
|
+
} from './migration.js';
|
|
14
|
+
export type {
|
|
15
|
+
Migration,
|
|
16
|
+
MigrationIndexDescription,
|
|
17
|
+
MigrationEngine,
|
|
18
|
+
} from './migration.js';
|
|
19
|
+
export type { UserInfo } from './presence.js';
|
|
20
|
+
export * from './patch.js';
|
|
21
|
+
export * from './oids.js';
|
|
22
|
+
export * from './EventSubscriber.js';
|
|
23
|
+
export * from './undo.js';
|
|
24
|
+
export * from './batching.js';
|
|
25
|
+
export * from './files.js';
|
|
26
|
+
export type { Ref } from './refs.js';
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
COMPOUND_INDEX_LOWER_BOUND_SEPARATOR,
|
|
4
|
+
COMPOUND_INDEX_SEPARATOR,
|
|
5
|
+
COMPOUND_INDEX_UPPER_BOUND_SEPARATOR,
|
|
6
|
+
createCompoundIndexValue,
|
|
7
|
+
createLowerBoundIndexValue,
|
|
8
|
+
createUpperBoundIndexValue,
|
|
9
|
+
} from './indexes.js';
|
|
10
|
+
|
|
11
|
+
describe('compound indexes', () => {
|
|
12
|
+
it('can be created from two string values', () => {
|
|
13
|
+
expect(createCompoundIndexValue('a', 'b')).toEqual(
|
|
14
|
+
`a${COMPOUND_INDEX_SEPARATOR}b`,
|
|
15
|
+
);
|
|
16
|
+
});
|
|
17
|
+
it('can be created from two number values', () => {
|
|
18
|
+
expect(createCompoundIndexValue(1, 2)).toEqual(
|
|
19
|
+
`1${COMPOUND_INDEX_SEPARATOR}2`,
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
it('can be created from an array and string value', () => {
|
|
23
|
+
expect(createCompoundIndexValue(['a', 'b'], 'c')).toEqual([
|
|
24
|
+
`a${COMPOUND_INDEX_SEPARATOR}c`,
|
|
25
|
+
`b${COMPOUND_INDEX_SEPARATOR}c`,
|
|
26
|
+
]);
|
|
27
|
+
});
|
|
28
|
+
it('can be created from a string and array of numbers', () => {
|
|
29
|
+
expect(createCompoundIndexValue('a', [1, 2])).toEqual([
|
|
30
|
+
`a${COMPOUND_INDEX_SEPARATOR}1`,
|
|
31
|
+
`a${COMPOUND_INDEX_SEPARATOR}2`,
|
|
32
|
+
]);
|
|
33
|
+
});
|
|
34
|
+
it('can be created from an array of strings and an array of booleans', () => {
|
|
35
|
+
expect(createCompoundIndexValue(['a', 'b'], [true, false])).toEqual([
|
|
36
|
+
`a${COMPOUND_INDEX_SEPARATOR}true`,
|
|
37
|
+
`a${COMPOUND_INDEX_SEPARATOR}false`,
|
|
38
|
+
`b${COMPOUND_INDEX_SEPARATOR}true`,
|
|
39
|
+
`b${COMPOUND_INDEX_SEPARATOR}false`,
|
|
40
|
+
]);
|
|
41
|
+
});
|
|
42
|
+
it('remove duplicate array compound index values', () => {
|
|
43
|
+
expect(
|
|
44
|
+
createCompoundIndexValue(['a', 'b', 'b'], [true, false, true]),
|
|
45
|
+
).toEqual([
|
|
46
|
+
`a${COMPOUND_INDEX_SEPARATOR}true`,
|
|
47
|
+
`a${COMPOUND_INDEX_SEPARATOR}false`,
|
|
48
|
+
`b${COMPOUND_INDEX_SEPARATOR}true`,
|
|
49
|
+
`b${COMPOUND_INDEX_SEPARATOR}false`,
|
|
50
|
+
]);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('creates lower bound value for multi-value indexes', () => {
|
|
54
|
+
expect(createLowerBoundIndexValue('a', 'b')).toEqual(
|
|
55
|
+
`a${COMPOUND_INDEX_SEPARATOR}b${COMPOUND_INDEX_SEPARATOR}${COMPOUND_INDEX_LOWER_BOUND_SEPARATOR}`,
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('creates upper bound value for multi-value indexes', () => {
|
|
60
|
+
expect(createUpperBoundIndexValue('a', 'b')).toEqual(
|
|
61
|
+
`a${COMPOUND_INDEX_SEPARATOR}b${COMPOUND_INDEX_UPPER_BOUND_SEPARATOR}`,
|
|
62
|
+
);
|
|
63
|
+
}),
|
|
64
|
+
it('will always be grouped by the first item', () => {
|
|
65
|
+
expect(
|
|
66
|
+
[
|
|
67
|
+
createCompoundIndexValue('aaaaaa', 'b'),
|
|
68
|
+
createCompoundIndexValue('aaaaaa', 'c'),
|
|
69
|
+
createCompoundIndexValue('aaaaaa', 'd'),
|
|
70
|
+
createCompoundIndexValue('aaaaaa', 'e'),
|
|
71
|
+
// extra nefarious!
|
|
72
|
+
createCompoundIndexValue('aaaaaaa', 'no!'),
|
|
73
|
+
createCompoundIndexValue('aaaaaa ', 'no!'),
|
|
74
|
+
createCompoundIndexValue(`aaaaaa😎`, 'no!'),
|
|
75
|
+
createCompoundIndexValue(`aaaaaa😎😎`, 'no!'),
|
|
76
|
+
createCompoundIndexValue('aaa', 'no!'),
|
|
77
|
+
createCompoundIndexValue(`aaaaaaa😎no!`, 'no!'),
|
|
78
|
+
].sort(),
|
|
79
|
+
).toEqual([
|
|
80
|
+
`aaaaaa ${COMPOUND_INDEX_SEPARATOR}no!`,
|
|
81
|
+
`aaaaaaa😎no!${COMPOUND_INDEX_SEPARATOR}no!`,
|
|
82
|
+
`aaaaaaa${COMPOUND_INDEX_SEPARATOR}no!`,
|
|
83
|
+
`aaaaaa😎😎${COMPOUND_INDEX_SEPARATOR}no!`,
|
|
84
|
+
`aaaaaa😎${COMPOUND_INDEX_SEPARATOR}no!`,
|
|
85
|
+
// this is the contiguous block of relevant values
|
|
86
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}b`,
|
|
87
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}c`,
|
|
88
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}d`,
|
|
89
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}e`,
|
|
90
|
+
// here ends the block
|
|
91
|
+
`aaa${COMPOUND_INDEX_SEPARATOR}no!`,
|
|
92
|
+
]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('will be bounded by upper and lower bound values', () => {
|
|
96
|
+
const match = 'aaaaaa';
|
|
97
|
+
const sorted = [
|
|
98
|
+
createCompoundIndexValue(match, 'b'),
|
|
99
|
+
createCompoundIndexValue(match, 'c'),
|
|
100
|
+
createCompoundIndexValue(match, 'd'),
|
|
101
|
+
createCompoundIndexValue(match, 'e'),
|
|
102
|
+
// extra nefarious!
|
|
103
|
+
createCompoundIndexValue(`${match}a`, 'no!'),
|
|
104
|
+
createCompoundIndexValue(`${match} `, 'no!'),
|
|
105
|
+
createCompoundIndexValue(`${match}😎`, 'no!'),
|
|
106
|
+
createCompoundIndexValue(`${match}😎😎`, 'no!'),
|
|
107
|
+
createCompoundIndexValue('aaa', 'no!'),
|
|
108
|
+
createCompoundIndexValue(`${match}a😎no!`, 'no!'),
|
|
109
|
+
].sort();
|
|
110
|
+
|
|
111
|
+
const boundedBlock = sorted.filter(
|
|
112
|
+
(value) =>
|
|
113
|
+
value > createLowerBoundIndexValue(match) &&
|
|
114
|
+
value < createUpperBoundIndexValue(match),
|
|
115
|
+
);
|
|
116
|
+
expect(boundedBlock).toEqual([
|
|
117
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}b`,
|
|
118
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}c`,
|
|
119
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}d`,
|
|
120
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}e`,
|
|
121
|
+
]);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('will be bounded by upper and lower bounds when matching multiple sections', () => {
|
|
125
|
+
const match1 = 'aaaaaa';
|
|
126
|
+
const match2 = 'bbbbbb';
|
|
127
|
+
const sorted = [
|
|
128
|
+
createCompoundIndexValue(match1, match2, 'b'),
|
|
129
|
+
createCompoundIndexValue(match1, match2, 'c'),
|
|
130
|
+
createCompoundIndexValue(match1, match2, 'd'),
|
|
131
|
+
createCompoundIndexValue(match1, match2, 'e'),
|
|
132
|
+
// extra nefarious!
|
|
133
|
+
createCompoundIndexValue(`${match1}a`, match2, 'no!'),
|
|
134
|
+
createCompoundIndexValue(`${match1} `, match2, 'no!'),
|
|
135
|
+
createCompoundIndexValue(`${match1}😎`, match2, 'no!'),
|
|
136
|
+
createCompoundIndexValue(`${match1}😎😎`, match2, 'no!'),
|
|
137
|
+
createCompoundIndexValue('aaa', match2, 'no!'),
|
|
138
|
+
createCompoundIndexValue(`${match1}a😎no!`, match2, 'no!'),
|
|
139
|
+
createCompoundIndexValue(match1, `${match2}a😎no!`, 'no!'),
|
|
140
|
+
createCompoundIndexValue(match1, `${match2} `, 'no!'),
|
|
141
|
+
createCompoundIndexValue(match1, ' ', 'no!'),
|
|
142
|
+
createCompoundIndexValue(match1, '\u00001', 'no!'),
|
|
143
|
+
createCompoundIndexValue(match1, '\uE007f', 'no!'),
|
|
144
|
+
].sort();
|
|
145
|
+
expect(
|
|
146
|
+
sorted.filter(
|
|
147
|
+
(value) =>
|
|
148
|
+
value > createLowerBoundIndexValue(match1, match2) &&
|
|
149
|
+
value < createUpperBoundIndexValue(match1, match2),
|
|
150
|
+
),
|
|
151
|
+
).toEqual([
|
|
152
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}b`,
|
|
153
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}c`,
|
|
154
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}d`,
|
|
155
|
+
`aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}e`,
|
|
156
|
+
]);
|
|
157
|
+
});
|
|
158
|
+
});
|
package/src/indexes.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StorageCollectionSchema,
|
|
3
|
+
StorageSyntheticIndexSchema,
|
|
4
|
+
CollectionCompoundIndex,
|
|
5
|
+
} from './index.js';
|
|
6
|
+
|
|
7
|
+
// unlikely to be used unicode character
|
|
8
|
+
export const COMPOUND_INDEX_SEPARATOR = '\uFFFFFE';
|
|
9
|
+
// 1 lower than separator
|
|
10
|
+
export const COMPOUND_INDEX_LOWER_BOUND_SEPARATOR = '\u0000';
|
|
11
|
+
// 1 higher than separator
|
|
12
|
+
export const COMPOUND_INDEX_UPPER_BOUND_SEPARATOR = '\uFFFFFF';
|
|
13
|
+
|
|
14
|
+
type IndexableFieldValue = string | number | boolean | any[];
|
|
15
|
+
export type CompoundIndexValue = string | string[];
|
|
16
|
+
|
|
17
|
+
export function createCompoundIndexValue(
|
|
18
|
+
...fields: IndexableFieldValue[]
|
|
19
|
+
): CompoundIndexValue {
|
|
20
|
+
const value = expandArrayIndex(fields);
|
|
21
|
+
if (value.length === 1) {
|
|
22
|
+
return value[0];
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function createUpperBoundIndexValue(
|
|
28
|
+
...fields: IndexableFieldValue[]
|
|
29
|
+
): string {
|
|
30
|
+
return (
|
|
31
|
+
fields.join(COMPOUND_INDEX_SEPARATOR) +
|
|
32
|
+
`${COMPOUND_INDEX_UPPER_BOUND_SEPARATOR}`
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function createLowerBoundIndexValue(
|
|
37
|
+
...fields: IndexableFieldValue[]
|
|
38
|
+
): string {
|
|
39
|
+
return (
|
|
40
|
+
fields.join(COMPOUND_INDEX_SEPARATOR) +
|
|
41
|
+
`${COMPOUND_INDEX_SEPARATOR}${COMPOUND_INDEX_LOWER_BOUND_SEPARATOR}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whenever an array value is included in a compound index, we have to expand
|
|
47
|
+
* the value to include all permutations of values in the array.
|
|
48
|
+
* For example if we had an index id + tags on a document
|
|
49
|
+
*
|
|
50
|
+
* {
|
|
51
|
+
* id: '1',
|
|
52
|
+
* tags: ['a', 'b']
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* we want to create an index:
|
|
56
|
+
*
|
|
57
|
+
* id_tags: ['1#a', '1#b']
|
|
58
|
+
*
|
|
59
|
+
* If multiple arrays are indexed we have to exponentially expand...
|
|
60
|
+
*
|
|
61
|
+
* id_tags_tags2: ['1#a#a', '1#a#b', '1#b#a', '1#b#b']
|
|
62
|
+
*
|
|
63
|
+
* To generalize this we construct a 2-level array of strings.
|
|
64
|
+
* Iterating over indexed values, we expand each item into N items if
|
|
65
|
+
* the current value is an array.
|
|
66
|
+
*
|
|
67
|
+
* Then we combine all nested arrays into compound index values.
|
|
68
|
+
* This will produce an array of 1 element if none of the indexed values
|
|
69
|
+
* were arrays. The caller should unwrap that.
|
|
70
|
+
*
|
|
71
|
+
* This function also deduplicates values.
|
|
72
|
+
*/
|
|
73
|
+
function expandArrayIndex(fields: IndexableFieldValue[]): string[] {
|
|
74
|
+
let value: string[][] = [[]];
|
|
75
|
+
for (const field of fields) {
|
|
76
|
+
if (Array.isArray(field)) {
|
|
77
|
+
const newValue: string[][] = [];
|
|
78
|
+
for (const previousValue of value) {
|
|
79
|
+
for (const fieldValue of field) {
|
|
80
|
+
newValue.push(previousValue.concat(fieldValue));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
value = newValue;
|
|
84
|
+
} else {
|
|
85
|
+
for (const item of value) {
|
|
86
|
+
item.push(`${field}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return Array.from(
|
|
91
|
+
new Set(
|
|
92
|
+
value.map((item) => {
|
|
93
|
+
return item.join(COMPOUND_INDEX_SEPARATOR);
|
|
94
|
+
}),
|
|
95
|
+
),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function computeSynthetics(schema: StorageCollectionSchema, obj: any) {
|
|
100
|
+
const result: Record<string, any> = {};
|
|
101
|
+
for (const [name, property] of Object.entries(schema.synthetics || {})) {
|
|
102
|
+
result[name] = sanitizeIndexValue(
|
|
103
|
+
(property as StorageSyntheticIndexSchema<any>).compute(obj),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function computeCompoundIndices(
|
|
110
|
+
schema: StorageCollectionSchema<any, any, any>,
|
|
111
|
+
doc: any,
|
|
112
|
+
): any {
|
|
113
|
+
return Object.entries(schema.compounds || {}).reduce<
|
|
114
|
+
Record<string, CompoundIndexValue>
|
|
115
|
+
>((acc, [indexKey, index]) => {
|
|
116
|
+
acc[indexKey] = createCompoundIndexValue(
|
|
117
|
+
...(index as CollectionCompoundIndex<any, any>).of.map(
|
|
118
|
+
(key) => doc[key] as string | number,
|
|
119
|
+
),
|
|
120
|
+
);
|
|
121
|
+
return acc;
|
|
122
|
+
}, {} as Record<string, CompoundIndexValue>);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function assignIndexValues(
|
|
126
|
+
schema: StorageCollectionSchema<any, any, any>,
|
|
127
|
+
doc: any,
|
|
128
|
+
) {
|
|
129
|
+
Object.assign(doc, computeSynthetics(schema, doc));
|
|
130
|
+
Object.assign(doc, computeCompoundIndices(schema, doc));
|
|
131
|
+
return doc;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function sanitizeIndexValue(
|
|
135
|
+
value: unknown,
|
|
136
|
+
): string | number | (string | number)[] {
|
|
137
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
if (typeof value === 'boolean' || value === null) {
|
|
141
|
+
return `${value}`;
|
|
142
|
+
}
|
|
143
|
+
if (value === undefined) {
|
|
144
|
+
// this shouldn't happen ,but for resiliency...
|
|
145
|
+
return 'undefined';
|
|
146
|
+
}
|
|
147
|
+
if (Array.isArray(value)) {
|
|
148
|
+
return value.map(sanitizeIndexValue) as any;
|
|
149
|
+
}
|
|
150
|
+
throw new Error(`Unsupported index value: ${value}`);
|
|
151
|
+
}
|