@prisma-next/sql-contract 0.3.0-dev.13 → 0.3.0-dev.130

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 (60) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +84 -10
  3. package/dist/factories.d.mts +48 -0
  4. package/dist/factories.d.mts.map +1 -0
  5. package/dist/factories.mjs +84 -0
  6. package/dist/factories.mjs.map +1 -0
  7. package/dist/pack-types.d.mts +13 -0
  8. package/dist/pack-types.d.mts.map +1 -0
  9. package/dist/pack-types.mjs +1 -0
  10. package/dist/types-CB821Pqa.d.mts +197 -0
  11. package/dist/types-CB821Pqa.d.mts.map +1 -0
  12. package/dist/types-DRR5stkj.mjs +13 -0
  13. package/dist/types-DRR5stkj.mjs.map +1 -0
  14. package/dist/types.d.mts +2 -0
  15. package/dist/types.mjs +3 -0
  16. package/dist/validate.d.mts +11 -0
  17. package/dist/validate.d.mts.map +1 -0
  18. package/dist/validate.mjs +437 -0
  19. package/dist/validate.mjs.map +1 -0
  20. package/dist/validators-CNxeypbZ.mjs +234 -0
  21. package/dist/validators-CNxeypbZ.mjs.map +1 -0
  22. package/dist/validators.d.mts +71 -0
  23. package/dist/validators.d.mts.map +1 -0
  24. package/dist/validators.mjs +3 -0
  25. package/package.json +21 -25
  26. package/src/construct.ts +181 -0
  27. package/src/exports/types.ts +21 -0
  28. package/src/exports/validate.ts +6 -0
  29. package/src/exports/validators.ts +1 -1
  30. package/src/factories.ts +41 -8
  31. package/src/index.ts +1 -0
  32. package/src/types.ts +176 -9
  33. package/src/validate.ts +560 -0
  34. package/src/validators.ts +184 -18
  35. package/dist/exports/factories.d.ts +0 -2
  36. package/dist/exports/factories.d.ts.map +0 -1
  37. package/dist/exports/factories.js +0 -83
  38. package/dist/exports/factories.js.map +0 -1
  39. package/dist/exports/pack-types.d.ts +0 -2
  40. package/dist/exports/pack-types.d.ts.map +0 -1
  41. package/dist/exports/pack-types.js +0 -1
  42. package/dist/exports/pack-types.js.map +0 -1
  43. package/dist/exports/types.d.ts +0 -2
  44. package/dist/exports/types.d.ts.map +0 -1
  45. package/dist/exports/types.js +0 -1
  46. package/dist/exports/types.js.map +0 -1
  47. package/dist/exports/validators.d.ts +0 -2
  48. package/dist/exports/validators.d.ts.map +0 -1
  49. package/dist/exports/validators.js +0 -96
  50. package/dist/exports/validators.js.map +0 -1
  51. package/dist/factories.d.ts +0 -38
  52. package/dist/factories.d.ts.map +0 -1
  53. package/dist/index.d.ts +0 -4
  54. package/dist/index.d.ts.map +0 -1
  55. package/dist/pack-types.d.ts +0 -10
  56. package/dist/pack-types.d.ts.map +0 -1
  57. package/dist/types.d.ts +0 -68
  58. package/dist/types.d.ts.map +0 -1
  59. package/dist/validators.d.ts +0 -35
  60. package/dist/validators.d.ts.map +0 -1
