@simetrajs/designer 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,60 @@
1
+ # Object modules: TypeScript hooks of a command
2
+
3
+ How the server code of an application adds behaviour to the generated
4
+ commands of an object; the router is `SKILL.md`. The full contract is §6.9 of
5
+ the P4 runtime spec in the Simetra repository
6
+ (`docs/superpowers/specs/2026-10-26-p4-runtime-design.md`).
7
+
8
+ ## Shape
9
+
10
+ An object module is a plain object typed by the generated entity type
11
+ (`entities.d.ts` from `compile --out`) and keyed by 1C write events:
12
+
13
+ ```ts
14
+ import { defineObjectModule } from "simetra/model"
15
+ import type { Catalogs } from "./generated/entities" // the compile --out file
16
+
17
+ export const client = defineObjectModule<Catalogs.Client>({
18
+ beforeWrite({ ctx, draft }) {
19
+ return draft
20
+ },
21
+ onWrite({ ctx, row }) {},
22
+ })
23
+ ```
24
+
25
+ - `beforeWrite` (1C `ПередЗаписью`) runs before the core function and returns
26
+ the draft `{ values?, tabularSections? }` the phase writes. It never adds
27
+ `id` or `mark`; the runtime checks the returned draft again.
28
+ - `onWrite` (1C `ПриЗаписи`) sees the written row in the same transaction.
29
+ - Both run on the operations `create`, `update` and `markDeletion`;
30
+ `delete` raises no module event. On `markDeletion` the draft is empty and
31
+ the mark is `ctx.mark`.
32
+ - The host passes modules to `createCommandRuntime` (`simetra/server`) as
33
+ `objectModules`, keyed `"<Kind>.<Name>"` (`"Catalog.Client"`). An unknown
34
+ object or hook key fails runtime creation, not the first command.
35
+
36
+ ## The context
37
+
38
+ - `ctx.action` is the operation (`create`, `update`, `markDeletion`),
39
+ `ctx.isNew` is `create`; `ctx.user` is the current user.
40
+ - `ctx.additionalProperties` is shared by the hooks of one phase only.
41
+ - `ctx.run(operation)` runs a phase of another object in the same
42
+ transaction, with its own rights check and hooks; `ctx.read(query)` reads in
43
+ that transaction.
44
+ - `ctx.reject(code, path?)` fails the command with `command.rejected`.
45
+ - A `ctx` method works only while its hook runs. Never keep `ctx` for later:
46
+ a late call fails the command (`command.internal`).
47
+ - A failure the hook catches still fails the command; await every `ctx.run`.
48
+
49
+ ## No external I/O in a hook
50
+
51
+ A serialization failure or a deadlock reruns the whole transaction, hooks
52
+ included. A hook never calls HTTP, mail or a queue outside the database. Put
53
+ such work into `ctx.afterCommit(fn)`: it runs after `COMMIT`, at most once,
54
+ and only for non-critical work (a crash after `COMMIT` loses it); its error
55
+ does not undo the command. On a serverless host, pass `defer` (`waitUntil`) to
56
+ `createCommandRuntime`, or `run` waits for this work before it returns.
57
+
58
+ A hook gets no database client and no SQL text. A row rule that must hold for
59
+ every writer belongs in the database (`EventSubscription`, row rules, rights),
60
+ not in a hook.