@metaobjectsdev/codegen-ts 0.24.5 → 1.0.0-rc.1
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/README.md +37 -12
- package/dist/api-surface.d.ts +29 -0
- package/dist/api-surface.d.ts.map +1 -1
- package/dist/api-surface.js +47 -0
- package/dist/api-surface.js.map +1 -1
- package/dist/column-mapper.d.ts +12 -0
- package/dist/column-mapper.d.ts.map +1 -1
- package/dist/column-mapper.js +1 -0
- package/dist/column-mapper.js.map +1 -1
- package/dist/constants.d.ts +24 -11
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +54 -19
- package/dist/constants.js.map +1 -1
- package/dist/db-emitting.d.ts +6 -0
- package/dist/db-emitting.d.ts.map +1 -0
- package/dist/db-emitting.js +26 -0
- package/dist/db-emitting.js.map +1 -0
- package/dist/generator-registry.d.ts.map +1 -1
- package/dist/generator-registry.js +14 -1
- package/dist/generator-registry.js.map +1 -1
- package/dist/generator.d.ts +5 -0
- package/dist/generator.d.ts.map +1 -1
- package/dist/generator.js.map +1 -1
- package/dist/generators/agent-docs-file.d.ts +19 -0
- package/dist/generators/agent-docs-file.d.ts.map +1 -0
- package/dist/generators/agent-docs-file.js +257 -0
- package/dist/generators/agent-docs-file.js.map +1 -0
- package/dist/generators/agent-requirements-page.d.ts +22 -0
- package/dist/generators/agent-requirements-page.d.ts.map +1 -0
- package/dist/generators/agent-requirements-page.js +175 -0
- package/dist/generators/agent-requirements-page.js.map +1 -0
- package/dist/generators/agent-schema-input.d.ts +69 -0
- package/dist/generators/agent-schema-input.d.ts.map +1 -0
- package/dist/generators/agent-schema-input.js +23 -0
- package/dist/generators/agent-schema-input.js.map +1 -0
- package/dist/generators/agent-schema-page.d.ts +21 -0
- package/dist/generators/agent-schema-page.d.ts.map +1 -0
- package/dist/generators/agent-schema-page.js +241 -0
- package/dist/generators/agent-schema-page.js.map +1 -0
- package/dist/generators/agent-ui-page.d.ts +22 -0
- package/dist/generators/agent-ui-page.d.ts.map +1 -0
- package/dist/generators/agent-ui-page.js +253 -0
- package/dist/generators/agent-ui-page.js.map +1 -0
- package/dist/generators/api-docs-file.d.ts.map +1 -1
- package/dist/generators/api-docs-file.js +13 -6
- package/dist/generators/api-docs-file.js.map +1 -1
- package/dist/generators/api-model.d.ts +6 -0
- package/dist/generators/api-model.d.ts.map +1 -1
- package/dist/generators/api-model.js +54 -35
- package/dist/generators/api-model.js.map +1 -1
- package/dist/generators/callable-file.js +1 -1
- package/dist/generators/callable-file.js.map +1 -1
- package/dist/generators/docs-data-builder.d.ts.map +1 -1
- package/dist/generators/docs-data-builder.js +47 -2
- package/dist/generators/docs-data-builder.js.map +1 -1
- package/dist/generators/docs-data.d.ts +6 -2
- package/dist/generators/docs-data.d.ts.map +1 -1
- package/dist/generators/index.d.ts +3 -8
- package/dist/generators/index.d.ts.map +1 -1
- package/dist/generators/index.js +19 -14
- package/dist/generators/index.js.map +1 -1
- package/dist/generators/names-file.d.ts +17 -0
- package/dist/generators/names-file.d.ts.map +1 -0
- package/dist/generators/names-file.js +95 -0
- package/dist/generators/names-file.js.map +1 -0
- package/dist/generators/requirements-markdown.d.ts +18 -1
- package/dist/generators/requirements-markdown.d.ts.map +1 -1
- package/dist/generators/requirements-markdown.js +11 -9
- package/dist/generators/requirements-markdown.js.map +1 -1
- package/dist/generators/routes-file-hono.d.ts +17 -2
- package/dist/generators/routes-file-hono.d.ts.map +1 -1
- package/dist/generators/routes-file-hono.js +9 -11
- package/dist/generators/routes-file-hono.js.map +1 -1
- package/dist/generators/routes-file.d.ts +17 -2
- package/dist/generators/routes-file.d.ts.map +1 -1
- package/dist/generators/routes-file.js +10 -8
- package/dist/generators/routes-file.js.map +1 -1
- package/dist/generators/template-generator.d.ts.map +1 -1
- package/dist/generators/template-generator.js +20 -6
- package/dist/generators/template-generator.js.map +1 -1
- package/dist/index.d.ts +20 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +43 -4
- package/dist/index.js.map +1 -1
- package/dist/metaobjects-config.d.ts +38 -1
- package/dist/metaobjects-config.d.ts.map +1 -1
- package/dist/metaobjects-config.js +9 -1
- package/dist/metaobjects-config.js.map +1 -1
- package/dist/names.d.ts +275 -0
- package/dist/names.d.ts.map +1 -0
- package/dist/names.js +433 -0
- package/dist/names.js.map +1 -0
- package/dist/overwrite-policy.d.ts.map +1 -1
- package/dist/overwrite-policy.js +15 -3
- package/dist/overwrite-policy.js.map +1 -1
- package/dist/projection/extract-view-spec.d.ts +25 -1
- package/dist/projection/extract-view-spec.d.ts.map +1 -1
- package/dist/projection/extract-view-spec.js +79 -6
- package/dist/projection/extract-view-spec.js.map +1 -1
- package/dist/reference-templates.d.ts +1 -1
- package/dist/reference-templates.d.ts.map +1 -1
- package/dist/reference-templates.js +1 -1
- package/dist/reference-templates.js.map +1 -1
- package/dist/render-context.d.ts +12 -1
- package/dist/render-context.d.ts.map +1 -1
- package/dist/render-context.js +1 -0
- package/dist/render-context.js.map +1 -1
- package/dist/retired-codegen-attrs.d.ts +32 -0
- package/dist/retired-codegen-attrs.d.ts.map +1 -0
- package/dist/retired-codegen-attrs.js +47 -0
- package/dist/retired-codegen-attrs.js.map +1 -0
- package/dist/routes-expose.d.ts +63 -0
- package/dist/routes-expose.d.ts.map +1 -0
- package/dist/routes-expose.js +57 -0
- package/dist/routes-expose.js.map +1 -0
- package/dist/runner.d.ts +18 -0
- package/dist/runner.d.ts.map +1 -1
- package/dist/runner.js +109 -7
- package/dist/runner.js.map +1 -1
- package/dist/templates/callable-file.d.ts +7 -2
- package/dist/templates/callable-file.d.ts.map +1 -1
- package/dist/templates/callable-file.js +54 -9
- package/dist/templates/callable-file.js.map +1 -1
- package/dist/templates/drizzle-schema.d.ts.map +1 -1
- package/dist/templates/drizzle-schema.js +236 -65
- package/dist/templates/drizzle-schema.js.map +1 -1
- package/dist/templates/entity-constants.d.ts +15 -6
- package/dist/templates/entity-constants.d.ts.map +1 -1
- package/dist/templates/entity-constants.js +85 -161
- package/dist/templates/entity-constants.js.map +1 -1
- package/dist/templates/entity-file.d.ts.map +1 -1
- package/dist/templates/entity-file.js +38 -4
- package/dist/templates/entity-file.js.map +1 -1
- package/dist/templates/entity-meta-file.d.ts +15 -1
- package/dist/templates/entity-meta-file.d.ts.map +1 -1
- package/dist/templates/entity-meta-file.js +12 -2
- package/dist/templates/entity-meta-file.js.map +1 -1
- package/dist/templates/entity-ui-descriptor.d.ts +112 -0
- package/dist/templates/entity-ui-descriptor.d.ts.map +1 -0
- package/dist/templates/entity-ui-descriptor.js +238 -0
- package/dist/templates/entity-ui-descriptor.js.map +1 -0
- package/dist/templates/field-meta.d.ts +46 -9
- package/dist/templates/field-meta.d.ts.map +1 -1
- package/dist/templates/field-meta.js +95 -21
- package/dist/templates/field-meta.js.map +1 -1
- package/dist/templates/names-decl.d.ts +38 -0
- package/dist/templates/names-decl.d.ts.map +1 -0
- package/dist/templates/names-decl.js +104 -0
- package/dist/templates/names-decl.js.map +1 -0
- package/dist/templates/projection-decl.d.ts +21 -0
- package/dist/templates/projection-decl.d.ts.map +1 -1
- package/dist/templates/projection-decl.js +53 -29
- package/dist/templates/projection-decl.js.map +1 -1
- package/dist/templates/queries-file.d.ts.map +1 -1
- package/dist/templates/queries-file.js +17 -2
- package/dist/templates/queries-file.js.map +1 -1
- package/dist/templates/requirement-test.d.ts.map +1 -1
- package/dist/templates/requirement-test.js +13 -2
- package/dist/templates/requirement-test.js.map +1 -1
- package/dist/templates/routes-file-hono.d.ts +2 -1
- package/dist/templates/routes-file-hono.d.ts.map +1 -1
- package/dist/templates/routes-file-hono.js +11 -2
- package/dist/templates/routes-file-hono.js.map +1 -1
- package/dist/templates/routes-file.d.ts +2 -1
- package/dist/templates/routes-file.d.ts.map +1 -1
- package/dist/templates/routes-file.js +54 -21
- package/dist/templates/routes-file.js.map +1 -1
- package/dist/templates/value-object-file.d.ts.map +1 -1
- package/dist/templates/value-object-file.js +15 -1
- package/dist/templates/value-object-file.js.map +1 -1
- package/dist/templates/view-decl.d.ts +37 -1
- package/dist/templates/view-decl.d.ts.map +1 -1
- package/dist/templates/view-decl.js +27 -3
- package/dist/templates/view-decl.js.map +1 -1
- package/dist/templates/zod-validators.d.ts +21 -0
- package/dist/templates/zod-validators.d.ts.map +1 -1
- package/dist/templates/zod-validators.js +40 -5
- package/dist/templates/zod-validators.js.map +1 -1
- package/dist/view-context.d.ts +24 -0
- package/dist/view-context.d.ts.map +1 -0
- package/dist/view-context.js +81 -0
- package/dist/view-context.js.map +1 -0
- package/package.json +6 -6
- package/src/api-surface.ts +53 -0
- package/src/column-mapper.ts +13 -0
- package/src/constants.ts +91 -16
- package/src/db-emitting.ts +31 -0
- package/src/generator-registry.ts +14 -4
- package/src/generator.ts +5 -0
- package/src/generators/agent-docs-file.ts +297 -0
- package/src/generators/agent-requirements-page.ts +199 -0
- package/src/generators/agent-schema-input.ts +88 -0
- package/src/generators/agent-schema-page.ts +297 -0
- package/src/generators/agent-ui-page.ts +290 -0
- package/src/generators/api-docs-file.ts +13 -6
- package/src/generators/api-model.ts +63 -34
- package/src/generators/callable-file.ts +1 -1
- package/src/generators/docs-data-builder.ts +46 -2
- package/src/generators/docs-data.ts +6 -2
- package/src/generators/index.ts +25 -14
- package/src/generators/names-file.ts +98 -0
- package/src/generators/requirements-markdown.ts +36 -2
- package/src/generators/routes-file-hono.ts +19 -10
- package/src/generators/routes-file.ts +20 -7
- package/src/generators/template-generator.ts +20 -6
- package/src/index.ts +64 -4
- package/src/metaobjects-config.ts +48 -2
- package/src/names.ts +629 -0
- package/src/overwrite-policy.ts +15 -3
- package/src/projection/extract-view-spec.ts +85 -6
- package/src/reference/entity.ts +21 -2
- package/src/reference/names.ts +125 -0
- package/src/reference/queries.ts +19 -3
- package/src/reference/routes-hono.ts +19 -9
- package/src/reference/routes.ts +25 -9
- package/src/reference-templates.ts +1 -1
- package/src/render-context.ts +13 -1
- package/src/retired-codegen-attrs.ts +52 -0
- package/src/routes-expose.ts +86 -0
- package/src/runner.ts +122 -11
- package/src/templates/callable-file.ts +60 -8
- package/src/templates/drizzle-schema.ts +248 -60
- package/src/templates/entity-constants.ts +95 -184
- package/src/templates/entity-file.ts +46 -4
- package/src/templates/entity-meta-file.ts +16 -3
- package/src/templates/entity-ui-descriptor.ts +305 -0
- package/src/templates/field-meta.ts +100 -19
- package/src/templates/names-decl.ts +165 -0
- package/src/templates/projection-decl.ts +72 -31
- package/src/templates/queries-file.ts +17 -2
- package/src/templates/requirement-test.ts +13 -2
- package/src/templates/routes-file-hono.ts +14 -2
- package/src/templates/routes-file.ts +69 -24
- package/src/templates/value-object-file.ts +15 -1
- package/src/templates/view-decl.ts +53 -3
- package/src/templates/zod-validators.ts +41 -5
- package/src/view-context.ts +88 -0
package/src/names.ts
ADDED
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* §A2/§A3 — the ONE place a data name is resolved for a generator run.
|
|
3
|
+
*
|
|
4
|
+
* Both the names artifact (namesFile) and the entity generator that consumes it call this,
|
|
5
|
+
* so the constant and the binding it describes cannot be produced by different resolvers or
|
|
6
|
+
* different arguments. That is the whole rule; a name computed twice is a name that can
|
|
7
|
+
* disagree with itself.
|
|
8
|
+
*/
|
|
9
|
+
import {
|
|
10
|
+
primaryRdbSource,
|
|
11
|
+
resolveColumnName,
|
|
12
|
+
resolveIndexName,
|
|
13
|
+
resolveTableSchema,
|
|
14
|
+
isMetaObject,
|
|
15
|
+
isMetaSource,
|
|
16
|
+
PHYSICAL_NAME_ATTR_BY_KIND,
|
|
17
|
+
SOURCE_ATTR_SCHEMA,
|
|
18
|
+
SOURCE_ROLE_PRIMARY,
|
|
19
|
+
TYPE_INDEX,
|
|
20
|
+
TYPE_OBJECT,
|
|
21
|
+
TYPE_SOURCE,
|
|
22
|
+
type ColumnNamingStrategy,
|
|
23
|
+
type MetaObject,
|
|
24
|
+
type MetaSource,
|
|
25
|
+
} from "@metaobjectsdev/metadata";
|
|
26
|
+
import { code, imp, type Code } from "ts-poet";
|
|
27
|
+
import { crossEntitySpecifier } from "./import-path.js";
|
|
28
|
+
import type { RenderContext } from "./render-context.js";
|
|
29
|
+
|
|
30
|
+
export interface FieldNames { readonly name: string; readonly column: string; }
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* One `source.rdb` child, under the ROLE it plays.
|
|
34
|
+
*
|
|
35
|
+
* The physical name is carried under a key NAMED FOR THE KIND — `table`, `view`,
|
|
36
|
+
* `materializedView`, `proc`, `function` — and that key is not invented here: it is
|
|
37
|
+
* `PHYSICAL_NAME_ATTR_BY_KIND`, the metamodel's own FR-016/ADR-0018 alias map, the same
|
|
38
|
+
* one the canonical serializer rewrites through. So the artifact spells a physical name
|
|
39
|
+
* the way the metadata that declared it does.
|
|
40
|
+
*
|
|
41
|
+
* This is the half of the shape that earns the restructure. A single flat `name` held a
|
|
42
|
+
* table, a view and a stored procedure in the same run, told apart only by a sibling
|
|
43
|
+
* `kind`; under `as const`, `LedgerNames.sources.replica.table` is now a compile error,
|
|
44
|
+
* because that source is a view. The read site answers the question instead of the reader
|
|
45
|
+
* having to.
|
|
46
|
+
*/
|
|
47
|
+
export interface SourceNames {
|
|
48
|
+
readonly type: string;
|
|
49
|
+
readonly subType: string;
|
|
50
|
+
/** The `@kind` value, defaulted per ADR-0007 Rule 3 — the discriminator for the alias below. */
|
|
51
|
+
readonly kind: string;
|
|
52
|
+
readonly schema?: string | undefined;
|
|
53
|
+
/** The physical name, under the alias for `kind`. Exactly one of these is present. */
|
|
54
|
+
readonly table?: string | undefined;
|
|
55
|
+
readonly view?: string | undefined;
|
|
56
|
+
readonly materializedView?: string | undefined;
|
|
57
|
+
readonly proc?: string | undefined;
|
|
58
|
+
readonly function?: string | undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* One `identity.*` or `index.*` child.
|
|
63
|
+
*
|
|
64
|
+
* `subType` is load-bearing rather than decorative: it is the ONLY thing distinguishing a
|
|
65
|
+
* unique alternate key from a non-unique lookup index, which is the whole reason ADR-0040
|
|
66
|
+
* put uniqueness in the type rather than in an attribute.
|
|
67
|
+
*
|
|
68
|
+
* `index` — the database name — is present only where a shared resolver produces it:
|
|
69
|
+
* `identity.secondary` and `index.lookup`, via `resolveIndexName`. It is deliberately
|
|
70
|
+
* ABSENT on `identity.primary`, because no such name exists to carry: migrate hardcodes
|
|
71
|
+
* `<table>_pkey` on Postgres, emits an unnamed PK on SQLite, and no port's codegen names a
|
|
72
|
+
* primary key at all. Carrying it would restate a migrate-only, dialect-conditional
|
|
73
|
+
* formula in an artifact whose entire promise is that a name is spelled once — the #293
|
|
74
|
+
* defect, re-created by the mechanism built to prevent it.
|
|
75
|
+
*/
|
|
76
|
+
export interface KeyNames {
|
|
77
|
+
readonly type: string;
|
|
78
|
+
readonly subType: string;
|
|
79
|
+
readonly name: string;
|
|
80
|
+
/** The database index name. Present for `identity.secondary` and `index.lookup` only. */
|
|
81
|
+
readonly index?: string | undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The object whose names artifact this one extends. */
|
|
85
|
+
export interface SuperNames {
|
|
86
|
+
readonly name: string;
|
|
87
|
+
readonly package?: string | undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface ObjectNames {
|
|
91
|
+
/** The metamodel type — always `object`. */
|
|
92
|
+
readonly type: string;
|
|
93
|
+
/** The metamodel subType — `entity` | `projection` | `value`. */
|
|
94
|
+
readonly subType: string;
|
|
95
|
+
/**
|
|
96
|
+
* The object's OWN name — `"Customer"`, not `"TBL_CUST_MASTER"`.
|
|
97
|
+
*
|
|
98
|
+
* It held the physical name until 0.25.0, which is the one change here that a hand-
|
|
99
|
+
* written consumer can adopt WITHOUT a compile error: `pgTable(CustomerNames.name, …)`
|
|
100
|
+
* still compiles and now binds a table called `Customer`. The release note leads with
|
|
101
|
+
* that; no gate here can see it, because the code that breaks is not generated.
|
|
102
|
+
*/
|
|
103
|
+
readonly name: string;
|
|
104
|
+
/**
|
|
105
|
+
* Every `source.rdb` child, keyed by effective `@role` (`primary` | `replica`).
|
|
106
|
+
*
|
|
107
|
+
* Role is the honest axis: the loader requires exactly one primary, and every consumer
|
|
108
|
+
* that binds a second source picks it by role. Keying by role is also what finally gives
|
|
109
|
+
* a WRITE-THROUGH entity's replica view a home — it declares two physical names, the
|
|
110
|
+
* artifact carried one, and both TypeScript and C# emitted the second as a literal.
|
|
111
|
+
*
|
|
112
|
+
* Empty on a FRAGMENT: an abstract base with no source of its own contributes columns
|
|
113
|
+
* and must never acquire a physical name it never declared.
|
|
114
|
+
*/
|
|
115
|
+
readonly sources: Readonly<Record<string, SourceNames>>;
|
|
116
|
+
/** The sources DECLARED HERE — what this artifact emits. See `ownFields`. */
|
|
117
|
+
readonly ownSources: Readonly<Record<string, SourceNames>>;
|
|
118
|
+
/**
|
|
119
|
+
* Every field, INHERITED INCLUDED. This is what a consumer looks a column up in, so a
|
|
120
|
+
* lookup for an inherited field must hit — miss and the caller falls back to a literal
|
|
121
|
+
* (see `columnExpr`), which is the whole defect this artifact exists to remove.
|
|
122
|
+
*/
|
|
123
|
+
readonly fields: Readonly<Record<string, FieldNames>>;
|
|
124
|
+
/**
|
|
125
|
+
* The fields DECLARED HERE — what the artifact EMITS. Inherited ones are declared by
|
|
126
|
+
* the super's artifact and reached through it, so a subtype states each physical name
|
|
127
|
+
* once instead of restating its parent's.
|
|
128
|
+
*
|
|
129
|
+
* ADR-0039's ONE sanctioned own-accessor use, in the exact form the ADR names: codegen
|
|
130
|
+
* emitting a generated subclass, iterating own members so inherited ones are not
|
|
131
|
+
* re-emitted.
|
|
132
|
+
*/
|
|
133
|
+
readonly ownFields: Readonly<Record<string, FieldNames>>;
|
|
134
|
+
/** Every `identity.*` child, inherited included; keyed by metamodel name. */
|
|
135
|
+
readonly identities: Readonly<Record<string, KeyNames>>;
|
|
136
|
+
/** The identities DECLARED HERE. See `ownFields`. */
|
|
137
|
+
readonly ownIdentities: Readonly<Record<string, KeyNames>>;
|
|
138
|
+
/** Every `index.*` child, inherited included; keyed by metamodel name. */
|
|
139
|
+
readonly indexes: Readonly<Record<string, KeyNames>>;
|
|
140
|
+
/** The indexes DECLARED HERE. See `ownFields`. */
|
|
141
|
+
readonly ownIndexes: Readonly<Record<string, KeyNames>>;
|
|
142
|
+
/** The nearest ancestor carrying an artifact of its own, when there is one. */
|
|
143
|
+
readonly superNames?: SuperNames | undefined;
|
|
144
|
+
/**
|
|
145
|
+
* True when the primary source is the SUPER's rather than declared here — a TPH
|
|
146
|
+
* subtype, which shares its base's single table. Structural (the two resolve to the
|
|
147
|
+
* SAME source node), never an equality test on the resolved strings: the physical
|
|
148
|
+
* name, kind and schema then all come from the super's artifact rather than being
|
|
149
|
+
* restated.
|
|
150
|
+
*/
|
|
151
|
+
readonly inheritsSource: boolean;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* One source node's names, keyed by the metamodel's own kind→alias map.
|
|
156
|
+
*
|
|
157
|
+
* `readOnly` is deliberately NOT carried, and its removal is the shape's own rule applied
|
|
158
|
+
* to itself: it is not metadata at all but a derivation over `@kind` (`source.isReadOnly()`),
|
|
159
|
+
* and a sweep of all five ports found ZERO consumers, generated or hand-written. An
|
|
160
|
+
* artifact that mirrors the metadata tree carries what was declared; a reader who wants
|
|
161
|
+
* read-only-ness asks `kind`, which is the thing the author actually wrote.
|
|
162
|
+
*/
|
|
163
|
+
function sourceNamesOf(source: MetaSource): SourceNames {
|
|
164
|
+
const kind = source.effectiveKind;
|
|
165
|
+
// The metamodel's map, never a local switch: a sixth @kind must not need an edit here to
|
|
166
|
+
// be spelled correctly, and a local copy is a second answer to a question that has one.
|
|
167
|
+
const alias = PHYSICAL_NAME_ATTR_BY_KIND.get(kind);
|
|
168
|
+
const schema = source.attr(SOURCE_ATTR_SCHEMA);
|
|
169
|
+
return {
|
|
170
|
+
type: TYPE_SOURCE,
|
|
171
|
+
subType: source.subType,
|
|
172
|
+
kind,
|
|
173
|
+
...(typeof schema === "string" && schema !== "" ? { schema } : {}),
|
|
174
|
+
...(alias === undefined ? {} : { [alias]: source.physicalName }),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Every `source.rdb` child of `obj`, keyed by effective role. */
|
|
179
|
+
function sourcesOf(sources: readonly MetaSource[], where: string): Record<string, SourceNames> {
|
|
180
|
+
const out: Record<string, SourceNames> = {};
|
|
181
|
+
for (const src of sources) {
|
|
182
|
+
const role = src.role;
|
|
183
|
+
const resolved = sourceNamesOf(src);
|
|
184
|
+
const existing = out[role];
|
|
185
|
+
if (existing === undefined) {
|
|
186
|
+
out[role] = resolved;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
// The refusal is about DISAGREEMENT, not about the count. An abstract base and the
|
|
190
|
+
// child that extends it may each declare a `@role: primary` source naming the same
|
|
191
|
+
// relation; that is legal and stays legal, because the records then compare equal.
|
|
192
|
+
//
|
|
193
|
+
// Two sources in one role that resolve DIFFERENTLY is the real problem, and silently
|
|
194
|
+
// keeping one is the `dropped` failure mode this artifact makes impossible: the second
|
|
195
|
+
// name is carried nowhere, read by nobody, and the binding quietly takes the first's.
|
|
196
|
+
//
|
|
197
|
+
// WHAT IS COMPARED. The whole resolved record — kind, schema and the physical name
|
|
198
|
+
// under its alias — for EVERY role. `primaryRdbSource` now compares the same address
|
|
199
|
+
// (`sourceAddressKey`) for `primary`, so the two doors agree by construction rather
|
|
200
|
+
// than by coincidence.
|
|
201
|
+
//
|
|
202
|
+
// This comment used to claim the two were "deliberately the SAME rule" when they were
|
|
203
|
+
// not, and the gap was reachable: two `@role: primary` sources agreeing on `@table`
|
|
204
|
+
// but disagreeing on `@schema` loaded clean, were ACCEPTED by `primaryRdbSource` and
|
|
205
|
+
// refused here, so `meta gen` failed on a model every other door admitted. Worse, the
|
|
206
|
+
// weaker key made the accepted answer port-dependent — `primaries[0]` is the inherited
|
|
207
|
+
// source in TS/C#/Python and the own source on the JVM. That is now closed at the
|
|
208
|
+
// authority; this stays because it also covers the non-primary roles (two disagreeing
|
|
209
|
+
// REPLICAs) and because it is what builds the keyed map.
|
|
210
|
+
if (JSON.stringify(existing) !== JSON.stringify(resolved)) {
|
|
211
|
+
throw new Error(
|
|
212
|
+
`${where} declares more than one source.rdb with @role: "${role}", and they ` +
|
|
213
|
+
`disagree on the object's physical address: ${JSON.stringify(existing)} vs ` +
|
|
214
|
+
`${JSON.stringify(resolved)}. The names artifact keys sources by role, so the ` +
|
|
215
|
+
`second has nowhere to go.`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Every `source.rdb` child of `obj`. `own` restricts to those declared here. */
|
|
223
|
+
function rdbSourcesOf(obj: MetaObject, own: boolean): MetaSource[] {
|
|
224
|
+
// ADR-0039: children() resolves through `extends`; ownChildren() is the sanctioned
|
|
225
|
+
// own-only twin for "what does THIS artifact declare".
|
|
226
|
+
const kids = own ? obj.ownChildren() : obj.children();
|
|
227
|
+
return kids.filter((c): c is MetaSource => isMetaSource(c));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Every `identity.*` / `index.*` child, keyed by metamodel name. */
|
|
231
|
+
function keysOf(
|
|
232
|
+
nodes: readonly { readonly name: string; readonly type: string; readonly subType: string }[],
|
|
233
|
+
): Record<string, KeyNames> {
|
|
234
|
+
const out: Record<string, KeyNames> = {};
|
|
235
|
+
for (const node of nodes) {
|
|
236
|
+
// resolveIndexName owns BOTH the package strip and the empty-name refusal, so the
|
|
237
|
+
// artifact and the DDL cannot disagree about what an index is called — and an
|
|
238
|
+
// `index.lookup` with an empty name (which the loader accepts, unlike an identity)
|
|
239
|
+
// fails here instead of reaching an emitter.
|
|
240
|
+
const hasIndexName = INDEX_NAMED_SUBTYPES.has(`${node.type}.${node.subType}`);
|
|
241
|
+
out[node.name] = {
|
|
242
|
+
type: node.type,
|
|
243
|
+
subType: node.subType,
|
|
244
|
+
name: node.name,
|
|
245
|
+
...(hasIndexName ? { index: resolveIndexName(node) } : {}),
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
return out;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The nodes whose database index name the artifact carries.
|
|
253
|
+
*
|
|
254
|
+
* A closed set rather than "anything with a name", because the rule is narrow and worth
|
|
255
|
+
* stating: the artifact carries a physical name only where ONE resolver, shared by codegen
|
|
256
|
+
* and migrate, produces it. `identity.primary` and `identity.reference` have names that are
|
|
257
|
+
* addressing handles, not database names — see {@link KeyNames}.
|
|
258
|
+
*/
|
|
259
|
+
const INDEX_NAMED_SUBTYPES: ReadonlySet<string> = new Set([
|
|
260
|
+
"identity.secondary",
|
|
261
|
+
"index.lookup",
|
|
262
|
+
]);
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Whether `obj` DECLARES anything a names artifact carries.
|
|
266
|
+
*
|
|
267
|
+
* One predicate, because the artifact has four collections and the two places that ask
|
|
268
|
+
* this question must agree about all four. They used to ask about fields alone, and the
|
|
269
|
+
* cost was precise: an intermediate abstract declaring only an `identity.secondary` — a
|
|
270
|
+
* key hoisted onto a chain, which is the whole reason such a node exists — answered "no".
|
|
271
|
+
* {@link namesArtifactSuperOf} then walked past it and {@link resolveSuperFragmentNames}
|
|
272
|
+
* emitted nothing for it, so its key appeared in NEITHER the child's own set nor the
|
|
273
|
+
* grandparent's spread. `drizzle-schema.ts` still emitted `uniqueIndex(<E>Names.
|
|
274
|
+
* identities.<key>.index)` against it, and the generated code did not compile.
|
|
275
|
+
*
|
|
276
|
+
* ADR-0039's sanctioned own-accessor use: the question is what this node declares, not
|
|
277
|
+
* what it can see. An inherited key belongs to the ancestor that declared it, and is
|
|
278
|
+
* reached through that ancestor's artifact.
|
|
279
|
+
*/
|
|
280
|
+
function declaresNamesContent(obj: MetaObject): boolean {
|
|
281
|
+
return obj.ownFields().length > 0 ||
|
|
282
|
+
obj.ownIdentities().length > 0 ||
|
|
283
|
+
obj.ownLookupIndexes().length > 0;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* The nearest ancestor of `obj` that carries a names artifact of its own, or undefined.
|
|
288
|
+
*
|
|
289
|
+
* Walks past an ancestor with nothing to contribute — an abstract marker with no fields,
|
|
290
|
+
* no keys and no source emits no artifact, so there is nothing to extend and the search
|
|
291
|
+
* continues upward rather than stopping at a name that does not exist.
|
|
292
|
+
*/
|
|
293
|
+
export function namesArtifactSuperOf(obj: MetaObject): MetaObject | undefined {
|
|
294
|
+
let cur = obj.superData;
|
|
295
|
+
while (cur !== undefined) {
|
|
296
|
+
// The exported guard, never `as MetaObject` and never a duck-type check on a method
|
|
297
|
+
// name: `superData` is only a MetaData, and CLAUDE.md makes this the required
|
|
298
|
+
// mechanism — two physical copies of the package in one process give a class object
|
|
299
|
+
// and an instance different identities, so `instanceof` returns false for a real node.
|
|
300
|
+
if (isMetaObject(cur) && (declaresNamesContent(cur) || primaryRdbSource(cur) !== undefined)) {
|
|
301
|
+
return cur;
|
|
302
|
+
}
|
|
303
|
+
cur = cur.superData;
|
|
304
|
+
}
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export function resolveObjectNames(
|
|
309
|
+
obj: MetaObject,
|
|
310
|
+
strategy?: ColumnNamingStrategy,
|
|
311
|
+
): ObjectNames | undefined {
|
|
312
|
+
// #248: an object participates in the database iff it declares (or inherits) a primary
|
|
313
|
+
// source. Never gate on the object subtype. ADR-0039: resolving children().
|
|
314
|
+
//
|
|
315
|
+
// primaryRdbSource, not a scan of our own: it is THE primary-source lookup for the whole
|
|
316
|
+
// toolchain, and it carries the divergence refusal that used to live in this function
|
|
317
|
+
// (see below). A second scan here would be a lookup written twice — the same defect one
|
|
318
|
+
// level down from the one this file exists to prevent (a NAME resolved twice).
|
|
319
|
+
const source = primaryRdbSource(obj);
|
|
320
|
+
if (source === undefined) return undefined;
|
|
321
|
+
const ownFieldList = obj.ownFields();
|
|
322
|
+
|
|
323
|
+
const fields: Record<string, FieldNames> = {};
|
|
324
|
+
// ADR-0039: fields() is the RESOLVING accessor — inherited fields must appear, and an
|
|
325
|
+
// inherited @column must resolve, or the constant disagrees with the DDL.
|
|
326
|
+
for (const f of obj.fields()) {
|
|
327
|
+
fields[f.name] = { name: f.name, column: resolveColumnName(f, strategy) };
|
|
328
|
+
}
|
|
329
|
+
// ADR-0039's sanctioned own-accessor use: what this artifact DECLARES. See ObjectNames.
|
|
330
|
+
const ownFields: Record<string, FieldNames> = {};
|
|
331
|
+
for (const f of ownFieldList) {
|
|
332
|
+
ownFields[f.name] = { name: f.name, column: resolveColumnName(f, strategy) };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const superObj = namesArtifactSuperOf(obj);
|
|
336
|
+
const superNames: SuperNames | undefined = superObj === undefined
|
|
337
|
+
? undefined
|
|
338
|
+
: { name: superObj.name, package: superObj.package };
|
|
339
|
+
// Identity of the resolved source NODE, not equality of the resolved strings: a
|
|
340
|
+
// divergence guard is exactly what this codebase forbids here, and the question being
|
|
341
|
+
// asked is structural — did this object declare a source, or is it using its parent's?
|
|
342
|
+
const inheritsSource = source !== undefined && superObj !== undefined &&
|
|
343
|
+
primaryRdbSource(superObj) === source;
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
// Read the physical name off the primary SOURCE ALREADY IN HAND rather than calling
|
|
347
|
+
// resolveTableName() for it. Both now delegate to primaryRdbSource, so this is no longer
|
|
348
|
+
// about avoiding a second, differently-written lookup — it is that resolveTableName adds
|
|
349
|
+
// a no-source FALLBACK (pluralize(snake(name))) this function must not take: an object
|
|
350
|
+
// with no primary source returns undefined above, and must never acquire a table name it
|
|
351
|
+
// never declared. Mirrors the C# port (CSharpNaming.ResolveObjectNames).
|
|
352
|
+
const name = source.physicalName;
|
|
353
|
+
|
|
354
|
+
// The divergence refusal — an object whose @role: primary sources resolve to more than
|
|
355
|
+
// one physical name — used to live HERE, and that was the defect. Every consumer
|
|
356
|
+
// downstream references this name unconditionally (no per-site equality guard; see
|
|
357
|
+
// drizzle-schema.ts), but this function runs only when the `names` generator is in the
|
|
358
|
+
// run, so with namesFile() unwired nothing refused at all: `meta migrate` emitted DDL
|
|
359
|
+
// against the PARENT's table and ObjectManager read and wrote it, silently, on every
|
|
360
|
+
// run. A refusal that depends on which consumer asked is not a refusal.
|
|
361
|
+
//
|
|
362
|
+
// It now lives in primaryRdbSource (@metaobjectsdev/metadata's naming.ts), called
|
|
363
|
+
// above, so resolveTableName, resolveTableSchema, MetaObject.dbTable and this function
|
|
364
|
+
// all inherit it from one implementation. See that function's doc for the reachability
|
|
365
|
+
// analysis — the shape loads with ZERO errors — and for why the check must be
|
|
366
|
+
// DIRECTION-BLIND rather than comparing against the first primary WRITABLE source.
|
|
367
|
+
// names.test.ts pins both directions through this entry point; naming.test.ts (in
|
|
368
|
+
// @metaobjectsdev/metadata) pins the other three doors.
|
|
369
|
+
|
|
370
|
+
return {
|
|
371
|
+
type: TYPE_OBJECT,
|
|
372
|
+
subType: obj.subType,
|
|
373
|
+
// The object's OWN name. `source.physicalName` (resolved above as `name`) is now
|
|
374
|
+
// reached through `sources.<role>.<alias>`, which is the point of the restructure:
|
|
375
|
+
// one key stopped meaning a table, a view and a procedure depending on the object.
|
|
376
|
+
name: obj.name,
|
|
377
|
+
sources: sourcesOf(rdbSourcesOf(obj, false), obj.name),
|
|
378
|
+
ownSources: sourcesOf(rdbSourcesOf(obj, true), obj.name),
|
|
379
|
+
fields,
|
|
380
|
+
ownFields,
|
|
381
|
+
identities: keysOf(obj.identities()),
|
|
382
|
+
ownIdentities: keysOf(obj.ownIdentities()),
|
|
383
|
+
indexes: keysOf(obj.lookupIndexes()),
|
|
384
|
+
ownIndexes: keysOf(obj.ownLookupIndexes()),
|
|
385
|
+
superNames,
|
|
386
|
+
inheritsSource,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* The names FRAGMENT for an object that a sourced object extends but which declares no
|
|
392
|
+
* source of its own — the `BaseEntity` pattern: shared fields, no table.
|
|
393
|
+
*
|
|
394
|
+
* Separate from {@link resolveObjectNames} on purpose, and the separation is the #248 rule
|
|
395
|
+
* intact rather than weakened. "Has a primary source" still decides whether an object is a
|
|
396
|
+
* database participant, so an `object.value` carrying fields resolves to nothing here as it
|
|
397
|
+
* always has. A fragment is emitted only for an object REACHED from a participant by
|
|
398
|
+
* walking `extends` upward — which is the only context in which its fields are columns at
|
|
399
|
+
* all. It carries no `kind`/`name`/`readOnly`, because it has no physical name and must
|
|
400
|
+
* never acquire one.
|
|
401
|
+
*
|
|
402
|
+
* Returns undefined when the object declares nothing of its own: an abstract marker has
|
|
403
|
+
* nothing to extend, and emitting an empty artifact for it would put a name in the import
|
|
404
|
+
* graph that says nothing. "Nothing" is {@link declaresNamesContent} — fields OR keys, the
|
|
405
|
+
* same question {@link namesArtifactSuperOf} asks, so the walk and the emit cannot disagree
|
|
406
|
+
* about which ancestors exist.
|
|
407
|
+
*/
|
|
408
|
+
export function resolveSuperFragmentNames(
|
|
409
|
+
obj: MetaObject,
|
|
410
|
+
strategy?: ColumnNamingStrategy,
|
|
411
|
+
): ObjectNames | undefined {
|
|
412
|
+
if (!declaresNamesContent(obj)) return undefined;
|
|
413
|
+
const ownFieldList = obj.ownFields();
|
|
414
|
+
|
|
415
|
+
const fields: Record<string, FieldNames> = {};
|
|
416
|
+
for (const f of obj.fields()) {
|
|
417
|
+
fields[f.name] = { name: f.name, column: resolveColumnName(f, strategy) };
|
|
418
|
+
}
|
|
419
|
+
const ownFields: Record<string, FieldNames> = {};
|
|
420
|
+
for (const f of ownFieldList) {
|
|
421
|
+
ownFields[f.name] = { name: f.name, column: resolveColumnName(f, strategy) };
|
|
422
|
+
}
|
|
423
|
+
const superObj = namesArtifactSuperOf(obj);
|
|
424
|
+
return {
|
|
425
|
+
type: TYPE_OBJECT,
|
|
426
|
+
subType: obj.subType,
|
|
427
|
+
name: obj.name,
|
|
428
|
+
// Empty, and emitted as empty rather than omitted: a child spreads `...Super.sources`
|
|
429
|
+
// unconditionally, so the key has to exist. A fragment declares no source and must
|
|
430
|
+
// never acquire a physical name it never wrote.
|
|
431
|
+
sources: {},
|
|
432
|
+
ownSources: {},
|
|
433
|
+
fields,
|
|
434
|
+
ownFields,
|
|
435
|
+
identities: keysOf(obj.identities()),
|
|
436
|
+
ownIdentities: keysOf(obj.ownIdentities()),
|
|
437
|
+
indexes: keysOf(obj.lookupIndexes()),
|
|
438
|
+
ownIndexes: keysOf(obj.ownLookupIndexes()),
|
|
439
|
+
superNames: superObj === undefined
|
|
440
|
+
? undefined
|
|
441
|
+
: { name: superObj.name, package: superObj.package },
|
|
442
|
+
inheritsSource: false,
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* §B2 — the shared builder every `<Object>Names` reference call site used to hand-roll:
|
|
448
|
+
* check whether the artifact exists in this run (`ctx.includeNames`), resolve the
|
|
449
|
+
* constant (`resolveObjectNames`), and build the ts-poet import symbol pointing at
|
|
450
|
+
* `<Object>.names.ts`. Returns undefined whenever the artifact does not exist for this
|
|
451
|
+
* object in this run — a PRESENCE guard ("is the artifact in this run at all"), never the
|
|
452
|
+
* divergence refusal that `primaryRdbSource` owns: this function never compares a
|
|
453
|
+
* resolved value to a literal, it only asks whether the constant exists.
|
|
454
|
+
*
|
|
455
|
+
* `fromPackage` is the package of the FILE BEING EMITTED — the file that will hold the
|
|
456
|
+
* `import { <Object>Names } from …` line — and defaults to `obj.package`, correct for
|
|
457
|
+
* every site that emits `obj`'s OWN module (the entity generator referencing its own
|
|
458
|
+
* names artifact, same object on both ends). A caller emitting a DIFFERENT object's file
|
|
459
|
+
* and reaching across packages for `obj`'s names artifact — an M:N routes file, which
|
|
460
|
+
* lives in the SOURCE entity's package, importing the junction/target's `<X>Names` — MUST
|
|
461
|
+
* pass the emitting file's own package explicitly. Making this an explicit parameter
|
|
462
|
+
* (rather than assuming same-package the way a plain sibling specifier does) is what
|
|
463
|
+
* surfaces that choice: see routes-file.ts's `resolveJunctionColumn`, the one site that
|
|
464
|
+
* got it wrong by assuming same-package.
|
|
465
|
+
*/
|
|
466
|
+
export function namesRef(
|
|
467
|
+
obj: MetaObject,
|
|
468
|
+
ctx: RenderContext,
|
|
469
|
+
fromPackage: string | undefined = obj.package,
|
|
470
|
+
): { readonly resolved: ObjectNames; readonly symbol: Code } | undefined {
|
|
471
|
+
if (!ctx.includeNames) return undefined;
|
|
472
|
+
const resolved = resolveObjectNames(obj, ctx.columnNamingStrategy);
|
|
473
|
+
if (resolved === undefined) return undefined;
|
|
474
|
+
const symbol = code`${imp(
|
|
475
|
+
`${obj.name}Names@${crossEntitySpecifier(
|
|
476
|
+
ctx.selfTarget.outputLayout,
|
|
477
|
+
fromPackage,
|
|
478
|
+
obj.package,
|
|
479
|
+
`${obj.name}.names`,
|
|
480
|
+
ctx.extStyle,
|
|
481
|
+
)}`,
|
|
482
|
+
)}`;
|
|
483
|
+
return { resolved, symbol };
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Adapts a `namesRef()` result to the `{ name, symbol }` shape `renderEntityConstants` /
|
|
488
|
+
* `renderEntityMetaFile` accept. Kept as a separate, narrower shape from `namesRef`'s own
|
|
489
|
+
* `{ resolved, symbol }` rather than folded together: those two functions' parameter
|
|
490
|
+
* predates this helper, and an adopter who already ejected the ADR-0034 reference
|
|
491
|
+
* template calls them with exactly this `{ name, symbol }` shape — widening the required
|
|
492
|
+
* shape to `resolved` would fail to compile in every such copy.
|
|
493
|
+
*/
|
|
494
|
+
export function namesConstArg(
|
|
495
|
+
names: { readonly resolved: ObjectNames; readonly symbol: Code } | undefined,
|
|
496
|
+
role: string = SOURCE_ROLE_PRIMARY,
|
|
497
|
+
): { readonly name: string; readonly symbol: Code } | undefined {
|
|
498
|
+
// `resolved.name` is optional because a FRAGMENT (an abstract base contributing columns,
|
|
499
|
+
// with no source of its own) genuinely has no physical name. No caller reaches here with
|
|
500
|
+
// one — `namesRef` goes through `resolveObjectNames`, which requires a primary source —
|
|
501
|
+
// but returning undefined rather than asserting keeps the fragment case from acquiring a
|
|
502
|
+
// `$table` it never declared, which is the phantom-table failure #248 exists to prevent.
|
|
503
|
+
const src = names?.resolved.sources[role];
|
|
504
|
+
if (names === undefined || src === undefined) return undefined;
|
|
505
|
+
const alias = PHYSICAL_NAME_ATTR_BY_KIND.get(src.kind);
|
|
506
|
+
const physical = alias === undefined ? undefined : src[alias as keyof SourceNames];
|
|
507
|
+
// A fragment (an abstract base contributing columns, no source of its own) genuinely
|
|
508
|
+
// has no physical name. Returning undefined rather than asserting keeps it from
|
|
509
|
+
// acquiring a `$table` it never declared — the phantom-table failure #248 prevents.
|
|
510
|
+
if (typeof physical !== "string") return undefined;
|
|
511
|
+
return { name: physical, symbol: names.symbol };
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* A member access that stays valid whatever the key is: `.email` for an identifier,
|
|
516
|
+
* `["2fa-idx"]` otherwise.
|
|
517
|
+
*
|
|
518
|
+
* Needed because two of the artifact's four collections are keyed by an AUTHOR-CHOSEN
|
|
519
|
+
* name. Field keys are field names and are always identifiers; an index name is whatever
|
|
520
|
+
* the author wrote (`uq_cust_email` is an identifier, `2fa-idx` is not), so a dot access
|
|
521
|
+
* built from it does not parse. One helper rather than a per-site guess.
|
|
522
|
+
*/
|
|
523
|
+
function member(key: string): string {
|
|
524
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? `.${key}` : `[${JSON.stringify(key)}]`;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* The physical-name expression every §A6 `$table` / view-name site builds: the constant
|
|
529
|
+
* when the artifact is present, the literal otherwise. No equality guard — but the two
|
|
530
|
+
* branches earn that two different ways, and both are worth naming:
|
|
531
|
+
*
|
|
532
|
+
* - A TABLE literal (`drizzle-schema.ts`, `entity-constants.ts`) comes from
|
|
533
|
+
* `dbTable`/`resolveTableName`, which delegate to `primaryRdbSource` — the same lookup
|
|
534
|
+
* `resolveObjectNames` uses — so a disagreeing object has already thrown.
|
|
535
|
+
* - A VIEW literal (`projection-decl.ts`, `view-decl.ts`) comes from `viewName()`
|
|
536
|
+
* (`extract-view-spec.ts`), which reads OWN read-only sources only. That cannot diverge:
|
|
537
|
+
* a concrete projection may not inherit a source (`ERR_PROJECTION_INHERITED_SOURCE`) and
|
|
538
|
+
* the loader allows one own primary, so there is nothing for it to disagree with.
|
|
539
|
+
*
|
|
540
|
+
* Either way a reference here is the single spelling, never a lookalike computed twice.
|
|
541
|
+
*
|
|
542
|
+
* `role` selects WHICH source — the parameter that did not exist while the artifact held
|
|
543
|
+
* one name. A write-through entity declares two physical names; passing `replica` is how
|
|
544
|
+
* the read view stops being a literal in TypeScript and C# alike.
|
|
545
|
+
*/
|
|
546
|
+
export function physicalNameExpr(
|
|
547
|
+
names: { readonly symbol: Code } | undefined,
|
|
548
|
+
literal: string,
|
|
549
|
+
obj: MetaObject,
|
|
550
|
+
role: string = SOURCE_ROLE_PRIMARY,
|
|
551
|
+
): Code {
|
|
552
|
+
const alias = sourceAliasOf(obj, role);
|
|
553
|
+
// No alias means either no source in that role or a @kind carrying no physical-name
|
|
554
|
+
// slot. Falling back to the literal keeps a future @kind from emitting `undefined` into
|
|
555
|
+
// a table binding, which would fail at the database rather than at the compiler.
|
|
556
|
+
if (names === undefined || alias === undefined) return code`${JSON.stringify(literal)}`;
|
|
557
|
+
return code`${names.symbol}.sources${member(role)}.${alias}`;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* The physical-name alias key for `obj`'s source in `role` — `table`, `view`, `proc`, …
|
|
562
|
+
*
|
|
563
|
+
* Derived from the object rather than taken as a parameter, so a call site cannot pass an
|
|
564
|
+
* alias that disagrees with the source it is describing. Reads the metamodel's own
|
|
565
|
+
* `PHYSICAL_NAME_ATTR_BY_KIND`; a local switch here would be a second answer to a
|
|
566
|
+
* question the metamodel already answers, which is the defect class this file exists for.
|
|
567
|
+
*/
|
|
568
|
+
export function sourceAliasOf(obj: MetaObject, role: string = SOURCE_ROLE_PRIMARY): string | undefined {
|
|
569
|
+
const src = rdbSourcesOf(obj, false).find((c) => c.role === role);
|
|
570
|
+
return src === undefined ? undefined : PHYSICAL_NAME_ATTR_BY_KIND.get(src.effectiveKind);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* A source's `@schema` expression, or undefined when the source declares none.
|
|
575
|
+
*
|
|
576
|
+
* Undefined rather than a literal fallback: an absent `@schema` means "the dialect's
|
|
577
|
+
* default", which a caller expresses by omitting the qualifier entirely — emitting `""`
|
|
578
|
+
* or `"public"` would be this artifact inventing a name the author never wrote.
|
|
579
|
+
*/
|
|
580
|
+
export function sourceSchemaExpr(
|
|
581
|
+
names: { readonly resolved: ObjectNames; readonly symbol: Code } | undefined,
|
|
582
|
+
role: string = SOURCE_ROLE_PRIMARY,
|
|
583
|
+
): Code | undefined {
|
|
584
|
+
const src = names?.resolved.sources[role];
|
|
585
|
+
if (names === undefined || src === undefined || src.schema === undefined) return undefined;
|
|
586
|
+
return code`${names.symbol}.sources${member(role)}.schema`;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* An index's database-name expression — the constant when the artifact carries the node,
|
|
591
|
+
* the literal otherwise.
|
|
592
|
+
*
|
|
593
|
+
* The literal arm goes through `resolveIndexName` rather than reading `node.name`, so the
|
|
594
|
+
* names-off path and the names-on path answer with the same function. That is the whole
|
|
595
|
+
* lesson of `fdb4118f1`: two spellings of one name agree until they do not.
|
|
596
|
+
*/
|
|
597
|
+
export function indexNameExpr(
|
|
598
|
+
names: { readonly resolved: ObjectNames; readonly symbol: Code } | undefined,
|
|
599
|
+
node: { readonly name: string; readonly type: string; readonly subType: string },
|
|
600
|
+
): Code {
|
|
601
|
+
const collection = node.type === TYPE_INDEX ? "indexes" : "identities";
|
|
602
|
+
const entry = names?.resolved[collection][node.name];
|
|
603
|
+
return names !== undefined && entry !== undefined && entry.index !== undefined
|
|
604
|
+
? code`${names.symbol}.${collection}${member(node.name)}.index`
|
|
605
|
+
: code`${JSON.stringify(resolveIndexName(node))}`;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* A field's physical-column expression: the constant when the given artifact carries the
|
|
610
|
+
* field, the literal otherwise.
|
|
611
|
+
*
|
|
612
|
+
* The literal arm is a PRESENCE guard — "no artifact in this run", the documented ADR-0034
|
|
613
|
+
* opt-out — and NOT a divergence guard. It used to carry a second sanctioned case: a TPH
|
|
614
|
+
* fold emitting a subtype's own columns, which the BASE's artifact never saw. That was
|
|
615
|
+
* never a presence question; the constant existed the whole time, in the subtype's own
|
|
616
|
+
* artifact. Callers now resolve the declaring entity's ref before falling back
|
|
617
|
+
* (`drizzle-schema.ts`), so a miss no longer has a known-good explanation and the fallback
|
|
618
|
+
* is the last resort it was meant to be.
|
|
619
|
+
*/
|
|
620
|
+
export function columnExpr(
|
|
621
|
+
names: { readonly resolved: ObjectNames; readonly symbol: Code } | undefined,
|
|
622
|
+
fieldName: string,
|
|
623
|
+
literal: string,
|
|
624
|
+
): Code {
|
|
625
|
+
const entry = names?.resolved.fields[fieldName];
|
|
626
|
+
return names !== undefined && entry !== undefined
|
|
627
|
+
? code`${names.symbol}.fields.${fieldName}.column`
|
|
628
|
+
: code`${JSON.stringify(literal)}`;
|
|
629
|
+
}
|
package/src/overwrite-policy.ts
CHANGED
|
@@ -610,10 +610,22 @@ export function decideAndWrite(
|
|
|
610
610
|
return {
|
|
611
611
|
path,
|
|
612
612
|
status: "refused",
|
|
613
|
+
// The hint states WHAT HAPPENED, per file, and points at the recovery rather
|
|
614
|
+
// than restating it. The two remedies it used to name were both WRONG for the
|
|
615
|
+
// generator with the most at stake: `requirementTests()` emits a stub whose
|
|
616
|
+
// entire value is the body you write over it, and whose own header says "do not
|
|
617
|
+
// rename the test — the name is the link", so "move your edits into a
|
|
618
|
+
// non-generated file" asks for something the artifact forbids, and
|
|
619
|
+
// "--baseline=fresh to discard them" discards the only content the file has.
|
|
620
|
+
// The sequence that actually keeps an edit is three commands long and identical
|
|
621
|
+
// for every file in the run, so `runner.ts` prints it once; a doc pointer is
|
|
622
|
+
// what a direct `decideAndWrite` caller (a public export — no runner involved)
|
|
623
|
+
// gets instead, and it is never wrong.
|
|
613
624
|
conflictHint: kase.hasRecord
|
|
614
|
-
? "this file has been edited since it was generated
|
|
615
|
-
"
|
|
616
|
-
"
|
|
625
|
+
? "this file has been edited since it was generated, and there is no " +
|
|
626
|
+
".gen-state snapshot body on this machine to merge against — it was NOT " +
|
|
627
|
+
"overwritten, and your version is intact on disk. Recovery: " +
|
|
628
|
+
"docs/features/own-your-codegen.md."
|
|
617
629
|
: "no record of generating this file, and its content differs from fresh " +
|
|
618
630
|
"output — it was NOT overwritten. Move it aside, or re-run with " +
|
|
619
631
|
"--baseline=fresh to overwrite it and adopt fresh output as the baseline.",
|