@prisma-next/sql-contract 0.14.0-dev.4 → 0.14.0-dev.41

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 (52) hide show
  1. package/dist/contract-view.d.mts +61 -0
  2. package/dist/contract-view.d.mts.map +1 -0
  3. package/dist/contract-view.mjs +39 -0
  4. package/dist/contract-view.mjs.map +1 -0
  5. package/dist/entity-kinds-CHiydmat.mjs +131 -0
  6. package/dist/entity-kinds-CHiydmat.mjs.map +1 -0
  7. package/dist/entity-kinds.d.mts +1 -1
  8. package/dist/entity-kinds.mjs +1 -1
  9. package/dist/factories.d.mts +5 -3
  10. package/dist/factories.d.mts.map +1 -1
  11. package/dist/factories.mjs +5 -4
  12. package/dist/factories.mjs.map +1 -1
  13. package/dist/index-type-validation.d.mts +1 -1
  14. package/dist/resolve-storage-table.d.mts +2 -2
  15. package/dist/{sql-storage-Dga0jwP2.d.mts → sql-storage-Dc1Dy3Vb.d.mts} +60 -19
  16. package/dist/sql-storage-Dc1Dy3Vb.d.mts.map +1 -0
  17. package/dist/{entity-kinds-Cl36zL5j.mjs → storage-value-set-C4XInPlX.mjs} +21 -128
  18. package/dist/storage-value-set-C4XInPlX.mjs.map +1 -0
  19. package/dist/{storage-value-set-WnYsIFM8.d.mts → storage-value-set-DXL-7PYo.d.mts} +14 -2
  20. package/dist/storage-value-set-DXL-7PYo.d.mts.map +1 -0
  21. package/dist/{types-B1N8w0I2.d.mts → types-ChH7hULD.d.mts} +24 -54
  22. package/dist/types-ChH7hULD.d.mts.map +1 -0
  23. package/dist/types-KiOT4SAV.mjs +114 -0
  24. package/dist/types-KiOT4SAV.mjs.map +1 -0
  25. package/dist/types.d.mts +10 -4
  26. package/dist/types.d.mts.map +1 -0
  27. package/dist/types.mjs +3 -3
  28. package/dist/validators.d.mts +9 -6
  29. package/dist/validators.d.mts.map +1 -1
  30. package/dist/validators.mjs +109 -17
  31. package/dist/validators.mjs.map +1 -1
  32. package/package.json +8 -7
  33. package/src/column-type-resolution.ts +26 -0
  34. package/src/contract-view.ts +98 -0
  35. package/src/exports/contract-view.ts +11 -0
  36. package/src/exports/types.ts +17 -4
  37. package/src/factories.ts +12 -2
  38. package/src/ir/sql-storage.ts +80 -17
  39. package/src/ir/storage-column.ts +3 -0
  40. package/src/ir/storage-entry-schemas.ts +1 -0
  41. package/src/ir/storage-table.ts +21 -0
  42. package/src/ir/storage-value-set.ts +5 -0
  43. package/src/types.ts +51 -8
  44. package/src/validators.ts +215 -40
  45. package/dist/entity-kinds-Cl36zL5j.mjs.map +0 -1
  46. package/dist/sql-storage-Dga0jwP2.d.mts.map +0 -1
  47. package/dist/storage-value-set-WnYsIFM8.d.mts.map +0 -1
  48. package/dist/types-B-eiQXff.mjs +0 -191
  49. package/dist/types-B-eiQXff.mjs.map +0 -1
  50. package/dist/types-B1N8w0I2.d.mts.map +0 -1
  51. package/src/ir/build-sql-namespace.ts +0 -110
  52. package/src/ir/sql-unbound-namespace.ts +0 -74
