@prisma/orm-family-sql 8.0.0-rc.24-dev.34 → 8.0.0-rc.24-dev.35

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.
Files changed (27) hide show
  1. package/dist/{config-types-CGDhWE3o.mjs → config-types-CFByuvww.mjs} +2 -2
  2. package/dist/{config-types-CGDhWE3o.mjs.map → config-types-CFByuvww.mjs.map} +1 -1
  3. package/dist/{contract-builder-BqswyknO.mjs → contract-builder-DIb0oV8O.mjs} +70 -25
  4. package/dist/contract-builder-DIb0oV8O.mjs.map +1 -0
  5. package/dist/contract-prisma7__provider.mjs +4 -4
  6. package/dist/contract-psl.mjs +1 -1
  7. package/dist/contract-psl__attribute-specs.mjs +1 -1
  8. package/dist/contract-psl__provider.mjs +2 -2
  9. package/dist/contract-ts.mjs +3 -3
  10. package/dist/contract-ts__config-types.mjs +1 -1
  11. package/dist/contract-ts__contract-builder.mjs +2 -2
  12. package/dist/{control-DUbuxp4j.mjs → control-BdGTPj7m.mjs} +3 -3
  13. package/dist/{control-DUbuxp4j.mjs.map → control-BdGTPj7m.mjs.map} +1 -1
  14. package/dist/{derived-checks-BoHNzt-a-4Vi-QMdx.mjs → derived-checks-9Ku2scaj-0iT0uU_6.mjs} +265 -113
  15. package/dist/derived-checks-9Ku2scaj-0iT0uU_6.mjs.map +1 -0
  16. package/dist/family.mjs +2 -2
  17. package/dist/family__control.mjs +1 -1
  18. package/dist/family__pack.mjs +2 -2
  19. package/dist/{generator-functions-Bze9nVHL-Bg9PVsti.mjs → generator-functions-Bze9nVHL-BK4YIqLK.mjs} +2 -2
  20. package/dist/{generator-functions-Bze9nVHL-Bg9PVsti.mjs.map → generator-functions-Bze9nVHL-BK4YIqLK.mjs.map} +1 -1
  21. package/dist/{interpreter-DoQwVOyS-BMiOIZcg.mjs → interpreter-DoQwVOyS-jsuDJlaX.mjs} +4 -4
  22. package/dist/{interpreter-DoQwVOyS-BMiOIZcg.mjs.map → interpreter-DoQwVOyS-jsuDJlaX.mjs.map} +1 -1
  23. package/dist/{psl-field-resolution-CpxUePg1-tvt3yqqs.mjs → psl-field-resolution-CpxUePg1-DIrhHYx0.mjs} +2 -2
  24. package/dist/{psl-field-resolution-CpxUePg1-tvt3yqqs.mjs.map → psl-field-resolution-CpxUePg1-DIrhHYx0.mjs.map} +1 -1
  25. package/package.json +18 -18
  26. package/dist/contract-builder-BqswyknO.mjs.map +0 -1
  27. package/dist/derived-checks-BoHNzt-a-4Vi-QMdx.mjs.map +0 -1
@@ -30,10 +30,114 @@ import { mergeCapabilityMatrices } from "@prisma/orm-framework/components/compon
30
30
  import { encodeEnumTypeMembers, resolveToOneRelationNullable } from "@prisma/orm-framework/contract-authoring";
31
31
  import { buildExecutionSection, computeProfileHash, computeStorageHash } from "@prisma/orm-framework/contract/hashing";
32
32
  import { applySpecifierDefaultControlPolicy } from "@prisma/orm-framework/contract/apply-specifier-default-control-policy";
33
- //#region ../../../2-sql-family/2-authoring/contract-ts/dist/derived-checks-BoHNzt-a.mjs
33
+ //#region ../../../2-sql-family/2-authoring/contract-ts/dist/derived-checks-9Ku2scaj.mjs
34
34
  function contractError(code, message, options) {
35
35
  return structuredError(code, message, options);
36
36
  }
