@agentshouse/mdmodel 0.1.0-ccf1367190878 → 0.1.0-cdbd463e01dc2
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/API.md +197 -178
- package/README.md +21 -22
- package/dist/blocks.d.ts.map +1 -1
- package/dist/blocks.js +35 -20
- package/dist/definition.d.ts +3 -1
- package/dist/definition.d.ts.map +1 -1
- package/dist/definition.js +112 -24
- package/dist/divide.d.ts.map +1 -1
- package/dist/divide.js +27 -17
- package/dist/document.d.ts +5 -5
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +304 -187
- package/dist/event.d.ts +78 -0
- package/dist/event.d.ts.map +1 -0
- package/dist/event.js +230 -0
- package/dist/facts.d.ts +15 -0
- package/dist/facts.d.ts.map +1 -0
- package/dist/facts.js +39 -0
- package/dist/index.d.ts +22 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -6
- package/dist/kinds.d.ts +19 -0
- package/dist/kinds.d.ts.map +1 -0
- package/dist/kinds.js +161 -0
- package/dist/links.d.ts +2 -0
- package/dist/links.d.ts.map +1 -1
- package/dist/links.js +107 -50
- package/dist/paths.d.ts +9 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +28 -2
- package/dist/pattern.d.ts +10 -0
- package/dist/pattern.d.ts.map +1 -0
- package/dist/pattern.js +355 -0
- package/dist/project.d.ts +1 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +38 -15
- package/dist/query/language.d.ts +223 -0
- package/dist/query/language.d.ts.map +1 -0
- package/dist/query/language.js +565 -0
- package/dist/query/match.d.ts +14 -0
- package/dist/query/match.d.ts.map +1 -0
- package/dist/query/match.js +121 -0
- package/dist/query/rows.d.ts +32 -0
- package/dist/query/rows.d.ts.map +1 -0
- package/dist/query/rows.js +161 -0
- package/dist/query/sql.d.ts +39 -0
- package/dist/query/sql.d.ts.map +1 -0
- package/dist/query/sql.js +279 -0
- package/dist/references.d.ts.map +1 -1
- package/dist/references.js +6 -1
- package/dist/reserved.d.ts +5 -0
- package/dist/reserved.d.ts.map +1 -0
- package/dist/reserved.js +20 -0
- package/dist/schema.d.ts +1 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +55 -4
- package/dist/types.d.ts +35 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +29 -4
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +301 -85
- package/dist/yaml.d.ts.map +1 -1
- package/dist/yaml.js +36 -3
- package/fixtures/README.md +26 -6
- package/fixtures/cases/conditional-section/context.json +1 -0
- package/fixtures/cases/conditional-section/expected.json +6 -0
- package/fixtures/cases/conditional-section/library/questions/hosting.md +9 -0
- package/fixtures/cases/conditional-section/library/questions/payments.md +12 -0
- package/fixtures/cases/conditional-section/schema.yaml +14 -0
- package/fixtures/cases/cross-room-link/context.json +1 -0
- package/fixtures/cases/cross-room-link/expected.json +1 -0
- package/fixtures/cases/cross-room-link/library/clients/acme.md +6 -0
- package/fixtures/cases/cross-room-link/schema.yaml +3 -0
- package/fixtures/cases/cross-room-reference/context.json +1 -0
- package/fixtures/cases/cross-room-reference/expected.json +1 -0
- package/fixtures/cases/cross-room-reference/library/clients/acme.md +9 -0
- package/fixtures/cases/cross-room-reference/schema.yaml +9 -0
- package/fixtures/cases/link-into-a-reserved-name/context.json +1 -0
- package/fixtures/cases/link-into-a-reserved-name/expected.json +5 -0
- package/fixtures/cases/link-into-a-reserved-name/library/notes/a.md +6 -0
- package/fixtures/cases/link-into-a-reserved-name/schema.yaml +3 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/context.json +1 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/expected.json +5 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/library/notes/b.md +5 -0
- package/fixtures/cases/member-on-an-untyped-document/context.json +1 -0
- package/fixtures/cases/member-on-an-untyped-document/expected.json +6 -0
- package/fixtures/cases/member-on-an-untyped-document/library/note.md +4 -0
- package/fixtures/cases/member-on-another-type/context.json +1 -0
- package/fixtures/cases/member-on-another-type/expected.json +6 -0
- package/fixtures/cases/member-on-another-type/library/note.md +4 -0
- package/fixtures/cases/member-on-another-type/schema.yaml +7 -0
- package/fixtures/cases/reference-into-a-reserved-name/context.json +1 -0
- package/fixtures/cases/reference-into-a-reserved-name/expected.json +6 -0
- package/fixtures/cases/reference-into-a-reserved-name/library/notes/a.md +4 -0
- package/fixtures/cases/reference-into-a-reserved-name/library/settings/brief.md +1 -0
- package/fixtures/cases/reference-into-a-reserved-name/schema.yaml +3 -0
- package/fixtures/cases/room-md-declares-a-type/context.json +1 -0
- package/fixtures/cases/room-md-declares-a-type/expected.json +6 -0
- package/fixtures/cases/room-md-declares-a-type/library/ROOM.md +5 -0
- package/fixtures/cases/typed-path-names-another-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-another-type/expected.json +6 -0
- package/fixtures/cases/typed-path-names-another-type/library/settings/prefs/one.md +3 -0
- package/fixtures/cases/typed-path-names-another-type/schema.yaml +6 -0
- package/fixtures/cases/typed-path-names-its-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-its-type/expected.json +3 -0
- package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/deeper/two.md +1 -0
- package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/one.md +4 -0
- package/fixtures/cases/typed-path-names-its-type/schema.yaml +6 -0
- package/fixtures/cases/typed-path-names-no-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-no-type/expected.json +6 -0
- package/fixtures/cases/typed-path-names-no-type/library/settings/prefs/one.md +1 -0
- package/fixtures/cases/typed-path-names-no-type/schema.yaml +6 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/expected.json +6 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/library/rooms/team/tasks/plan.md +4 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/expected.json +6 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/library/rooms/team/tasks/plan.md +6 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/expected.json +3 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/plan.md +1 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/tasks/plan.md +5 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/expected.json +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/library/rooms/team/tasks/plan.md +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/schema.yaml +5 -0
- package/fixtures/division.json +26 -26
- package/fixtures/queries.json +913 -0
- package/package.json +7 -2
- package/dist/condition.d.ts +0 -149
- package/dist/condition.d.ts.map +0 -1
- package/dist/condition.js +0 -500
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
import { crossedFacts, escapePointer, pointerFacts, pointerSegments, } from '../definition.js';
|
|
2
|
+
import { fitsKind, kindOf, kindWords } from '../kinds.js';
|
|
3
|
+
import { normalize } from '../paths.js';
|
|
4
|
+
import { plain } from '../yaml.js';
|
|
5
|
+
export class QueryError extends Error {
|
|
6
|
+
code;
|
|
7
|
+
place;
|
|
8
|
+
accepts;
|
|
9
|
+
retryable = false;
|
|
10
|
+
constructor(code, at, problem, accepts = []) {
|
|
11
|
+
super(`${code} at ${placeText(placed(at))}: ${problem}`);
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.place = placed(at);
|
|
14
|
+
this.accepts = accepts;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const RANGE = ['$gt', '$gte', '$lt', '$lte'];
|
|
18
|
+
const SINGLE = ['$eq', '$ne', '$in', '$nin', '$all', '$size', '$exists', '$prefix'];
|
|
19
|
+
const FILTER = [
|
|
20
|
+
'rooms',
|
|
21
|
+
'types',
|
|
22
|
+
'where',
|
|
23
|
+
'pathPrefix',
|
|
24
|
+
'folder',
|
|
25
|
+
'paths',
|
|
26
|
+
'fileIds',
|
|
27
|
+
'hop',
|
|
28
|
+
'backlinks',
|
|
29
|
+
'text',
|
|
30
|
+
];
|
|
31
|
+
const RELATED = ['types', 'where', 'pathPrefix', 'field'];
|
|
32
|
+
const PARTS = [
|
|
33
|
+
'fields',
|
|
34
|
+
'sections',
|
|
35
|
+
'preamble',
|
|
36
|
+
'markdown',
|
|
37
|
+
'backlinks',
|
|
38
|
+
'change',
|
|
39
|
+
'fileId',
|
|
40
|
+
'revision',
|
|
41
|
+
];
|
|
42
|
+
const ORDER = ['by', 'direction', 'nulls', 'stringSort', 'locale', 'localeOptions'];
|
|
43
|
+
const STRAY_ESCAPE = /~(?![01])/u;
|
|
44
|
+
export function bodied(shape) {
|
|
45
|
+
return shape.sections !== null || shape.preamble || shape.markdown;
|
|
46
|
+
}
|
|
47
|
+
export function invalid(at, problem, accepts = []) {
|
|
48
|
+
throw new QueryError('invalid-query', at, problem, accepts);
|
|
49
|
+
}
|
|
50
|
+
function placed(at) {
|
|
51
|
+
if (typeof at !== 'string')
|
|
52
|
+
return at;
|
|
53
|
+
return at === '' ? [] : [at];
|
|
54
|
+
}
|
|
55
|
+
export function placeText(at) {
|
|
56
|
+
return at.reduce((text, step) => {
|
|
57
|
+
if (typeof step === 'number')
|
|
58
|
+
return `${text}[${step}]`;
|
|
59
|
+
if (step.startsWith('/') || text === '')
|
|
60
|
+
return `${text}${step}`;
|
|
61
|
+
return `${text}.${step}`;
|
|
62
|
+
}, '');
|
|
63
|
+
}
|
|
64
|
+
function place(at, step) {
|
|
65
|
+
return [...placed(at), step];
|
|
66
|
+
}
|
|
67
|
+
export function known(value, allowed, at) {
|
|
68
|
+
for (const name of Object.keys(value)) {
|
|
69
|
+
if (!allowed.includes(name))
|
|
70
|
+
invalid(place(at, name), 'unknown part', allowed);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function list(input, at) {
|
|
74
|
+
if (!Array.isArray(input))
|
|
75
|
+
invalid(at, 'expects a list');
|
|
76
|
+
return input;
|
|
77
|
+
}
|
|
78
|
+
function value(input, at) {
|
|
79
|
+
if (Array.isArray(input))
|
|
80
|
+
invalid(at, 'a list is not a value');
|
|
81
|
+
if (input !== null &&
|
|
82
|
+
typeof input !== 'string' &&
|
|
83
|
+
typeof input !== 'boolean' &&
|
|
84
|
+
!(typeof input === 'number' && Number.isFinite(input))) {
|
|
85
|
+
invalid(at, 'not a value');
|
|
86
|
+
}
|
|
87
|
+
return input;
|
|
88
|
+
}
|
|
89
|
+
function values(input, at) {
|
|
90
|
+
return list(input, at).map((item, index) => value(item, place(at, index)));
|
|
91
|
+
}
|
|
92
|
+
function names(input, at) {
|
|
93
|
+
const listed = list(input, at);
|
|
94
|
+
if (listed.some((item) => typeof item !== 'string'))
|
|
95
|
+
invalid(at, 'expects a list of names');
|
|
96
|
+
return [...new Set(listed)].sort();
|
|
97
|
+
}
|
|
98
|
+
function pathsOf(input) {
|
|
99
|
+
const listed = list(input, 'paths');
|
|
100
|
+
if (listed.some((item) => typeof item !== 'string' || item === '' || normalize(item) !== item)) {
|
|
101
|
+
invalid('paths', 'expects a list of Paths');
|
|
102
|
+
}
|
|
103
|
+
return [...new Set(listed)].sort();
|
|
104
|
+
}
|
|
105
|
+
function fileIdsOf(input) {
|
|
106
|
+
const listed = list(input, 'fileIds');
|
|
107
|
+
if (listed.some((item) => typeof item !== 'string' || item === '')) {
|
|
108
|
+
invalid('fileIds', 'expects a list of file IDs');
|
|
109
|
+
}
|
|
110
|
+
return [...new Set(listed)].sort();
|
|
111
|
+
}
|
|
112
|
+
function phrase(input) {
|
|
113
|
+
if (typeof input !== 'string' || input === '')
|
|
114
|
+
invalid('text', 'expects a phrase');
|
|
115
|
+
return input;
|
|
116
|
+
}
|
|
117
|
+
function directory(input, at) {
|
|
118
|
+
if (typeof input !== 'string' || normalize(input) !== input) {
|
|
119
|
+
invalid(at, 'expects a normalized directory');
|
|
120
|
+
}
|
|
121
|
+
return input;
|
|
122
|
+
}
|
|
123
|
+
function itemOf(facts) {
|
|
124
|
+
return facts.type === 'array' ? facts.items : facts;
|
|
125
|
+
}
|
|
126
|
+
function fits(facts, value) {
|
|
127
|
+
const item = itemOf(facts);
|
|
128
|
+
if (item === undefined || (item.type === undefined && item.values === undefined))
|
|
129
|
+
return true;
|
|
130
|
+
const kind = kindOf(item);
|
|
131
|
+
return kind !== null && fitsKind(kind, item, value);
|
|
132
|
+
}
|
|
133
|
+
function textless(facts) {
|
|
134
|
+
const item = itemOf(facts);
|
|
135
|
+
if (item?.values !== undefined)
|
|
136
|
+
return !item.values.some((held) => typeof held === 'string');
|
|
137
|
+
return item?.type !== undefined && item.type !== 'string';
|
|
138
|
+
}
|
|
139
|
+
function typing(facts, rooms, types, at) {
|
|
140
|
+
const read = rooms === null ? facts.rooms : facts.rooms.filter((room) => rooms.includes(room.locator));
|
|
141
|
+
const acquiring = !facts.listed &&
|
|
142
|
+
(rooms === null ||
|
|
143
|
+
rooms.some((locator) => !facts.rooms.some((room) => room.locator === locator)));
|
|
144
|
+
const available = !acquiring && read.every((room) => room.available);
|
|
145
|
+
const declared = read.flatMap((room) => room.types);
|
|
146
|
+
for (const name of types ?? []) {
|
|
147
|
+
if (name === 'any' || !available || declared.some((type) => type.name === name))
|
|
148
|
+
continue;
|
|
149
|
+
throw new QueryError('unknown-type', place(at, 'types'), `no model it reads declares ${name}`, [...new Set(declared.map((type) => type.name))].sort());
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
untyped: types === null || types.includes('any') || !available,
|
|
153
|
+
types: types === null ? declared : declared.filter((type) => types.includes(type.name)),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function pointer(input, at) {
|
|
157
|
+
if (typeof input !== 'string' || !input.startsWith('/') || STRAY_ESCAPE.test(input)) {
|
|
158
|
+
invalid(at, 'expects a JSON Pointer');
|
|
159
|
+
}
|
|
160
|
+
return input;
|
|
161
|
+
}
|
|
162
|
+
function specs(typed, input, at) {
|
|
163
|
+
const field = pointer(input, at);
|
|
164
|
+
const declared = typed.types.flatMap((type) => {
|
|
165
|
+
const spec = pointerFacts(type, field);
|
|
166
|
+
return spec === undefined ? [] : [spec];
|
|
167
|
+
});
|
|
168
|
+
if (declared.length === 0 && !typed.untyped) {
|
|
169
|
+
throw new QueryError('unknown-field', at, `no MD Type it reads declares ${field}`, nearby(typed.types, field));
|
|
170
|
+
}
|
|
171
|
+
if (!declared.some((spec) => spec.conditional))
|
|
172
|
+
return declared;
|
|
173
|
+
return declared.map((spec) => ({
|
|
174
|
+
facts: { prose: false },
|
|
175
|
+
through: several(spec),
|
|
176
|
+
conditional: true,
|
|
177
|
+
}));
|
|
178
|
+
}
|
|
179
|
+
function nearby(types, field) {
|
|
180
|
+
const segments = pointerSegments(field);
|
|
181
|
+
for (let length = segments.length - 1; length > 0; length -= 1) {
|
|
182
|
+
const prefix = segments
|
|
183
|
+
.slice(0, length)
|
|
184
|
+
.map((segment) => `/${escapePointer(segment)}`)
|
|
185
|
+
.join('');
|
|
186
|
+
const children = types.flatMap((type) => {
|
|
187
|
+
const reached = pointerFacts(type, prefix);
|
|
188
|
+
return (reached === undefined ? [] : crossedFacts(reached.facts)).flatMap((crossed) => Object.keys(crossed.properties ?? {}).map((key) => `${prefix}/${escapePointer(key)}`));
|
|
189
|
+
});
|
|
190
|
+
if (children.length > 0)
|
|
191
|
+
return [...new Set(children)].sort();
|
|
192
|
+
}
|
|
193
|
+
return [
|
|
194
|
+
...new Set(types.flatMap((type) => Object.keys(type.fields).map((name) => `/${escapePointer(name)}`))),
|
|
195
|
+
].sort();
|
|
196
|
+
}
|
|
197
|
+
function several(spec) {
|
|
198
|
+
return spec.through || spec.facts.type === 'array';
|
|
199
|
+
}
|
|
200
|
+
function single(spec) {
|
|
201
|
+
return spec.facts.type !== undefined && spec.facts.type !== 'array';
|
|
202
|
+
}
|
|
203
|
+
function every(declared, holds) {
|
|
204
|
+
return declared.length > 0 && declared.every(holds);
|
|
205
|
+
}
|
|
206
|
+
function kind(declared, checked, at) {
|
|
207
|
+
if (checked === null ||
|
|
208
|
+
declared.length === 0 ||
|
|
209
|
+
declared.some((spec) => fits(spec.facts, checked))) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
throw new QueryError('wrong-kind', at, `${JSON.stringify(checked)} is not of its field's kind`, [
|
|
213
|
+
...new Set(declared.flatMap((spec) => kindWords(itemOf(spec.facts)))),
|
|
214
|
+
]);
|
|
215
|
+
}
|
|
216
|
+
function test(declared, input, at) {
|
|
217
|
+
if (!plain(input)) {
|
|
218
|
+
const equal = value(input, at);
|
|
219
|
+
kind(declared, equal, at);
|
|
220
|
+
return { op: 'eq', value: equal };
|
|
221
|
+
}
|
|
222
|
+
const operators = Object.keys(input);
|
|
223
|
+
if (operators.length === 0)
|
|
224
|
+
invalid(at, 'names no operator');
|
|
225
|
+
if (operators.some((operator) => RANGE.includes(operator))) {
|
|
226
|
+
const stray = operators.find((operator) => !RANGE.includes(operator));
|
|
227
|
+
if (stray !== undefined)
|
|
228
|
+
invalid(place(at, stray), 'cannot join a range');
|
|
229
|
+
if (every(declared, (spec) => spec.facts.type === 'array'))
|
|
230
|
+
invalid(at, 'a range on a list');
|
|
231
|
+
if (every(declared, (spec) => spec.facts.type === 'boolean' || spec.facts.type === 'object')) {
|
|
232
|
+
throw new QueryError('wrong-kind', at, 'its field does not compare in order');
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
op: 'range',
|
|
236
|
+
bounds: RANGE.filter((operator) => operators.includes(operator)).map((operator) => {
|
|
237
|
+
const bound = value(input[operator], place(at, operator));
|
|
238
|
+
if (bound === null)
|
|
239
|
+
invalid(place(at, operator), 'a range bound is never null');
|
|
240
|
+
kind(declared, bound, place(at, operator));
|
|
241
|
+
return [operator.slice(1), bound];
|
|
242
|
+
}),
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
if (operators.length > 1)
|
|
246
|
+
invalid(place(at, operators[1]), 'names a second operator');
|
|
247
|
+
const [operator] = operators;
|
|
248
|
+
const where = place(at, operator);
|
|
249
|
+
if (!SINGLE.includes(operator)) {
|
|
250
|
+
invalid(where, 'unknown operator', [...SINGLE, ...RANGE]);
|
|
251
|
+
}
|
|
252
|
+
const operand = input[operator];
|
|
253
|
+
if (operator === '$eq' || operator === '$ne') {
|
|
254
|
+
const compared = value(operand, where);
|
|
255
|
+
kind(declared, compared, where);
|
|
256
|
+
return operator === '$eq' ? { op: 'eq', value: compared } : { op: 'ne', value: compared };
|
|
257
|
+
}
|
|
258
|
+
if (operator === '$in' || operator === '$nin' || operator === '$all') {
|
|
259
|
+
const compared = values(operand, where);
|
|
260
|
+
compared.forEach((item, index) => kind(declared, item, place(where, index)));
|
|
261
|
+
if (operator === '$in')
|
|
262
|
+
return { op: 'in', values: compared };
|
|
263
|
+
if (operator === '$nin')
|
|
264
|
+
return { op: 'nin', values: compared };
|
|
265
|
+
if (compared.includes(null))
|
|
266
|
+
invalid(where, 'a list holds no null');
|
|
267
|
+
if (every(declared, single))
|
|
268
|
+
invalid(where, 'the field is not a list');
|
|
269
|
+
return { op: 'all', values: compared };
|
|
270
|
+
}
|
|
271
|
+
if (operator === '$size') {
|
|
272
|
+
if (!Number.isSafeInteger(operand) || operand < 0) {
|
|
273
|
+
invalid(where, 'expects a nonnegative integer');
|
|
274
|
+
}
|
|
275
|
+
if (every(declared, single))
|
|
276
|
+
invalid(where, 'the field is not a list');
|
|
277
|
+
return { op: 'size', size: operand };
|
|
278
|
+
}
|
|
279
|
+
if (operator === '$prefix') {
|
|
280
|
+
if (typeof operand !== 'string')
|
|
281
|
+
invalid(where, 'expects a string');
|
|
282
|
+
if (every(declared, (spec) => textless(spec.facts))) {
|
|
283
|
+
throw new QueryError('wrong-kind', where, 'its field holds no text');
|
|
284
|
+
}
|
|
285
|
+
return { op: 'prefix', prefix: operand };
|
|
286
|
+
}
|
|
287
|
+
if (typeof operand !== 'boolean')
|
|
288
|
+
invalid(where, 'expects true or false');
|
|
289
|
+
return { op: 'exists', exists: operand };
|
|
290
|
+
}
|
|
291
|
+
function condition(typed, input, at) {
|
|
292
|
+
if (!plain(input))
|
|
293
|
+
invalid(at, 'expects an object of field predicates');
|
|
294
|
+
const conditions = [];
|
|
295
|
+
for (const name of Object.keys(input).sort()) {
|
|
296
|
+
const where = place(at, name);
|
|
297
|
+
const entry = input[name];
|
|
298
|
+
if (name === '$and' || name === '$or') {
|
|
299
|
+
const nested = list(entry, where).map((item, index) => condition(typed, item, place(where, index)));
|
|
300
|
+
if (nested.length === 0)
|
|
301
|
+
invalid(where, 'expects a nonempty list');
|
|
302
|
+
conditions.push(name === '$and' ? { every: nested } : { some: nested });
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
if (name.startsWith('$'))
|
|
306
|
+
invalid(where, 'unknown operator');
|
|
307
|
+
conditions.push({ field: name, test: test(specs(typed, name, where), entry, where) });
|
|
308
|
+
}
|
|
309
|
+
if (conditions.length === 0)
|
|
310
|
+
invalid(at, 'names no field');
|
|
311
|
+
return conditions.length === 1 ? conditions[0] : { every: conditions };
|
|
312
|
+
}
|
|
313
|
+
function scope(facts, rooms, input, at) {
|
|
314
|
+
const types = input.types === undefined ? null : names(input.types, place(at, 'types'));
|
|
315
|
+
const typed = typing(facts, rooms, types, at);
|
|
316
|
+
return {
|
|
317
|
+
typed,
|
|
318
|
+
types,
|
|
319
|
+
where: input.where === undefined || (plain(input.where) && Object.keys(input.where).length === 0)
|
|
320
|
+
? null
|
|
321
|
+
: condition(typed, input.where, place(at, 'where')),
|
|
322
|
+
pathPrefix: input.pathPrefix === undefined ? null : directory(input.pathPrefix, place(at, 'pathPrefix')),
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
function referenceName(typed, field, at) {
|
|
326
|
+
const declared = specs(typed, field, at);
|
|
327
|
+
if (declared.length > 0 &&
|
|
328
|
+
!declared.some((spec) => spec.conditional ||
|
|
329
|
+
spec.facts.reference !== undefined ||
|
|
330
|
+
spec.facts.items?.reference !== undefined)) {
|
|
331
|
+
invalid(at, 'not a reference field');
|
|
332
|
+
}
|
|
333
|
+
return field;
|
|
334
|
+
}
|
|
335
|
+
function hopOf(facts, rooms, root, input) {
|
|
336
|
+
if (!plain(input))
|
|
337
|
+
invalid('hop', 'expects an object');
|
|
338
|
+
known(input, RELATED, 'hop');
|
|
339
|
+
const field = referenceName(root, input.field, ['hop', 'field']);
|
|
340
|
+
const { types, where, pathPrefix } = scope(facts, rooms, input, 'hop');
|
|
341
|
+
return { types, where, pathPrefix, field };
|
|
342
|
+
}
|
|
343
|
+
function backlinksOf(facts, rooms, input) {
|
|
344
|
+
if (!plain(input))
|
|
345
|
+
invalid('backlinks', 'expects an object');
|
|
346
|
+
known(input, RELATED, 'backlinks');
|
|
347
|
+
const { typed, types, where, pathPrefix } = scope(facts, rooms, input, 'backlinks');
|
|
348
|
+
const field = input.field === undefined ? null : referenceName(typed, input.field, ['backlinks', 'field']);
|
|
349
|
+
return { types, where, pathPrefix, field };
|
|
350
|
+
}
|
|
351
|
+
function keyPlan(typed, input, at, path) {
|
|
352
|
+
if (input === 'room' || input === 'type' || input === 'folder' || (path && input === 'path')) {
|
|
353
|
+
return { of: input };
|
|
354
|
+
}
|
|
355
|
+
if (plain(input) && Object.hasOwn(input, 'field') && Object.keys(input).length === 1) {
|
|
356
|
+
if (specs(typed, input.field, place(at, 'field')).some(several)) {
|
|
357
|
+
invalid(place(at, 'field'), 'a path that can hold several values');
|
|
358
|
+
}
|
|
359
|
+
return { of: 'field', field: input.field };
|
|
360
|
+
}
|
|
361
|
+
return invalid(at, path ? 'expects room, path, type, folder or a field' : 'expects room, type, folder or a field');
|
|
362
|
+
}
|
|
363
|
+
function direction(input, at) {
|
|
364
|
+
if (input === undefined)
|
|
365
|
+
return 'asc';
|
|
366
|
+
if (input !== 'asc' && input !== 'desc')
|
|
367
|
+
invalid(at, 'expects asc or desc');
|
|
368
|
+
return input;
|
|
369
|
+
}
|
|
370
|
+
function orderOptions(input, at) {
|
|
371
|
+
const options = {
|
|
372
|
+
direction: direction(input.direction, place(at, 'direction')),
|
|
373
|
+
};
|
|
374
|
+
if (input.nulls !== undefined) {
|
|
375
|
+
if (input.nulls !== 'first' && input.nulls !== 'last') {
|
|
376
|
+
invalid(place(at, 'nulls'), 'expects first or last');
|
|
377
|
+
}
|
|
378
|
+
options.nulls = input.nulls;
|
|
379
|
+
}
|
|
380
|
+
if (input.stringSort !== undefined) {
|
|
381
|
+
if (input.stringSort !== 'lexical' && input.stringSort !== 'locale') {
|
|
382
|
+
invalid(place(at, 'stringSort'), 'expects lexical or locale');
|
|
383
|
+
}
|
|
384
|
+
options.stringSort = input.stringSort;
|
|
385
|
+
}
|
|
386
|
+
if (input.locale !== undefined) {
|
|
387
|
+
if (typeof input.locale !== 'string' || !collates(input.locale, undefined)) {
|
|
388
|
+
invalid(place(at, 'locale'), 'expects a locale name');
|
|
389
|
+
}
|
|
390
|
+
options.locale = input.locale;
|
|
391
|
+
}
|
|
392
|
+
if (input.localeOptions !== undefined) {
|
|
393
|
+
if (!plain(input.localeOptions) || !collates(options.locale, input.localeOptions)) {
|
|
394
|
+
invalid(place(at, 'localeOptions'), 'expects collation options');
|
|
395
|
+
}
|
|
396
|
+
options.localeOptions = input.localeOptions;
|
|
397
|
+
}
|
|
398
|
+
return options;
|
|
399
|
+
}
|
|
400
|
+
function collates(locale, options) {
|
|
401
|
+
try {
|
|
402
|
+
''.localeCompare('', locale, options);
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
catch {
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function parts(typed, input, at, fields) {
|
|
410
|
+
if (input === undefined)
|
|
411
|
+
return null;
|
|
412
|
+
if (input === 'all')
|
|
413
|
+
return 'all';
|
|
414
|
+
const named = list(input, at);
|
|
415
|
+
named.forEach((name, index) => {
|
|
416
|
+
if (fields)
|
|
417
|
+
specs(typed, name, place(at, index));
|
|
418
|
+
else if (typeof name !== 'string')
|
|
419
|
+
invalid(place(at, index), 'expects a name');
|
|
420
|
+
});
|
|
421
|
+
return named;
|
|
422
|
+
}
|
|
423
|
+
function flag(input, at) {
|
|
424
|
+
if (input === undefined)
|
|
425
|
+
return false;
|
|
426
|
+
if (input !== true)
|
|
427
|
+
invalid(at, 'expects true');
|
|
428
|
+
return true;
|
|
429
|
+
}
|
|
430
|
+
function documentShape(typed, input) {
|
|
431
|
+
const orderBy = (input.orderBy === undefined ? [] : list(input.orderBy, 'orderBy')).map((clause, index) => {
|
|
432
|
+
const at = place('orderBy', index);
|
|
433
|
+
if (!plain(clause))
|
|
434
|
+
invalid(at, 'expects an order clause');
|
|
435
|
+
known(clause, ORDER, at);
|
|
436
|
+
return {
|
|
437
|
+
key: keyPlan(typed, clause.by, place(at, 'by'), true),
|
|
438
|
+
options: orderOptions(clause, at),
|
|
439
|
+
};
|
|
440
|
+
});
|
|
441
|
+
const select = input.select;
|
|
442
|
+
if (select === 'all') {
|
|
443
|
+
return {
|
|
444
|
+
kind: 'documents',
|
|
445
|
+
fields: 'all',
|
|
446
|
+
sections: 'all',
|
|
447
|
+
preamble: true,
|
|
448
|
+
markdown: true,
|
|
449
|
+
backlinks: true,
|
|
450
|
+
change: true,
|
|
451
|
+
fileId: true,
|
|
452
|
+
revision: true,
|
|
453
|
+
orderBy,
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
if (!plain(select))
|
|
457
|
+
invalid('select', "expects 'all' or the parts to return");
|
|
458
|
+
known(select, PARTS, 'select');
|
|
459
|
+
return {
|
|
460
|
+
kind: 'documents',
|
|
461
|
+
fields: parts(typed, select.fields, ['select', 'fields'], true),
|
|
462
|
+
sections: parts(typed, select.sections, ['select', 'sections'], false),
|
|
463
|
+
preamble: flag(select.preamble, ['select', 'preamble']),
|
|
464
|
+
markdown: flag(select.markdown, ['select', 'markdown']),
|
|
465
|
+
backlinks: flag(select.backlinks, ['select', 'backlinks']),
|
|
466
|
+
change: flag(select.change, ['select', 'change']),
|
|
467
|
+
fileId: flag(select.fileId, ['select', 'fileId']),
|
|
468
|
+
revision: flag(select.revision, ['select', 'revision']),
|
|
469
|
+
orderBy,
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
function groupShape(typed, input) {
|
|
473
|
+
const groupBy = list(input.groupBy, 'groupBy').map((key, index) => keyPlan(typed, key, place('groupBy', index), false));
|
|
474
|
+
if (groupBy.length === 0)
|
|
475
|
+
invalid('groupBy', 'names no key');
|
|
476
|
+
const grouped = groupBy.map((key) => JSON.stringify(key));
|
|
477
|
+
const orderBy = (input.orderBy === undefined ? [] : list(input.orderBy, 'orderBy')).map((clause, index) => {
|
|
478
|
+
const at = place('orderBy', index);
|
|
479
|
+
if (!plain(clause))
|
|
480
|
+
invalid(at, 'expects an order clause');
|
|
481
|
+
known(clause, ['by', 'direction'], at);
|
|
482
|
+
const by = clause.by === 'count'
|
|
483
|
+
? 'count'
|
|
484
|
+
: grouped.indexOf(JSON.stringify(keyPlan(typed, clause.by, place(at, 'by'), false)));
|
|
485
|
+
if (by === -1)
|
|
486
|
+
invalid(place(at, 'by'), 'orders by a key the query does not group by');
|
|
487
|
+
return { by, direction: direction(clause.direction, place(at, 'direction')) };
|
|
488
|
+
});
|
|
489
|
+
return { kind: 'groups', groupBy, orderBy };
|
|
490
|
+
}
|
|
491
|
+
export function planOf(input, facts) {
|
|
492
|
+
if (!plain(input))
|
|
493
|
+
invalid('query', 'expects a query object');
|
|
494
|
+
const grouped = input.groupBy !== undefined;
|
|
495
|
+
if (grouped && input.select !== undefined)
|
|
496
|
+
invalid('select', 'a grouped query selects nothing');
|
|
497
|
+
if (!grouped && input.select === undefined)
|
|
498
|
+
invalid('select', 'a Document query must select');
|
|
499
|
+
known(input, [...FILTER, 'orderBy', 'limit', 'after', 'total', grouped ? 'groupBy' : 'select'], '');
|
|
500
|
+
const limit = limitOf(input.limit);
|
|
501
|
+
const rooms = input.rooms === undefined ? null : names(input.rooms, 'rooms');
|
|
502
|
+
const { typed, ...root } = scope(facts, rooms, input, '');
|
|
503
|
+
const shape = grouped ? groupShape(typed, input) : documentShape(typed, input);
|
|
504
|
+
return {
|
|
505
|
+
key: queryKey(input),
|
|
506
|
+
rooms,
|
|
507
|
+
root,
|
|
508
|
+
folder: input.folder === undefined ? null : directory(input.folder, 'folder'),
|
|
509
|
+
paths: input.paths === undefined ? null : pathsOf(input.paths),
|
|
510
|
+
fileIds: input.fileIds === undefined ? null : fileIdsOf(input.fileIds),
|
|
511
|
+
hop: input.hop === undefined ? null : hopOf(facts, rooms, typed, input.hop),
|
|
512
|
+
backlinks: input.backlinks === undefined ? null : backlinksOf(facts, rooms, input.backlinks),
|
|
513
|
+
text: input.text === undefined ? null : phrase(input.text),
|
|
514
|
+
shape,
|
|
515
|
+
limit,
|
|
516
|
+
after: continuationOf(input.after, shape.orderBy.length + 1),
|
|
517
|
+
total: flag(input.total, 'total'),
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
function continuationOf(input, arity) {
|
|
521
|
+
if (input === undefined)
|
|
522
|
+
return null;
|
|
523
|
+
const values = typeof input === 'string' ? decodedCursor(input) : null;
|
|
524
|
+
if (values === null || (values.length !== 0 && values.length !== arity)) {
|
|
525
|
+
invalid('after', 'expects the next an earlier result of this query returned');
|
|
526
|
+
}
|
|
527
|
+
return { text: input, values: values.length === 0 ? null : values };
|
|
528
|
+
}
|
|
529
|
+
function limitOf(input) {
|
|
530
|
+
if (input === undefined)
|
|
531
|
+
return null;
|
|
532
|
+
if (!Number.isSafeInteger(input) || input < 0) {
|
|
533
|
+
invalid('limit', 'expects a nonnegative integer');
|
|
534
|
+
}
|
|
535
|
+
return input;
|
|
536
|
+
}
|
|
537
|
+
export function cursorOf(values) {
|
|
538
|
+
return JSON.stringify(values);
|
|
539
|
+
}
|
|
540
|
+
export function decodedCursor(text) {
|
|
541
|
+
try {
|
|
542
|
+
const decoded = JSON.parse(text);
|
|
543
|
+
return Array.isArray(decoded) ? decoded : null;
|
|
544
|
+
}
|
|
545
|
+
catch {
|
|
546
|
+
return null;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
export function queryKey(query) {
|
|
550
|
+
return canonical(query);
|
|
551
|
+
}
|
|
552
|
+
function canonical(value) {
|
|
553
|
+
if (Array.isArray(value))
|
|
554
|
+
return `[${value.map(canonical).join(',')}]`;
|
|
555
|
+
if (typeof value === 'object' && value !== null) {
|
|
556
|
+
return `{${Object.keys(value)
|
|
557
|
+
.sort()
|
|
558
|
+
.map((name) => `${JSON.stringify(name)}:${canonical(value[name])}`)
|
|
559
|
+
.join(',')}}`;
|
|
560
|
+
}
|
|
561
|
+
return JSON.stringify(value);
|
|
562
|
+
}
|
|
563
|
+
export function identityOf(plan) {
|
|
564
|
+
return canonical(plan);
|
|
565
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Definition } from '../definition.js';
|
|
2
|
+
import { type QueryPlan } from './language.js';
|
|
3
|
+
import { type HeldValue } from './rows.js';
|
|
4
|
+
export type Candidate = {
|
|
5
|
+
readonly path: string;
|
|
6
|
+
readonly type: string | null;
|
|
7
|
+
readonly fileId: string | null;
|
|
8
|
+
readonly fields: Readonly<Record<string, unknown>>;
|
|
9
|
+
readonly definition?: Pick<Definition, 'frontmatter'>;
|
|
10
|
+
readonly text: string | null;
|
|
11
|
+
};
|
|
12
|
+
export declare function heldAt(candidate: Candidate, field: string): readonly HeldValue[];
|
|
13
|
+
export declare function matches(plan: QueryPlan, candidate: Candidate): boolean;
|
|
14
|
+
//# sourceMappingURL=match.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"match.d.ts","sourceRoot":"","sources":["../../src/query/match.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAGnD,OAAO,EAGL,KAAK,SAAS,EAIf,MAAM,eAAe,CAAC;AACvB,OAAO,EAAqB,KAAK,SAAS,EAAE,MAAM,WAAW,CAAC;AAE9D,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACnD,QAAQ,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;IACtD,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B,CAAC;AAyHF,wBAAgB,MAAM,CAAC,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,SAAS,EAAE,CAEhF;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,GAAG,OAAO,CAgBtE"}
|