@prisma-next/sql-contract 0.3.0-pr.99.5 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +80 -11
  3. package/dist/factories.d.mts +29 -0
  4. package/dist/factories.d.mts.map +1 -0
  5. package/dist/factories.mjs +64 -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-BYQMEXGG.d.mts +176 -0
  11. package/dist/types-BYQMEXGG.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 +9 -0
  17. package/dist/validate.d.mts.map +1 -0
  18. package/dist/validate.mjs +107 -0
  19. package/dist/validate.mjs.map +1 -0
  20. package/dist/validators-BjZ6lOS1.mjs +281 -0
  21. package/dist/validators-BjZ6lOS1.mjs.map +1 -0
  22. package/dist/validators.d.mts +61 -0
  23. package/dist/validators.d.mts.map +1 -0
  24. package/dist/validators.mjs +3 -0
  25. package/package.json +18 -21
  26. package/src/exports/factories.ts +1 -11
  27. package/src/exports/types.ts +23 -6
  28. package/src/exports/validate.ts +1 -0
  29. package/src/exports/validators.ts +1 -1
  30. package/src/factories.ts +29 -57
  31. package/src/index.ts +1 -0
  32. package/src/types.ts +126 -31
  33. package/src/validate.ts +227 -0
  34. package/src/validators.ts +296 -64
  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 -109
  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 -106
  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/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "@prisma-next/sql-contract",
3
- "version": "0.3.0-pr.99.5",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "SQL contract types, validators, and IR factories for Prisma Next",
7
7
  "dependencies": {
8
8
  "arktype": "^2.1.25",
9
- "@prisma-next/contract": "0.3.0-pr.99.5"
9
+ "@prisma-next/contract": "0.3.0",
10
+ "@prisma-next/framework-components": "0.3.0"
10
11
  },
11
12
  "devDependencies": {
12
- "tsup": "8.5.1",
13
+ "tsdown": "0.18.4",
13
14
  "typescript": "5.9.3",
14
- "vitest": "4.0.16",
15
+ "vitest": "4.0.17",
15
16
  "@prisma-next/test-utils": "0.0.1",
17
+ "@prisma-next/tsdown": "0.0.0",
16
18
  "@prisma-next/tsconfig": "0.0.0"
17
19
  },
18
20
  "files": [
@@ -20,25 +22,20 @@
20
22
  "src"
21
23
  ],
22
24
  "exports": {
23
- "./types": {
24
- "types": "./dist/exports/types.d.ts",
25
- "import": "./dist/exports/types.js"
26
- },
27
- "./validators": {
28
- "types": "./dist/exports/validators.d.ts",
29
- "import": "./dist/exports/validators.js"
30
- },
31
- "./factories": {
32
- "types": "./dist/exports/factories.d.ts",
33
- "import": "./dist/exports/factories.js"
34
- },
35
- "./pack-types": {
36
- "types": "./dist/exports/pack-types.d.ts",
37
- "import": "./dist/exports/pack-types.js"
38
- }
25
+ "./factories": "./dist/factories.mjs",
26
+ "./pack-types": "./dist/pack-types.mjs",
27
+ "./types": "./dist/types.mjs",
28
+ "./validate": "./dist/validate.mjs",
29
+ "./validators": "./dist/validators.mjs",
30
+ "./package.json": "./package.json"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/prisma/prisma-next.git",
35
+ "directory": "packages/2-sql/1-core/contract"
39
36
  },
