@yaks/lens 0.0.0-stage → 0.2.36
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 +202 -0
- package/README.md +228 -2
- package/_dist/compile.d.ts +30 -0
- package/_dist/compile.d.ts.map +1 -0
- package/_dist/described.d.ts +12 -0
- package/_dist/described.d.ts.map +1 -0
- package/_dist/document.d.ts +59 -0
- package/_dist/document.d.ts.map +1 -0
- package/_dist/graph.d.ts +3 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/mod.d.ts +7 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/plugin.d.ts +9 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/steps.d.ts +10 -0
- package/_dist/steps.d.ts.map +1 -0
- package/_dist/view.d.ts +40 -0
- package/_dist/view.d.ts.map +1 -0
- package/_dist/vocab.d.ts +4 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/compile.js +318 -0
- package/compile.js.map +1 -0
- package/compile.ts +290 -0
- package/deno.json +1 -0
- package/described.js +80 -0
- package/described.js.map +1 -0
- package/described.ts +85 -0
- package/document.js +268 -0
- package/document.js.map +1 -0
- package/document.ts +335 -0
- package/graph.js +5 -0
- package/graph.js.map +1 -0
- package/graph.ts +4 -0
- package/mod.js +7 -0
- package/mod.js.map +1 -0
- package/mod.ts +6 -0
- package/package.json +28 -4
- package/plugin.js +454 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +597 -0
- package/schema.js +61 -0
- package/schema.js.map +1 -0
- package/schema.ts +53 -0
- package/steps.js +35 -0
- package/steps.js.map +1 -0
- package/steps.ts +47 -0
- package/tools.fixture.json +190 -0
- package/view.js +430 -0
- package/view.js.map +1 -0
- package/view.ts +448 -0
- package/vocab.js +11 -0
- package/vocab.js.map +1 -0
- package/vocab.json +34 -0
- package/vocab.ts +8 -0
package/plugin.ts
ADDED
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
import type { Bundle, Plugin } from 'jsr:@yaks/graph@0.2.36'
|
|
2
|
+
import { Refused, Stale } from '@yaks/graph/errors'
|
|
3
|
+
import { after, each } from '@yaks/fp'
|
|
4
|
+
import { and, every, line, map, or, parse, present } from '@yaks/query'
|
|
5
|
+
import { loadVocab, type Vocab } from '@yaks/vocab'
|
|
6
|
+
import { compile, type Lens, type Speaks } from './compile.js'
|
|
7
|
+
import { docs } from './vocab.js'
|
|
8
|
+
import type { ReadTx } from 'jsr:@yaks/graph@0.2.36'
|
|
9
|
+
|
|
10
|
+
// The effectful shell supplies facts to the compiled, pure whole-change lens.
|
|
11
|
+
// The transaction repeats translation against current facts. Comparing its
|
|
12
|
+
// complete output guards relation membership as well as individual values:
|
|
13
|
+
// a concurrent acknowledgement cannot escape an explicit clear.
|
|
14
|
+
let facts = (
|
|
15
|
+
lens: Lens,
|
|
16
|
+
rows: Bundle[],
|
|
17
|
+
tx: ReadTx,
|
|
18
|
+
): Bundle[] | Promise<Bundle[]> => {
|
|
19
|
+
let queries = [
|
|
20
|
+
...new Set(
|
|
21
|
+
lens.views.flatMap((v) =>
|
|
22
|
+
Object.values(v.props).flatMap((p) =>
|
|
23
|
+
'ack' in p
|
|
24
|
+
? [
|
|
25
|
+
line`.${p.ack.component}&?${
|
|
26
|
+
p.ack.order.split('.')[0]
|
|
27
|
+
}&?${p.ack.mark}&*`,
|
|
28
|
+
]
|
|
29
|
+
: []
|
|
30
|
+
)
|
|
31
|
+
),
|
|
32
|
+
),
|
|
33
|
+
]
|
|
34
|
+
return after(
|
|
35
|
+
each(
|
|
36
|
+
queries,
|
|
37
|
+
[] as Bundle[],
|
|
38
|
+
(out, q) => after(tx.read(q), (more) => [...out, ...more]),
|
|
39
|
+
),
|
|
40
|
+
(related) => {
|
|
41
|
+
let refs = lens.views.flatMap((v) =>
|
|
42
|
+
Object.values(v.props).flatMap((p) =>
|
|
43
|
+
'ack' in p
|
|
44
|
+
? related.flatMap((r) => {
|
|
45
|
+
let eid =
|
|
46
|
+
(r[p.ack.component] as Record<string, unknown> | undefined)
|
|
47
|
+
?.[p.ack.ref]
|
|
48
|
+
return typeof eid == 'string' ? [eid] : []
|
|
49
|
+
})
|
|
50
|
+
: []
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
return after(
|
|
54
|
+
tx.get([...new Set([...rows.map((r) => r.entity.eid), ...refs])]),
|
|
55
|
+
(
|
|
56
|
+
held,
|
|
57
|
+
) => [
|
|
58
|
+
...new Map([...related, ...held].map((r) => [r.entity.eid, r]))
|
|
59
|
+
.values(),
|
|
60
|
+
],
|
|
61
|
+
)
|
|
62
|
+
},
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
type Check = {
|
|
66
|
+
rows: Bundle[]
|
|
67
|
+
speaks?: Speaks
|
|
68
|
+
translated: Bundle[]
|
|
69
|
+
now: number
|
|
70
|
+
canonical?: boolean
|
|
71
|
+
}
|
|
72
|
+
let checked = '$lensCheck'
|
|
73
|
+
// A transaction may gather the same facts in a different component order.
|
|
74
|
+
// Identity pointers describe storage; the write's identity is its eid.
|
|
75
|
+
let change = (rows: Bundle[]) =>
|
|
76
|
+
rows.map((row) => ({ ...row, entity: { eid: row.entity.eid } }))
|
|
77
|
+
let canonical = (value: unknown): string | undefined =>
|
|
78
|
+
JSON.stringify(
|
|
79
|
+
value,
|
|
80
|
+
(_key, item) =>
|
|
81
|
+
item && typeof item == 'object' && !Array.isArray(item)
|
|
82
|
+
? Object.fromEntries(
|
|
83
|
+
Object.entries(item).sort(([a], [b]) => a.localeCompare(b)),
|
|
84
|
+
)
|
|
85
|
+
: item,
|
|
86
|
+
)
|
|
87
|
+
let same = (a: Bundle[], b: Bundle[]) =>
|
|
88
|
+
canonical(change(a)) == canonical(change(b))
|
|
89
|
+
export let lenses = (
|
|
90
|
+
report: (error: unknown) => void = (error) => console.error(error),
|
|
91
|
+
pending?: { vocab: Vocab; rows: Bundle[]; speaks?: Speaks },
|
|
92
|
+
): Plugin => {
|
|
93
|
+
let expansion = pending ? compile(pending.rows, pending.speaks) : undefined
|
|
94
|
+
// The host supplies its retained immutable chain at composition. Re-reading
|
|
95
|
+
// the same metadata on every ask/view/answer adds billed reads to all pages.
|
|
96
|
+
let steps = (tx: ReadTx) => pending ? pending.rows : tx.read('._lens')
|
|
97
|
+
if (
|
|
98
|
+
expansion && !expansion.renames.length &&
|
|
99
|
+
!expansion.views.some((v) => pending!.vocab.comp(v.from))
|
|
100
|
+
) {
|
|
101
|
+
expansion = undefined
|
|
102
|
+
}
|
|
103
|
+
let current = (
|
|
104
|
+
rows: Bundle[],
|
|
105
|
+
tx: ReadTx,
|
|
106
|
+
): Bundle[] | Promise<Bundle[]> => {
|
|
107
|
+
if (!expansion) return rows
|
|
108
|
+
let lens = expansion
|
|
109
|
+
if (lens.renames.length && !lens.views.length) {
|
|
110
|
+
return rows.map((r) => {
|
|
111
|
+
let out = { ...r }
|
|
112
|
+
for (let { rename: { from, to } } of lens.renames) {
|
|
113
|
+
let [fc, fp] = from.split('.'), [tc, tp] = to.split('.')
|
|
114
|
+
let source = r[fc] as Record<string, unknown> | undefined
|
|
115
|
+
let target = r[tc] as Record<string, unknown> | undefined
|
|
116
|
+
let value = target && Object.hasOwn(target, tp)
|
|
117
|
+
? target[tp]
|
|
118
|
+
: source?.[fp]
|
|
119
|
+
if (source && Object.hasOwn(source, fp) && value != null) {
|
|
120
|
+
out[tc] = { ...out[tc] as object, [tp]: value }
|
|
121
|
+
}
|
|
122
|
+
if (source && Object.hasOwn(source, fp)) {
|
|
123
|
+
let comp = { ...out[fc] as Record<string, unknown> }
|
|
124
|
+
delete comp[fp]
|
|
125
|
+
if (Object.keys(comp).length) out[fc] = comp
|
|
126
|
+
else delete out[fc]
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return out
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return after(facts(lens, rows, tx), (held) =>
|
|
134
|
+
after(
|
|
135
|
+
each(
|
|
136
|
+
lens.views.filter((v) => pending!.vocab.comp(v.from)),
|
|
137
|
+
held,
|
|
138
|
+
(out, v) =>
|
|
139
|
+
after(
|
|
140
|
+
tx.read(line`.${v.from}&?created&?updated&*`),
|
|
141
|
+
(source) => [...out, ...source],
|
|
142
|
+
),
|
|
143
|
+
),
|
|
144
|
+
(all) => {
|
|
145
|
+
let at = new Map(all.map((r) => [r.entity.eid, r]))
|
|
146
|
+
for (let row of rows) {
|
|
147
|
+
let was = at.get(row.entity.eid)
|
|
148
|
+
let merged = { ...was, ...row } as Bundle
|
|
149
|
+
for (let [c, p] of Object.entries(row)) {
|
|
150
|
+
if (
|
|
151
|
+
c != 'entity' && !c.startsWith('$') && p &&
|
|
152
|
+
typeof p == 'object'
|
|
153
|
+
) merged[c] = { ...was?.[c] as object, ...p }
|
|
154
|
+
}
|
|
155
|
+
at.set(row.entity.eid, merged)
|
|
156
|
+
}
|
|
157
|
+
let sources = [...at.values()].filter((r) =>
|
|
158
|
+
lens.views.some((v) => !!r[v.from])
|
|
159
|
+
)
|
|
160
|
+
let moved = lens.put(sources, {
|
|
161
|
+
facts: [...at.values()],
|
|
162
|
+
migrate: true,
|
|
163
|
+
})
|
|
164
|
+
for (let row of moved) {
|
|
165
|
+
let was = at.get(row.entity.eid)
|
|
166
|
+
let merged = { ...was, ...row } as Bundle
|
|
167
|
+
for (let [c, p] of Object.entries(row)) {
|
|
168
|
+
if (
|
|
169
|
+
c != 'entity' && !c.startsWith('$') && p && typeof p == 'object'
|
|
170
|
+
) merged[c] = { ...was?.[c] as object, ...p }
|
|
171
|
+
}
|
|
172
|
+
for (let v of lens.views) delete merged[v.from]
|
|
173
|
+
delete merged.$was
|
|
174
|
+
at.set(row.entity.eid, merged)
|
|
175
|
+
}
|
|
176
|
+
return rows.map((r) => {
|
|
177
|
+
let held = at.get(r.entity.eid)!
|
|
178
|
+
let out = { ...r }
|
|
179
|
+
for (let v of lens.views) {
|
|
180
|
+
for (let p of Object.values(v.props)) {
|
|
181
|
+
let c = 'path' in p
|
|
182
|
+
? p.path.split('.')[0]
|
|
183
|
+
: 'ack' in p
|
|
184
|
+
? p.ack.mark
|
|
185
|
+
: undefined
|
|
186
|
+
if (!c) continue
|
|
187
|
+
let asked = !!r[v.from] || c in r ||
|
|
188
|
+
'ack' in p && p.ack.component in r
|
|
189
|
+
if (asked && held[c] !== undefined) out[c] = held[c]
|
|
190
|
+
}
|
|
191
|
+
delete out[v.from]
|
|
192
|
+
}
|
|
193
|
+
return out
|
|
194
|
+
})
|
|
195
|
+
},
|
|
196
|
+
))
|
|
197
|
+
}
|
|
198
|
+
let forwardView = (
|
|
199
|
+
{ tx, vocab }: import('jsr:@yaks/graph@0.2.36').ReadContext,
|
|
200
|
+
original: import('@yaks/query').Query,
|
|
201
|
+
): import('jsr:@yaks/graph@0.2.36').ReadView | null => {
|
|
202
|
+
if (!expansion || !vocab) return null
|
|
203
|
+
if (expansion.renames.length && !expansion.views.length) {
|
|
204
|
+
let lens = expansion
|
|
205
|
+
let text = JSON.stringify(original)
|
|
206
|
+
if (
|
|
207
|
+
!lens.renames.some(({ rename }) => {
|
|
208
|
+
let from = rename.from.split('.')[0], to = rename.to.split('.')[0]
|
|
209
|
+
return [from, to].some((c) =>
|
|
210
|
+
text.includes(`"${c}"`) || text.includes(`"${c}.`)
|
|
211
|
+
)
|
|
212
|
+
}) && !text.includes('every')
|
|
213
|
+
) return null
|
|
214
|
+
let destinations = new Set(
|
|
215
|
+
lens.renames.map(({ rename }) => rename.to.split('.')[0]),
|
|
216
|
+
)
|
|
217
|
+
let selection = false
|
|
218
|
+
map(original, (clause) => {
|
|
219
|
+
if (
|
|
220
|
+
clause.kind == 'pred' && clause.path.length > 1 &&
|
|
221
|
+
destinations.has(clause.path[0]) && clause.op != '?'
|
|
222
|
+
) selection = true
|
|
223
|
+
return clause
|
|
224
|
+
})
|
|
225
|
+
// An optional aspect or whole owner read cannot change membership.
|
|
226
|
+
// Let storage select finite owners, then expand just their source cells.
|
|
227
|
+
if (!selection) {
|
|
228
|
+
let offers = false
|
|
229
|
+
map(original, (clause) => {
|
|
230
|
+
if (
|
|
231
|
+
clause.kind == 'pred' && clause.op == '?' &&
|
|
232
|
+
destinations.has(clause.path[0])
|
|
233
|
+
) offers = true
|
|
234
|
+
if (
|
|
235
|
+
clause.kind == 'fields' &&
|
|
236
|
+
clause.fields.some((f) => destinations.has(f.path[0]))
|
|
237
|
+
) offers = true
|
|
238
|
+
return clause
|
|
239
|
+
})
|
|
240
|
+
if (!offers) return null
|
|
241
|
+
return {
|
|
242
|
+
original,
|
|
243
|
+
query: and(
|
|
244
|
+
map(original, (c) =>
|
|
245
|
+
[
|
|
246
|
+
'fields',
|
|
247
|
+
'every',
|
|
248
|
+
'count',
|
|
249
|
+
'distinct',
|
|
250
|
+
'tally',
|
|
251
|
+
'order',
|
|
252
|
+
'limit',
|
|
253
|
+
'after',
|
|
254
|
+
].includes(c.kind)
|
|
255
|
+
? and()
|
|
256
|
+
: c),
|
|
257
|
+
every(),
|
|
258
|
+
),
|
|
259
|
+
vocab,
|
|
260
|
+
dependencies: [
|
|
261
|
+
...lens.sources,
|
|
262
|
+
...lens.renames.map((r) => r.rename.to),
|
|
263
|
+
],
|
|
264
|
+
expand: (rows: Bundle[]) => current(rows, tx),
|
|
265
|
+
answer: (rows: Bundle[]) => rows,
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
let selectors = map(
|
|
269
|
+
original,
|
|
270
|
+
(c) =>
|
|
271
|
+
[
|
|
272
|
+
'fields',
|
|
273
|
+
'every',
|
|
274
|
+
'count',
|
|
275
|
+
'distinct',
|
|
276
|
+
'tally',
|
|
277
|
+
'order',
|
|
278
|
+
'limit',
|
|
279
|
+
'after',
|
|
280
|
+
].includes(c.kind) ||
|
|
281
|
+
c.kind == 'pred' && destinations.has(c.path[0])
|
|
282
|
+
? and()
|
|
283
|
+
: c,
|
|
284
|
+
)
|
|
285
|
+
let sources = map(
|
|
286
|
+
lens.find((p) => {
|
|
287
|
+
let [c, f] = p.split('.')
|
|
288
|
+
return !!pending!.vocab.prop(c, f)
|
|
289
|
+
}),
|
|
290
|
+
(c) => c.kind == 'every' ? and() : c,
|
|
291
|
+
)
|
|
292
|
+
return {
|
|
293
|
+
original,
|
|
294
|
+
// Retain entity/session ownership predicates on both arms. Only a
|
|
295
|
+
// genuine destination filter requires finding unconverted sources.
|
|
296
|
+
query: and(
|
|
297
|
+
or(
|
|
298
|
+
map(original, (c) =>
|
|
299
|
+
[
|
|
300
|
+
'fields',
|
|
301
|
+
'every',
|
|
302
|
+
'count',
|
|
303
|
+
'distinct',
|
|
304
|
+
'tally',
|
|
305
|
+
'order',
|
|
306
|
+
'limit',
|
|
307
|
+
'after',
|
|
308
|
+
].includes(c.kind)
|
|
309
|
+
? and()
|
|
310
|
+
: c),
|
|
311
|
+
and(selectors, sources),
|
|
312
|
+
),
|
|
313
|
+
every(),
|
|
314
|
+
),
|
|
315
|
+
vocab,
|
|
316
|
+
dependencies: [
|
|
317
|
+
...lens.sources,
|
|
318
|
+
...lens.renames.map((r) => r.rename.to),
|
|
319
|
+
],
|
|
320
|
+
expand: (rows: Bundle[]) => current(rows, tx),
|
|
321
|
+
answer: (rows: Bundle[]) => rows,
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
let canonical = [
|
|
325
|
+
...new Set(
|
|
326
|
+
expansion.views.flatMap((v) =>
|
|
327
|
+
Object.values(v.props).flatMap((p) =>
|
|
328
|
+
'path' in p
|
|
329
|
+
? [p.path.split('.')[0]]
|
|
330
|
+
: 'ack' in p
|
|
331
|
+
? [p.ack.mark]
|
|
332
|
+
: []
|
|
333
|
+
)
|
|
334
|
+
),
|
|
335
|
+
),
|
|
336
|
+
]
|
|
337
|
+
if (
|
|
338
|
+
!canonical.some((c) =>
|
|
339
|
+
JSON.stringify(original).includes(`"${c}"`) ||
|
|
340
|
+
JSON.stringify(original).includes(`"${c}.`)
|
|
341
|
+
)
|
|
342
|
+
) return null
|
|
343
|
+
return {
|
|
344
|
+
original,
|
|
345
|
+
query: and(
|
|
346
|
+
or(
|
|
347
|
+
map(
|
|
348
|
+
original,
|
|
349
|
+
(c) =>
|
|
350
|
+
c.kind == 'pred' &&
|
|
351
|
+
expansion!.views.some((v) =>
|
|
352
|
+
Object.values(v.props).some((p) =>
|
|
353
|
+
'ack' in p && c.path[0] == p.ack.mark
|
|
354
|
+
)
|
|
355
|
+
) ||
|
|
356
|
+
[
|
|
357
|
+
'fields',
|
|
358
|
+
'every',
|
|
359
|
+
'count',
|
|
360
|
+
'distinct',
|
|
361
|
+
'tally',
|
|
362
|
+
'order',
|
|
363
|
+
'limit',
|
|
364
|
+
'after',
|
|
365
|
+
].includes(c.kind)
|
|
366
|
+
? and()
|
|
367
|
+
: c,
|
|
368
|
+
),
|
|
369
|
+
...expansion.views.filter((v) => vocab.comp(v.from)).map((v) =>
|
|
370
|
+
present(v.from)
|
|
371
|
+
),
|
|
372
|
+
),
|
|
373
|
+
every(),
|
|
374
|
+
),
|
|
375
|
+
vocab,
|
|
376
|
+
dependencies: [...expansion.dependencies, ...expansion.sources],
|
|
377
|
+
expand: (rows: Bundle[]) => current(rows, tx),
|
|
378
|
+
answer: (rows: Bundle[]) => rows,
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return ({
|
|
382
|
+
name: 'lens',
|
|
383
|
+
admission: () => true,
|
|
384
|
+
reads: (opts) => !!opts.speaks || !!expansion,
|
|
385
|
+
vocab: docs,
|
|
386
|
+
requests: ['$speaks', checked, '$lensKeep'],
|
|
387
|
+
hooks: {
|
|
388
|
+
normalize: (rows, tx, _error, context) => {
|
|
389
|
+
if (rows.some((r) => r[checked] || r.$lensKeep)) {
|
|
390
|
+
throw new Refused('reserved lens transaction check')
|
|
391
|
+
}
|
|
392
|
+
if (!expansion && !rows.some((r) => r.$speaks !== undefined)) {
|
|
393
|
+
return rows
|
|
394
|
+
}
|
|
395
|
+
let vocab = (context?.graph as { vocab?: Vocab } | undefined)?.vocab
|
|
396
|
+
let translate = () =>
|
|
397
|
+
after(
|
|
398
|
+
steps(tx),
|
|
399
|
+
(steps) =>
|
|
400
|
+
each(rows, [] as Bundle[], (out, row) => {
|
|
401
|
+
if (
|
|
402
|
+
row.$speaks === undefined &&
|
|
403
|
+
(!expansion ||
|
|
404
|
+
expansion.renames.length && !expansion.views.length)
|
|
405
|
+
) {
|
|
406
|
+
return [...out, row]
|
|
407
|
+
}
|
|
408
|
+
let speaks = row.$speaks as Speaks
|
|
409
|
+
if (
|
|
410
|
+
speaks !== undefined &&
|
|
411
|
+
(!speaks || typeof speaks != 'object' ||
|
|
412
|
+
Array.isArray(speaks))
|
|
413
|
+
) {
|
|
414
|
+
throw new Error(
|
|
415
|
+
'Lens: $speaks must be a package version map',
|
|
416
|
+
)
|
|
417
|
+
}
|
|
418
|
+
let { $speaks: _, ...bundle } = row
|
|
419
|
+
let spoken = speaks === undefined
|
|
420
|
+
? undefined
|
|
421
|
+
: compile(steps, speaks)
|
|
422
|
+
let canonical = !spoken ||
|
|
423
|
+
!spoken.views.length && !!expansion?.views.length
|
|
424
|
+
let lens = canonical && expansion
|
|
425
|
+
? expansion
|
|
426
|
+
: spoken ?? compile(steps)
|
|
427
|
+
if (!lens.views.length) {
|
|
428
|
+
return [...out, ...lens.put([bundle as Bundle])]
|
|
429
|
+
}
|
|
430
|
+
return after(facts(lens, [bundle as Bundle], tx), (held) => {
|
|
431
|
+
let now = Date.now()
|
|
432
|
+
let translated = lens.put([bundle as Bundle], {
|
|
433
|
+
facts: held,
|
|
434
|
+
vocab,
|
|
435
|
+
now,
|
|
436
|
+
canonical,
|
|
437
|
+
})
|
|
438
|
+
if (!lens.views.length) {
|
|
439
|
+
return [...out, ...translated]
|
|
440
|
+
}
|
|
441
|
+
let check: Check = {
|
|
442
|
+
rows: [bundle as Bundle],
|
|
443
|
+
speaks,
|
|
444
|
+
translated,
|
|
445
|
+
now,
|
|
446
|
+
canonical,
|
|
447
|
+
}
|
|
448
|
+
return [
|
|
449
|
+
...out,
|
|
450
|
+
...translated.map((b, i) =>
|
|
451
|
+
i ? b : { ...b, [checked]: check }
|
|
452
|
+
),
|
|
453
|
+
]
|
|
454
|
+
})
|
|
455
|
+
}),
|
|
456
|
+
)
|
|
457
|
+
let refuse = (error: unknown): never => {
|
|
458
|
+
report(error)
|
|
459
|
+
throw error instanceof Stale ? error : new Refused(
|
|
460
|
+
error instanceof Error ? error.message : String(error),
|
|
461
|
+
)
|
|
462
|
+
}
|
|
463
|
+
try {
|
|
464
|
+
let out = translate()
|
|
465
|
+
return out instanceof Promise ? out.catch(refuse) : out
|
|
466
|
+
} catch (error) {
|
|
467
|
+
return refuse(error)
|
|
468
|
+
}
|
|
469
|
+
},
|
|
470
|
+
precondition: (rows, tx, _error, context) => {
|
|
471
|
+
let checks = rows.flatMap((r) =>
|
|
472
|
+
r[checked] ? [r[checked] as Check] : []
|
|
473
|
+
)
|
|
474
|
+
if (!checks.length) return rows
|
|
475
|
+
let vocab = (context?.graph as { vocab?: Vocab } | undefined)?.vocab
|
|
476
|
+
return after(
|
|
477
|
+
steps(tx),
|
|
478
|
+
(steps) =>
|
|
479
|
+
after(
|
|
480
|
+
each(checks, undefined, (_, check) => {
|
|
481
|
+
let lens = check.canonical && expansion
|
|
482
|
+
? expansion
|
|
483
|
+
: compile(steps, check.speaks)
|
|
484
|
+
return after(facts(lens, check.rows, tx), (held) => {
|
|
485
|
+
let out = lens.put(check.rows, {
|
|
486
|
+
facts: held,
|
|
487
|
+
vocab,
|
|
488
|
+
now: check.now,
|
|
489
|
+
canonical: check.canonical,
|
|
490
|
+
})
|
|
491
|
+
if (!same(out, check.translated)) {
|
|
492
|
+
throw new Refused(
|
|
493
|
+
'Lens: referenced facts changed before the write',
|
|
494
|
+
)
|
|
495
|
+
}
|
|
496
|
+
return undefined
|
|
497
|
+
})
|
|
498
|
+
}),
|
|
499
|
+
() =>
|
|
500
|
+
rows.map((r) => {
|
|
501
|
+
let { [checked]: _, $lensKeep: historic, ...out } = r
|
|
502
|
+
if (historic) {
|
|
503
|
+
let keep = historic as {
|
|
504
|
+
component: string
|
|
505
|
+
value: Bundle[string]
|
|
506
|
+
}
|
|
507
|
+
out[keep.component] = keep.value
|
|
508
|
+
}
|
|
509
|
+
return out as Bundle
|
|
510
|
+
}),
|
|
511
|
+
),
|
|
512
|
+
)
|
|
513
|
+
},
|
|
514
|
+
},
|
|
515
|
+
ask: ({ opts, tx }, query) =>
|
|
516
|
+
opts.speaks
|
|
517
|
+
? after(
|
|
518
|
+
steps(tx),
|
|
519
|
+
(rows) => compile(rows, opts.speaks).ask(query),
|
|
520
|
+
)
|
|
521
|
+
: query,
|
|
522
|
+
view: ({ opts, tx, vocab }, original) => {
|
|
523
|
+
if (!opts.speaks) return forwardView({ opts, tx, vocab }, original)
|
|
524
|
+
return after(steps(tx), (steps) => {
|
|
525
|
+
let lens = compile(steps, opts.speaks)
|
|
526
|
+
if (!lens.views.length) {
|
|
527
|
+
return forwardView({ opts, tx, vocab }, original)
|
|
528
|
+
}
|
|
529
|
+
let mentions = (v: Lens['views'][number]) =>
|
|
530
|
+
JSON.stringify(original).includes(`\"${v.from}\"`) ||
|
|
531
|
+
JSON.stringify(original).includes(`\"${v.from}.`)
|
|
532
|
+
if (!lens.views.some(mentions)) return null
|
|
533
|
+
if (!vocab) throw new Error('Lens: a graph view needs its vocabulary')
|
|
534
|
+
return {
|
|
535
|
+
original,
|
|
536
|
+
// Exact old filters, windowing and aggregates run after inversion.
|
|
537
|
+
// A view can also match absence and joins, so all canonical candidates
|
|
538
|
+
// remain eligible; current callers never take this path.
|
|
539
|
+
query: (() => {
|
|
540
|
+
let mandatory = lens.views.filter((v) =>
|
|
541
|
+
original.clauses.some((c) =>
|
|
542
|
+
c.kind == 'pred' && c.path[0] == v.from && c.op != '?' &&
|
|
543
|
+
!(c.op == '=' && c.value?.kind == 'scalar' && c.value.raw == '')
|
|
544
|
+
)
|
|
545
|
+
)
|
|
546
|
+
return mandatory.length
|
|
547
|
+
? and(
|
|
548
|
+
...mandatory.map((v) =>
|
|
549
|
+
vocab.comp(v.from)
|
|
550
|
+
? or(parse(v.match), present(v.from))
|
|
551
|
+
: parse(v.match)
|
|
552
|
+
),
|
|
553
|
+
every(),
|
|
554
|
+
)
|
|
555
|
+
: and(every())
|
|
556
|
+
})(),
|
|
557
|
+
vocab: loadVocab(lens.schema(vocab.docs)),
|
|
558
|
+
dependencies: [...lens.dependencies, ...lens.sources],
|
|
559
|
+
saved: true,
|
|
560
|
+
answer: (rows: Bundle[]) =>
|
|
561
|
+
after(
|
|
562
|
+
facts(lens, rows, tx),
|
|
563
|
+
(held) => lens.get(rows, { facts: held, vocab, now: opts.now }),
|
|
564
|
+
),
|
|
565
|
+
}
|
|
566
|
+
})
|
|
567
|
+
},
|
|
568
|
+
answer: ({ opts, tx, vocab }, rows) => {
|
|
569
|
+
if (!opts.speaks) return current(rows, tx)
|
|
570
|
+
return after(steps(tx), (steps) => {
|
|
571
|
+
let lens = compile(steps, opts.speaks)
|
|
572
|
+
if (!lens.views.length) {
|
|
573
|
+
return after(
|
|
574
|
+
current(rows, tx),
|
|
575
|
+
(expanded) =>
|
|
576
|
+
opts.patch
|
|
577
|
+
? after(
|
|
578
|
+
facts(lens, expanded, tx),
|
|
579
|
+
(held) => lens.get(expanded, { facts: held, patch: true }),
|
|
580
|
+
)
|
|
581
|
+
: lens.get(expanded),
|
|
582
|
+
)
|
|
583
|
+
}
|
|
584
|
+
return after(
|
|
585
|
+
facts(lens, rows, tx),
|
|
586
|
+
(held) =>
|
|
587
|
+
lens.get(rows, {
|
|
588
|
+
facts: held,
|
|
589
|
+
vocab,
|
|
590
|
+
now: opts.now,
|
|
591
|
+
patch: opts.patch,
|
|
592
|
+
}),
|
|
593
|
+
)
|
|
594
|
+
})
|
|
595
|
+
},
|
|
596
|
+
})
|
|
597
|
+
}
|
package/schema.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** Translate declarations by the same inverse as read views. Components keep
|
|
2
|
+
* their membership; a cross-component target remains an old-readable word. */ export let schema = (docs, pairs)=>{
|
|
3
|
+
let out = docs;
|
|
4
|
+
for (let { from: [fc, fp], to: [tc, tp] } of pairs.toReversed()){
|
|
5
|
+
let source = out.findIndex((d)=>d.$defs?.[fc]?.properties);
|
|
6
|
+
let target = out.findIndex((d)=>d.$defs?.[tc]?.properties?.[tp]);
|
|
7
|
+
if (source < 0 || target < 0) continue;
|
|
8
|
+
let sourceDoc = out[source], sourceComp = sourceDoc.$defs[fc];
|
|
9
|
+
let targetComp = out[target].$defs[tc];
|
|
10
|
+
let props = {
|
|
11
|
+
...sourceComp.properties,
|
|
12
|
+
[fp]: targetComp.properties[tp]
|
|
13
|
+
};
|
|
14
|
+
if (fc == tc) delete props[tp];
|
|
15
|
+
let required = sourceComp.required?.map((p)=>fc == tc && p == tp ? fp : p);
|
|
16
|
+
if (targetComp.required?.includes(tp)) {
|
|
17
|
+
required = [
|
|
18
|
+
...new Set([
|
|
19
|
+
...required ?? [],
|
|
20
|
+
fp
|
|
21
|
+
])
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
let next = {
|
|
25
|
+
...sourceDoc,
|
|
26
|
+
$defs: {
|
|
27
|
+
...sourceDoc.$defs,
|
|
28
|
+
[fc]: {
|
|
29
|
+
...sourceComp,
|
|
30
|
+
properties: props,
|
|
31
|
+
...required ? {
|
|
32
|
+
required
|
|
33
|
+
} : {}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
out = out.map((d, i)=>{
|
|
38
|
+
if (i == source) return next;
|
|
39
|
+
if (fc != tc || !d.$defs?.[tc]?.properties?.[tp]) return d;
|
|
40
|
+
let comp = d.$defs[tc], properties = {
|
|
41
|
+
...comp.properties
|
|
42
|
+
};
|
|
43
|
+
delete properties[tp];
|
|
44
|
+
return {
|
|
45
|
+
...d,
|
|
46
|
+
$defs: {
|
|
47
|
+
...d.$defs,
|
|
48
|
+
[tc]: {
|
|
49
|
+
...comp,
|
|
50
|
+
properties,
|
|
51
|
+
...comp.required ? {
|
|
52
|
+
required: comp.required.filter((p)=>p != tp)
|
|
53
|
+
} : {}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=schema.js.map
|
package/schema.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sources":["./schema.ts"],"names":[],"mappings":"AAIA;4EAC4E,GAC5E,OAAO,IAAI,SAAS,CAAC,MAAkB;EACrC,IAAI,MAAM;EACV,KAAK,IAAI,EAAE,MAAM,CAAC,IAAI,GAAG,EAAE,IAAI,CAAC,IAAI,GAAG,EAAE,IAAI,MAAM,UAAU,GAAI;IAC/D,IAAI,SAAS,IAAI,SAAS,CAAC,CAAC,IAAM,EAAE,KAAK,EAAE,CAAC,GAAG,EAAE;IACjD,IAAI,SAAS,IAAI,SAAS,CAAC,CAAC,IAAM,EAAE,KAAK,EAAE,CAAC,GAAG,EAAE,YAAY,CAAC,GAAG;IACjE,IAAI,SAAS,KAAK,SAAS,GAAG;IAC9B,IAAI,YAAY,GAAG,CAAC,OAAO,EAAE,aAAa,UAAU,KAAK,AAAC,CAAC,GAAG;IAC9D,IAAI,aAAa,GAAG,CAAC,OAAO,CAAC,KAAK,AAAC,CAAC,GAAG;IACvC,IAAI,QAAQ;MAAE,GAAG,WAAW,UAAU;MAAE,CAAC,GAAG,EAAE,WAAW,UAAU,AAAC,CAAC,GAAG;IAAC;IACzE,IAAI,MAAM,IAAI,OAAO,KAAK,CAAC,GAAG;IAC9B,IAAI,WAAW,WAAW,QAAQ,EAAE,IAAI,CAAC,IAAM,MAAM,MAAM,KAAK,KAAK,KAAK;IAC1E,IAAI,WAAW,QAAQ,EAAE,SAAS,KAAK;MACrC,WAAW;WAAI,IAAI,IAAI;aAAK,YAAY,EAAE;UAAG;SAAG;OAAE;IACpD;IACA,IAAI,OAAO;MACT,GAAG,SAAS;MACZ,OAAO;QACL,GAAG,UAAU,KAAK;QAClB,CAAC,GAAG,EAAE;UACJ,GAAG,UAAU;UACb,YAAY;UACZ,GAAI,WAAW;YAAE;UAAS,IAAI,CAAC,CAAC;QAClC;MACF;IACF;IACA,MAAM,IAAI,GAAG,CAAC,CAAC,GAAG;MAChB,IAAI,KAAK,QAAQ,OAAO;MACxB,IAAI,MAAM,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,EAAE,YAAY,CAAC,GAAG,EAAE,OAAO;MACzD,IAAI,OAAO,EAAE,KAAK,CAAC,GAAG,EAAE,aAAa;QAAE,GAAG,KAAK,UAAU;MAAC;MAC1D,OAAO,UAAU,CAAC,GAAG;MACrB,OAAO;QACL,GAAG,CAAC;QACJ,OAAO;UACL,GAAG,EAAE,KAAK;UACV,CAAC,GAAG,EAAE;YACJ,GAAG,IAAI;YACP;YACA,GAAI,KAAK,QAAQ,GACb;cAAE,UAAU,KAAK,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAM,KAAK;YAAI,IACjD,CAAC,CAAC;UACR;QACF;MACF;IACF;EACF;EACA,OAAO;AACT,EAAC"}
|