37
+ /** How an error message names what the name names, such as `A table in namespace "public"`. */
38
+ function nameSiteSubject(site) {
39
+ switch (site.kind) {
40
+ case "namespace":
41
+ if (site.modelName !== void 0) return `The namespace of model "${site.modelName}"`;
42
+ if (site.tableName !== void 0) return `The namespace of table "${site.tableName}"`;
43
+ if (site.entityKind !== void 0 && site.entityName !== void 0) return `The namespace of "${site.entityKind}" entity "${site.entityName}"`;
44
+ return "A namespace";
45
+ case "table": return `A table in namespace "${site.namespaceId}"`;
46
+ case "column": return `A column of table "${site.tableName}"`;
47
+ case "model": return site.namespaceId === void 0 ? "A model" : `A model in namespace "${site.namespaceId}"`;
48
+ case "field": return site.modelName === void 0 ? "A field of a model" : `A field of model "${site.modelName}"`;
49
+ case "relation": return `A relation of model "${site.modelName}"`;
50
+ case "enum": return `An enum in namespace "${site.namespaceId}"`;
51
+ case "valueSet": return `A value set in namespace "${site.namespaceId}"`;
52
+ case "valueObject": return "A value object";
53
+ case "valueObjectField": return `A field of value object "${site.valueObjectName}"`;
54
+ case "databaseType": return "A database type";
55
+ case "extensionAlias": return "An extension alias";
56
+ case "entity": return `A "${site.entityKind}" entity in namespace "${site.namespaceId}"`;
57
+ }
58
+ }
59
+ /** The fields of the site that locate it, leaving out those it does not know. */
60
+ function definedFields(site) {
61
+ const fields = {};
62
+ for (const [key, value] of Object.entries(site)) if (typeof value === "string") fields[key] = value;
63
+ return fields;
64
+ }
65
+ /** The error metadata that says what the name names and locates it. An entity's `kind` is its entity kind, as in `CONTRACT.NAME_DUPLICATE`. */
66
+ function nameSiteMeta(site) {
67
+ return site.kind === "entity" ? {
68
+ kind: site.entityKind,
69
+ namespaceId: site.namespaceId
70
+ } : definedFields(site);
71
+ }
72
+ /** How an error message names an author's object of named entries, such as `The fields of model "User"`. */
73
+ function namedEntriesSiteSubject(site) {
74
+ switch (site.kind) {
75
+ case "field": return site.modelName === void 0 ? "The fields of a model" : `The fields of model "${site.modelName}"`;
76
+ case "relation": return site.modelName === void 0 ? "The relations of a model" : `The relations of model "${site.modelName}"`;
77
+ case "model": return "The models of the contract";
78
+ case "enum": return "The enums of the contract";
79
+ case "databaseType": return "The database types of the contract";
80
+ case "extensionPack": return "The extensions of the contract";
81
+ }
82
+ }
83
+ /** Why `name` cannot be an object's own key, if it cannot. */
84
+ function nameUnusableAsKeyReason(name) {
85
+ return name === "__proto__" ? "JavaScript reads as the prototype of an object rather than as a key" : void 0;
86
+ }
87
+ /** The entry of `entries` named `name`, when `entries` owns one; an inherited property such as `constructor` is not an entry. */
88
+ function readNamedEntry(entries, name) {
89
+ return Object.hasOwn(entries, name) ? entries[name] : void 0;
90
+ }
91
+ /** Stores `entry` in `entries` under `name`. Refuses a name that cannot be an object's own key. */
92
+ function writeNamedEntry(entries, name, entry, site) {
93
+ refuseNameUnusableAsKey(name, site);
94
+ entries[name] = entry;
95
+ }
96
+ function refuseNameUnusableAsKey(name, site) {
97
+ const reason = nameUnusableAsKeyReason(name);
98
+ if (reason === void 0) return;
99
+ throw contractError("CONTRACT.NAME_UNUSABLE_AS_KEY", `${nameSiteSubject(site)} is named "${name}", which ${reason}. Choose another name.`, { meta: {
100
+ name,
101
+ ...nameSiteMeta(site),
102
+ reason: "name-not-a-key"
103
+ } });
104
+ }
105
+ /**
106
+ * The object of named entries stored in `entries` under `name`, storing an empty one first when there is none. Refuses `name` as {@link writeNamedEntry} does.
107
+ */
108
+ function ensureNamedEntries(entries, name, site) {
109
+ refuseNameUnusableAsKey(name, site);
110
+ const existing = readNamedEntry(entries, name);
111
+ if (existing !== void 0) return existing;
112
+ const created = {};
113
+ entries[name] = created;
114
+ return created;
115
+ }
116
+ /** An object of the named entries, each stored with {@link writeNamedEntry}. */
117
+ function namedEntriesFrom(pairs, site) {
118
+ const entries = {};
119
+ for (const [name, entry] of pairs) writeNamedEntry(entries, name, entry, site);
120
+ return entries;
121
+ }
122
+ /**
123
+ * Refuses an author's object of named entries that is not a plain object: its prototype is neither the plain object one nor `null`. A key written as `__proto__` in an object literal replaces the prototype and stores no entry, so no write ever sees that name.
124
+ */
125
+ function assertPlainNamedEntries(entries, site) {
126
+ const prototype = Object.getPrototypeOf(entries);
127
+ if (prototype === Object.prototype || prototype === null) return;
128
+ throw contractError("CONTRACT.NAME_UNUSABLE_AS_KEY", `${namedEntriesSiteSubject(site)} are not a plain object: their prototype has been replaced. A key written as "__proto__" in an object literal does this. Use a plain object, and choose another name for a "__proto__" key.`, { meta: {
129
+ ...definedFields(site),
130
+ reason: "prototype-replaced"
131
+ } });
132
+ }
133
+ /** A namespace name whose declaration names no model or table. */
134
+ const namespaceNameSite = {
135
+ kind: "namespace",
136
+ modelName: void 0,
137
+ tableName: void 0,
138
+ entityKind: void 0,
139
+ entityName: void 0
140
+ };
37
141
  /**
38
142
  * Whether a field is stored in a list column. A list of scalars is; a list of value objects is not, because it is stored in one column whose value is the whole list, as one JSON array.
39
143
  */