40
37
  "scripts": {
41
- "build": "tsup --config tsup.config.ts && tsc --project tsconfig.build.json",
38
+ "build": "tsdown",
42
39
  "test": "vitest run",
43
40
  "test:coverage": "vitest run --coverage",
44
41
  "typecheck": "tsc --noEmit",
@@ -1,11 +1 @@
1
- export {
2
- col,
3
- contract,
4
- fk,
5
- index,
6
- model,
7
- pk,
8
- storage,
9
- table,
10
- unique,
11
- } from '../factories';
1
+ export { col, fk, index, model, pk, table, unique } from '../factories';
@@ -1,18 +1,35 @@
1
1
  export type {
2
+ CodecTypesOf,
3
+ ContractWithTypeMaps,
2
4
  ExtractCodecTypes,
3
- ExtractOperationTypes,
5
+ ExtractFieldInputTypes,
6
+ ExtractFieldOutputTypes,
7
+ ExtractQueryOperationTypes,
8
+ ExtractTypeMapsFromContract,
9
+ FieldInputTypesOf,
10
+ FieldOutputTypesOf,
4
11
  ForeignKey,
12
+ ForeignKeyOptions,
5
13
  ForeignKeyReferences,
6
14
  Index,
7
- ModelDefinition,
8
- ModelField,
9
- ModelStorage,
15
+ OperationTypesOf,
10
16
  PrimaryKey,
11
- SqlContract,
12
- SqlMappings,
17
+ QueryOperationTypeEntry,
18
+ QueryOperationTypesBase,
19
+ QueryOperationTypesOf,
20
+ ReferentialAction,
21
+ ResolveCodecTypes,
22
+ ResolveOperationTypes,
23
+ SqlModelFieldStorage,
24
+ SqlModelStorage,
25
+ SqlQueryOperationTypes,
13
26
  SqlStorage,
14
27
  StorageColumn,
15
28
  StorageTable,
16
29
  StorageTypeInstance,
30
+ TypeMaps,
31
+ TypeMapsPhantomKey,
17
32
  UniqueConstraint,
18
33
  } from '../types';
34
+
35
+ export { applyFkDefaults, DEFAULT_FK_CONSTRAINT, DEFAULT_FK_INDEX } from '../types';
@@ -0,0 +1 @@
1
+ export { validateContract } from '../validate';
@@ -1 +1 @@
1
- export { validateModel, validateSqlContract, validateStorage } from '../validators';
1
+ export * from '../validators';
package/src/factories.ts CHANGED
@@ -1,27 +1,18 @@
1
+ import type { ScalarFieldType } from '@prisma-next/contract/types';
1
2
  import type {
2
3
  ForeignKey,
4
+ ForeignKeyOptions,
3
5
  ForeignKeyReferences,
4
6
  Index,
5
- ModelDefinition,
6
- ModelField,
7
- ModelStorage,
8
7
  PrimaryKey,
9
- SqlContract,
10
- SqlMappings,
11
- SqlStorage,
8
+ SqlModelFieldStorage,
9
+ SqlModelStorage,
12
10
  StorageColumn,
13
11
  StorageTable,
14
12
  UniqueConstraint,
15
13
  } from './types';
14
+ import { applyFkDefaults } from './types';
16
15
 
17
- /**
18
- * Creates a StorageColumn with nativeType and codecId.
19
- *
20
- * @param nativeType - Native database type identifier (e.g., 'int4', 'text', 'vector')
21
- * @param codecId - Codec identifier (e.g., 'pg/int4@1', 'pg/text@1')
22
- * @param nullable - Whether the column is nullable (default: false)
23
- * @returns StorageColumn with nativeType and codecId
24
- */
25
16
  export function col(nativeType: string, codecId: string, nullable = false): StorageColumn {
26
17
  return {
27
18
  nativeType,
@@ -52,16 +43,20 @@ export function fk(
52
43
  columns: readonly string[],
53
44
  refTable: string,
54
45
  refColumns: readonly string[],
55
- name?: string,
46
+ opts?: ForeignKeyOptions & { constraint?: boolean; index?: boolean },
56
47
  ): ForeignKey {
57
48
  const references: ForeignKeyReferences = {
58
49
  table: refTable,
59
50
  columns: refColumns,
60
51
  };
52
+
61
53
  return {
62
54
  columns,
63
55
  references,
64
- ...(name !== undefined && { name }),
56
+ ...(opts?.name !== undefined && { name: opts.name }),
57
+ ...(opts?.onDelete !== undefined && { onDelete: opts.onDelete }),
58
+ ...(opts?.onUpdate !== undefined && { onUpdate: opts.onUpdate }),
59
+ ...applyFkDefaults({ constraint: opts?.constraint, index: opts?.index }),
65
60
  };
66
61
  }
67
62
 
@@ -84,50 +79,27 @@ export function table(
84
79
  }
85
80
 
86
81
  export function model(
87
- table: string,
88
- fields: Record<string, ModelField>,
82
+ tableName: string,
83
+ fields: Record<string, SqlModelFieldStorage>,
89
84
  relations: Record<string, unknown> = {},
90
- ): ModelDefinition {
91
- const storage: ModelStorage = { table };
85
+ ): {
86
+ storage: SqlModelStorage;
87
+ fields: Record<string, { readonly nullable: boolean; readonly type: ScalarFieldType }>;
88
+ relations: Record<string, unknown>;
89
+ } {
90
+ const storage: SqlModelStorage = { table: tableName, fields };
91
+ const domainFields = Object.fromEntries(
92
+ Object.entries(fields).map(([name, field]) => [
93
+ name,
94
+ {
95
+ nullable: field.nullable ?? false,
96
+ type: { kind: 'scalar' as const, codecId: field.codecId ?? 'core/unknown@1' },
97
+ },
98
+ ]),
99
+ ) as Record<string, { nullable: boolean; type: ScalarFieldType }>;
92
100
  return {
93
101
  storage,
94
- fields,
102
+ fields: domainFields,
95
103
  relations,
96
104
  };
97
105
  }
98
-
99
- export function storage(tables: Record<string, StorageTable>): SqlStorage {
100
- return { tables };
101
- }
102
-
103
- export function contract(opts: {
104
- target: string;
105
- coreHash: string;
106
- storage: SqlStorage;
107
- models?: Record<string, ModelDefinition>;
108
- relations?: Record<string, unknown>;
109
- mappings?: Partial<SqlMappings>;
110
- schemaVersion?: '1';
111
- targetFamily?: 'sql';
112
- profileHash?: string;
113
- capabilities?: Record<string, Record<string, boolean>>;
114
- extensionPacks?: Record<string, unknown>;
115
- meta?: Record<string, unknown>;
116
- sources?: Record<string, unknown>;
117
- }): SqlContract {
118
- return {
119
- schemaVersion: opts.schemaVersion ?? '1',
120
- target: opts.target,
121
- targetFamily: opts.targetFamily ?? 'sql',
122
- coreHash: opts.coreHash,
123
- storage: opts.storage,
124
- models: opts.models ?? {},
125
- relations: opts.relations ?? {},
126
- mappings: (opts.mappings ?? {}) as SqlMappings,
127
- ...(opts.profileHash !== undefined && { profileHash: opts.profileHash }),
128
- ...(opts.capabilities !== undefined && { capabilities: opts.capabilities }),
129
- ...(opts.extensionPacks !== undefined && { extensionPacks: opts.extensionPacks }),
130
- ...(opts.meta !== undefined && { meta: opts.meta }),
131
- ...(opts.sources !== undefined && { sources: opts.sources as Record<string, unknown> }),
132
- } as SqlContract;
133
- }
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,4 +1,4 @@
1
- import type { ContractBase } from '@prisma-next/contract/types';
1
+ import type { ColumnDefault, StorageBase } from '@prisma-next/contract/types';
2
2
 
3
3
  /**
4
4
  * A column definition in storage.
@@ -22,6 +22,11 @@ export type StorageColumn = {
22
22
  * Mutually exclusive with `typeParams`.
23
23
  */
24
24
  readonly typeRef?: string;
25
+ /**
26
+ * Default value for the column.
27
+ * Can be a literal value or database function.
28
+ */
29
+ readonly default?: ColumnDefault;
25
30
  };
26
31
 
27
32
  export type PrimaryKey = {
@@ -37,6 +42,16 @@ export type UniqueConstraint = {
37
42
  export type Index = {
38
43
  readonly columns: readonly string[];
39
44
  readonly name?: string;
45
+ /**
46
+ * Optional access method identifier.
47
+ * Extension-specific methods are represented as strings and interpreted
48
+ * by the owning extension package.
49
+ */
50
+ readonly using?: string;
51
+ /**
52
+ * Optional extension-owned index configuration payload.
53
+ */
54
+ readonly config?: Record<string, unknown>;
40
55
  };
41
56
 
42
57
  export type ForeignKeyReferences = {
@@ -44,10 +59,24 @@ export type ForeignKeyReferences = {
44
59
  readonly columns: readonly string[];
45
60
  };
46
61
 
62
+ export type ReferentialAction = 'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault';
63
+
64
+ export type ForeignKeyOptions = {
65
+ readonly name?: string;
66
+ readonly onDelete?: ReferentialAction;
67
+ readonly onUpdate?: ReferentialAction;
68
+ };
69
+
47
70
  export type ForeignKey = {
48
71
  readonly columns: readonly string[];
49
72
  readonly references: ForeignKeyReferences;
50
73
  readonly name?: string;
74
+ readonly onDelete?: ReferentialAction;
75
+ readonly onUpdate?: ReferentialAction;
76
+ /** Whether to emit FK constraint DDL (ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY). */
77
+ readonly constraint: boolean;
78
+ /** Whether to emit a backing index for the FK columns. */
79
+ readonly index: boolean;
51
80
  };
52
81
 
53
82
  export type StorageTable = {
@@ -74,7 +103,7 @@ export type StorageTypeInstance = {
74
103
  readonly typeParams: Record<string, unknown>;
75
104
  };
76
105
 
77
- export type SqlStorage = {
106
+ export type SqlStorage<THash extends string = string> = StorageBase<THash> & {
78
107
  readonly tables: Record<string, StorageTable>;
79
108
  /**
80
109
  * Named type instances for parameterized/custom types.
@@ -83,44 +112,110 @@ export type SqlStorage = {
83
112
  readonly types?: Record<string, StorageTypeInstance>;
84
113
  };
85
114
 
86
- export type ModelField = {
115
+ export type SqlModelFieldStorage = {
87
116
  readonly column: string;
117
+ readonly codecId?: string;
118
+ readonly nullable?: boolean;
88
119
  };
89
120
 
90
- export type ModelStorage = {
121
+ export type SqlModelStorage = {
91
122
  readonly table: string;
123
+ readonly fields: Record<string, SqlModelFieldStorage>;
92
124
  };
93
125
 
94
- export type ModelDefinition = {
95
- readonly storage: ModelStorage;
96
- readonly fields: Record<string, ModelField>;
97
- readonly relations: Record<string, unknown>;
126
+ export const DEFAULT_FK_CONSTRAINT = true;
127
+ export const DEFAULT_FK_INDEX = true;
128
+
129
+ export function applyFkDefaults(
130
+ fk: { constraint?: boolean | undefined; index?: boolean | undefined },
131
+ overrideDefaults?: { constraint?: boolean | undefined; index?: boolean | undefined },
132
+ ): { constraint: boolean; index: boolean } {
133
+ return {
134
+ constraint: fk.constraint ?? overrideDefaults?.constraint ?? DEFAULT_FK_CONSTRAINT,
135
+ index: fk.index ?? overrideDefaults?.index ?? DEFAULT_FK_INDEX,
136
+ };
137
+ }
138
+
139
+ export type TypeMaps<
140
+ TCodecTypes extends Record<string, { output: unknown }> = Record<string, never>,
141
+ TOperationTypes extends Record<string, unknown> = Record<string, never>,
142
+ TQueryOperationTypes extends Record<string, unknown> = Record<string, never>,
143
+ TFieldOutputTypes extends Record<string, Record<string, unknown>> = Record<string, never>,
144
+ TFieldInputTypes extends Record<string, Record<string, unknown>> = Record<string, never>,
145
+ > = {
146
+ readonly codecTypes: TCodecTypes;
147
+ readonly operationTypes: TOperationTypes;
148
+ readonly queryOperationTypes: TQueryOperationTypes;
149
+ readonly fieldOutputTypes: TFieldOutputTypes;
150
+ readonly fieldInputTypes: TFieldInputTypes;
98
151
  };
99
152
 
100
- export type SqlMappings = {
101
- readonly modelToTable?: Record<string, string>;
102
- readonly tableToModel?: Record<string, string>;
103
- readonly fieldToColumn?: Record<string, Record<string, string>>;
104
- readonly columnToField?: Record<string, Record<string, string>>;
105
- readonly codecTypes: Record<string, { readonly output: unknown }>;
106
- readonly operationTypes: Record<string, Record<string, unknown>>;
153
+ export type CodecTypesOf<T> = [T] extends [never]
154
+ ? Record<string, never>
155
+ : T extends { readonly codecTypes: infer C }
156
+ ? C extends Record<string, { output: unknown }>
157
+ ? C
158
+ : Record<string, never>
159
+ : Record<string, never>;
160
+
161
+ export type OperationTypesOf<T> = [T] extends [never]
162
+ ? Record<string, never>
163
+ : T extends { readonly operationTypes: infer O }
164
+ ? O extends Record<string, unknown>
165
+ ? O
166
+ : Record<string, never>
167
+ : Record<string, never>;
168
+
169
+ export type QueryOperationTypeEntry = {
170
+ readonly args: readonly { readonly codecId: string; readonly nullable: boolean }[];
171
+ readonly returns: { readonly codecId: string; readonly nullable: boolean };
107
172
  };
108
173
 
109
- export type SqlContract<
110
- S extends SqlStorage = SqlStorage,
111
- M extends Record<string, unknown> = Record<string, unknown>,
112
- R extends Record<string, unknown> = Record<string, unknown>,
113
- Map extends SqlMappings = SqlMappings,
114
- > = ContractBase & {
115
- readonly targetFamily: string;
116
- readonly storage: S;
117
- readonly models: M;
118
- readonly relations: R;
119
- readonly mappings: Map;
120
- };
174
+ export type SqlQueryOperationTypes<T extends Record<string, QueryOperationTypeEntry>> = T;
175
+
176
+ export type QueryOperationTypesBase = Record<string, QueryOperationTypeEntry>;
121
177
 
122
- export type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> =
123
- TContract['mappings']['codecTypes'];
178
+ export type QueryOperationTypesOf<T> = [T] extends [never]
179
+ ? Record<string, never>
180
+ : T extends { readonly queryOperationTypes: infer Q }
181
+ ? Q extends Record<string, unknown>
182
+ ? Q
183
+ : Record<string, never>
184
+ : Record<string, never>;
185
+
186
+ export type TypeMapsPhantomKey = '__@prisma-next/sql-contract/typeMaps@__';
187
+
188
+ export type ContractWithTypeMaps<TContract, TTypeMaps> = TContract & {
189
+ readonly [K in TypeMapsPhantomKey]?: TTypeMaps;
190
+ };
124
191
 
125
- export type ExtractOperationTypes<TContract extends SqlContract<SqlStorage>> =
126
- TContract['mappings']['operationTypes'];
192
+ export type ExtractTypeMapsFromContract<T> = TypeMapsPhantomKey extends keyof T
193
+ ? NonNullable<T[TypeMapsPhantomKey & keyof T]>
194
+ : never;
195
+
196
+ export type FieldOutputTypesOf<T> = [T] extends [never]
197
+ ? Record<string, never>
198
+ : T extends { readonly fieldOutputTypes: infer F }
199
+ ? F extends Record<string, Record<string, unknown>>
200
+ ? F
201
+ : Record<string, never>
202
+ : Record<string, never>;
203
+
204
+ export type FieldInputTypesOf<T> = [T] extends [never]
205
+ ? Record<string, never>
206
+ : T extends { readonly fieldInputTypes: infer F }
207
+ ? F extends Record<string, Record<string, unknown>>
208
+ ? F
209
+ : Record<string, never>
210
+ : Record<string, never>;
211
+
212
+ export type ExtractCodecTypes<T> = CodecTypesOf<ExtractTypeMapsFromContract<T>>;
213
+ export type ExtractQueryOperationTypes<T> = QueryOperationTypesOf<ExtractTypeMapsFromContract<T>>;
214
+ export type ExtractFieldOutputTypes<T> = FieldOutputTypesOf<ExtractTypeMapsFromContract<T>>;
215
+ export type ExtractFieldInputTypes<T> = FieldInputTypesOf<ExtractTypeMapsFromContract<T>>;
216
+
217
+ export type ResolveCodecTypes<TContract, TTypeMaps> = [TTypeMaps] extends [never]
218
+ ? ExtractCodecTypes<TContract>
219
+ : CodecTypesOf<TTypeMaps>;
220
+
221
+ export type ResolveOperationTypes<_TContract, TTypeMaps> = OperationTypesOf<TTypeMaps>;