@@ -0,0 +1,11 @@
1
+ export type {
2
+ SqlBuiltinKind,
3
+ SqlSchemaQualifiedView,
4
+ SqlSingleNamespaceAccessors,
5
+ SqlSingleNamespaceView,
6
+ } from '../contract-view';
7
+ export {
8
+ buildSqlSchemaQualifiedView,
9
+ buildSqlSingleNamespaceView,
10
+ SQL_BUILTIN_KINDS,
11
+ } from '../contract-view';
@@ -1,3 +1,7 @@
1
+ export type {
2
+ StorageColumnMapAt,
3
+ StorageColumnTypeAcrossNamespaces,
4
+ } from '../column-type-resolution';
1
5
  export type {
2
6
  CheckConstraintInput,
3
7
  CodecTypesOf,
@@ -6,6 +10,8 @@ export type {
6
10
  ExtractFieldInputTypes,
7
11
  ExtractFieldOutputTypes,
8
12
  ExtractQueryOperationTypes,
13
+ ExtractStorageColumnInputTypes,
14
+ ExtractStorageColumnTypes,
9
15
  ExtractTypeMapsFromContract,
10
16
  FieldInputTypesOf,
11
17
  FieldOutputTypesOf,
@@ -13,6 +19,8 @@ export type {
13
19
  ForeignKeyOptions,
14
20
  ForeignKeyReferenceInput,
15
21
  IndexInput,
22
+ NamespacedFieldTypeMap,
23
+ NamespacedStorageColumnTypeMap,
16
24
  PrimaryKeyInput,
17
25
  QueryOperationReturn,
18
26
  QueryOperationSelfSpec,
@@ -21,16 +29,20 @@ export type {
21
29
  QueryOperationTypesOf,
22
30
  ReferentialAction,
23
31
  ResolveCodecTypes,
32
+ SqlAuthoringContributions,
24
33
  SqlControlDriverInstance,
25
34
  SqlModelFieldStorage,
26
35
  SqlModelStorage,
27
36
  SqlNamespace,
28
37
  SqlNamespaceEntries,
29
- SqlNamespaceTablesInput,
38
+ SqlNamespaceFactory,
39
+ SqlNamespaceInput,
30
40
  SqlQueryOperationTypes,
31
41
  SqlStorageInput,
32
42
  SqlStorageTypeEntry,
33
43
  StorageColumnInput,
44
+ StorageColumnInputTypesOf,
45
+ StorageColumnTypesOf,
34
46
  StorageTableInput,
35
47
  StorageTypeInstance,
36
48
  StorageTypeInstanceInput,
@@ -41,8 +53,6 @@ export type {
41
53
  } from '../types';
42
54
  export {
43
55
  applyFkDefaults,
44
- buildSqlNamespace,
45
- buildSqlNamespaceMap,
46
56
  CheckConstraint,
47
57
  CODEC_INSTANCE_KIND,
48
58
  DEFAULT_FK_CONSTRAINT,
@@ -50,11 +60,14 @@ export {
50
60
  ForeignKey,
51
61
  ForeignKeyReference,
52
62
  Index,
63
+ isMaterializedSqlNamespace,
64
+ isSqlAuthoringContributions,
53
65
  isStorageTypeInstance,
66
+ isStorageValueSet,
54
67
  PrimaryKey,
68
+ SqlNamespaceBase,
55
69
  SqlNode,
56
70
  SqlStorage,
57
- SqlUnboundNamespace,
58
71
  StorageColumn,
59
72
  StorageTable,
60
73
  StorageValueSet,
package/src/factories.ts CHANGED
@@ -14,8 +14,18 @@ import {
14
14
  UniqueConstraint,
15
15
  } from './types';
16
16
 
17
- export function col(nativeType: string, codecId: string, nullable = false): StorageColumn {
18
- return new StorageColumn({ nativeType, codecId, nullable });
17
+ export function col(
18
+ nativeType: string,
19
+ codecId: string,
20
+ nullable = false,
21
+ opts?: { readonly many?: boolean },
22
+ ): StorageColumn {
23
+ return new StorageColumn({
24
+ nativeType,
25
+ codecId,
26
+ nullable,
27
+ ...(opts?.many !== undefined && { many: opts.many }),
28
+ });
19
29
  }
20
30
 
21
31
  export function pk(...columns: readonly string[]): PrimaryKey {
@@ -1,5 +1,12 @@
1
1
  import type { StorageHashBase } from '@prisma-next/contract/types';
2
- import { freezeNode, type Namespace, type Storage } from '@prisma-next/framework-components/ir';
2
+ import type { AuthoringContributions } from '@prisma-next/framework-components/authoring';
3
+ import {
4
+ freezeNode,
5
+ isPlainRecord,
6
+ type Namespace,
7
+ NamespaceBase,
8
+ type Storage,
9
+ } from '@prisma-next/framework-components/ir';
3
10
  import { SqlNode } from './sql-node';
4
11
  import type { StorageTable } from './storage-table';
5
12
  import {
@@ -19,15 +26,45 @@ import type { StorageValueSet } from './storage-value-set';
19
26
  */
20
27
  export type SqlStorageTypeEntry = StorageTypeInstance | StorageTypeInstanceInput;
21
28
 
22
- export interface SqlNamespaceTablesInput {
29
+ export interface SqlNamespaceInput {
23
30
  readonly id: string;
24
31
  readonly entries: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
25
32
  }
26
33
 
34
+ /**
35
+ * Target-supplied factory that materializes a `Namespace` from a SQL
36
+ * `SqlNamespaceInput` (used to populate `SqlStorage.namespaces`).
37
+ */
38
+ export type SqlNamespaceFactory = (input: SqlNamespaceInput) => Namespace;
39
+
40
+ /**
41
+ * SQL-family extension of the framework `AuthoringContributions`. SQL target
42
+ * packs add a `createNamespace` factory so the PSL/TS authoring paths can
43
+ * materialize namespaces (and merge lowered extension-block entities) without
44
+ * each consumer re-specifying it. The factory is SQL-specific, so it lives here
45
+ * rather than on the framework `AuthoringContributions` base.
46
+ */
47
+ export interface SqlAuthoringContributions extends AuthoringContributions {
48
+ readonly createNamespace?: SqlNamespaceFactory;
49
+ }
50
+
51
+ /**
52
+ * Narrows framework `AuthoringContributions` to the SQL-family shape by testing
53
+ * for the SQL-specific `createNamespace` capability.
54
+ */
55
+ export function isSqlAuthoringContributions(
56
+ authoring: AuthoringContributions | undefined,
57
+ ): authoring is SqlAuthoringContributions {
58
+ if (authoring === undefined || !Object.hasOwn(authoring, 'createNamespace')) {
59
+ return false;
60
+ }
61
+ return typeof Reflect.get(authoring, 'createNamespace') === 'function';
62
+ }
63
+
27
64
  export interface SqlStorageInput<THash extends string = string> {
28
65
  readonly storageHash: StorageHashBase<THash>;
29
66
  readonly types?: Record<string, SqlStorageTypeEntry>;
30
- readonly namespaces: Readonly<Record<string, SqlNamespace>>;
67
+ readonly namespaces: Readonly<Record<string, SqlNamespaceBase>>;
31
68
  }
32
69
 
33
70
  /**
@@ -63,22 +100,48 @@ export type SqlNamespaceEntries = Readonly<Record<string, Readonly<Record<string
63
100
  };
64
101
 
65
102
  /**
66
- * SQL family namespace. `entries` is the open ADR 224 dictionary —
67
- * `entries[entityKind][entityName]` addresses any entity. Emitted
68
- * contract literals satisfy this structurally (no prototype getters
69
- * needed). For typed access to specific kinds, use the class getters
70
- * on the concretion or `ns.entries.table` / `ns.entries.valueSet` directly.
103
+ * Structural interface for SQL family namespaces. Generated `.d.ts` contract
104
+ * types satisfy this structurally (no prototype methods). The runtime
105
+ * abstract class `SqlNamespaceBase` extends this.
106
+ *
107
+ * `qualifyTable` is optional so JSON-shaped contract types (which carry no
108
+ * methods) are accepted where `SqlNamespace` is required. Hydrated
109
+ * `SqlNamespaceBase` instances always have it.
71
110
  */
72
- export type SqlNamespace = Namespace & {
111
+ export interface SqlNamespace {
112
+ readonly kind: string;
113
+ readonly id: string;
73
114
  readonly entries: SqlNamespaceEntries;
74
- /**
75
- * Render a dialect-qualified table reference for runtime SQL emission.
76
- * Present on materialised target concretions (`PostgresSchema`,
77
- * `SqliteDatabase`, …) and family placeholders; omitted on emitted
78
- * contract structural namespace literals (methods are not serialised).
79
- */
80
115
  qualifyTable?(tableName: string): string;
81
- };
116
+ }
117
+
118
+ /**
119
+ * Abstract SQL family namespace base class. Target concretions (`PostgresSchema`,
120
+ * `SqliteDatabase`, …) extend this — it is never instantiated directly.
121
+ * `entries` is the open ADR 224 dictionary: `entries[entityKind][entityName]`
122
+ * addresses any entity.
123
+ */
124
+ export abstract class SqlNamespaceBase extends NamespaceBase implements SqlNamespace {
125
+ abstract override readonly id: string;
126
+ abstract override readonly entries: SqlNamespaceEntries;
127
+
128
+ abstract qualifyTable(tableName: string): string;
129
+ }
130
+
131
+ /**
132
+ * Realm-safe guard for hydrated `SqlNamespaceBase` concretions. Checks
133
+ * `qualifyTable` structurally instead of `instanceof NamespaceBase`, so it
134
+ * survives duplicate-module boundaries (e.g. dist e2e where the target and
135
+ * the family carry separate copies of `@prisma-next/framework-components`).
136
+ *
137
+ * Every concrete `SqlNamespaceBase` subclass (`PostgresSchema`, `SqliteDatabase`,
138
+ * `TestSqlNamespace`, …) implements `qualifyTable`. Raw `SqlNamespaceInput`
139
+ * objects (`{ id, entries }`) do not.
140
+ */
141
+ export function isMaterializedSqlNamespace(x: unknown): x is SqlNamespaceBase {
142
+ if (typeof x !== 'object' || x === null || !('qualifyTable' in x)) return false;
143
+ return typeof x.qualifyTable === 'function';
144
+ }
82
145
 
83
146
  export class SqlStorage<THash extends string = string> extends SqlNode implements Storage {
84
147
  readonly storageHash: StorageHashBase<THash>;
@@ -128,7 +191,7 @@ function normaliseTypeEntry(name: string, entry: SqlStorageTypeEntry): StorageTy
128
191
  }
129
192
  return toStorageTypeInstance(entry);
130
193
  }
131
- const rawKind = (entry as { kind?: unknown }).kind;
194
+ const rawKind = isPlainRecord(entry) ? entry['kind'] : undefined;
132
195
  const kindDescription =
133
196
  rawKind === undefined
134
197
  ? 'missing `kind` discriminator'
@@ -16,6 +16,7 @@ export interface StorageColumnInput {
16
16
  readonly nativeType: string;
17
17
  readonly codecId: string;
18
18
  readonly nullable: boolean;
19
+ readonly many?: boolean;
19
20
  readonly typeParams?: Record<string, unknown>;
20
21
  readonly typeRef?: string;
21
22
  readonly default?: ColumnDefault;
@@ -40,6 +41,7 @@ export class StorageColumn extends SqlNode {
40
41
  readonly nativeType: string;
41
42
  readonly codecId: string;
42
43
  readonly nullable: boolean;
44
+ declare readonly many?: boolean;
43
45
  declare readonly typeParams?: Record<string, unknown>;
44
46
  declare readonly typeRef?: string;
45
47
  declare readonly default?: ColumnDefault;
@@ -51,6 +53,7 @@ export class StorageColumn extends SqlNode {
51
53
  this.nativeType = input.nativeType;
52
54
  this.codecId = input.codecId;
53
55
  this.nullable = input.nullable;
56
+ if (input.many !== undefined) this.many = input.many;
54
57
  if (input.typeParams !== undefined) this.typeParams = input.typeParams;
55
58
  if (input.typeRef !== undefined) this.typeRef = input.typeRef;
56
59
  if (input.default !== undefined) this.default = input.default;
@@ -39,6 +39,7 @@ const StorageColumnSchema = type({
39
39
  nativeType: 'string',
40
40
  codecId: 'string',
41
41
  nullable: 'boolean',
42
+ 'many?': 'boolean',
42
43
  'typeParams?': 'Record<string, unknown>',
43
44
  'typeRef?': 'string',
44
45
  'default?': ColumnDefaultSchema,
@@ -68,4 +68,25 @@ export class StorageTable extends SqlNode {
68
68
  }
69
69
  freezeNode(this);
70
70
  }
71
+
72
+ /**
73
+ * Runtime guard that a namespace `table` entry is really a `StorageTable`.
74
+ * The compiler already types the entry as `StorageTable`, but a
75
+ * freshly-deserialized contract may carry plain JSON at that slot until
76
+ * hydration; this duck-types the structural shape. Accepts `undefined` so
77
+ * optional-chained entry lookups pass straight through.
78
+ */
79
+ static is(value: StorageTable | undefined): value is StorageTable {
80
+ if (typeof value !== 'object' || value === null) return false;
81
+ return 'columns' in value && 'uniques' in value && 'indexes' in value && 'foreignKeys' in value;
82
+ }
83
+
84
+ static assert(
85
+ value: StorageTable | undefined,
86
+ coordinate: string,
87
+ ): asserts value is StorageTable {
88
+ if (!StorageTable.is(value)) {
89
+ throw new Error(`Expected a StorageTable at ${coordinate}`);
90
+ }
91
+ }
71
92
  }
@@ -41,3 +41,8 @@ export class StorageValueSet extends SqlNode {
41
41
  freezeNode(this);
42
42
  }
43
43
  }
44
+
45
+ export function isStorageValueSet(value: unknown): value is StorageValueSet {
46
+ if (typeof value !== 'object' || value === null) return false;
47
+ return 'kind' in value && value.kind === 'valueSet' && 'values' in value;
48
+ }
package/src/types.ts CHANGED
@@ -10,7 +10,6 @@ export interface SqlControlDriverInstance<T extends string = string>
10
10
  ): Promise<{ readonly rows: Row[] }>;
11
11
  }
12
12
 
13
- export { buildSqlNamespace, buildSqlNamespaceMap } from './ir/build-sql-namespace';
14
13
  export { CheckConstraint, type CheckConstraintInput } from './ir/check-constraint';
15
14
  export {
16
15
  ForeignKey,
@@ -25,14 +24,18 @@ export { PrimaryKey, type PrimaryKeyInput } from './ir/primary-key';
25
24
  export { Index, type IndexInput } from './ir/sql-index';
26
25
  export { SqlNode } from './ir/sql-node';
27
26
  export {
27
+ isMaterializedSqlNamespace,
28
+ isSqlAuthoringContributions,
29
+ type SqlAuthoringContributions,
28
30
  type SqlNamespace,
31
+ SqlNamespaceBase,
29
32
  type SqlNamespaceEntries,
30
- type SqlNamespaceTablesInput,
33
+ type SqlNamespaceFactory,
34
+ type SqlNamespaceInput,
31
35
  SqlStorage,
32
36
  type SqlStorageInput,
33
37
  type SqlStorageTypeEntry,
34
38
  } from './ir/sql-storage';
35
- export { SqlUnboundNamespace } from './ir/sql-unbound-namespace';
36
39
  export { StorageColumn, type StorageColumnInput } from './ir/storage-column';
37
40
  export { StorageTable, type StorageTableInput } from './ir/storage-table';
38
41
  export {
@@ -42,7 +45,11 @@ export {
42
45
  type StorageTypeInstanceInput,
43
46
  toStorageTypeInstance,
44
47
  } from './ir/storage-type-instance';
45
- export { StorageValueSet, type StorageValueSetInput } from './ir/storage-value-set';
48
+ export {
49
+ isStorageValueSet,
50
+ StorageValueSet,
51
+ type StorageValueSetInput,
52
+ } from './ir/storage-value-set';
46
53
  export {
47
54
  UniqueConstraint,
48
55
  type UniqueConstraintInput,
@@ -83,16 +90,25 @@ export function applyFkDefaults(
83
90
  // Shared by the output and input field-type maps and their extractors.
84
91
  export type NamespacedFieldTypeMap = Record<string, Record<string, Record<string, unknown>>>;
85
92
 
93
+ export type NamespacedStorageColumnTypeMap = Record<
94
+ string,
95
+ Record<string, Record<string, unknown>>
96
+ >;
97
+
86
98
  export type TypeMaps<
87
99
  TCodecTypes extends Record<string, { output: unknown }> = Record<string, never>,
88
100
  TQueryOperationTypes extends Record<string, unknown> = Record<string, never>,
89
101
  TFieldOutputTypes extends NamespacedFieldTypeMap = Record<string, never>,
90
102
  TFieldInputTypes extends NamespacedFieldTypeMap = Record<string, never>,
103
+ TStorageColumnTypes extends NamespacedStorageColumnTypeMap = Record<string, never>,
104
+ TStorageColumnInputTypes extends NamespacedStorageColumnTypeMap = Record<string, never>,
91
105
  > = {
92
106
  readonly codecTypes: TCodecTypes;
93
107
  readonly queryOperationTypes: TQueryOperationTypes;
94
108
  readonly fieldOutputTypes: TFieldOutputTypes;
95
109
  readonly fieldInputTypes: TFieldInputTypes;
110
+ readonly storageColumnTypes: TStorageColumnTypes;
111
+ readonly storageColumnInputTypes: TStorageColumnInputTypes;
96
112
  };
97
113
 
98
114
  export type CodecTypesOf<T> = [T] extends [never]
@@ -107,12 +123,19 @@ export type CodecTypesOf<T> = [T] extends [never]
107
123
  * Dispatch hint identifying the first-argument target of an operation.
108
124
  *
109
125
  * Used by ORM column helpers to decide whether an operation is reachable on a
110
- * field. Either names a concrete codec identity or a set of capability traits
111
- * that the field's codec must carry.
126
+ * field. Names a concrete codec identity, a set of capability traits the
127
+ * field's codec must carry, or targets list-typed (`many`) fields. Element
128
+ * capability gating for list ops travels in `elementTraits`.
112
129
  */
113
130
  export type QueryOperationSelfSpec =
114
- | { readonly codecId: string; readonly traits?: never }
115
- | { readonly traits: readonly CodecTrait[]; readonly codecId?: never };
131
+ | { readonly codecId: string; readonly traits?: never; readonly many?: never }
132
+ | { readonly traits: readonly CodecTrait[]; readonly codecId?: never; readonly many?: never }
133
+ | {
134
+ readonly many: true;
135
+ readonly elementTraits?: readonly CodecTrait[];
136
+ readonly codecId?: never;
137
+ readonly traits?: never;
138
+ };
116
139
 
117
140
  /**
118
141
  * Structural shape an operation's impl must return: any value carrying a
@@ -172,10 +195,30 @@ export type FieldInputTypesOf<T> = [T] extends [never]
172
195
  : Record<string, never>
173
196
  : Record<string, never>;
174
197
 
198
+ export type StorageColumnTypesOf<T> = [T] extends [never]
199
+ ? Record<string, never>
200
+ : T extends { readonly storageColumnTypes: infer F }
201
+ ? F extends NamespacedStorageColumnTypeMap
202
+ ? F
203
+ : Record<string, never>
204
+ : Record<string, never>;
205
+
206
+ export type StorageColumnInputTypesOf<T> = [T] extends [never]
207
+ ? Record<string, never>
208
+ : T extends { readonly storageColumnInputTypes: infer F }
209
+ ? F extends NamespacedStorageColumnTypeMap
210
+ ? F
211
+ : Record<string, never>
212
+ : Record<string, never>;
213
+
175
214
  export type ExtractCodecTypes<T> = CodecTypesOf<ExtractTypeMapsFromContract<T>>;
176
215
  export type ExtractQueryOperationTypes<T> = QueryOperationTypesOf<ExtractTypeMapsFromContract<T>>;
177
216
  export type ExtractFieldOutputTypes<T> = FieldOutputTypesOf<ExtractTypeMapsFromContract<T>>;
178
217
  export type ExtractFieldInputTypes<T> = FieldInputTypesOf<ExtractTypeMapsFromContract<T>>;
218
+ export type ExtractStorageColumnTypes<T> = StorageColumnTypesOf<ExtractTypeMapsFromContract<T>>;
219
+ export type ExtractStorageColumnInputTypes<T> = StorageColumnInputTypesOf<
220
+ ExtractTypeMapsFromContract<T>
221
+ >;
179
222
 
180
223
  export type ResolveCodecTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never]
181
224
  ? ExtractCodecTypes<TContract>