package/src/factories.ts CHANGED
@@ -1,5 +1,11 @@
1
+ import type {
2
+ ExecutionHashBase,
3
+ ProfileHashBase,
4
+ StorageHashBase,
5
+ } from '@prisma-next/contract/types';
1
6
  import type {
2
7
  ForeignKey,
8
+ ForeignKeyOptions,
3
9
  ForeignKeyReferences,
4
10
  Index,
5
11
  ModelDefinition,
@@ -13,6 +19,7 @@ import type {
13
19
  StorageTable,
14
20
  UniqueConstraint,
15
21
  } from './types';
22
+ import { applyFkDefaults } from './types';
16
23
 
17
24
  /**
18
25
  * Creates a StorageColumn with nativeType and codecId.
@@ -52,16 +59,20 @@ export function fk(
52
59
  columns: readonly string[],
53
60
  refTable: string,
54
61
  refColumns: readonly string[],
55
- name?: string,
62
+ opts?: ForeignKeyOptions & { constraint?: boolean; index?: boolean },
56
63
  ): ForeignKey {
57
64
  const references: ForeignKeyReferences = {
58
65
  table: refTable,
59
66
  columns: refColumns,
60
67
  };
68
+
61
69
  return {
62
70
  columns,
63
71
  references,
64
- ...(name !== undefined && { name }),
72
+ ...(opts?.name !== undefined && { name: opts.name }),
73
+ ...(opts?.onDelete !== undefined && { onDelete: opts.onDelete }),
74
+ ...(opts?.onUpdate !== undefined && { onUpdate: opts.onUpdate }),
75
+ ...applyFkDefaults({ constraint: opts?.constraint, index: opts?.index }),
65
76
  };
66
77
  }
67
78
 
@@ -100,26 +111,40 @@ export function storage(tables: Record<string, StorageTable>): SqlStorage {
100
111
  return { tables };
101
112
  }
102
113
 
103
- export function contract(opts: {
114
+ export function contract<
115
+ TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,
116
+ TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,
117
+ TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,
118
+ >(opts: {
104
119
  target: string;
105
- coreHash: string;
120
+ storageHash: TStorageHash;
121
+ executionHash?: TExecutionHash;
106
122
  storage: SqlStorage;
107
123
  models?: Record<string, ModelDefinition>;
108
124
  relations?: Record<string, unknown>;
109
125
  mappings?: Partial<SqlMappings>;
110
126
  schemaVersion?: '1';
111
127
  targetFamily?: 'sql';
112
- profileHash?: string;
128
+ profileHash?: TProfileHash;
113
129
  capabilities?: Record<string, Record<string, boolean>>;
114
130
  extensionPacks?: Record<string, unknown>;
115
131
  meta?: Record<string, unknown>;
116
132
  sources?: Record<string, unknown>;
117
- }): SqlContract {
133
+ }): SqlContract<
134
+ SqlStorage,
135
+ Record<string, unknown>,
136
+ Record<string, unknown>,
137
+ SqlMappings,
138
+ TStorageHash,
139
+ TExecutionHash,
140
+ TProfileHash
141
+ > {
118
142
  return {
119
143
  schemaVersion: opts.schemaVersion ?? '1',
120
144
  target: opts.target,
121
145
  targetFamily: opts.targetFamily ?? 'sql',
122
- coreHash: opts.coreHash,
146
+ storageHash: opts.storageHash,
147
+ ...(opts.executionHash !== undefined && { executionHash: opts.executionHash }),
123
148
  storage: opts.storage,
124
149
  models: opts.models ?? {},
125
150
  relations: opts.relations ?? {},
@@ -129,5 +154,13 @@ export function contract(opts: {
129
154
  ...(opts.extensionPacks !== undefined && { extensionPacks: opts.extensionPacks }),
130
155
  ...(opts.meta !== undefined && { meta: opts.meta }),
131
156
  ...(opts.sources !== undefined && { sources: opts.sources as Record<string, unknown> }),
132
- } as SqlContract;
157
+ } as SqlContract<
158
+ SqlStorage,
159
+ Record<string, unknown>,
160
+ Record<string, unknown>,
161
+ SqlMappings,
162
+ TStorageHash,
163
+ TExecutionHash,
164
+ TProfileHash
165
+ >;
133
166
  }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './exports/factories';
2
2
  export * from './exports/types';
3
+ export * from './exports/validate';
3
4
  export * from './exports/validators';
package/src/types.ts CHANGED
@@ -1,9 +1,40 @@
1
- import type { ContractBase } from '@prisma-next/contract/types';
1
+ import type {
2
+ ColumnDefault,
3
+ ContractBase,
4
+ DomainRelationOn,
5
+ ExecutionHashBase,
6
+ ExecutionSection,
7
+ ProfileHashBase,
8
+ StorageHashBase,
9
+ } from '@prisma-next/contract/types';
2
10
 
11
+ /**
12
+ * A column definition in storage.
13
+ *
14
+ * `typeParams` is optional because most columns use non-parameterized types.
15
+ * Columns with parameterized types can either inline `typeParams` or reference
16
+ * a named {@link StorageTypeInstance} via `typeRef`.
17
+ */
3
18
  export type StorageColumn = {
4
19
  readonly nativeType: string;
5
20
  readonly codecId: string;
6
21
  readonly nullable: boolean;
22
+ /**
23
+ * Opaque, codec-owned JS/type parameters.
24
+ * The codec that owns `codecId` defines the shape and semantics.
25
+ * Mutually exclusive with `typeRef`.
26
+ */
27
+ readonly typeParams?: Record<string, unknown>;
28
+ /**
29
+ * Reference to a named type instance in `storage.types`.
30
+ * Mutually exclusive with `typeParams`.
31
+ */
32
+ readonly typeRef?: string;
33
+ /**
34
+ * Default value for the column.
35
+ * Can be a literal value or database function.
36
+ */
37
+ readonly default?: ColumnDefault;
7
38
  };
8
39
 
9
40
  export type PrimaryKey = {
@@ -19,6 +50,16 @@ export type UniqueConstraint = {
19
50
  export type Index = {
20
51
  readonly columns: readonly string[];
21
52
  readonly name?: string;
53
+ /**
54
+ * Optional access method identifier.
55
+ * Extension-specific methods are represented as strings and interpreted
56
+ * by the owning extension package.
57
+ */
58
+ readonly using?: string;
59
+ /**
60
+ * Optional extension-owned index configuration payload.
61
+ */
62
+ readonly config?: Record<string, unknown>;
22
63
  };
23
64
 
24
65
  export type ForeignKeyReferences = {
@@ -26,10 +67,24 @@ export type ForeignKeyReferences = {
26
67
  readonly columns: readonly string[];
27
68
  };
28
69
 
70
+ export type ReferentialAction = 'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault';
71
+
72
+ export type ForeignKeyOptions = {
73
+ readonly name?: string;
74
+ readonly onDelete?: ReferentialAction;
75
+ readonly onUpdate?: ReferentialAction;
76
+ };
77
+
29
78
  export type ForeignKey = {
30
79
  readonly columns: readonly string[];
31
80
  readonly references: ForeignKeyReferences;
32
81
  readonly name?: string;
82
+ readonly onDelete?: ReferentialAction;
83
+ readonly onUpdate?: ReferentialAction;
84
+ /** Whether to emit FK constraint DDL (ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY). */
85
+ readonly constraint: boolean;
86
+ /** Whether to emit a backing index for the FK columns. */
87
+ readonly index: boolean;
33
88
  };
34
89
 
35
90
  export type StorageTable = {
@@ -40,8 +95,29 @@ export type StorageTable = {
40
95
  readonly foreignKeys: ReadonlyArray<ForeignKey>;
41
96
  };
42
97
 
98
+ /**
99
+ * A named, parameterized type instance.
100
+ * These are registered in `storage.types` for reuse across columns
101
+ * and to enable ergonomic schema surfaces like `schema.types.MyType`.
102
+ *
103
+ * Unlike {@link StorageColumn}, `typeParams` is required here because
104
+ * `StorageTypeInstance` exists specifically to define reusable parameterized types.
105
+ * A type instance without parameters would be redundant—columns can reference
106
+ * the codec directly via `codecId`.
107
+ */
108
+ export type StorageTypeInstance = {
109
+ readonly codecId: string;
110
+ readonly nativeType: string;
111
+ readonly typeParams: Record<string, unknown>;
112
+ };
113
+
43
114
  export type SqlStorage = {
44
115
  readonly tables: Record<string, StorageTable>;
116
+ /**
117
+ * Named type instances for parameterized/custom types.
118
+ * Columns can reference these via `typeRef`.
119
+ */
120
+ readonly types?: Record<string, StorageTypeInstance>;
45
121
  };
46
122
 
47
123
  export type ModelField = {
@@ -56,6 +132,24 @@ export type ModelDefinition = {
56
132
  readonly storage: ModelStorage;
57
133
  readonly fields: Record<string, ModelField>;
58
134
  readonly relations: Record<string, unknown>;
135
+ readonly owner?: string;
136
+ };
137
+
138
+ export type SqlModelFieldStorage = {
139
+ readonly column: string;
140
+ readonly codecId?: string;
141
+ readonly nullable?: boolean;
142
+ };
143
+
144
+ export type SqlModelStorage = {
145
+ readonly table: string;
146
+ readonly fields: Record<string, SqlModelFieldStorage>;
147
+ };
148
+
149
+ export type SqlRelation = {
150
+ readonly to: string;
151
+ readonly cardinality: '1:1' | '1:N' | 'N:1';
152
+ readonly on: DomainRelationOn;
59
153
  };
60
154
 
61
155
  export type SqlMappings = {
@@ -63,8 +157,68 @@ export type SqlMappings = {
63
157
  readonly tableToModel?: Record<string, string>;
64
158
  readonly fieldToColumn?: Record<string, Record<string, string>>;
65
159
  readonly columnToField?: Record<string, Record<string, string>>;
66
- readonly codecTypes: Record<string, { readonly output: unknown }>;
67
- readonly operationTypes: Record<string, Record<string, unknown>>;
160
+ };
161
+
162
+ export const DEFAULT_FK_CONSTRAINT = true;
163
+ export const DEFAULT_FK_INDEX = true;
164
+
165
+ export function applyFkDefaults(
166
+ fk: { constraint?: boolean | undefined; index?: boolean | undefined },
167
+ overrideDefaults?: { constraint?: boolean | undefined; index?: boolean | undefined },
168
+ ): { constraint: boolean; index: boolean } {
169
+ return {
170
+ constraint: fk.constraint ?? overrideDefaults?.constraint ?? DEFAULT_FK_CONSTRAINT,
171
+ index: fk.index ?? overrideDefaults?.index ?? DEFAULT_FK_INDEX,
172
+ };
173
+ }
174
+
175
+ export type TypeMaps<
176
+ TCodecTypes extends Record<string, { output: unknown }> = Record<string, never>,
177
+ TOperationTypes extends Record<string, unknown> = Record<string, never>,
178
+ TQueryOperationTypes extends Record<string, unknown> = Record<string, never>,
179
+ > = {
180
+ readonly codecTypes: TCodecTypes;
181
+ readonly operationTypes: TOperationTypes;
182
+ readonly queryOperationTypes: TQueryOperationTypes;
183
+ };
184
+
185
+ export type CodecTypesOf<T> = [T] extends [never]
186
+ ? Record<string, never>
187
+ : T extends { readonly codecTypes: infer C }
188
+ ? C extends Record<string, { output: unknown }>
189
+ ? C
190
+ : Record<string, never>
191
+ : Record<string, never>;
192
+
193
+ export type OperationTypesOf<T> = [T] extends [never]
194
+ ? Record<string, never>
195
+ : T extends { readonly operationTypes: infer O }
196
+ ? O extends Record<string, unknown>
197
+ ? O
198
+ : Record<string, never>
199
+ : Record<string, never>;
200
+
201
+ export type QueryOperationTypeEntry = {
202
+ readonly args: readonly { readonly codecId: string; readonly nullable: boolean }[];
203
+ readonly returns: { readonly codecId: string; readonly nullable: boolean };
204
+ };
205
+
206
+ export type SqlQueryOperationTypes<T extends Record<string, QueryOperationTypeEntry>> = T;
207
+
208
+ export type QueryOperationTypesBase = Record<string, QueryOperationTypeEntry>;
209
+
210
+ export type QueryOperationTypesOf<T> = [T] extends [never]
211
+ ? Record<string, never>
212
+ : T extends { readonly queryOperationTypes: infer Q }
213
+ ? Q extends Record<string, unknown>
214
+ ? Q
215
+ : Record<string, never>
216
+ : Record<string, never>;
217
+
218
+ export type TypeMapsPhantomKey = '__@prisma-next/sql-contract/typeMaps@__';
219
+
220
+ export type ContractWithTypeMaps<TContract, TTypeMaps> = TContract & {
221
+ readonly [K in TypeMapsPhantomKey]?: TTypeMaps;
68
222
  };
69
223
 
70
224
  export type SqlContract<
@@ -72,16 +226,29 @@ export type SqlContract<
72
226
  M extends Record<string, unknown> = Record<string, unknown>,
73
227
  R extends Record<string, unknown> = Record<string, unknown>,
74
228
  Map extends SqlMappings = SqlMappings,
75
- > = ContractBase & {
229
+ TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,
230
+ TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,
231
+ TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,
232
+ > = ContractBase<TStorageHash, TExecutionHash, TProfileHash, M> & {
76
233
  readonly targetFamily: string;
77
234
  readonly storage: S;
78
- readonly models: M;
79
235
  readonly relations: R;
80
236
  readonly mappings: Map;
237
+ readonly execution?: ExecutionSection;
81
238
  };
82
239
 
83
- export type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> =
84
- TContract['mappings']['codecTypes'];
240
+ export type ExtractTypeMapsFromContract<T> = TypeMapsPhantomKey extends keyof T
241
+ ? NonNullable<T[TypeMapsPhantomKey & keyof T]>
242
+ : never;
243
+
244
+ export type ExtractCodecTypes<T> = CodecTypesOf<ExtractTypeMapsFromContract<T>>;
245
+ export type ExtractOperationTypes<T> = OperationTypesOf<ExtractTypeMapsFromContract<T>>;
246
+ export type ExtractQueryOperationTypes<T> = QueryOperationTypesOf<ExtractTypeMapsFromContract<T>>;
247
+
248
+ export type ResolveCodecTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never]
249
+ ? ExtractCodecTypes<TContract>
250
+ : CodecTypesOf<TTypeMaps>;
85
251
 
86
- export type ExtractOperationTypes<TContract extends SqlContract<SqlStorage>> =
87
- TContract['mappings']['operationTypes'];
252
+ export type ResolveOperationTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never]
253
+ ? ExtractOperationTypes<TContract>
254
+ : OperationTypesOf<TTypeMaps>;