@agentshouse/mdruntime 0.1.0-alpha.9 → 0.1.0-c3903a68dcbbf
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/AGENTS.md +210 -0
- package/README.md +44 -0
- package/dist/adapter.d.ts +155 -12
- package/dist/adapter.d.ts.map +1 -1
- package/dist/adapter.js +2 -3
- package/dist/attachment.d.ts +3 -0
- package/dist/attachment.d.ts.map +1 -0
- package/dist/attachment.js +8 -0
- package/dist/bootstrap.d.ts +3 -7
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +29 -27
- package/dist/change-set.d.ts +33 -0
- package/dist/change-set.d.ts.map +1 -0
- package/dist/change-set.js +87 -0
- package/dist/change.d.ts +9 -0
- package/dist/change.d.ts.map +1 -1
- package/dist/change.js +6 -5
- package/dist/client.d.ts +40 -28
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +121 -177
- package/dist/collection.d.ts +15 -16
- package/dist/collection.d.ts.map +1 -1
- package/dist/collection.js +54 -87
- package/dist/connection.d.ts +57 -35
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +1475 -964
- package/dist/conversation.d.ts +41 -19
- package/dist/conversation.d.ts.map +1 -1
- package/dist/conversation.js +483 -327
- package/dist/durability.js +6 -6
- package/dist/file.d.ts +2 -2
- package/dist/file.d.ts.map +1 -1
- package/dist/file.js +48 -71
- package/dist/host.d.ts +7 -2
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +100 -102
- package/dist/identity.d.ts +1 -0
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +10 -9
- package/dist/index.d.ts +12 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -2
- package/dist/log.js +5 -6
- package/dist/memory.d.ts +6 -3
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +206 -210
- package/dist/model.d.ts +10 -25
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +18 -21
- package/dist/observation.d.ts +25 -0
- package/dist/observation.d.ts.map +1 -0
- package/dist/observation.js +85 -0
- package/dist/outcome.d.ts +17 -20
- package/dist/outcome.d.ts.map +1 -1
- package/dist/outcome.js +1 -23
- package/dist/overlay.d.ts +9 -2
- package/dist/overlay.d.ts.map +1 -1
- package/dist/overlay.js +187 -43
- package/dist/paths.d.ts +0 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +12 -19
- package/dist/query.d.ts +210 -10
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +420 -15
- package/dist/read-side.d.ts +14 -0
- package/dist/read-side.d.ts.map +1 -0
- package/dist/read-side.js +371 -0
- package/dist/refresh.d.ts +4 -5
- package/dist/refresh.d.ts.map +1 -1
- package/dist/refresh.js +45 -30
- package/dist/replica.d.ts +25 -5
- package/dist/replica.d.ts.map +1 -1
- package/dist/replica.js +232 -38
- package/dist/room-draft.d.ts +124 -0
- package/dist/room-draft.d.ts.map +1 -0
- package/dist/room-draft.js +327 -0
- package/dist/rows.d.ts +41 -0
- package/dist/rows.d.ts.map +1 -0
- package/dist/rows.js +100 -0
- package/dist/save.d.ts +8 -4
- package/dist/save.d.ts.map +1 -1
- package/dist/save.js +265 -350
- package/dist/search.d.ts +61 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +125 -0
- package/dist/status.d.ts +4 -6
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +9 -13
- package/dist/store.d.ts +68 -30
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +360 -134
- package/dist/transfer.d.ts +5 -0
- package/dist/transfer.d.ts.map +1 -1
- package/dist/transfer.js +1 -5
- package/dist/wire.d.ts +4 -3
- package/dist/wire.d.ts.map +1 -1
- package/dist/wire.js +50 -65
- package/package.json +6 -12
- package/dist/adapter.js.map +0 -1
- package/dist/bootstrap.js.map +0 -1
- package/dist/change.js.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/collection.js.map +0 -1
- package/dist/connection.js.map +0 -1
- package/dist/conversation.js.map +0 -1
- package/dist/durability.js.map +0 -1
- package/dist/file.js.map +0 -1
- package/dist/host.js.map +0 -1
- package/dist/identity.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/log.js.map +0 -1
- package/dist/memory.js.map +0 -1
- package/dist/model.js.map +0 -1
- package/dist/outcome.js.map +0 -1
- package/dist/overlay.js.map +0 -1
- package/dist/paths.js.map +0 -1
- package/dist/query.js.map +0 -1
- package/dist/refresh.js.map +0 -1
- package/dist/replica.js.map +0 -1
- package/dist/save.js.map +0 -1
- package/dist/status.js.map +0 -1
- package/dist/store.js.map +0 -1
- package/dist/transfer.js.map +0 -1
- package/dist/wire.js.map +0 -1
package/dist/query.js
CHANGED
|
@@ -1,18 +1,423 @@
|
|
|
1
|
-
|
|
1
|
+
import { normalize } from './paths.js';
|
|
2
|
+
export class QueryError extends Error {
|
|
3
|
+
constructor(code, at, problem) {
|
|
4
|
+
super(`${code} at ${at}: ${problem}`);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
const RANGE = ['$gt', '$gte', '$lt', '$lte'];
|
|
8
|
+
const SINGLE = ['$eq', '$ne', '$in', '$nin', '$all', '$size', '$exists'];
|
|
9
|
+
const FILTER = ['rooms', 'types', 'where', 'pathPrefix', 'hop', 'backlinks'];
|
|
10
|
+
const RELATED = ['types', 'where', 'pathPrefix', 'field'];
|
|
11
|
+
const PARTS = ['fields', 'sections', 'preamble', 'markdown', 'backlinks', 'change'];
|
|
12
|
+
const ORDER = ['by', 'direction', 'nulls', 'stringSort', 'locale', 'localeOptions'];
|
|
13
|
+
const CALENDAR_DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
14
|
+
export function invalid(at, problem) {
|
|
15
|
+
throw new QueryError('invalid-query', at, problem);
|
|
16
|
+
}
|
|
17
|
+
function place(at, name) {
|
|
18
|
+
return at === '' ? name : `${at}.${name}`;
|
|
19
|
+
}
|
|
20
|
+
export function plain(value) {
|
|
21
|
+
return (typeof value === 'object' &&
|
|
22
|
+
value !== null &&
|
|
23
|
+
!Array.isArray(value) &&
|
|
24
|
+
(Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null));
|
|
25
|
+
}
|
|
26
|
+
export function known(value, allowed, at) {
|
|
27
|
+
for (const name of Object.keys(value)) {
|
|
28
|
+
if (!allowed.includes(name))
|
|
29
|
+
invalid(place(at, name), 'unknown part');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function list(input, at) {
|
|
33
|
+
if (!Array.isArray(input))
|
|
34
|
+
invalid(at, 'expects a list');
|
|
35
|
+
return input;
|
|
36
|
+
}
|
|
37
|
+
function value(input, at) {
|
|
38
|
+
if (Array.isArray(input))
|
|
39
|
+
invalid(at, 'a list is not a value');
|
|
40
|
+
if (input !== null &&
|
|
41
|
+
typeof input !== 'string' &&
|
|
42
|
+
typeof input !== 'boolean' &&
|
|
43
|
+
!(typeof input === 'number' && Number.isFinite(input))) {
|
|
44
|
+
invalid(at, 'not a value');
|
|
45
|
+
}
|
|
46
|
+
return input;
|
|
47
|
+
}
|
|
48
|
+
function values(input, at) {
|
|
49
|
+
return list(input, at).map((item, index) => value(item, `${at}[${index}]`));
|
|
50
|
+
}
|
|
51
|
+
function names(input, at) {
|
|
52
|
+
const listed = list(input, at);
|
|
53
|
+
if (listed.some((item) => typeof item !== 'string'))
|
|
54
|
+
invalid(at, 'expects a list of names');
|
|
55
|
+
return [...new Set(listed)].sort();
|
|
56
|
+
}
|
|
57
|
+
function directory(input, at) {
|
|
58
|
+
if (typeof input !== 'string' || normalize(input) !== input) {
|
|
59
|
+
invalid(at, 'expects a normalized directory');
|
|
60
|
+
}
|
|
61
|
+
return input;
|
|
62
|
+
}
|
|
63
|
+
function calendarDate(value) {
|
|
64
|
+
const match = CALENDAR_DATE.exec(value);
|
|
65
|
+
if (match === null)
|
|
66
|
+
return false;
|
|
67
|
+
const [year, month, day] = match.slice(1).map(Number);
|
|
68
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
69
|
+
return (date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day);
|
|
70
|
+
}
|
|
71
|
+
function fits(spec, value) {
|
|
72
|
+
const item = spec.kind === 'list' ? spec.of : spec;
|
|
73
|
+
if (item.kind === 'number')
|
|
74
|
+
return typeof value === 'number';
|
|
75
|
+
if (typeof value !== 'string')
|
|
76
|
+
return false;
|
|
77
|
+
if (item.kind === 'enum')
|
|
78
|
+
return item.values.includes(value);
|
|
79
|
+
if (item.kind === 'date')
|
|
80
|
+
return calendarDate(value);
|
|
81
|
+
if (item.kind === 'reference')
|
|
82
|
+
return value !== '' && normalize(value) === value;
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
function reference(spec) {
|
|
86
|
+
return spec.kind === 'reference' || (spec.kind === 'list' && spec.of.kind === 'reference');
|
|
87
|
+
}
|
|
88
|
+
function typing(facts, rooms, types, at) {
|
|
89
|
+
const read = rooms === null ? facts.rooms : facts.rooms.filter((room) => rooms.includes(room.locator));
|
|
90
|
+
const acquiring = !facts.listed &&
|
|
91
|
+
(rooms === null ||
|
|
92
|
+
rooms.some((locator) => !facts.rooms.some((room) => room.locator === locator)));
|
|
93
|
+
const available = !acquiring && read.every((room) => room.available);
|
|
94
|
+
const declared = read.flatMap((room) => room.types);
|
|
95
|
+
for (const name of types ?? []) {
|
|
96
|
+
if (name === 'any' || !available || declared.some((type) => type.name === name))
|
|
97
|
+
continue;
|
|
98
|
+
throw new QueryError('unknown-type', place(at, 'types'), `no model it reads declares ${name}`);
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
untyped: types === null || types.includes('any') || !available,
|
|
102
|
+
types: types === null ? declared : declared.filter((type) => types.includes(type.name)),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function specs(typed, field, at) {
|
|
106
|
+
const declared = typed.types.flatMap((type) => Object.hasOwn(type.fields, field) ? [type.fields[field]] : []);
|
|
107
|
+
if (declared.length === 0 && !typed.untyped) {
|
|
108
|
+
throw new QueryError('unknown-field', at, `no MD Type it reads declares ${field}`);
|
|
109
|
+
}
|
|
110
|
+
return declared;
|
|
111
|
+
}
|
|
112
|
+
function kind(declared, checked, at) {
|
|
113
|
+
if (checked === null || declared.length === 0 || declared.some((spec) => fits(spec, checked))) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
throw new QueryError('wrong-kind', at, `${JSON.stringify(checked)} is not of its field's kind`);
|
|
117
|
+
}
|
|
118
|
+
function listed(declared, list) {
|
|
119
|
+
return declared.length > 0 && declared.every((spec) => (spec.kind === 'list') === list);
|
|
120
|
+
}
|
|
121
|
+
function test(declared, input, at) {
|
|
122
|
+
if (!plain(input)) {
|
|
123
|
+
const equal = value(input, at);
|
|
124
|
+
kind(declared, equal, at);
|
|
125
|
+
return { op: 'eq', value: equal };
|
|
126
|
+
}
|
|
127
|
+
const operators = Object.keys(input);
|
|
128
|
+
if (operators.length === 0)
|
|
129
|
+
invalid(at, 'names no operator');
|
|
130
|
+
if (operators.some((operator) => RANGE.includes(operator))) {
|
|
131
|
+
const stray = operators.find((operator) => !RANGE.includes(operator));
|
|
132
|
+
if (stray !== undefined)
|
|
133
|
+
invalid(place(at, stray), 'cannot join a range');
|
|
134
|
+
if (listed(declared, true))
|
|
135
|
+
invalid(at, 'a range on a list');
|
|
136
|
+
return {
|
|
137
|
+
op: 'range',
|
|
138
|
+
bounds: RANGE.filter((operator) => operators.includes(operator)).map((operator) => {
|
|
139
|
+
const bound = value(input[operator], place(at, operator));
|
|
140
|
+
if (bound === null)
|
|
141
|
+
invalid(place(at, operator), 'a range bound is never null');
|
|
142
|
+
kind(declared, bound, place(at, operator));
|
|
143
|
+
return [operator.slice(1), bound];
|
|
144
|
+
}),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (operators.length > 1)
|
|
148
|
+
invalid(place(at, operators[1]), 'names a second operator');
|
|
149
|
+
const [operator] = operators;
|
|
150
|
+
const where = place(at, operator);
|
|
151
|
+
if (!SINGLE.includes(operator))
|
|
152
|
+
invalid(where, 'unknown operator');
|
|
153
|
+
const operand = input[operator];
|
|
154
|
+
if (operator === '$eq' || operator === '$ne') {
|
|
155
|
+
const compared = value(operand, where);
|
|
156
|
+
kind(declared, compared, where);
|
|
157
|
+
return operator === '$eq' ? { op: 'eq', value: compared } : { op: 'ne', value: compared };
|
|
158
|
+
}
|
|
159
|
+
if (operator === '$in' || operator === '$nin' || operator === '$all') {
|
|
160
|
+
const compared = values(operand, where);
|
|
161
|
+
compared.forEach((item, index) => kind(declared, item, `${where}[${index}]`));
|
|
162
|
+
if (operator === '$in')
|
|
163
|
+
return { op: 'in', values: compared };
|
|
164
|
+
if (operator === '$nin')
|
|
165
|
+
return { op: 'nin', values: compared };
|
|
166
|
+
if (compared.includes(null))
|
|
167
|
+
invalid(where, 'a list holds no null');
|
|
168
|
+
if (listed(declared, false))
|
|
169
|
+
invalid(where, 'the field is not a list');
|
|
170
|
+
return { op: 'all', values: compared };
|
|
171
|
+
}
|
|
172
|
+
if (operator === '$size') {
|
|
173
|
+
if (!Number.isSafeInteger(operand) || operand < 0) {
|
|
174
|
+
invalid(where, 'expects a nonnegative integer');
|
|
175
|
+
}
|
|
176
|
+
if (listed(declared, false))
|
|
177
|
+
invalid(where, 'the field is not a list');
|
|
178
|
+
return { op: 'size', size: operand };
|
|
179
|
+
}
|
|
180
|
+
if (typeof operand !== 'boolean')
|
|
181
|
+
invalid(where, 'expects true or false');
|
|
182
|
+
return { op: 'exists', exists: operand };
|
|
183
|
+
}
|
|
184
|
+
function condition(typed, input, at) {
|
|
185
|
+
if (!plain(input))
|
|
186
|
+
invalid(at, 'expects an object of field predicates');
|
|
187
|
+
const conditions = [];
|
|
188
|
+
for (const name of Object.keys(input).sort()) {
|
|
189
|
+
const where = place(at, name);
|
|
190
|
+
const entry = input[name];
|
|
191
|
+
if (name === '$and' || name === '$or') {
|
|
192
|
+
const nested = list(entry, where).map((item, index) => condition(typed, item, `${where}[${index}]`));
|
|
193
|
+
if (nested.length === 0)
|
|
194
|
+
invalid(where, 'expects a nonempty list');
|
|
195
|
+
conditions.push(name === '$and' ? { every: nested } : { some: nested });
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (name.startsWith('$'))
|
|
199
|
+
invalid(where, 'unknown operator');
|
|
200
|
+
conditions.push({ field: name, test: test(specs(typed, name, where), entry, where) });
|
|
201
|
+
}
|
|
202
|
+
if (conditions.length === 0)
|
|
203
|
+
invalid(at, 'names no field');
|
|
204
|
+
return conditions.length === 1 ? conditions[0] : { every: conditions };
|
|
205
|
+
}
|
|
206
|
+
function scope(facts, rooms, input, at) {
|
|
207
|
+
const types = input.types === undefined ? null : names(input.types, place(at, 'types'));
|
|
208
|
+
const typed = typing(facts, rooms, types, at);
|
|
2
209
|
return {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
...structuredClone(document),
|
|
10
|
-
type: document.type ?? 'any',
|
|
11
|
-
room: room.room,
|
|
12
|
-
path,
|
|
13
|
-
backlinks: []
|
|
14
|
-
})))
|
|
210
|
+
typed,
|
|
211
|
+
types,
|
|
212
|
+
where: input.where === undefined || (plain(input.where) && Object.keys(input.where).length === 0)
|
|
213
|
+
? null
|
|
214
|
+
: condition(typed, input.where, place(at, 'where')),
|
|
215
|
+
pathPrefix: input.pathPrefix === undefined ? null : directory(input.pathPrefix, place(at, 'pathPrefix')),
|
|
15
216
|
};
|
|
16
217
|
}
|
|
17
|
-
|
|
18
|
-
|
|
218
|
+
function referenceField(typed, field, at) {
|
|
219
|
+
if (typeof field !== 'string')
|
|
220
|
+
invalid(at, 'expects a field name');
|
|
221
|
+
const declared = specs(typed, field, at);
|
|
222
|
+
if (declared.length > 0 && !declared.some(reference))
|
|
223
|
+
invalid(at, 'not a reference field');
|
|
224
|
+
return field;
|
|
225
|
+
}
|
|
226
|
+
function hopOf(facts, rooms, root, input) {
|
|
227
|
+
if (!plain(input))
|
|
228
|
+
invalid('hop', 'expects an object');
|
|
229
|
+
known(input, RELATED, 'hop');
|
|
230
|
+
const field = referenceField(root, input.field, 'hop.field');
|
|
231
|
+
const { types, where, pathPrefix } = scope(facts, rooms, input, 'hop');
|
|
232
|
+
return { types, where, pathPrefix, field };
|
|
233
|
+
}
|
|
234
|
+
function backlinksOf(facts, rooms, input) {
|
|
235
|
+
if (!plain(input))
|
|
236
|
+
invalid('backlinks', 'expects an object');
|
|
237
|
+
known(input, RELATED, 'backlinks');
|
|
238
|
+
const { typed, types, where, pathPrefix } = scope(facts, rooms, input, 'backlinks');
|
|
239
|
+
const field = input.field === undefined ? null : referenceField(typed, input.field, 'backlinks.field');
|
|
240
|
+
return { types, where, pathPrefix, field };
|
|
241
|
+
}
|
|
242
|
+
function keyPlan(typed, input, at, path) {
|
|
243
|
+
if (input === 'room' || input === 'type' || (path && input === 'path'))
|
|
244
|
+
return { of: input };
|
|
245
|
+
if (plain(input) && typeof input.field === 'string' && Object.keys(input).length === 1) {
|
|
246
|
+
specs(typed, input.field, place(at, 'field'));
|
|
247
|
+
return { of: 'field', field: input.field };
|
|
248
|
+
}
|
|
249
|
+
return invalid(at, path ? 'expects room, path, type or a field' : 'expects room, type or a field');
|
|
250
|
+
}
|
|
251
|
+
function direction(input, at) {
|
|
252
|
+
if (input === undefined)
|
|
253
|
+
return 'asc';
|
|
254
|
+
if (input !== 'asc' && input !== 'desc')
|
|
255
|
+
invalid(at, 'expects asc or desc');
|
|
256
|
+
return input;
|
|
257
|
+
}
|
|
258
|
+
function orderOptions(input, at) {
|
|
259
|
+
const options = {
|
|
260
|
+
direction: direction(input.direction, place(at, 'direction')),
|
|
261
|
+
};
|
|
262
|
+
if (input.nulls !== undefined) {
|
|
263
|
+
if (input.nulls !== 'first' && input.nulls !== 'last') {
|
|
264
|
+
invalid(place(at, 'nulls'), 'expects first or last');
|
|
265
|
+
}
|
|
266
|
+
options.nulls = input.nulls;
|
|
267
|
+
}
|
|
268
|
+
if (input.stringSort !== undefined) {
|
|
269
|
+
if (input.stringSort !== 'lexical' && input.stringSort !== 'locale') {
|
|
270
|
+
invalid(place(at, 'stringSort'), 'expects lexical or locale');
|
|
271
|
+
}
|
|
272
|
+
options.stringSort = input.stringSort;
|
|
273
|
+
}
|
|
274
|
+
if (input.locale !== undefined) {
|
|
275
|
+
if (typeof input.locale !== 'string' || !collates(input.locale, undefined)) {
|
|
276
|
+
invalid(place(at, 'locale'), 'expects a locale name');
|
|
277
|
+
}
|
|
278
|
+
options.locale = input.locale;
|
|
279
|
+
}
|
|
280
|
+
if (input.localeOptions !== undefined) {
|
|
281
|
+
if (!plain(input.localeOptions) || !collates(options.locale, input.localeOptions)) {
|
|
282
|
+
invalid(place(at, 'localeOptions'), 'expects collation options');
|
|
283
|
+
}
|
|
284
|
+
options.localeOptions = input.localeOptions;
|
|
285
|
+
}
|
|
286
|
+
return options;
|
|
287
|
+
}
|
|
288
|
+
function collates(locale, options) {
|
|
289
|
+
try {
|
|
290
|
+
''.localeCompare('', locale, options);
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function parts(typed, input, at, fields) {
|
|
298
|
+
if (input === undefined)
|
|
299
|
+
return null;
|
|
300
|
+
if (input === 'all')
|
|
301
|
+
return 'all';
|
|
302
|
+
const named = list(input, at);
|
|
303
|
+
named.forEach((name, index) => {
|
|
304
|
+
if (typeof name !== 'string')
|
|
305
|
+
invalid(`${at}[${index}]`, 'expects a name');
|
|
306
|
+
if (fields)
|
|
307
|
+
specs(typed, name, `${at}[${index}]`);
|
|
308
|
+
});
|
|
309
|
+
return named;
|
|
310
|
+
}
|
|
311
|
+
function flag(input, at) {
|
|
312
|
+
if (input === undefined)
|
|
313
|
+
return false;
|
|
314
|
+
if (input !== true)
|
|
315
|
+
invalid(at, 'expects true');
|
|
316
|
+
return true;
|
|
317
|
+
}
|
|
318
|
+
function documentShape(typed, input) {
|
|
319
|
+
const orderBy = (input.orderBy === undefined ? [] : list(input.orderBy, 'orderBy')).map((clause, index) => {
|
|
320
|
+
const at = `orderBy[${index}]`;
|
|
321
|
+
if (!plain(clause))
|
|
322
|
+
invalid(at, 'expects an order clause');
|
|
323
|
+
known(clause, ORDER, at);
|
|
324
|
+
return {
|
|
325
|
+
key: keyPlan(typed, clause.by, place(at, 'by'), true),
|
|
326
|
+
options: orderOptions(clause, at),
|
|
327
|
+
};
|
|
328
|
+
});
|
|
329
|
+
const select = input.select;
|
|
330
|
+
if (select === 'all') {
|
|
331
|
+
return {
|
|
332
|
+
kind: 'documents',
|
|
333
|
+
fields: 'all',
|
|
334
|
+
sections: 'all',
|
|
335
|
+
preamble: true,
|
|
336
|
+
markdown: true,
|
|
337
|
+
backlinks: true,
|
|
338
|
+
change: true,
|
|
339
|
+
orderBy,
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
if (!plain(select))
|
|
343
|
+
invalid('select', "expects 'all' or the parts to return");
|
|
344
|
+
known(select, PARTS, 'select');
|
|
345
|
+
return {
|
|
346
|
+
kind: 'documents',
|
|
347
|
+
fields: parts(typed, select.fields, 'select.fields', true),
|
|
348
|
+
sections: parts(typed, select.sections, 'select.sections', false),
|
|
349
|
+
preamble: flag(select.preamble, 'select.preamble'),
|
|
350
|
+
markdown: flag(select.markdown, 'select.markdown'),
|
|
351
|
+
backlinks: flag(select.backlinks, 'select.backlinks'),
|
|
352
|
+
change: flag(select.change, 'select.change'),
|
|
353
|
+
orderBy,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
function groupShape(typed, input) {
|
|
357
|
+
const groupBy = list(input.groupBy, 'groupBy').map((key, index) => keyPlan(typed, key, `groupBy[${index}]`, false));
|
|
358
|
+
if (groupBy.length === 0)
|
|
359
|
+
invalid('groupBy', 'names no key');
|
|
360
|
+
const grouped = groupBy.map((key) => JSON.stringify(key));
|
|
361
|
+
const orderBy = (input.orderBy === undefined ? [] : list(input.orderBy, 'orderBy')).map((clause, index) => {
|
|
362
|
+
const at = `orderBy[${index}]`;
|
|
363
|
+
if (!plain(clause))
|
|
364
|
+
invalid(at, 'expects an order clause');
|
|
365
|
+
known(clause, ['by', 'direction'], at);
|
|
366
|
+
const by = clause.by === 'count'
|
|
367
|
+
? 'count'
|
|
368
|
+
: grouped.indexOf(JSON.stringify(keyPlan(typed, clause.by, place(at, 'by'), false)));
|
|
369
|
+
if (by === -1)
|
|
370
|
+
invalid(place(at, 'by'), 'orders by a key the query does not group by');
|
|
371
|
+
return { by, direction: direction(clause.direction, place(at, 'direction')) };
|
|
372
|
+
});
|
|
373
|
+
return { kind: 'groups', groupBy, orderBy };
|
|
374
|
+
}
|
|
375
|
+
export function planOf(input, facts) {
|
|
376
|
+
if (!plain(input))
|
|
377
|
+
invalid('query', 'expects a query object');
|
|
378
|
+
const grouped = input.groupBy !== undefined;
|
|
379
|
+
if (grouped && input.select !== undefined)
|
|
380
|
+
invalid('select', 'a grouped query selects nothing');
|
|
381
|
+
if (!grouped && input.select === undefined)
|
|
382
|
+
invalid('select', 'a Document query must select');
|
|
383
|
+
known(input, [...FILTER, 'orderBy', 'limit', grouped ? 'groupBy' : 'select'], '');
|
|
384
|
+
const limit = limitOf(input.limit);
|
|
385
|
+
const rooms = input.rooms === undefined ? null : names(input.rooms, 'rooms');
|
|
386
|
+
const { typed, ...root } = scope(facts, rooms, input, '');
|
|
387
|
+
return {
|
|
388
|
+
rooms,
|
|
389
|
+
root,
|
|
390
|
+
hop: input.hop === undefined ? null : hopOf(facts, rooms, typed, input.hop),
|
|
391
|
+
backlinks: input.backlinks === undefined ? null : backlinksOf(facts, rooms, input.backlinks),
|
|
392
|
+
shape: grouped ? groupShape(typed, input) : documentShape(typed, input),
|
|
393
|
+
limit,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
export function limitOf(input) {
|
|
397
|
+
if (input === undefined)
|
|
398
|
+
return null;
|
|
399
|
+
if (!Number.isSafeInteger(input) || input < 0) {
|
|
400
|
+
invalid('limit', 'expects a nonnegative integer');
|
|
401
|
+
}
|
|
402
|
+
return input;
|
|
403
|
+
}
|
|
404
|
+
export function filterOf(input, facts) {
|
|
405
|
+
if (!plain(input))
|
|
406
|
+
invalid('documents', 'expects a filter object');
|
|
407
|
+
known(input, FILTER, '');
|
|
408
|
+
return planOf({ ...input, select: {} }, facts);
|
|
409
|
+
}
|
|
410
|
+
function canonical(value) {
|
|
411
|
+
if (Array.isArray(value))
|
|
412
|
+
return `[${value.map(canonical).join(',')}]`;
|
|
413
|
+
if (typeof value === 'object' && value !== null) {
|
|
414
|
+
return `{${Object.keys(value)
|
|
415
|
+
.sort()
|
|
416
|
+
.map((name) => `${JSON.stringify(name)}:${canonical(value[name])}`)
|
|
417
|
+
.join(',')}}`;
|
|
418
|
+
}
|
|
419
|
+
return JSON.stringify(value);
|
|
420
|
+
}
|
|
421
|
+
export function identityOf(plan) {
|
|
422
|
+
return canonical(plan);
|
|
423
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ChangeSet } from './change-set.js';
|
|
2
|
+
import { type KeyedResult, type QueryChange } from './observation.js';
|
|
3
|
+
import type { Plan, QueryResult } from './query.js';
|
|
4
|
+
import type { SearchedDocument } from './search.js';
|
|
5
|
+
export type ReadSide = {
|
|
6
|
+
apply(set: ChangeSet): void;
|
|
7
|
+
query(plan: Plan): Promise<QueryResult>;
|
|
8
|
+
watch(plan: Plan, identity: string, deliver: (message: KeyedResult | QueryChange) => void): () => void;
|
|
9
|
+
documents(): readonly SearchedDocument[];
|
|
10
|
+
document(room: string, path: string): SearchedDocument | null;
|
|
11
|
+
close(): void;
|
|
12
|
+
};
|
|
13
|
+
export declare function createReadSide(): ReadSide;
|
|
14
|
+
//# sourceMappingURL=read-side.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"read-side.d.ts","sourceRoot":"","sources":["../src/read-side.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,EAAU,KAAK,WAAW,EAAE,KAAK,WAAW,EAAe,MAAM,kBAAkB,CAAC;AAC3F,OAAO,KAAK,EAOV,IAAI,EAGJ,WAAW,EAIZ,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAqDpD,MAAM,MAAM,QAAQ,GAAG;IACrB,KAAK,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC;IAC5B,KAAK,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IACxC,KAAK,CACH,IAAI,EAAE,IAAI,EACV,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,CAAC,OAAO,EAAE,WAAW,GAAG,WAAW,KAAK,IAAI,GACpD,MAAM,IAAI,CAAC;IACd,SAAS,IAAI,SAAS,gBAAgB,EAAE,CAAC;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CAAC;IAC9D,KAAK,IAAI,IAAI,CAAC;CACf,CAAC;AAySF,wBAAgB,cAAc,IAAI,QAAQ,CA4HzC"}
|