@burnsred/entity-sync 0.0.0-stage → 3.0.0-rc.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.
- package/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +11 -0
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +240 -2
- package/agents/entity-editor.md +16 -0
- package/bin/entity-sync.mjs +36 -0
- package/dist/cli.d.mts +20 -0
- package/dist/commands/asserts.d.mts +4 -0
- package/dist/commands/check.d.mts +6 -0
- package/dist/commands/inspect.d.mts +30 -0
- package/dist/commands/plan.d.mts +28 -0
- package/dist/commands/schema.d.mts +39 -0
- package/dist/config.d.mts +175 -0
- package/dist/index.d.mts +5 -0
- package/dist/lib/cache.d.mts +120 -0
- package/dist/lib/diff.d.mts +228 -0
- package/dist/lib/field-map.d.mts +93 -0
- package/dist/lib/gen-asserts.d.mts +70 -0
- package/dist/lib/graph.d.mts +90 -0
- package/dist/lib/parse-entities.d.mts +257 -0
- package/dist/lib/parse-schema.d.mts +98 -0
- package/dist/lib/resolve.d.mts +135 -0
- package/dist/project.d.mts +60 -0
- package/hooks/editor-guard.mjs +86 -0
- package/hooks/hooks.json +15 -0
- package/package.json +54 -4
- package/skills/entity/SKILL.md +105 -0
- package/skills/entity/reference/create.md +74 -0
- package/skills/entity/reference/editor.md +80 -0
- package/skills/entity/reference/enums.md +89 -0
- package/skills/entity/reference/fields.md +101 -0
- package/skills/entity/reference/sync.md +191 -0
- package/src/cli.mjs +110 -0
- package/src/commands/asserts.mjs +52 -0
- package/src/commands/check.mjs +88 -0
- package/src/commands/inspect.mjs +290 -0
- package/src/commands/plan.mjs +542 -0
- package/src/commands/schema.mjs +206 -0
- package/src/config.mjs +205 -0
- package/src/index.mjs +19 -0
- package/src/lib/cache.mjs +323 -0
- package/src/lib/diff.mjs +706 -0
- package/src/lib/field-map.mjs +438 -0
- package/src/lib/gen-asserts.mjs +478 -0
- package/src/lib/graph.mjs +284 -0
- package/src/lib/parse-entities.mjs +935 -0
- package/src/lib/parse-schema.mjs +667 -0
- package/src/lib/resolve.mjs +455 -0
- package/src/project.mjs +115 -0
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
|
+
"author": "Burnsred",
|
|
3
|
+
"description": "Keep @burnsred/entity definitions in sync with a DRF OpenAPI schema; ships the /entity Claude Code plugin",
|
|
4
|
+
"license": "MIT",
|
|
2
5
|
"name": "@burnsred/entity-sync",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
6
|
+
"version": "3.0.0-rc.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"bin": {
|
|
9
|
+
"entity-sync": "./bin/entity-sync.mjs"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"@burnsred/source": "./src/index.mjs",
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"default": "./src/index.mjs"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"main": "./src/index.mjs",
|
|
20
|
+
"types": "./dist/index.d.mts",
|
|
21
|
+
"files": [
|
|
22
|
+
"bin/**",
|
|
23
|
+
"dist/**",
|
|
24
|
+
"src/**",
|
|
25
|
+
"!src/**/*.test.*",
|
|
26
|
+
".claude-plugin/**",
|
|
27
|
+
"skills/**",
|
|
28
|
+
"agents/**",
|
|
29
|
+
"hooks/**",
|
|
30
|
+
"!hooks/**/*.test.*",
|
|
31
|
+
"./*.md"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json --noCheck",
|
|
35
|
+
"version": "node scripts/sync-version.mjs && git add .claude-plugin/plugin.json skills/entity/SKILL.md"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+ssh://git@bitbucket.org/burnsred/burnsred-entity-library.git"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=22.12.0"
|
|
46
|
+
},
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"openapi-typescript": "^7.13.0",
|
|
49
|
+
"yaml": "^2.9.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"prettier": ">=3",
|
|
53
|
+
"typescript": ">=5.4"
|
|
54
|
+
},
|
|
55
|
+
"gitHead": "85be5e063e99f77255bf58361148213c0ef12bb6"
|
|
56
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: entity
|
|
3
|
+
description: Create and edit @burnsred/entity domain entities, and keep them in sync with the broker's OpenAPI schema. Use when the user wants to add a new entity, sync entities with the API, or modify an entity's fields or relationships.
|
|
4
|
+
argument-hint: '[sync|create]'
|
|
5
|
+
disable-model-invocation: true
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Entity Management
|
|
9
|
+
|
|
10
|
+
<!-- entity-sync-version: 3.0.0-rc.0 -->
|
|
11
|
+
|
|
12
|
+
An Entity describes a domain model - its fields, validation rules, data
|
|
13
|
+
conversion logic, and (via a duck) its API integration. All methods are static;
|
|
14
|
+
Entity is never instantiated directly.
|
|
15
|
+
|
|
16
|
+
The project's API is a separate Django DRF project, referred to as 'the broker',
|
|
17
|
+
which publishes an OpenAPI schema via drf-spectacular.
|
|
18
|
+
|
|
19
|
+
Entities are exercised by the project dependencies:
|
|
20
|
+
|
|
21
|
+
- @burnsred/entity
|
|
22
|
+
- @burnsred/entity-duck
|
|
23
|
+
- @burnsred/entity-duck-namespace-auth
|
|
24
|
+
- @burnsred/entity-duck-namespace-drf
|
|
25
|
+
- @burnsred/entity-duck-namespace-screen
|
|
26
|
+
- @burnsred/entity-duck-query
|
|
27
|
+
- @burnsred/entity-form
|
|
28
|
+
- @burnsred/higher-order-component
|
|
29
|
+
|
|
30
|
+
Entities live in the `entitiesDir` named in the project's
|
|
31
|
+
`entity-sync.config.mjs` (default `src/entities`). The schema types live at its
|
|
32
|
+
`schema.dts` (default `src/api/schema.d.ts`).
|
|
33
|
+
|
|
34
|
+
## Before anything else
|
|
35
|
+
|
|
36
|
+
1. Confirm `entity-sync.config.mjs` (or `.js` / `.json`) exists at the project
|
|
37
|
+
root and `package.json` has the `entity:*` scripts. If either is missing,
|
|
38
|
+
stop and point the user at the `@burnsred/entity-sync` README setup section.
|
|
39
|
+
2. Run `npm run --silent entity:version`. This skill was written for the
|
|
40
|
+
version in the `entity-sync-version` comment above. If the major or minor
|
|
41
|
+
differ, tell the user the installed CLI and this skill disagree (update
|
|
42
|
+
whichever is behind) before continuing.
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
The user invokes this skill as `/entity` with optional arguments. Read only the
|
|
47
|
+
reference file for the task at hand - they are written to stand alone, and
|
|
48
|
+
loading all of them wastes most of what it costs.
|
|
49
|
+
|
|
50
|
+
| invocation | read |
|
|
51
|
+
| ---------------- | --------------------------------------------- |
|
|
52
|
+
| `/entity sync` | `${CLAUDE_SKILL_DIR}/reference/sync.md` |
|
|
53
|
+
| `/entity create` | `${CLAUDE_SKILL_DIR}/reference/create.md` |
|
|
54
|
+
| `/entity` | neither - ask the user what they'd like to do |
|
|
55
|
+
|
|
56
|
+
The reference files:
|
|
57
|
+
|
|
58
|
+
| file | contents |
|
|
59
|
+
| ----------- | ----------------------------------------------------------------------------- |
|
|
60
|
+
| `sync.md` | the sync procedure, reading the report, annotations, suppressions, delegation |
|
|
61
|
+
| `create.md` | scaffolding a new entity, ducks and endpoints |
|
|
62
|
+
| `fields.md` | the Fields / Record convention and the schema-to-field mapping table |
|
|
63
|
+
| `enums.md` | choices dicts, the schemaEnum tag, enum assertions |
|
|
64
|
+
| `editor.md` | the per-entity editor brief (used when the plugin agent is unavailable) |
|
|
65
|
+
|
|
66
|
+
`fields.md` and `enums.md` are the authoring rules for one file's contents. Both
|
|
67
|
+
tasks need them, and so does the entity editor - which reads them itself, so
|
|
68
|
+
never inline them into a dispatch.
|
|
69
|
+
|
|
70
|
+
## Delegation
|
|
71
|
+
|
|
72
|
+
Editing an entity file is subagent work. Dispatch an entity editor with the
|
|
73
|
+
entity name and its report section; it reads the authoring rules itself and
|
|
74
|
+
returns a summary. The orchestrator keeps the report, the wave ordering, the
|
|
75
|
+
decisions that need the user, and `npm run entity:check` - which is
|
|
76
|
+
project-wide and cannot be delegated. `reference/sync.md` has the details.
|
|
77
|
+
|
|
78
|
+
How to dispatch depends on how this skill was installed:
|
|
79
|
+
|
|
80
|
+
- **As the `entity-sync` Claude Code plugin** (preferred): use the
|
|
81
|
+
`entity-sync:entity-editor` subagent. Its tools exclude Write, and the
|
|
82
|
+
plugin's hook stops it running anything but `entity:inspect`, eslint and
|
|
83
|
+
prettier - including `entity:check`.
|
|
84
|
+
- **Standalone** (copied by `npx skills`): dispatch a general-purpose subagent,
|
|
85
|
+
model sonnet, with the prompt "Read `${CLAUDE_SKILL_DIR}/reference/editor.md`
|
|
86
|
+
and follow it", then the entity name, component and report section. The same
|
|
87
|
+
limits apply, but only as instructions.
|
|
88
|
+
|
|
89
|
+
## Debugging the tooling
|
|
90
|
+
|
|
91
|
+
`npm run entity:inspect -- <command>` reads the same parsed data the sync uses:
|
|
92
|
+
|
|
93
|
+
| command | shows |
|
|
94
|
+
| ------------------ | ------------------------------------------------------ |
|
|
95
|
+
| `component <Name>` | one component's fields, with every modifier and format |
|
|
96
|
+
| `components` | all component names and kinds |
|
|
97
|
+
| `path <substring>` | matching endpoints and the component each returns |
|
|
98
|
+
| `entity <Name>` | one entity's declarations as parsed |
|
|
99
|
+
| `entities` | all entity classes, their duck and resolved `apiBase` |
|
|
100
|
+
| `enums` | choices dicts, their component and any value drift |
|
|
101
|
+
|
|
102
|
+
When the broker is the thing that is wrong (a `SerializerMethodField` typed as
|
|
103
|
+
`string`, an enum with a hashed name, choices lost behind an explicit
|
|
104
|
+
`CharField`), the fix belongs in the broker. The `entity-broker` plugin carries
|
|
105
|
+
those rules; suggest it rather than suppressing the field here.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Task: create
|
|
2
|
+
|
|
3
|
+
Read `fields.md` (beside this file) for the Fields / Record convention and the
|
|
4
|
+
field mapping table, and `enums.md` if any field is enum-typed.
|
|
5
|
+
|
|
6
|
+
Look at the component first: `npm run entity:inspect -- component <Name>` shows
|
|
7
|
+
every field with its format and modifiers, and `npm run entity:inspect -- path
|
|
8
|
+
<substring>` finds the endpoint and the component it returns.
|
|
9
|
+
|
|
10
|
+
When creating entities:
|
|
11
|
+
|
|
12
|
+
- create a separate file for each in the `entitiesDir` (default
|
|
13
|
+
`src/entities`), named in PascalCase with a .ts extension.
|
|
14
|
+
- export:
|
|
15
|
+
- the entity as `default`
|
|
16
|
+
- the types `EntityNameFields` and `EntityNameRecord` - both must be
|
|
17
|
+
**exported**, or the sync cannot assert against them
|
|
18
|
+
- add it to the entities barrel (`index.ts`), sorted alphabetically, **only if
|
|
19
|
+
it needs store registration** - in BurnsRED projects that barrel is the
|
|
20
|
+
store's registry, not a list of every entity. A nested-only entity is reached
|
|
21
|
+
through its parent and is deliberately absent.
|
|
22
|
+
- follow the Fields / Record convention
|
|
23
|
+
- add the annotation that records what it models: a schema tag naming the
|
|
24
|
+
component when the mapping isn't derivable from `apiBase`, or a noSchema tag
|
|
25
|
+
with a reason when it models no API resource. The tag table is in `sync.md`.
|
|
26
|
+
- for any field the schema types as a `*Enum` component, add a choices dict in
|
|
27
|
+
the same file and reference its type - see `enums.md`. Check first whether one
|
|
28
|
+
already exists: `npm run entity:inspect -- enums`
|
|
29
|
+
- finish with `npm run entity:check` to confirm it agrees with the schema
|
|
30
|
+
|
|
31
|
+
## Entities with endpoints
|
|
32
|
+
|
|
33
|
+
If the entity can be accessed at an API endpoint (it is present under `paths`),
|
|
34
|
+
we need to:
|
|
35
|
+
|
|
36
|
+
- add the static class properties `paginated` and `paths`
|
|
37
|
+
- add a `Duck` definition, following the project's existing ducks (app name
|
|
38
|
+
constant, duck class)
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
class ExampleEntity extends Entity {
|
|
42
|
+
static paginated = true;
|
|
43
|
+
|
|
44
|
+
static paths = {
|
|
45
|
+
apiBase: '/example/',
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// ...
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
ExampleEntity.duck = new DjangoRestFramework({
|
|
52
|
+
app: duckAppName,
|
|
53
|
+
entity: ExampleEntity,
|
|
54
|
+
name: 'ExampleEntity',
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`apiBase` is resolved against the axios `baseURL`, which the sync knows as
|
|
59
|
+
`apiBasePrefix` in `entity-sync.config.mjs`; so with `apiBasePrefix:
|
|
60
|
+
'BASE_API_URL'` a bare `'/example/'` means `<BASE_API_URL path>/example/`. For
|
|
61
|
+
another Django app, interpolate its settings constant:
|
|
62
|
+
`` `${settings.OTHER_API_URL}/example/` ``. Every constant used this way must
|
|
63
|
+
be in the config's `settingsPrefixes`, mirroring `src/settings`; an unknown one
|
|
64
|
+
shows up as an unresolved `apiBase` in the report.
|
|
65
|
+
|
|
66
|
+
## Examples
|
|
67
|
+
|
|
68
|
+
Use the project's own entities as references - `npm run entity:inspect --
|
|
69
|
+
entities` lists them with their duck and `apiBase`. Look for:
|
|
70
|
+
|
|
71
|
+
- one with an endpoint, nested fields and an annotation
|
|
72
|
+
- a nested-only entity with no duck
|
|
73
|
+
- a minimal scaffold (often `BasicEntity.ts`)
|
|
74
|
+
- the auth whoami entity with a specialised `DuckAuth` (often `User.ts`)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Entity editor brief
|
|
2
|
+
|
|
3
|
+
You bring one entity file into line with the broker's OpenAPI schema.
|
|
4
|
+
|
|
5
|
+
You are dispatched by the `/entity sync` procedure with an entity name, the
|
|
6
|
+
component it maps to, and that entity's section of the drift report (default
|
|
7
|
+
`.entity-sync/report.md`). That section is your complete work list.
|
|
8
|
+
|
|
9
|
+
## First, read the rules
|
|
10
|
+
|
|
11
|
+
Before editing, read these files, which sit beside this one:
|
|
12
|
+
|
|
13
|
+
- `fields.md` - the Fields / Record convention and the schema-to-field mapping
|
|
14
|
+
table
|
|
15
|
+
- `enums.md` - only if a field in your work list is enum-typed
|
|
16
|
+
(`enum-missing`, `enum-untyped`, a `type-text` op on a choices value, or a
|
|
17
|
+
`$ref` to a `*Enum` component)
|
|
18
|
+
|
|
19
|
+
Do not guess a mapping. Every schema signal in the report has a row in that
|
|
20
|
+
table. To see a component or entity exactly as the sync parsed it, use
|
|
21
|
+
`npm run entity:inspect -- component <Name>` or
|
|
22
|
+
`npm run entity:inspect -- entity <Name>`.
|
|
23
|
+
|
|
24
|
+
## Then
|
|
25
|
+
|
|
26
|
+
1. Read the entity file (its path is in the report section).
|
|
27
|
+
2. Apply every op in your work list. Advisory ops (`readOnly` hints,
|
|
28
|
+
`enum-untyped`) count: you are already in the file, so fold them in.
|
|
29
|
+
3. Keep the `Fields` and `Record` types in step with the field declarations.
|
|
30
|
+
A field whose `dataToValue` converts - a nested `EntityField`, a `DateField`,
|
|
31
|
+
a `DateTimeField` - needs its substitution in the `Omit<...>` of the Record
|
|
32
|
+
type. This is the single most common thing a sync gets wrong, and it fails
|
|
33
|
+
two entities away in the parent, so check it explicitly before you finish.
|
|
34
|
+
4. `npx eslint --fix <the files you touched>`. Not a project `lint` script with
|
|
35
|
+
the files appended: that usually lints the whole tree as well.
|
|
36
|
+
|
|
37
|
+
## Scope
|
|
38
|
+
|
|
39
|
+
Edit only the entity file you were given (plus its own choices dicts, which live
|
|
40
|
+
in the same file). If a fix seems to require touching another entity, stop and
|
|
41
|
+
report it instead - the orchestrator owns the dependency ordering and another
|
|
42
|
+
editor may be in that file right now.
|
|
43
|
+
|
|
44
|
+
Never run `npm run entity:check` (or `entity-sync check`, or `tsc`), and do not
|
|
45
|
+
ask for it. It is a project-wide `tsc` that takes 30s+ and reports failures from
|
|
46
|
+
files you are not responsible for. The orchestrator runs it once per wave. Your
|
|
47
|
+
job is to make your one file right; proving the whole tree compiles is not
|
|
48
|
+
yours.
|
|
49
|
+
|
|
50
|
+
Do not create new files. A report op that needs an entity that does not exist
|
|
51
|
+
yet is `/entity create` work - report it and stop.
|
|
52
|
+
|
|
53
|
+
Do not add a schemaIgnore suppression to make a disagreement go away unless the
|
|
54
|
+
report explicitly says the schema is wrong for that field. A suppression is a
|
|
55
|
+
standing decision that outlives this sync; silencing drift you did not
|
|
56
|
+
understand is the failure mode it exists to prevent. Report it instead.
|
|
57
|
+
|
|
58
|
+
## What to return
|
|
59
|
+
|
|
60
|
+
Your final message is data for the orchestrator, not prose for a human. Keep it
|
|
61
|
+
short - the whole point of this delegation is that the file contents and your
|
|
62
|
+
reasoning stay out of the caller's context.
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
OrderEntity -> OrderDetail: ok
|
|
66
|
+
status: CharField + OrderStatus dict (enum-missing)
|
|
67
|
+
placed_on: DateField, Moment in Record (type-date)
|
|
68
|
+
customer: IdField (was EntityField; schema says Format: uuid)
|
|
69
|
+
lint: clean
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
If something is unresolved, say so on its own line, name the field, and say what
|
|
73
|
+
decision is needed:
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
BLOCKED tags: SerializerMethodField typed `string`, entity declares
|
|
77
|
+
TagEntityFields[]. Needs a schemaIgnore reason or a broker fix.
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Never paste the file, a diff, or the report back.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Enums
|
|
2
|
+
|
|
3
|
+
A broker `ChoicesField` reaches the schema as a `*Enum` component - a bare
|
|
4
|
+
string-literal union. Model it as a frozen dict plus a type alias of the same
|
|
5
|
+
name, so one import gives both `Status.draft` at call sites and `Status` as a
|
|
6
|
+
type:
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
/**
|
|
10
|
+
* @schemaEnum OrderStatusEnum
|
|
11
|
+
*/
|
|
12
|
+
export const OrderStatus = {
|
|
13
|
+
draft: 'draft',
|
|
14
|
+
awaiting_approval: 'awaiting_approval',
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
export type OrderStatus = (typeof OrderStatus)[keyof typeof OrderStatus];
|
|
18
|
+
|
|
19
|
+
export type OrderEntityFields = {
|
|
20
|
+
status: OrderStatus;
|
|
21
|
+
};
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The rules:
|
|
25
|
+
|
|
26
|
+
- **Drop the `Enum` suffix** from the dict's name; the schemaEnum tag preserves
|
|
27
|
+
it. The tag is the only thing binding a dict to a component - there is no
|
|
28
|
+
name-based inference, because many dicts have no broker counterpart and
|
|
29
|
+
guessing would report every one of them as unresolved.
|
|
30
|
+
- **An untagged dict is local**, not unresolved. Client-side enums (widget
|
|
31
|
+
kinds, UI modes) have no serializer behind them; they need no annotation and
|
|
32
|
+
get no assertion.
|
|
33
|
+
- **Keys are the wire value verbatim** (`draft: 'draft'`). No casing transform,
|
|
34
|
+
so the mapping stays mechanical and reversible.
|
|
35
|
+
- **The type alias shares the const's name.** TypeScript keeps types and values
|
|
36
|
+
in separate namespaces, so this is legal and halves the names to remember.
|
|
37
|
+
Older dicts may use a `Value` suffix (`OrderStatusValue`) or invert the pair
|
|
38
|
+
(`OrderStatusEnum` / `OrderStatus`); both still work and are not worth a
|
|
39
|
+
rename, since the assertion compares the union whatever the name.
|
|
40
|
+
- **`BlankEnum` and `NullEnum` stay out of the dict.** They are how
|
|
41
|
+
drf-spectacular says "may be unset", not members of the set - a field is typed
|
|
42
|
+
`OrderStatusEnum | BlankEnum` upstream. Put them in the Fields type instead:
|
|
43
|
+
`status: OrderStatus | ''`. Only reach for a schemaIgnore suppression when
|
|
44
|
+
deliberately closing the set against the wire.
|
|
45
|
+
- **A label map is separate and optional.** Name it `OrderStatusChoices`:
|
|
46
|
+
`as const satisfies Record<OrderStatus, string>` buys exhaustiveness for free,
|
|
47
|
+
so a value added upstream fails the build until it is labelled.
|
|
48
|
+
|
|
49
|
+
Do **not** pass `options` to a field, and do not use `EnumField`. `options` is
|
|
50
|
+
read by no validator, and `EnumField` is backed by `EntityEnum`, which turns the
|
|
51
|
+
runtime value into an Immutable `Map` - that would break every
|
|
52
|
+
`status === OrderStatus.draft` comparison and every `data-status={status}`
|
|
53
|
+
badge selector in the app. `CharField` plus a dict is the pattern.
|
|
54
|
+
|
|
55
|
+
Check whether a dict already exists before adding one:
|
|
56
|
+
`npm run entity:inspect -- enums`
|
|
57
|
+
|
|
58
|
+
## Assertions
|
|
59
|
+
|
|
60
|
+
Each tagged dict gets a generated assertion pinning it to its component in
|
|
61
|
+
**both** directions:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
export const assertOrderStatus: Exact<
|
|
65
|
+
OrderStatus,
|
|
66
|
+
components['schemas']['OrderStatusEnum']
|
|
67
|
+
> = true;
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The dict is asserted rather than the field because a field's wire type may be
|
|
71
|
+
widened with `BlankEnum`, which would make a field-level `Exact` demand the dict
|
|
72
|
+
carry `''`. Comparing the dict with its own component catches both a value
|
|
73
|
+
dropped upstream and a value the app invented, and keeps working for a field
|
|
74
|
+
that carries a schemaIgnore suppression.
|
|
75
|
+
|
|
76
|
+
## Op kinds
|
|
77
|
+
|
|
78
|
+
Three op kinds report enum drift:
|
|
79
|
+
|
|
80
|
+
| kind | severity | means |
|
|
81
|
+
| -------------- | -------- | ----------------------------------------------------------------------- |
|
|
82
|
+
| `enum-missing` | shape | a field refs a `*Enum` component no dict models yet |
|
|
83
|
+
| `enum-untyped` | advisory | a dict exists, but the field is typed `string` or inlines its values |
|
|
84
|
+
| `type-text` | shape | the declared type disagrees with the dict's values outright |
|
|
85
|
+
|
|
86
|
+
`enum-untyped` is advisory because the data flowing through such a field is
|
|
87
|
+
already correct - it is a DRY problem, not a wrong type. The report's `## Enums`
|
|
88
|
+
section additionally lists any untagged dict whose values already match a
|
|
89
|
+
component exactly, since those are one schemaEnum line from being checked.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# Fields, Records and field mapping
|
|
2
|
+
|
|
3
|
+
The authoring rules for a single entity's field declarations. This file is
|
|
4
|
+
self-contained: it is what an `entity-editor` subagent reads before touching a
|
|
5
|
+
file, so keep it free of sync-procedure context.
|
|
6
|
+
|
|
7
|
+
For enum-typed fields, read `enums.md` (beside this file) as well.
|
|
8
|
+
|
|
9
|
+
## The Fields / Record convention
|
|
10
|
+
|
|
11
|
+
`EntityNameFields` describes the **raw API response shape**. `EntityNameRecord`
|
|
12
|
+
describes the shape after `dataToRecord`, substituting anything the field
|
|
13
|
+
converts on the way in:
|
|
14
|
+
|
|
15
|
+
- a nested `EntityField` becomes the child's `Record` type, or
|
|
16
|
+
`List<ChildRecord>` when `many`
|
|
17
|
+
- a `DateField` / `DateTimeField` becomes a `Moment` (`dataToValue` returns one)
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// the wire shape
|
|
21
|
+
export type ExampleEntityFields = {
|
|
22
|
+
uuid: string;
|
|
23
|
+
start_date: string;
|
|
24
|
+
nested: NestedEntityFields[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// the shape after dataToRecord
|
|
28
|
+
export type ExampleEntityRecord = EntityRecord<
|
|
29
|
+
Omit<ExampleEntityFields, 'start_date' | 'nested'> & {
|
|
30
|
+
start_date: Moment;
|
|
31
|
+
nested: List<NestedEntityRecord>;
|
|
32
|
+
}
|
|
33
|
+
>;
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
For a working reference in the project, find an entity with an endpoint and a
|
|
37
|
+
nested list (`npm run entity:inspect -- entities`) and follow its `Omit`
|
|
38
|
+
substitution.
|
|
39
|
+
|
|
40
|
+
Both types must be **exported**, or the sync cannot assert against them.
|
|
41
|
+
|
|
42
|
+
## Field mapping
|
|
43
|
+
|
|
44
|
+
The machine-readable copy of this table is `src/lib/field-map.mjs` in
|
|
45
|
+
`@burnsred/entity-sync`; the two are kept in step there.
|
|
46
|
+
|
|
47
|
+
| schema signal | field |
|
|
48
|
+
| ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
|
|
49
|
+
| `Format: uuid` | `IdField()`; `CharField({ many: true })` for a list, since `IdField` rejects `many` |
|
|
50
|
+
| `Format: date` / `date-time` | `DateField()` / `DateTimeField()` - and a `Moment` in the Record type |
|
|
51
|
+
| `Format: decimal` | `CharField()` - DRF serialises decimals as strings |
|
|
52
|
+
| `$ref` to a `*Enum` component | `CharField()` plus a choices dict, referenced by its type alias in the Fields type - see `enums.md`. Never `EnumField` |
|
|
53
|
+
| `$ref` to an object component | `EntityField({ entity: X })`, `{ many: true }` if an array |
|
|
54
|
+
| `number` | `IntegerField()`, or `NumberField()` for a float/double format |
|
|
55
|
+
| `boolean` | `BooleanField()` |
|
|
56
|
+
| `string` | `CharField()`, or `TextField()` when the UI wants a textarea - the schema types both as `string`, so either passes |
|
|
57
|
+
| `additionalProperties` | `AnyField()` typed `Record<string, V>` - a DRF `DictField`; the library has no keyed-collection field |
|
|
58
|
+
| no type information | `AnyField()` - only for genuine untyped JSON |
|
|
59
|
+
| `readonly` | `readOnly: true` |
|
|
60
|
+
|
|
61
|
+
A DRF `PrimaryKeyRelatedField` or `SlugRelatedField` sends just the FK value, so
|
|
62
|
+
it is an `IdField` (or `IntegerField` for a Django integer PK) - not an
|
|
63
|
+
`EntityField`. `EntityField` is only for nested or embedded objects. The schema
|
|
64
|
+
tells you which: `Format: uuid` means the FK value, a `$ref` means the object.
|
|
65
|
+
|
|
66
|
+
`blank` is not derived from the schema. In the entity library it only gates the
|
|
67
|
+
`isRequired` validator, which is a form concern; nullability is expressed in the
|
|
68
|
+
type instead.
|
|
69
|
+
|
|
70
|
+
## Gotchas
|
|
71
|
+
|
|
72
|
+
### Field.readOnly
|
|
73
|
+
|
|
74
|
+
The configuration `Field.readOnly` isn't currently implemented, but it should be.
|
|
75
|
+
Adding this config is tolerated by `FieldOptions`. If the schema indicates that a
|
|
76
|
+
field is `readOnly`, add it as `Field({ readOnly: true })` as a statement of
|
|
77
|
+
intent.
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
export interface FieldOptions {
|
|
81
|
+
// ...
|
|
82
|
+
/** Allow extra properties from subclasses. */
|
|
83
|
+
[key: string]: unknown;
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Because it is unimplemented, `entity:plan` reports a missing `readOnly` as
|
|
88
|
+
advisory rather than as drift. It also never reports the reverse: an entity
|
|
89
|
+
declaring `readOnly` on a field the schema says is writable is choosing to be
|
|
90
|
+
stricter than the server, which is a legitimate decision.
|
|
91
|
+
|
|
92
|
+
In the Fields type, mark the same fields with the per-property `readonly`
|
|
93
|
+
modifier (`readonly uuid: string;`), never `Readonly<{ ... }>` or
|
|
94
|
+
`Readonly<T>`: the sync parser cannot see members inside the wrapper, and
|
|
95
|
+
`Readonly<T[]>` fails the `Exact` assertion.
|
|
96
|
+
|
|
97
|
+
### IdField does not support the config: `many`
|
|
98
|
+
|
|
99
|
+
Use `CharField({ many: true })` instead.
|
|
100
|
+
|
|
101
|
+
> Uncaught Error: IdField.constructor: "many" option is not supported.
|