bonescript-compiler 0.5.2 → 0.5.4
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 -21
- package/dist/algorithm_catalog.js +166 -166
- package/dist/cli.d.ts +1 -2
- package/dist/cli.js +543 -75
- package/dist/cli.js.map +1 -1
- package/dist/emit_capability.d.ts +0 -13
- package/dist/emit_capability.js +128 -292
- package/dist/emit_capability.js.map +1 -1
- package/dist/emit_composition.js +3 -37
- package/dist/emit_composition.js.map +1 -1
- package/dist/emit_deploy.js +162 -162
- package/dist/emit_events.d.ts +0 -1
- package/dist/emit_events.js +275 -342
- package/dist/emit_events.js.map +1 -1
- package/dist/emit_full.js +106 -268
- package/dist/emit_full.js.map +1 -1
- package/dist/emit_maintenance.js +249 -249
- package/dist/emit_runtime.d.ts +11 -17
- package/dist/emit_runtime.js +688 -29
- package/dist/emit_runtime.js.map +1 -1
- package/dist/emit_sourcemap.js +66 -66
- package/dist/emit_tests.js +0 -37
- package/dist/emit_tests.js.map +1 -1
- package/dist/emitter.js +16 -82
- package/dist/emitter.js.map +1 -1
- package/dist/extension_manager.d.ts +2 -2
- package/dist/extension_manager.js +3 -6
- package/dist/extension_manager.js.map +1 -1
- package/dist/ir.d.ts +0 -4
- package/dist/lowering.d.ts +14 -5
- package/dist/lowering.js +417 -66
- package/dist/lowering.js.map +1 -1
- package/dist/module_loader.d.ts +2 -2
- package/dist/module_loader.js +23 -20
- package/dist/module_loader.js.map +1 -1
- package/dist/optimizer.js +1 -1
- package/dist/optimizer.js.map +1 -1
- package/dist/scaffold.d.ts +2 -2
- package/dist/scaffold.js +319 -315
- package/dist/scaffold.js.map +1 -1
- package/dist/source_map.js.map +1 -0
- package/dist/test.js.map +1 -0
- package/dist/test_typechecker.d.ts +5 -0
- package/dist/test_typechecker.js +126 -0
- package/dist/test_typechecker.js.map +1 -0
- package/dist/typechecker.d.ts +0 -5
- package/dist/typechecker.js +13 -68
- package/dist/typechecker.js.map +1 -1
- package/dist/verifier.d.ts +1 -5
- package/dist/verifier.js +35 -140
- package/dist/verifier.js.map +1 -1
- package/package.json +52 -62
- package/src/algorithm_catalog.ts +345 -345
- package/src/ast.d.ts +244 -0
- package/src/ast.ts +334 -334
- package/src/cli.ts +624 -98
- package/src/emit_batch.ts +140 -140
- package/src/emit_capability.ts +436 -617
- package/src/emit_composition.ts +196 -229
- package/src/emit_deploy.ts +190 -190
- package/src/emit_events.ts +307 -377
- package/src/emit_extras.ts +240 -240
- package/src/emit_full.ts +309 -475
- package/src/emit_maintenance.ts +459 -459
- package/src/emit_runtime.ts +730 -17
- package/src/emit_sourcemap.ts +140 -140
- package/src/emit_tests.ts +205 -246
- package/src/emit_websocket.ts +229 -229
- package/src/emitter.ts +578 -642
- package/src/extension_manager.ts +187 -189
- package/src/formatter.ts +297 -297
- package/src/index.ts +88 -88
- package/src/ir.ts +215 -216
- package/src/lexer.d.ts +195 -0
- package/src/lexer.ts +630 -630
- package/src/lowering.ts +556 -168
- package/src/module_loader.ts +114 -112
- package/src/optimizer.ts +196 -196
- package/src/parse_decls.d.ts +13 -0
- package/src/parse_decls.ts +409 -409
- package/src/parse_decls2.d.ts +13 -0
- package/src/parse_decls2.ts +244 -244
- package/src/parse_expr.d.ts +7 -0
- package/src/parse_expr.ts +197 -197
- package/src/parse_types.d.ts +6 -0
- package/src/parse_types.ts +54 -54
- package/src/parser.d.ts +10 -0
- package/src/parser.ts +1 -1
- package/src/parser_base.d.ts +19 -0
- package/src/parser_base.ts +57 -57
- package/src/parser_recovery.ts +153 -153
- package/src/scaffold.ts +375 -371
- package/src/solver.ts +330 -330
- package/src/typechecker.d.ts +52 -0
- package/src/typechecker.ts +591 -657
- package/src/types.d.ts +38 -0
- package/src/types.ts +122 -122
- package/src/verifier.ts +46 -152
- package/README.md +0 -382
- package/dist/commands/check.d.ts +0 -5
- package/dist/commands/check.js +0 -34
- package/dist/commands/check.js.map +0 -1
- package/dist/commands/compile.d.ts +0 -5
- package/dist/commands/compile.js +0 -215
- package/dist/commands/compile.js.map +0 -1
- package/dist/commands/debug.d.ts +0 -5
- package/dist/commands/debug.js +0 -59
- package/dist/commands/debug.js.map +0 -1
- package/dist/commands/diff.d.ts +0 -5
- package/dist/commands/diff.js +0 -125
- package/dist/commands/diff.js.map +0 -1
- package/dist/commands/fmt.d.ts +0 -5
- package/dist/commands/fmt.js +0 -49
- package/dist/commands/fmt.js.map +0 -1
- package/dist/commands/init.d.ts +0 -5
- package/dist/commands/init.js +0 -96
- package/dist/commands/init.js.map +0 -1
- package/dist/commands/ir.d.ts +0 -5
- package/dist/commands/ir.js +0 -27
- package/dist/commands/ir.js.map +0 -1
- package/dist/commands/lex.d.ts +0 -5
- package/dist/commands/lex.js +0 -21
- package/dist/commands/lex.js.map +0 -1
- package/dist/commands/parse.d.ts +0 -5
- package/dist/commands/parse.js +0 -30
- package/dist/commands/parse.js.map +0 -1
- package/dist/commands/test.d.ts +0 -5
- package/dist/commands/test.js +0 -61
- package/dist/commands/test.js.map +0 -1
- package/dist/commands/verify_determinism.d.ts +0 -5
- package/dist/commands/verify_determinism.js +0 -64
- package/dist/commands/verify_determinism.js.map +0 -1
- package/dist/commands/watch.d.ts +0 -5
- package/dist/commands/watch.js +0 -50
- package/dist/commands/watch.js.map +0 -1
- package/dist/emit_auth.d.ts +0 -18
- package/dist/emit_auth.js +0 -507
- package/dist/emit_auth.js.map +0 -1
- package/dist/emit_database.d.ts +0 -7
- package/dist/emit_database.js +0 -74
- package/dist/emit_database.js.map +0 -1
- package/dist/emit_index.d.ts +0 -6
- package/dist/emit_index.js +0 -202
- package/dist/emit_index.js.map +0 -1
- package/dist/emit_models.d.ts +0 -12
- package/dist/emit_models.js +0 -171
- package/dist/emit_models.js.map +0 -1
- package/dist/emit_openapi.d.ts +0 -9
- package/dist/emit_openapi.js +0 -308
- package/dist/emit_openapi.js.map +0 -1
- package/dist/emit_package.d.ts +0 -7
- package/dist/emit_package.js +0 -70
- package/dist/emit_package.js.map +0 -1
- package/dist/emit_router.d.ts +0 -12
- package/dist/emit_router.js +0 -390
- package/dist/emit_router.js.map +0 -1
- package/dist/lowering_channels.d.ts +0 -11
- package/dist/lowering_channels.js +0 -103
- package/dist/lowering_channels.js.map +0 -1
- package/dist/lowering_entities.d.ts +0 -11
- package/dist/lowering_entities.js +0 -232
- package/dist/lowering_entities.js.map +0 -1
- package/dist/lowering_helpers.d.ts +0 -13
- package/dist/lowering_helpers.js +0 -76
- package/dist/lowering_helpers.js.map +0 -1
- package/src/commands/check.ts +0 -33
- package/src/commands/compile.ts +0 -191
- package/src/commands/debug.ts +0 -33
- package/src/commands/diff.ts +0 -108
- package/src/commands/fmt.ts +0 -22
- package/src/commands/init.ts +0 -72
- package/src/commands/ir.ts +0 -23
- package/src/commands/lex.ts +0 -17
- package/src/commands/parse.ts +0 -24
- package/src/commands/test.ts +0 -36
- package/src/commands/verify_determinism.ts +0 -66
- package/src/commands/watch.ts +0 -25
- package/src/emit_auth.ts +0 -513
- package/src/emit_database.ts +0 -75
- package/src/emit_index.ts +0 -210
- package/src/emit_models.ts +0 -176
- package/src/emit_openapi.ts +0 -318
- package/src/emit_package.ts +0 -69
- package/src/emit_router.ts +0 -409
- package/src/lowering_channels.ts +0 -108
- package/src/lowering_entities.ts +0 -258
- package/src/lowering_helpers.ts +0 -75
package/src/emit_capability.ts
CHANGED
|
@@ -1,617 +1,436 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* BoneScript Capability Body Emitter
|
|
3
|
-
*
|
|
4
|
-
* Translates IR effects and preconditions into real TypeScript + SQL.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
let
|
|
72
|
-
for (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
else if (ch === "
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
case "
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
const
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
)
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
lines.push(
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
lines.push(`${indent}if (!${f.paramName}) {`);
|
|
438
|
-
lines.push(`${indent} return res.status(404).json({ error: { code: "NOT_FOUND", message: "${f.paramName} not found" } });`);
|
|
439
|
-
lines.push(`${indent}}`);
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
} else {
|
|
443
|
-
// Multiple fetches from DIFFERENT tables — run in parallel with Promise.all
|
|
444
|
-
lines.push(`${indent}// Parallel fetch from ${fetchGroups.length} tables`);
|
|
445
|
-
const resultVars: string[] = [];
|
|
446
|
-
const fetchExprs: string[] = [];
|
|
447
|
-
|
|
448
|
-
for (const [tableName, group] of fetchGroups) {
|
|
449
|
-
if (group.length === 1) {
|
|
450
|
-
const f = group[0];
|
|
451
|
-
const idExpr = `req.body.${f.idField} || req.params.id`;
|
|
452
|
-
resultVars.push(`__r_${f.paramName}`);
|
|
453
|
-
fetchExprs.push(`queryOne(\`SELECT * FROM ${tableName} WHERE id = $1\`, [${idExpr}])`);
|
|
454
|
-
} else {
|
|
455
|
-
// Same-table batch within a multi-table parallel fetch
|
|
456
|
-
const idExprs = group.map(f => `req.body.${f.idField} || req.params.id`);
|
|
457
|
-
resultVars.push(`__rows_${tableName}`);
|
|
458
|
-
fetchExprs.push(`query(\`SELECT * FROM ${tableName} WHERE id = ANY($1::uuid[])\`, [[${idExprs.join(", ")}]])`);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
lines.push(`${indent}const [${resultVars.join(", ")}] = await Promise.all([`);
|
|
463
|
-
for (const expr of fetchExprs) lines.push(`${indent} ${expr},`);
|
|
464
|
-
lines.push(`${indent}]);`);
|
|
465
|
-
|
|
466
|
-
// Unpack results
|
|
467
|
-
let resultIdx = 0;
|
|
468
|
-
for (const [tableName, group] of fetchGroups) {
|
|
469
|
-
if (group.length === 1) {
|
|
470
|
-
const f = group[0];
|
|
471
|
-
lines.push(`${indent}const ${f.paramName} = ${resultVars[resultIdx]};`);
|
|
472
|
-
lines.push(`${indent}if (!${f.paramName}) {`);
|
|
473
|
-
lines.push(`${indent} return res.status(404).json({ error: { code: "NOT_FOUND", message: "${f.paramName} not found" } });`);
|
|
474
|
-
lines.push(`${indent}}`);
|
|
475
|
-
} else {
|
|
476
|
-
const mapVar = `__map_${tableName}`;
|
|
477
|
-
lines.push(`${indent}const ${mapVar} = new Map((${resultVars[resultIdx]} as any[]).map((r: any) => [r.id, r]));`);
|
|
478
|
-
for (const f of group) {
|
|
479
|
-
const idExpr = `req.body.${f.idField} || req.params.id`;
|
|
480
|
-
lines.push(`${indent}const ${f.paramName} = ${mapVar}.get(${idExpr}) ?? null;`);
|
|
481
|
-
lines.push(`${indent}if (!${f.paramName}) {`);
|
|
482
|
-
lines.push(`${indent} return res.status(404).json({ error: { code: "NOT_FOUND", message: "${f.paramName} not found" } });`);
|
|
483
|
-
lines.push(`${indent}}`);
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
resultIdx++;
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
lines.push(``);
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
// 2. Precondition checks
|
|
493
|
-
if (method.preconditions.length > 0) {
|
|
494
|
-
lines.push(`${indent}// Preconditions`);
|
|
495
|
-
for (const pre of method.preconditions) {
|
|
496
|
-
try {
|
|
497
|
-
lines.push(compilePrecondition(parseExprStr(pre.expression), indent));
|
|
498
|
-
} catch {
|
|
499
|
-
lines.push(`${indent}// CHECK: ${pre.description}`);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
lines.push(``);
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
// 3. Effects — batch same-entity updates into single UPDATEs
|
|
506
|
-
if (method.effects.length > 0) {
|
|
507
|
-
lines.push(`${indent}// Effects (batched by entity to minimise round-trips)`);
|
|
508
|
-
|
|
509
|
-
const paramIdx = { n: 1 };
|
|
510
|
-
const compiled = method.effects.map(e => compileEffect(e, mod, system, paramIdx, method));
|
|
511
|
-
const { batches, standalones } = batchEffects(compiled);
|
|
512
|
-
|
|
513
|
-
// Emit batched UPDATEs
|
|
514
|
-
for (const batch of batches) {
|
|
515
|
-
if (batch.setClauses.length === 0) continue;
|
|
516
|
-
const idParamN = paramIdx.n++;
|
|
517
|
-
const setClauses = batch.setClauses.join(", ");
|
|
518
|
-
const sql = `UPDATE ${batch.tableName} SET ${setClauses}, updated_at = NOW() WHERE id = $${idParamN} RETURNING *`;
|
|
519
|
-
const params = [...batch.paramValues, batch.idParam].join(", ");
|
|
520
|
-
const resultVar = `__upd_${batch.entityParam}`;
|
|
521
|
-
lines.push(`${indent}// ${batch.descriptions.join("; ")}`);
|
|
522
|
-
lines.push(`${indent}const ${resultVar} = await query(\`${sql}\`, [${params}]);`);
|
|
523
|
-
lines.push(`${indent}if (!${resultVar} || ${resultVar}.length === 0) {`);
|
|
524
|
-
lines.push(`${indent} throw new Error("Update failed for ${batch.entityParam}");`);
|
|
525
|
-
lines.push(`${indent}}`);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
// Emit standalone effects (JSONB, array ops)
|
|
529
|
-
for (const s of standalones) {
|
|
530
|
-
const resultVar = `__eff_${standalones.indexOf(s)}`;
|
|
531
|
-
lines.push(`${indent}// ${s.description}`);
|
|
532
|
-
lines.push(`${indent}const ${resultVar} = await query(\`${s.sql}\`, [${s.params.join(", ")}]);`);
|
|
533
|
-
lines.push(`${indent}if (!${resultVar} || ${resultVar}.length === 0) {`);
|
|
534
|
-
lines.push(`${indent} throw new Error("Effect failed: ${s.description.replace(/"/g, '\\"')}");`);
|
|
535
|
-
lines.push(`${indent}}`);
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
// Fallback for effects that couldn't be compiled.
|
|
539
|
-
// Collection-level effects (e.g. list<T>.field = value) are not yet supported
|
|
540
|
-
// by the batch compiler — emit a clear TODO rather than silently dropping them.
|
|
541
|
-
// Effects that reference a completely unknown model are a hard error.
|
|
542
|
-
for (const effect of method.effects) {
|
|
543
|
-
const paramIdx2 = { n: 1 };
|
|
544
|
-
if (!compileEffect(effect, mod, system, paramIdx2, method)) {
|
|
545
|
-
const targetParts = effect.target.split(".");
|
|
546
|
-
const paramName = targetParts[0];
|
|
547
|
-
const param = method.input.find(p => p.name === paramName);
|
|
548
|
-
const isCollectionEffect = param && (param.type.startsWith("list<") || param.type.startsWith("set<"));
|
|
549
|
-
|
|
550
|
-
if (isCollectionEffect) {
|
|
551
|
-
// Collection-level effects: apply the field update to all items in the collection
|
|
552
|
-
// using a single batched UPDATE ... WHERE id = ANY($ids::uuid[])
|
|
553
|
-
const innerType = param.type.replace(/^(list|set)<(.+)>$/, "$2");
|
|
554
|
-
// Find the model for the inner element type
|
|
555
|
-
let elemModel: IR.IRModel | undefined;
|
|
556
|
-
for (const m of system.modules) {
|
|
557
|
-
elemModel = m.models.find(mdl => mdl.name === innerType || mdl.name.toLowerCase() === innerType.toLowerCase());
|
|
558
|
-
if (elemModel) break;
|
|
559
|
-
}
|
|
560
|
-
const tableName = elemModel ? (elemModel.name.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase() + "s") : (innerType.toLowerCase() + "s");
|
|
561
|
-
const fieldName = targetParts[1];
|
|
562
|
-
const valueTs = targetParts[1] ? effect.value : "null";
|
|
563
|
-
const opSql = effect.op === "add"
|
|
564
|
-
? `${fieldName} = ${fieldName} + $1`
|
|
565
|
-
: effect.op === "remove"
|
|
566
|
-
? `${fieldName} = ${fieldName} - $1`
|
|
567
|
-
: `${fieldName} = $1`;
|
|
568
|
-
lines.push(`${indent}// Collection effect: ${effect.target} ${effect.op === "assign" ? "=" : effect.op === "add" ? "+=" : "-="} ${effect.value}`);
|
|
569
|
-
lines.push(`${indent}if (${paramName} && ${paramName}.length > 0) {`);
|
|
570
|
-
lines.push(`${indent} const __ids_${paramName} = ${paramName}.map((x: any) => x.id ?? x);`);
|
|
571
|
-
lines.push(`${indent} await query(`);
|
|
572
|
-
lines.push(`${indent} \`UPDATE ${tableName} SET ${opSql}, updated_at = NOW() WHERE id = ANY($2::uuid[])\`,`);
|
|
573
|
-
lines.push(`${indent} [${effect.value}, __ids_${paramName}],`);
|
|
574
|
-
lines.push(`${indent} );`);
|
|
575
|
-
lines.push(`${indent}}`);
|
|
576
|
-
} else {
|
|
577
|
-
throw new Error(
|
|
578
|
-
`Unsupported effect in method '${method.name}': target '${effect.target}' could not be resolved to a known model field. ` +
|
|
579
|
-
`Ensure the effect target matches a declared entity field (e.g. 'entityName.fieldName').`
|
|
580
|
-
);
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
lines.push(``);
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
// 4. Event emissions
|
|
588
|
-
if (method.emissions.length > 0) {
|
|
589
|
-
lines.push(`${indent}// Emit events`);
|
|
590
|
-
for (const ev of method.emissions) {
|
|
591
|
-
const payload = buildEventPayload(method, fetches);
|
|
592
|
-
if (method.sync === "transactional") {
|
|
593
|
-
lines.push(`${indent}await eventBus.publish("${ev}", ${payload}, "${mod.name}", auth.trace_id, __client);`);
|
|
594
|
-
} else {
|
|
595
|
-
lines.push(`${indent}await eventBus.publish("${ev}", ${payload}, "${mod.name}", auth.trace_id);`);
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
lines.push(``);
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
// 5. Return result
|
|
602
|
-
const resultEntity = fetches[0];
|
|
603
|
-
if (resultEntity) {
|
|
604
|
-
lines.push(`${indent}res.json({ ok: true, action: "${method.name}", entity: ${resultEntity.paramName} });`);
|
|
605
|
-
} else {
|
|
606
|
-
lines.push(`${indent}res.json({ ok: true, action: "${method.name}" });`);
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
return lines.join("\n");
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
function buildEventPayload(method: IR.IRMethod, fetches: EntityFetch[]): string {
|
|
613
|
-
const fields: string[] = fetches.map(f => `${f.paramName}_id: ${f.paramName}?.id`);
|
|
614
|
-
fields.push(`timestamp: new Date().toISOString()`);
|
|
615
|
-
fields.push(`actor_id: auth.actor_id`);
|
|
616
|
-
return `{ ${fields.join(", ")} }`;
|
|
617
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* BoneScript Capability Body Emitter
|
|
3
|
+
*
|
|
4
|
+
* Translates IR effects and preconditions into real TypeScript + SQL.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as IR from "./ir";
|
|
8
|
+
|
|
9
|
+
// ─── Expression Parser ────────────────────────────────────────────────────────
|
|
10
|
+
// Parses the serialized expression strings from the IR back into a structured form.
|
|
11
|
+
|
|
12
|
+
type ExprKind =
|
|
13
|
+
| { kind: "literal"; value: string; raw: string }
|
|
14
|
+
| { kind: "field"; path: string[] }
|
|
15
|
+
| { kind: "binop"; op: string; left: Expr; right: Expr }
|
|
16
|
+
| { kind: "call"; name: string; args: Expr[] };
|
|
17
|
+
|
|
18
|
+
type Expr = ExprKind;
|
|
19
|
+
|
|
20
|
+
function parseExprStr(s: string): Expr {
|
|
21
|
+
s = s.trim();
|
|
22
|
+
|
|
23
|
+
// Strip outer parens
|
|
24
|
+
if (s.startsWith("(") && s.endsWith(")")) {
|
|
25
|
+
return parseExprStr(s.slice(1, -1));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// String literal
|
|
29
|
+
if (s.startsWith('"') && s.endsWith('"')) {
|
|
30
|
+
return { kind: "literal", value: s.slice(1, -1), raw: s };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Number literal
|
|
34
|
+
if (/^-?\d+(\.\d+)?$/.test(s)) {
|
|
35
|
+
return { kind: "literal", value: s, raw: s };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Boolean
|
|
39
|
+
if (s === "true" || s === "false") {
|
|
40
|
+
return { kind: "literal", value: s, raw: s };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Binary operators (check in precedence order, right-to-left to handle left-assoc)
|
|
44
|
+
const binOps = [" or ", " and ", " == ", " != ", " >= ", " <= ", " > ", " < ", " in ", " contains ", " + ", " - ", " * ", " / "];
|
|
45
|
+
for (const op of binOps) {
|
|
46
|
+
const idx = findBinOp(s, op);
|
|
47
|
+
if (idx !== -1) {
|
|
48
|
+
const left = parseExprStr(s.slice(0, idx));
|
|
49
|
+
const right = parseExprStr(s.slice(idx + op.length));
|
|
50
|
+
return { kind: "binop", op: op.trim(), left, right };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Function call: name(args)
|
|
55
|
+
const callMatch = s.match(/^(\w+)\((.*)?\)$/);
|
|
56
|
+
if (callMatch) {
|
|
57
|
+
const args = callMatch[2] ? splitArgs(callMatch[2]).map(parseExprStr) : [];
|
|
58
|
+
return { kind: "call", name: callMatch[1], args };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Field reference: a.b.c
|
|
62
|
+
if (/^[\w.]+$/.test(s)) {
|
|
63
|
+
return { kind: "field", path: s.split(".") };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Fallback: treat as opaque literal
|
|
67
|
+
return { kind: "literal", value: s, raw: s };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function findBinOp(s: string, op: string): number {
|
|
71
|
+
let depth = 0;
|
|
72
|
+
for (let i = 0; i <= s.length - op.length; i++) {
|
|
73
|
+
const ch = s[i];
|
|
74
|
+
if (ch === "(" || ch === "[") depth++;
|
|
75
|
+
else if (ch === ")" || ch === "]") depth--;
|
|
76
|
+
else if (depth === 0 && s.slice(i, i + op.length) === op) {
|
|
77
|
+
return i;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return -1;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function splitArgs(s: string): string[] {
|
|
84
|
+
const args: string[] = [];
|
|
85
|
+
let depth = 0;
|
|
86
|
+
let current = "";
|
|
87
|
+
for (const ch of s) {
|
|
88
|
+
if (ch === "(" || ch === "[") depth++;
|
|
89
|
+
else if (ch === ")" || ch === "]") depth--;
|
|
90
|
+
else if (ch === "," && depth === 0) {
|
|
91
|
+
args.push(current.trim());
|
|
92
|
+
current = "";
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
current += ch;
|
|
96
|
+
}
|
|
97
|
+
if (current.trim()) args.push(current.trim());
|
|
98
|
+
return args;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ─── Entity Resolution ────────────────────────────────────────────────────────
|
|
102
|
+
// Determines which entities need to be fetched from the DB for a capability.
|
|
103
|
+
|
|
104
|
+
interface EntityFetch {
|
|
105
|
+
paramName: string; // capability parameter name (e.g., "item")
|
|
106
|
+
entityType: string; // entity type name (e.g., "Item")
|
|
107
|
+
tableName: string; // SQL table name (e.g., "items")
|
|
108
|
+
idField: string; // request body field for the ID (e.g., "item_id")
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function toSnakeCase(s: string): string {
|
|
112
|
+
return s.replace(/([a-z])([A-Z])/g, "$1_$2").toLowerCase();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function getEntityFetches(method: IR.IRMethod, mod: IR.IRModule, system: IR.IRSystem): EntityFetch[] {
|
|
116
|
+
const fetches: EntityFetch[] = [];
|
|
117
|
+
const seen = new Set<string>();
|
|
118
|
+
|
|
119
|
+
// Build a map of all entity names → table names across the whole system
|
|
120
|
+
const allModels = new Map<string, string>(); // entityName → tableName
|
|
121
|
+
for (const m of system.modules) {
|
|
122
|
+
for (const model of m.models) {
|
|
123
|
+
allModels.set(model.name, toSnakeCase(model.name) + "s");
|
|
124
|
+
allModels.set(model.name.toLowerCase(), toSnakeCase(model.name) + "s");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const param of method.input) {
|
|
129
|
+
const tableName = allModels.get(param.type) || allModels.get(param.type.toLowerCase());
|
|
130
|
+
if (tableName && !seen.has(param.name)) {
|
|
131
|
+
seen.add(param.name);
|
|
132
|
+
fetches.push({
|
|
133
|
+
paramName: param.name,
|
|
134
|
+
entityType: param.type,
|
|
135
|
+
tableName,
|
|
136
|
+
idField: param.name + "_id",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return fetches;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ─── Precondition Compiler ────────────────────────────────────────────────────
|
|
145
|
+
|
|
146
|
+
interface CompiledPrecondition {
|
|
147
|
+
code: string; // TypeScript guard clause
|
|
148
|
+
description: string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function compilePrecondition(expr: Expr, indent: string): string {
|
|
152
|
+
const condition = exprToTs(expr, true);
|
|
153
|
+
const description = exprToDescription(expr).replace(/"/g, '\\"');
|
|
154
|
+
return [
|
|
155
|
+
`${indent}if (${condition}) {`,
|
|
156
|
+
`${indent} return res.status(422).json({ error: { code: "PRECONDITION_FAILED", message: ${JSON.stringify(description)} } });`,
|
|
157
|
+
`${indent}}`,
|
|
158
|
+
].join("\n");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function exprToTs(expr: Expr, negate: boolean = false): string {
|
|
162
|
+
const inner = exprToTsInner(expr);
|
|
163
|
+
return negate ? `!(${inner})` : inner;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function exprToTsInner(expr: Expr): string {
|
|
167
|
+
switch (expr.kind) {
|
|
168
|
+
case "literal":
|
|
169
|
+
if (expr.value === "true") return "true";
|
|
170
|
+
if (expr.value === "false") return "false";
|
|
171
|
+
if (/^"/.test(expr.raw)) return expr.raw;
|
|
172
|
+
return expr.value;
|
|
173
|
+
|
|
174
|
+
case "field":
|
|
175
|
+
// Convert field path to JS property access
|
|
176
|
+
return expr.path.join("?.");
|
|
177
|
+
|
|
178
|
+
case "binop": {
|
|
179
|
+
const l = exprToTsInner(expr.left);
|
|
180
|
+
const r = exprToTsInner(expr.right);
|
|
181
|
+
switch (expr.op) {
|
|
182
|
+
case "==": return `${l} === ${r}`;
|
|
183
|
+
case "!=": return `${l} !== ${r}`;
|
|
184
|
+
case "and": return `(${l} && ${r})`;
|
|
185
|
+
case "or": return `(${l} || ${r})`;
|
|
186
|
+
case "in": return `[${r}].flat().includes(${l})`;
|
|
187
|
+
case "contains": return `${l}?.includes(${r})`;
|
|
188
|
+
case ">": case "<": case ">=": case "<=":
|
|
189
|
+
case "+": case "-": case "*": case "/":
|
|
190
|
+
return `${l} ${expr.op} ${r}`;
|
|
191
|
+
default: return `${l} ${expr.op} ${r}`;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
case "call":
|
|
196
|
+
if (expr.name === "now") return "new Date()";
|
|
197
|
+
return `${expr.name}(${expr.args.map(exprToTsInner).join(", ")})`;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function exprToDescription(expr: Expr): string {
|
|
202
|
+
switch (expr.kind) {
|
|
203
|
+
case "literal": return expr.raw;
|
|
204
|
+
case "field": return expr.path.join(".");
|
|
205
|
+
case "binop": {
|
|
206
|
+
const l = exprToDescription(expr.left);
|
|
207
|
+
const r = exprToDescription(expr.right);
|
|
208
|
+
return `${l} ${expr.op} ${r}`;
|
|
209
|
+
}
|
|
210
|
+
case "call": return `${expr.name}(${expr.args.map(exprToDescription).join(", ")})`;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ─── Effect Compiler ──────────────────────────────────────────────────────────
|
|
215
|
+
|
|
216
|
+
interface CompiledEffect {
|
|
217
|
+
sql: string;
|
|
218
|
+
params: string[];
|
|
219
|
+
description: string;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function compileEffect(effect: IR.IREffect, mod: IR.IRModule, system: IR.IRSystem, paramIdx: { n: number }): CompiledEffect | null {
|
|
223
|
+
const targetParts = effect.target.split(".");
|
|
224
|
+
if (targetParts.length < 2) return null;
|
|
225
|
+
|
|
226
|
+
const entityParam = targetParts[0]; // e.g., "item" or "trade"
|
|
227
|
+
const fieldName = targetParts[1]; // e.g., "quantity" or "offered_items"
|
|
228
|
+
const nestedPath = targetParts.slice(2); // e.g., ["owner_id"] for nested JSONB
|
|
229
|
+
|
|
230
|
+
// Find the model for this entity param — search across all modules
|
|
231
|
+
const model = (() => {
|
|
232
|
+
for (const m of system.modules) {
|
|
233
|
+
const found = m.models.find(mdl =>
|
|
234
|
+
toSnakeCase(mdl.name) === entityParam ||
|
|
235
|
+
mdl.name.toLowerCase() === entityParam.toLowerCase()
|
|
236
|
+
);
|
|
237
|
+
if (found) return found;
|
|
238
|
+
}
|
|
239
|
+
return mod.models.find(m =>
|
|
240
|
+
toSnakeCase(m.name) === entityParam ||
|
|
241
|
+
m.name.toLowerCase() === entityParam.toLowerCase()
|
|
242
|
+
);
|
|
243
|
+
})();
|
|
244
|
+
if (!model) return null;
|
|
245
|
+
|
|
246
|
+
const tableName = toSnakeCase(model.name) + "s";
|
|
247
|
+
const valueExpr = parseExprStr(effect.value);
|
|
248
|
+
const valueTs = exprToTsInner(valueExpr);
|
|
249
|
+
const idParam = `req.body.${entityParam}_id || req.params.id`;
|
|
250
|
+
|
|
251
|
+
// Detect if the param is a list type (bulk operation)
|
|
252
|
+
const isBulk = effect.target.includes("[]") ||
|
|
253
|
+
(entityParam.endsWith("s") && !model.name.toLowerCase().endsWith("s"));
|
|
254
|
+
const bulkIdParam = `req.body.${entityParam}_ids || req.body.${entityParam}?.map((x: any) => x.id)`;
|
|
255
|
+
const whereClause = isBulk
|
|
256
|
+
? `WHERE id = ANY($2::uuid[])`
|
|
257
|
+
: `WHERE id = ${`$${2}`}`;
|
|
258
|
+
|
|
259
|
+
// Handle nested JSONB path: trade.offered_items.owner_id
|
|
260
|
+
if (nestedPath.length > 0) {
|
|
261
|
+
const jsonbField = fieldName;
|
|
262
|
+
const jsonbPath = nestedPath.join(".");
|
|
263
|
+
const p1 = `$${paramIdx.n++}`;
|
|
264
|
+
const p2 = `$${paramIdx.n++}`;
|
|
265
|
+
// Use jsonb_set to update nested path
|
|
266
|
+
const jsonbPathLiteral = `'{${nestedPath.join(",")}}'`;
|
|
267
|
+
return {
|
|
268
|
+
sql: `UPDATE ${tableName} SET ${jsonbField} = jsonb_set(COALESCE(${jsonbField}, '{}'), ${jsonbPathLiteral}, to_jsonb(${p1}::text), true), updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
269
|
+
params: [valueTs, idParam],
|
|
270
|
+
description: `${effect.target} = ${effect.value}`,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
switch (effect.op) {
|
|
275
|
+
case "assign": {
|
|
276
|
+
const p1 = `$${paramIdx.n++}`;
|
|
277
|
+
const p2 = `$${paramIdx.n++}`;
|
|
278
|
+
return {
|
|
279
|
+
sql: `UPDATE ${tableName} SET ${fieldName} = ${p1}, updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
280
|
+
params: [valueTs, idParam],
|
|
281
|
+
description: `${effect.target} = ${effect.value}`,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
case "add": {
|
|
285
|
+
const p1 = `$${paramIdx.n++}`;
|
|
286
|
+
const p2 = `$${paramIdx.n++}`;
|
|
287
|
+
const fieldType = model.fields.find(f => f.name === fieldName)?.type || "";
|
|
288
|
+
const isNumeric = ["uint", "int", "float"].includes(fieldType);
|
|
289
|
+
if (isNumeric) {
|
|
290
|
+
return {
|
|
291
|
+
sql: `UPDATE ${tableName} SET ${fieldName} = ${fieldName} + ${p1}, updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
292
|
+
params: [valueTs, idParam],
|
|
293
|
+
description: `${effect.target} += ${effect.value}`,
|
|
294
|
+
};
|
|
295
|
+
} else {
|
|
296
|
+
return {
|
|
297
|
+
sql: `UPDATE ${tableName} SET ${fieldName} = ${fieldName} || jsonb_build_array(${p1}::jsonb), updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
298
|
+
params: [valueTs, idParam],
|
|
299
|
+
description: `${effect.target} += ${effect.value}`,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
case "remove": {
|
|
304
|
+
const p1 = `$${paramIdx.n++}`;
|
|
305
|
+
const p2 = `$${paramIdx.n++}`;
|
|
306
|
+
const fieldType = model.fields.find(f => f.name === fieldName)?.type || "";
|
|
307
|
+
const isNumeric = ["uint", "int", "float"].includes(fieldType);
|
|
308
|
+
if (isNumeric) {
|
|
309
|
+
return {
|
|
310
|
+
sql: `UPDATE ${tableName} SET ${fieldName} = ${fieldName} - ${p1}, updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
311
|
+
params: [valueTs, idParam],
|
|
312
|
+
description: `${effect.target} -= ${effect.value}`,
|
|
313
|
+
};
|
|
314
|
+
} else {
|
|
315
|
+
return {
|
|
316
|
+
sql: `UPDATE ${tableName} SET ${fieldName} = (SELECT jsonb_agg(elem) FROM jsonb_array_elements(${fieldName}) elem WHERE elem != ${p1}::jsonb), updated_at = NOW() WHERE id = ${p2} RETURNING *`,
|
|
317
|
+
params: [valueTs, idParam],
|
|
318
|
+
description: `${effect.target} -= ${effect.value}`,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// ─── Main Capability Body Emitter ─────────────────────────────────────────────
|
|
326
|
+
|
|
327
|
+
export function emitCapabilityBody(
|
|
328
|
+
method: IR.IRMethod,
|
|
329
|
+
mod: IR.IRModule,
|
|
330
|
+
system: IR.IRSystem,
|
|
331
|
+
indent: string = " "
|
|
332
|
+
): string {
|
|
333
|
+
const lines: string[] = [];
|
|
334
|
+
const fetches = getEntityFetches(method, mod, system);
|
|
335
|
+
|
|
336
|
+
// 0. Destructure primitive params from req.body
|
|
337
|
+
const primitiveParams = method.input.filter(p => {
|
|
338
|
+
const isPrimitive = ["string", "uint", "int", "float", "bool", "timestamp", "uuid", "bytes", "json"].includes(p.type);
|
|
339
|
+
const isListOrSet = p.type.startsWith("list<") || p.type.startsWith("set<");
|
|
340
|
+
const isEntityFetch = fetches.some(f => f.paramName === p.name);
|
|
341
|
+
return (isPrimitive || isListOrSet) && !isEntityFetch;
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
if (primitiveParams.length > 0) {
|
|
345
|
+
const destructured = primitiveParams.map(p => p.name).join(", ");
|
|
346
|
+
lines.push(`${indent}const { ${destructured} } = req.body;`);
|
|
347
|
+
lines.push(``);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// 1. Fetch entities referenced in preconditions/effects
|
|
351
|
+
if (fetches.length > 0) {
|
|
352
|
+
lines.push(`${indent}// Fetch entities`);
|
|
353
|
+
for (const fetch of fetches) {
|
|
354
|
+
const idExpr = `req.body.${fetch.idField} || req.params.id`;
|
|
355
|
+
lines.push(`${indent}const ${fetch.paramName} = await queryOne(\`SELECT * FROM ${fetch.tableName} WHERE id = $1\`, [${idExpr}]);`);
|
|
356
|
+
lines.push(`${indent}if (!${fetch.paramName}) {`);
|
|
357
|
+
lines.push(`${indent} return res.status(404).json({ error: { code: "NOT_FOUND", message: "${fetch.paramName} not found" } });`);
|
|
358
|
+
lines.push(`${indent}}`);
|
|
359
|
+
}
|
|
360
|
+
lines.push(``);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// 2. Precondition checks
|
|
364
|
+
if (method.preconditions.length > 0) {
|
|
365
|
+
lines.push(`${indent}// Preconditions`);
|
|
366
|
+
for (const pre of method.preconditions) {
|
|
367
|
+
try {
|
|
368
|
+
const expr = parseExprStr(pre.expression);
|
|
369
|
+
lines.push(compilePrecondition(expr, indent));
|
|
370
|
+
} catch {
|
|
371
|
+
// Fallback: emit as comment if parsing fails
|
|
372
|
+
lines.push(`${indent}// CHECK: ${pre.description}`);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
lines.push(``);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// 3. Effects (applied in declaration order, each in its own query)
|
|
379
|
+
if (method.effects.length > 0) {
|
|
380
|
+
lines.push(`${indent}// Effects (applied in declaration order)`);
|
|
381
|
+
const effectResults: string[] = [];
|
|
382
|
+
|
|
383
|
+
for (const effect of method.effects) {
|
|
384
|
+
// Each effect gets its own parameter numbering starting at 1
|
|
385
|
+
const paramIdx = { n: 1 };
|
|
386
|
+
const compiled = compileEffect(effect, mod, system, paramIdx);
|
|
387
|
+
if (compiled) {
|
|
388
|
+
const resultVar = `__effect_${effectResults.length}`;
|
|
389
|
+
effectResults.push(resultVar);
|
|
390
|
+
lines.push(`${indent}const ${resultVar} = await query(\`${compiled.sql}\`, [${compiled.params.join(", ")}]);`);
|
|
391
|
+
lines.push(`${indent}if (!${resultVar} || ${resultVar}.length === 0) {`);
|
|
392
|
+
lines.push(`${indent} throw new Error("Effect failed: ${compiled.description.replace(/"/g, '\\"')}");`);
|
|
393
|
+
lines.push(`${indent}}`);
|
|
394
|
+
} else {
|
|
395
|
+
// Fallback for complex effects we can't compile
|
|
396
|
+
lines.push(`${indent}// EFFECT: ${effect.target} ${effect.op === "assign" ? "=" : effect.op === "add" ? "+=" : "-="} ${effect.value}`);
|
|
397
|
+
lines.push(`${indent}// TODO: Implement this effect manually`);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
lines.push(``);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// 4. Event emissions
|
|
404
|
+
if (method.emissions.length > 0) {
|
|
405
|
+
lines.push(`${indent}// Emit events`);
|
|
406
|
+
for (const ev of method.emissions) {
|
|
407
|
+
const payload = buildEventPayload(method, fetches);
|
|
408
|
+
if (method.sync === "transactional") {
|
|
409
|
+
lines.push(`${indent}await eventBus.publish("${ev}", ${payload}, "${mod.name}", auth.trace_id, __client);`);
|
|
410
|
+
} else {
|
|
411
|
+
lines.push(`${indent}await eventBus.publish("${ev}", ${payload}, "${mod.name}", auth.trace_id);`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
lines.push(``);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// 5. Return result
|
|
418
|
+
const resultEntity = fetches[0];
|
|
419
|
+
if (resultEntity) {
|
|
420
|
+
lines.push(`${indent}res.json({ ok: true, action: "${method.name}", entity: ${resultEntity.paramName} });`);
|
|
421
|
+
} else {
|
|
422
|
+
lines.push(`${indent}res.json({ ok: true, action: "${method.name}" });`);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return lines.join("\n");
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function buildEventPayload(method: IR.IRMethod, fetches: EntityFetch[]): string {
|
|
429
|
+
const fields: string[] = [];
|
|
430
|
+
for (const fetch of fetches) {
|
|
431
|
+
fields.push(`${fetch.paramName}_id: ${fetch.paramName}?.id`);
|
|
432
|
+
}
|
|
433
|
+
fields.push(`timestamp: new Date().toISOString()`);
|
|
434
|
+
fields.push(`actor_id: auth.actor_id`);
|
|
435
|
+
return `{ ${fields.join(", ")} }`;
|
|
436
|
+
}
|