@@ -285,11 +389,15 @@ function joinFieldOf(owner, relation, model, columnToField, column) {
285
389
  function lowerRelations(semanticModel, columnToField, lookups) {
286
390
  const { defaultNamespaceId } = lookups;
287
391
  const modelRelations = {};
392
+ const relationSite = {
393
+ kind: "relation",
394
+ modelName: semanticModel.modelName
395
+ };
288
396
  for (const relation of semanticModel.relations ?? []) {
289
397
  if (relation.spaceId !== void 0) {
290
398
  const targetNamespaceId = relation.namespaceId ?? defaultNamespaceId;
291
399
  const localFields = relation.on.parentColumns.map((col) => joinFieldOf(semanticModel, relation, semanticModel, columnToField, col));
292
- modelRelations[relation.fieldName] = {
400
+ writeNamedEntry(modelRelations, relation.fieldName, {
293
401
  to: crossRef(relation.toModel, targetNamespaceId, relation.spaceId),
294
402
  cardinality: "N:1",
295
403
  nullable: toOneRelationNullable(semanticModel, relation),
@@ -297,7 +405,7 @@ function lowerRelations(semanticModel, columnToField, lookups) {
297
405
  localFields,
298
406
  targetFields: relation.on.childColumns
299
407
  }
300
- };
408
+ }, relationSite);
301
409
  continue;
302
410
  }
303
411
  const owner = {
@@ -320,25 +428,25 @@ function lowerRelations(semanticModel, columnToField, lookups) {
320
428
  relationName: relation.fieldName,
321
429
  reason: "many-to-many-missing-through"
322
430
  } });
323
- modelRelations[relation.fieldName] = {
431
+ writeNamedEntry(modelRelations, relation.fieldName, {
324
432
  to,
325
433
  cardinality: "N:M",
326
434
  on,
327
435
  through: buildThroughDescriptor(relation.through, lookups.tableNamespaceByName, targetModel, semanticModel.modelName, relation.fieldName, defaultNamespaceId)
328
- };
329
- } else if (relation.cardinality === "1:N") modelRelations[relation.fieldName] = {
436
+ }, relationSite);
437
+ } else if (relation.cardinality === "1:N") writeNamedEntry(modelRelations, relation.fieldName, {
330
438
  to,
331
439
  cardinality: "1:N",
332
440
  on
333
- };
441
+ }, relationSite);
334
442
  else {
335
443
  if (relation.cardinality === "1:1") assertHasOneKeyUnique(semanticModel, relation, targetModel, on.targetFields);
336
- modelRelations[relation.fieldName] = {
444
+ writeNamedEntry(modelRelations, relation.fieldName, {
337
445
  to,
338
446
  cardinality: relation.cardinality,
339
447
  nullable: toOneRelationNullable(semanticModel, relation),
340
448
  on
341
- };
449
+ }, relationSite);
342
450
  }
343
451
  }
344
452
  return modelRelations;
@@ -488,13 +596,17 @@ function describeModel(model, context) {
488
596
  const fieldToColumn = {};
489
597
  const domainFields = {};
490
598
  const executionDefaults = [];
599
+ const fieldSite = {
600
+ kind: "field",
601
+ modelName: model.modelName
602
+ };
491
603
  for (const field of model.fields) {
492
604
  const executionDefault = executionDefaultOf(field);
493
- fieldToColumn[field.fieldName] = field.columnName;
494
- domainFields[field.fieldName] = buildDomainField(isValueObjectMember(field) ? field : {
605
+ writeNamedEntry(fieldToColumn, field.fieldName, field.columnName, fieldSite);
606
+ writeNamedEntry(domainFields, field.fieldName, buildDomainField(isValueObjectMember(field) ? field : {
495
607
  ...field,
496
608
  descriptor: resolveColumnDescriptor(field.descriptor, namespaceId, context.qualifyColumnType)
497
- }, context.defaultNamespaceId);
609
+ }, context.defaultNamespaceId), fieldSite);
498
610
  if (executionDefault) executionDefaults.push({
499
611
  ref: {
500
612
  namespace: namespaceId,
@@ -514,7 +626,7 @@ function describeModel(model, context) {
514
626
  }
515
627
  }, context)), context.defaultNamespaceId);
516
628
  const storageFields = {};
517
- for (const [fieldName, columnName] of Object.entries(fieldToColumn)) storageFields[fieldName] = { column: columnName };
629
+ for (const [fieldName, columnName] of Object.entries(fieldToColumn)) writeNamedEntry(storageFields, fieldName, { column: columnName }, fieldSite);
518
630
  const columnToField = new Map(Object.entries(fieldToColumn).map(([field, col]) => [col, field]));
519
631
  return {
520
632
  storage,
@@ -748,17 +860,26 @@ function encodeColumnDefault(defaultInput, codecLookup, resolveCodec, site, many
748
860
  * on the equivalent (`PSL_DUPLICATE_DECLARATION`); the TS path rejects it too.
749
861
  */
750
862
  function collectEntityFromColumn(collected, namespaceId, entityRef) {
751
- const forNs = collected[namespaceId] ?? {};
752
- const forKind = forNs[entityRef.entityKind] ?? {};
753
- const existing = forKind[entityRef.entityName];
863
+ const forNs = ensureNamedEntries(collected, namespaceId, {
864
+ kind: "namespace",
865
+ modelName: void 0,
866
+ tableName: void 0,
867
+ entityKind: entityRef.entityKind,
868
+ entityName: entityRef.entityName
869
+ });
870
+ const forKind = readNamedEntry(forNs, entityRef.entityKind) ?? {};
871
+ const existing = readNamedEntry(forKind, entityRef.entityName);
754
872
  if (existing !== void 0 && existing !== entityRef.entity) throw contractError("CONTRACT.NAME_DUPLICATE", `buildSqlContractFromDefinition: two different "${entityRef.entityKind}" entities named "${entityRef.entityName}" in namespace "${namespaceId}" — pack-entity names must be unique per namespace.`, { meta: {
755
873
  kind: entityRef.entityKind,
756
874
  name: entityRef.entityName,
757
875
  namespaceId
758
876
  } });
759
- forKind[entityRef.entityName] = entityRef.entity;
877
+ writeNamedEntry(forKind, entityRef.entityName, entityRef.entity, {
878
+ kind: "entity",
879
+ entityKind: entityRef.entityKind,
880
+ namespaceId
881
+ });
760
882
  forNs[entityRef.entityKind] = forKind;
761
- collected[namespaceId] = forNs;
762
883
  }
763
884
  /**
764
885
  * Merges a namespace's entities-channel attachments (lowered from the
@@ -771,25 +892,29 @@ function collectEntityFromColumn(collected, namespaceId, entityRef) {
771
892
  * a column does not throw.
772
893
  */
773
894
  function mergeColumnAndAttachedEntities(namespaceId, attached, columnCollected) {
774
- if (attached === void 0) return columnCollected;
775
- if (columnCollected === void 0) return attached;
776
- const kinds = /* @__PURE__ */ new Set([...Object.keys(attached), ...Object.keys(columnCollected)]);
895
+ if (attached === void 0 && columnCollected === void 0) return void 0;
896
+ const kinds = /* @__PURE__ */ new Set([...Object.keys(attached ?? {}), ...Object.keys(columnCollected ?? {})]);
777
897
  const result = {};
778
898
  for (const kind of kinds) {
779
- const attachedForKind = attached[kind];
780
- const columnForKind = columnCollected[kind];
781
- for (const [name, entity] of Object.entries(columnForKind ?? {})) {
782
- const existing = attachedForKind?.[name];
899
+ const attachedForKind = readNamedEntry(attached ?? {}, kind) ?? {};
900
+ const columnForKind = readNamedEntry(columnCollected ?? {}, kind) ?? {};
901
+ const entitiesOfKind = {};
902
+ const entitySite = {
903
+ kind: "entity",
904
+ entityKind: kind,
905
+ namespaceId
906
+ };
907
+ for (const [name, entity] of Object.entries(attachedForKind)) writeNamedEntry(entitiesOfKind, name, entity, entitySite);
908
+ for (const [name, entity] of Object.entries(columnForKind)) {
909
+ const existing = readNamedEntry(attachedForKind, name);
783
910
  if (existing !== void 0 && existing !== entity) throw contractError("CONTRACT.NAME_DUPLICATE", `buildSqlContractFromDefinition: two different "${kind}" entities named "${name}" in namespace "${namespaceId}" — a column-referenced entity conflicts with an attached one; pack-entity names must be unique per namespace.`, { meta: {
784
911
  kind,
785
912
  name,
786
913
  namespaceId
787
914
  } });
915
+ writeNamedEntry(entitiesOfKind, name, entity, entitySite);
788
916
  }
789
- result[kind] = {
790
- ...attachedForKind,
791
- ...columnForKind
792
- };
917
+ result[kind] = entitiesOfKind;
793
918
  }
794
919
  return result;
795
920
  }
@@ -838,7 +963,7 @@ function collectEntityTypeDescriptorsByDiscriminator(definition) {
838
963
  * registered descriptor, or whose descriptor output doesn't derive a
839
964
  * value-set, contribute nothing.
840
965
  */
841
- function deriveEntityValueSets(entitiesForNs, entityTypesByDiscriminator) {
966
+ function deriveEntityValueSets(namespaceId, entitiesForNs, entityTypesByDiscriminator) {
842
967
  if (entitiesForNs === void 0) return void 0;
843
968
  let result;
844
969
  for (const [kind, entitiesByName] of Object.entries(entitiesForNs)) {
@@ -848,7 +973,10 @@ function deriveEntityValueSets(entitiesForNs, entityTypesByDiscriminator) {
848
973
  const derivedValueSet = deriveValueSetFromEntity(descriptor.output, entity);
849
974
  if (derivedValueSet === void 0) continue;
850
975
  result ??= {};
851
- result[name] = derivedValueSet;
976
+ writeNamedEntry(result, name, derivedValueSet, {
977
+ kind: "valueSet",
978
+ namespaceId
979
+ });
852
980
  }
853
981
  }
854
982
  return result;
@@ -870,10 +998,10 @@ function mergeNamespaceValueSets(namespaceId, enumValueSets, packValueSets) {
870
998
  namespaceId
871
999
  } });
872
1000
  }
873
- return {
874
- ...enumValueSets,
875
- ...packValueSets
876
- };
1001
+ return namedEntriesFrom([...Object.entries(enumValueSets ?? {}), ...Object.entries(packValueSets ?? {})], {
1002
+ kind: "valueSet",
1003
+ namespaceId
1004
+ });
877
1005
  }
878
1006
  /**
879
1007
  * Names the target's rendered checks and lowers them into contract entities.
@@ -993,7 +1121,10 @@ function lowerTable(table, context) {
993
1121
  const checks = [];
994
1122
  for (const description of table.columns) {
995
1123
  const lowered = lowerColumn(description, placement, context);
996
- columns[description.columnName] = lowered.column;
1124
+ writeNamedEntry(columns, description.columnName, lowered.column, {
1125
+ kind: "column",
1126
+ tableName
1127
+ });
997
1128
  checks.push(...lowered.derivedChecks);
998
1129
  }
999
1130
  const uniques = table.uniques.map((u) => ({
@@ -1470,18 +1601,20 @@ function enabledCapabilities(matrix) {
1470
1601
  /** The value objects of a definition as storage entities, all in the default namespace; `undefined` when it has none. */
1471
1602
  function valueObjectsFromDefinition(definition, defaultNamespaceId) {
1472
1603
  if (definition.valueObjects === void 0 || definition.valueObjects.length === 0) return;
1473
- return Object.fromEntries(definition.valueObjects.map((valueObject) => [valueObject.name, { fields: Object.fromEntries(valueObject.fields.map((field) => [field.fieldName, buildValueObjectMember(field, defaultNamespaceId)])) }]));
1604
+ return namedEntriesFrom(definition.valueObjects.map((valueObject) => [valueObject.name, { fields: namedEntriesFrom(valueObject.fields.map((field) => [field.fieldName, buildValueObjectMember(field, defaultNamespaceId)]), {
1605
+ kind: "valueObjectField",
1606
+ valueObjectName: valueObject.name
1607
+ }) }]), { kind: "valueObject" });
1474
1608
  }
1475
1609
  /** The value objects and value sets storage will hold, in the shape storage readers take. */
1476
1610
  function storageOf(defaultNamespaceId, valueObjects, storageValueSetsByNs) {
1477
- const namespaceIds = /* @__PURE__ */ new Set([defaultNamespaceId, ...Object.keys(storageValueSetsByNs)]);
1478
- return { namespaces: Object.fromEntries([...namespaceIds].map((id) => [id, {
1611
+ return { namespaces: namedEntriesFrom([.../* @__PURE__ */ new Set([defaultNamespaceId, ...Object.keys(storageValueSetsByNs)])].map((id) => [id, {
1479
1612
  id,
1480
1613
  entries: {
1481
- valueSet: storageValueSetsByNs[id] ?? {},
1614
+ valueSet: readNamedEntry(storageValueSetsByNs, id) ?? {},
1482
1615
  ...id === defaultNamespaceId ? { valueObject: valueObjects ?? {} } : {}
1483
1616
  }
1484
- }])) };
1617
+ }]), namespaceNameSite) };
1485
1618
  }
1486
1619
  /**
1487
1620
  * Writes the value set of each discriminator, keyed `<Base>.<field>` in the base table's namespace and holding the variants' values as the column stores them, sorted, and points the discriminator column at it (ADR 309). A column an enum types keeps the enum's value set. An enum with the same name in the same namespace is refused, since the two value sets would share one entry.
@@ -1492,24 +1625,33 @@ function writeDiscriminatorValueSets(definition, tablesByNamespace, storageValue
1492
1625
  const { discriminator, variants } = model;
1493
1626
  if (discriminator === void 0 || variants === void 0) continue;
1494
1627
  const namespaceId = modelNamespaceId(model, defaultNamespaceId);
1495
- const table = tablesByNamespace[namespaceId]?.[model.tableName];
1628
+ const namespaceTables = readNamedEntry(tablesByNamespace, namespaceId);
1629
+ const table = namespaceTables === void 0 ? void 0 : readNamedEntry(namespaceTables, model.tableName);
1496
1630
  const columnName = model.fields.find((field) => field.fieldName === discriminator.field)?.columnName;
1497
- const column = columnName === void 0 ? void 0 : table?.columns[columnName];
1498
- if (table === void 0 || columnName === void 0 || column === void 0) continue;
1631
+ const column = columnName === void 0 || table === void 0 ? void 0 : readNamedEntry(table.columns, columnName);
1632
+ if (namespaceTables === void 0 || table === void 0 || columnName === void 0 || column === void 0) continue;
1499
1633
  if (column.valueSet !== void 0) continue;
1500
1634
  const { name: valueSetName, values } = discriminatorValueSet(model.modelName, discriminator.field, Object.keys(variants));
1501
- const namespaceValueSets = storageValueSetsByNs[namespaceId] ?? {};
1635
+ const namespaceValueSets = ensureNamedEntries(storageValueSetsByNs, namespaceId, {
1636
+ kind: "namespace",
1637
+ modelName: model.modelName,
1638
+ tableName: void 0,
1639
+ entityKind: void 0,
1640
+ entityName: void 0
1641
+ });
1502
1642
  if (Object.hasOwn(namespaceValueSets, valueSetName)) throw contractError("CONTRACT.ENUM_INVALID", `enumType("${valueSetName}"): has the name of the value set that the discriminator field "${discriminator.field}" of model "${model.modelName}" writes. Rename the enum.`, { meta: {
1503
1643
  enumName: valueSetName,
1504
1644
  model: model.modelName,
1505
1645
  field: discriminator.field,
1506
1646
  reason: "discriminator-value-set-name"
1507
1647
  } });
1508
- namespaceValueSets[valueSetName] = {
1648
+ writeNamedEntry(namespaceValueSets, valueSetName, {
1509
1649
  kind: "valueSet",
1510
1650
  values
1511
- };
1512
- storageValueSetsByNs[namespaceId] = namespaceValueSets;
1651
+ }, {
1652
+ kind: "valueSet",
1653
+ namespaceId
1654
+ });
1513
1655
  const render = checkRendererFor(table);
1514
1656
  const derivedChecks = render === void 0 ? [] : lowerRenderedChecks(model.tableName, render({
1515
1657
  tableName: model.tableName,
@@ -1518,24 +1660,27 @@ function writeDiscriminatorValueSets(definition, tablesByNamespace, storageValue
1518
1660
  memberValues: values,
1519
1661
  valueObject: void 0
1520
1662
  }));
1521
- const namespaceTables = tablesByNamespace[namespaceId] ?? {};
1522
- namespaceTables[model.tableName] = {
1663
+ const columns = { ...table.columns };
1664
+ writeNamedEntry(columns, columnName, {
1665
+ ...column,
1666
+ valueSet: {
1667
+ plane: "storage",
1668
+ entityKind: "valueSet",
1669
+ namespaceId,
1670
+ entityName: valueSetName
1671
+ }
1672
+ }, {
1673
+ kind: "column",
1674
+ tableName: model.tableName
1675
+ });
1676
+ writeNamedEntry(namespaceTables, model.tableName, {
1523
1677
  ...table,
1524
1678
  ...derivedChecks.length > 0 ? { checks: inCanonicalOrder([...(table.checks ?? []).map((check) => CheckConstraint.from(check)), ...derivedChecks]) } : {},
1525
- columns: {
1526
- ...table.columns,
1527
- [columnName]: {
1528
- ...column,
1529
- valueSet: {
1530
- plane: "storage",
1531
- entityKind: "valueSet",
1532
- namespaceId,
1533
- entityName: valueSetName
1534
- }
1535
- }
1536
- }
1537
- };
1538
- tablesByNamespace[namespaceId] = namespaceTables;
1679
+ columns
1680
+ }, {
1681
+ kind: "table",
1682
+ namespaceId
1683
+ });
1539
1684
  }
1540
1685
  }
1541
1686
  function buildSqlContractFromDefinition(definition, codecLookup, dataTypeLookup) {
@@ -1558,26 +1703,19 @@ function buildSqlContractFromDefinition(definition, codecLookup, dataTypeLookup)
1558
1703
  declarationKey: enumName,
1559
1704
  reason: "key-name-mismatch"
1560
1705
  } });
1561
- const nsId = defaultNamespaceId;
1562
- let domainSlot = domainEnumsByNs[nsId];
1563
- if (domainSlot === void 0) {
1564
- domainSlot = {};
1565
- domainEnumsByNs[nsId] = domainSlot;
1566
- }
1706
+ const enumSite = {
1707
+ kind: "enum",
1708
+ namespaceId: defaultNamespaceId
1709
+ };
1567
1710
  const storedMembers = encodeEnumTypeMembers(handle, codecLookup, "the column");
1568
- domainSlot[enumName] = {
1711
+ writeNamedEntry(ensureNamedEntries(domainEnumsByNs, defaultNamespaceId, namespaceNameSite), enumName, {
1569
1712
  codecId: handle.codecId,
1570
1713
  members: storedMembers
1571
- };
1572
- let storageSlot = storageValueSetsByNs[nsId];
1573
- if (storageSlot === void 0) {
1574
- storageSlot = {};
1575
- storageValueSetsByNs[nsId] = storageSlot;
1576
- }
1577
- storageSlot[enumName] = {
1714
+ }, enumSite);
1715
+ writeNamedEntry(ensureNamedEntries(storageValueSetsByNs, defaultNamespaceId, namespaceNameSite), enumName, {
1578
1716
  kind: "valueSet",
1579
1717
  values: storedMembers.map((member) => member.value)
1580
- };
1718
+ }, enumSite);
1581
1719
  }
1582
1720
  const modelDescriptionContext = {
1583
1721
  ...modelLookupsOf(definition),
@@ -1606,45 +1744,55 @@ function buildSqlContractFromDefinition(definition, codecLookup, dataTypeLookup)
1606
1744
  const modelStorages = components.map((c) => c.storage);
1607
1745
  const tables = mergeTables(modelStorages, tableNodes);
1608
1746
  authoringWarnings.push(...requiredUnmappedColumnWarnings(modelStorages, tables));
1609
- for (const table of tables) {
1610
- const namespaceTables = tablesByNamespace[table.namespaceId] ?? {};
1611
- namespaceTables[table.tableName] = lowerTable(table, tableLoweringContext);
1612
- tablesByNamespace[table.namespaceId] = namespaceTables;
1613
- }
1747
+ for (const table of tables) writeNamedEntry(ensureNamedEntries(tablesByNamespace, table.namespaceId, {
1748
+ kind: "namespace",
1749
+ modelName: void 0,
1750
+ tableName: table.tableName,
1751
+ entityKind: void 0,
1752
+ entityName: void 0
1753
+ }), table.tableName, lowerTable(table, tableLoweringContext), {
1754
+ kind: "table",
1755
+ namespaceId: table.namespaceId
1756
+ });
1614
1757
  writeDiscriminatorValueSets(definition, tablesByNamespace, storageValueSetsByNs, (table) => renderCheckExpressions !== void 0 && effectiveControlPolicy(table.control, definition.defaultControlPolicy) === "managed" ? renderCheckExpressions : void 0);
1615
1758
  const executionDefaults = components.flatMap((c) => c.domain.executionDefaults);
1616
1759
  const modelsByNamespace = {};
1617
- for (const { domain } of components) {
1618
- const namespaceModels = modelsByNamespace[domain.namespaceId] ?? {};
1619
- namespaceModels[domain.modelName] = domain.model;
1620
- modelsByNamespace[domain.namespaceId] = namespaceModels;
1621
- }
1760
+ for (const { domain } of components) writeNamedEntry(ensureNamedEntries(modelsByNamespace, domain.namespaceId, {
1761
+ kind: "namespace",
1762
+ modelName: domain.modelName,
1763
+ tableName: void 0,
1764
+ entityKind: void 0,
1765
+ entityName: void 0
1766
+ }), domain.modelName, domain.model, {
1767
+ kind: "model",
1768
+ namespaceId: domain.namespaceId
1769
+ });
1622
1770
  const roots = buildRoots(components);
1623
- const documentTypes = Object.fromEntries(Object.entries(databaseTypes).map(([name, entry]) => [name, toStorageTypeInstance({
1771
+ const documentTypes = namedEntriesFrom(Object.entries(databaseTypes).map(([name, entry]) => [name, toStorageTypeInstance({
1624
1772
  codecId: entry.codecId,
1625
1773
  dataType: sqlDataTypeOfCodec(entry.codecId, lookups).id,
1626
1774
  typeParams: entry.typeParams
1627
- })]));
1775
+ })]), { kind: "databaseType" });
1628
1776
  const namespaceCoordinateIds = collectStorageNamespaceCoordinateIds(definition);
1629
1777
  const { createNamespace } = definition;
1630
1778
  const entityTypesByDiscriminator = collectEntityTypeDescriptorsByDiscriminator(definition);
1631
- const namespaces = Object.fromEntries([...namespaceCoordinateIds].sort().map((id) => {
1632
- const entitiesForNs = mergeColumnAndAttachedEntities(id, definition.attachedEntities?.[id], collectedColumnEntities[id]);
1779
+ const namespaces = namedEntriesFrom([...namespaceCoordinateIds].sort().map((id) => {
1780
+ const entitiesForNs = mergeColumnAndAttachedEntities(id, definition.attachedEntities === void 0 ? void 0 : readNamedEntry(definition.attachedEntities, id), readNamedEntry(collectedColumnEntities, id));
1633
1781
  assertNoManagedEntityKinds(id, entitiesForNs);
1634
- const enumValueSetEntries = storageValueSetsByNs[id];
1635
- const packValueSetEntries = deriveEntityValueSets(entitiesForNs, entityTypesByDiscriminator);
1782
+ const enumValueSetEntries = readNamedEntry(storageValueSetsByNs, id);
1783
+ const packValueSetEntries = deriveEntityValueSets(id, entitiesForNs, entityTypesByDiscriminator);
1636
1784
  const valueSetEntries = enumValueSetEntries !== void 0 || packValueSetEntries !== void 0 ? mergeNamespaceValueSets(id, enumValueSetEntries, packValueSetEntries) : void 0;
1637
1785
  const nsInput = {
1638
1786
  id,
1639
1787
  entries: {
1640
- table: tablesByNamespace[id] ?? {},
1788
+ table: readNamedEntry(tablesByNamespace, id) ?? {},
1641
1789
  ...entitiesForNs,
1642
1790
  ...valueSetEntries !== void 0 && Object.keys(valueSetEntries).length > 0 ? { valueSet: valueSetEntries } : {},
1643
1791
  ...id === defaultNamespaceId && valueObjects !== void 0 ? { valueObject: valueObjects } : {}
1644
1792
  }
1645
1793
  };
1646
1794
  return [id, createNamespace(nsInput)];
1647
- }));
1795
+ }), namespaceNameSite);
1648
1796
  const storageWithoutHash = {
1649
1797
  ...Object.keys(documentTypes).length > 0 ? { types: documentTypes } : {},
1650
1798
  namespaces: defaultNamespaceId === UNBOUND_NAMESPACE_ID ? ensureUnboundNamespaceSlot(namespaces, createNamespace) : namespaces
@@ -1660,7 +1808,7 @@ function buildSqlContractFromDefinition(definition, codecLookup, dataTypeLookup)
1660
1808
  storageHash
1661
1809
  });
1662
1810
  const extensionNamespaces = definition.extensions ? Object.values(definition.extensions).map((pack) => pack.id) : void 0;
1663
- const extensions = { ...definition.extensions || {} };
1811
+ const extensions = namedEntriesFrom(Object.entries(definition.extensions ?? {}), { kind: "extensionAlias" });
1664
1812
  if (extensionNamespaces) {
1665
1813
  for (const namespace of extensionNamespaces) if (!Object.hasOwn(extensions, namespace)) extensions[namespace] = {};
1666
1814
  }
@@ -1682,14 +1830,14 @@ function buildSqlContractFromDefinition(definition, codecLookup, dataTypeLookup)
1682
1830
  const domainNamespaceIds = new Set(Object.keys(modelsByNamespace));
1683
1831
  if (domainNamespaceIds.size === 0) domainNamespaceIds.add(defaultNamespaceId);
1684
1832
  for (const nsId of Object.keys(domainEnumsByNs)) domainNamespaceIds.add(nsId);
1685
- const domainNamespaces = Object.fromEntries([...domainNamespaceIds].sort().map((namespaceId) => {
1686
- const modelsInNs = modelsByNamespace[namespaceId] ?? {};
1687
- const enumsInNs = domainEnumsByNs[namespaceId];
1833
+ const domainNamespaces = namedEntriesFrom([...domainNamespaceIds].sort().map((namespaceId) => {
1834
+ const modelsInNs = readNamedEntry(modelsByNamespace, namespaceId) ?? {};
1835
+ const enumsInNs = readNamedEntry(domainEnumsByNs, namespaceId);
1688
1836
  return [namespaceId, {
1689
1837
  models: modelsInNs,
1690
1838
  ...enumsInNs !== void 0 && Object.keys(enumsInNs).length > 0 ? { enum: enumsInNs } : {}
1691
1839
  }];
1692
- }));
1840
+ }), namespaceNameSite);
1693
1841
  const contract = {
1694
1842
  target,
1695
1843
  targetFamily,
@@ -1713,16 +1861,17 @@ function buildSqlContractFromDefinition(definition, codecLookup, dataTypeLookup)
1713
1861
  function buildRoots(components) {
1714
1862
  const rootEntries = components.flatMap(({ storage, domain }) => storage.kind === "ownTable" ? [{
1715
1863
  tableName: storage.table.tableName,
1864
+ modelName: domain.modelName,
1716
1865
  namespaceId: domain.namespaceId,
1717
1866
  ref: crossRef(domain.modelName, domain.namespaceId)
1718
1867
  }] : []);
1719
1868
  const rootTableNameCounts = /* @__PURE__ */ new Map();
1720
1869
  for (const entry of rootEntries) rootTableNameCounts.set(entry.tableName, (rootTableNameCounts.get(entry.tableName) ?? 0) + 1);
1721
1870
  const roots = {};
1722
- for (const entry of rootEntries) {
1723
- const key = (rootTableNameCounts.get(entry.tableName) ?? 0) > 1 ? `${entry.namespaceId}.${entry.tableName}` : entry.tableName;
1724
- roots[key] = entry.ref;
1725
- }
1871
+ for (const entry of rootEntries) writeNamedEntry(roots, (rootTableNameCounts.get(entry.tableName) ?? 0) > 1 ? `${entry.namespaceId}.${entry.tableName}` : entry.tableName, entry.ref, {
1872
+ kind: "table",
1873
+ namespaceId: entry.namespaceId
1874
+ });
1726
1875
  return roots;
1727
1876
  }
1728
1877
  /**
@@ -1783,7 +1932,7 @@ function stripDerivedChecksFromNonManagedTables(contract, createNamespace) {
1783
1932
  if (!isMaterializedSqlNamespace(namespace)) return contract;
1784
1933
  const tables = namespace.entries["table"];
1785
1934
  if (tables === void 0) {
1786
- namespaces[namespaceId] = namespace;
1935
+ writeNamedEntry(namespaces, namespaceId, namespace, namespaceNameSite);
1787
1936
  continue;
1788
1937
  }
1789
1938
  let namespaceStripped = false;
@@ -1791,20 +1940,23 @@ function stripDerivedChecksFromNonManagedTables(contract, createNamespace) {
1791
1940
  for (const [tableName, table] of Object.entries(tables)) {
1792
1941
  const stripped = StorageTable.is(table) ? withoutDerivedChecks(tableName, table, contract.defaultControlPolicy) : table;
1793
1942
  if (stripped !== table) namespaceStripped = true;
1794
- nextTables[tableName] = stripped;
1943
+ writeNamedEntry(nextTables, tableName, stripped, {
1944
+ kind: "table",
1945
+ namespaceId
1946
+ });
1795
1947
  }
1796
1948
  if (!namespaceStripped) {
1797
- namespaces[namespaceId] = namespace;
1949
+ writeNamedEntry(namespaces, namespaceId, namespace, namespaceNameSite);
1798
1950
  continue;
1799
1951
  }
1800
1952
  anyStripped = true;
1801
- namespaces[namespaceId] = createNamespace({
1953
+ writeNamedEntry(namespaces, namespaceId, createNamespace({
1802
1954
  id: namespace.id,
1803
1955
  entries: {
1804
1956
  ...namespace.entries,
1805
1957
  table: nextTables
1806
1958
  }
1807
- });
1959
+ }), namespaceNameSite);
1808
1960
  }
1809
1961
  if (!anyStripped) return contract;
1810
1962
  const storageWithoutHash = {
@@ -1858,6 +2010,6 @@ function withoutDerivedChecks(tableName, table, contractDefault) {
1858
2010
  });
1859
2011
  }
1860
2012
  //#endregion
1861
- export { isValueObjectMember as a, storedAsListColumn as c, contractError as i, toOneNullabilityContradictionMessage as l, buildSqlContractFromDefinition as n, mergeSharedTable as o, columnsPhrase as r, refuseTypesKey as s, applySqlSpecifierControlPolicy as t };
2013
+ export { contractError as a, mergeSharedTable as c, refuseTypesKey as d, storedAsListColumn as f, columnsPhrase as i, namedEntriesFrom as l, writeNamedEntry as m, assertPlainNamedEntries as n, ensureNamedEntries as o, toOneNullabilityContradictionMessage as p, buildSqlContractFromDefinition as r, isValueObjectMember as s, applySqlSpecifierControlPolicy as t, readNamedEntry as u };
1862
2014
 
1863
- //# sourceMappingURL=derived-checks-BoHNzt-a-4Vi-QMdx.mjs.map
2015
+ //# sourceMappingURL=derived-checks-9Ku2scaj-0iT0uU_6.mjs.map