@prisma-next/sql-contract 0.3.0-dev.5 → 0.3.0-dev.51

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 (58) hide show
  1. package/README.md +79 -10
  2. package/dist/factories.d.mts +48 -0
  3. package/dist/factories.d.mts.map +1 -0
  4. package/dist/factories.mjs +84 -0
  5. package/dist/factories.mjs.map +1 -0
  6. package/dist/pack-types.d.mts +13 -0
  7. package/dist/pack-types.d.mts.map +1 -0
  8. package/dist/pack-types.mjs +1 -0
  9. package/dist/types-T6o5-ZB3.d.mts +145 -0
  10. package/dist/types-T6o5-ZB3.d.mts.map +1 -0
  11. package/dist/types-kacOgEya.mjs +17 -0
  12. package/dist/types-kacOgEya.mjs.map +1 -0
  13. package/dist/types.d.mts +2 -0
  14. package/dist/types.mjs +3 -0
  15. package/dist/validate.d.mts +11 -0
  16. package/dist/validate.d.mts.map +1 -0
  17. package/dist/validate.mjs +244 -0
  18. package/dist/validate.mjs.map +1 -0
  19. package/dist/validators-Dz93b38w.mjs +214 -0
  20. package/dist/validators-Dz93b38w.mjs.map +1 -0
  21. package/dist/validators.d.mts +65 -0
  22. package/dist/validators.d.mts.map +1 -0
  23. package/dist/validators.mjs +3 -0
  24. package/package.json +24 -28
  25. package/src/exports/types.ts +5 -0
  26. package/src/exports/validate.ts +6 -0
  27. package/src/exports/validators.ts +1 -1
  28. package/src/factories.ts +41 -8
  29. package/src/index.ts +1 -0
  30. package/src/types.ts +88 -2
  31. package/src/validate.ts +448 -0
  32. package/src/validators.ts +161 -10
  33. package/dist/exports/factories.d.ts +0 -2
  34. package/dist/exports/factories.d.ts.map +0 -1
  35. package/dist/exports/factories.js +0 -83
  36. package/dist/exports/factories.js.map +0 -1
  37. package/dist/exports/pack-types.d.ts +0 -2
  38. package/dist/exports/pack-types.d.ts.map +0 -1
  39. package/dist/exports/pack-types.js +0 -1
  40. package/dist/exports/pack-types.js.map +0 -1
  41. package/dist/exports/types.d.ts +0 -2
  42. package/dist/exports/types.d.ts.map +0 -1
  43. package/dist/exports/types.js +0 -1
  44. package/dist/exports/types.js.map +0 -1
  45. package/dist/exports/validators.d.ts +0 -2
  46. package/dist/exports/validators.d.ts.map +0 -1
  47. package/dist/exports/validators.js +0 -96
  48. package/dist/exports/validators.js.map +0 -1
  49. package/dist/factories.d.ts +0 -38
  50. package/dist/factories.d.ts.map +0 -1
  51. package/dist/index.d.ts +0 -4
  52. package/dist/index.d.ts.map +0 -1
  53. package/dist/pack-types.d.ts +0 -10
  54. package/dist/pack-types.d.ts.map +0 -1
  55. package/dist/types.d.ts +0 -68
  56. package/dist/types.d.ts.map +0 -1
  57. package/dist/validators.d.ts +0 -35
  58. package/dist/validators.d.ts.map +0 -1
package/package.json CHANGED
@@ -1,50 +1,46 @@
1
1
  {
2
2
  "name": "@prisma-next/sql-contract",
3
- "version": "0.3.0-dev.5",
3
+ "version": "0.3.0-dev.51",
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-dev.5"
9
+ "@prisma-next/contract": "0.3.0-dev.51"
10
10
  },
11
11
  "devDependencies": {
12
- "@vitest/coverage-v8": "4.0.16",
13
- "tsup": "8.5.1",
12
+ "tsdown": "0.18.4",
14
13
  "typescript": "5.9.3",
15
- "vitest": "4.0.16",
16
- "@prisma-next/test-utils": "0.0.1"
14
+ "vitest": "4.0.17",
15
+ "@prisma-next/tsconfig": "0.0.0",
16
+ "@prisma-next/test-utils": "0.0.1",
17
+ "@prisma-next/tsdown": "0.0.0"
17
18
  },
18
19
  "files": [
19
20
  "dist",
20
21
  "src"
21
22
  ],
22
23
  "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
- }
24
+ "./factories": "./dist/factories.mjs",
25
+ "./pack-types": "./dist/pack-types.mjs",
26
+ "./types": "./dist/types.mjs",
27
+ "./validate": "./dist/validate.mjs",
28
+ "./validators": "./dist/validators.mjs",
29
+ "./package.json": "./package.json"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/prisma/prisma-next.git",
34
+ "directory": "packages/2-sql/1-core/contract"
39
35
  },
40
36
  "scripts": {
41
- "build": "tsup --config tsup.config.ts && tsc --project tsconfig.build.json",
37
+ "build": "tsdown",
42
38
  "test": "vitest run",
43
39
  "test:coverage": "vitest run --coverage",
44
- "typecheck": "tsc --project tsconfig.json --noEmit",
45
- "lint": "biome check . --config-path ../../../../biome.json --error-on-warnings",
46
- "lint:fix": "biome check --write . --config-path ../../../biome.json",
47
- "lint:fix:unsafe": "biome check --write --unsafe . --config-path ../../../biome.json",
48
- "clean": "node ../../../../scripts/clean.mjs"
40
+ "typecheck": "tsc --noEmit",
41
+ "lint": "biome check . --error-on-warnings",
42
+ "lint:fix": "biome check --write .",
43
+ "lint:fix:unsafe": "biome check --write --unsafe .",
44
+ "clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
49
45
  }
50
46
  }
@@ -2,16 +2,21 @@ export type {
2
2
  ExtractCodecTypes,
3
3
  ExtractOperationTypes,
4
4
  ForeignKey,
5
+ ForeignKeyOptions,
5
6
  ForeignKeyReferences,
6
7
  Index,
7
8
  ModelDefinition,
8
9
  ModelField,
9
10
  ModelStorage,
10
11
  PrimaryKey,
12
+ ReferentialAction,
11
13
  SqlContract,
12
14
  SqlMappings,
13
15
  SqlStorage,
14
16
  StorageColumn,
15
17
  StorageTable,
18
+ StorageTypeInstance,
16
19
  UniqueConstraint,
17
20
  } from '../types';
21
+
22
+ export { applyFkDefaults, DEFAULT_FK_CONSTRAINT, DEFAULT_FK_INDEX } from '../types';
@@ -0,0 +1,6 @@
1
+ export {
2
+ decodeContractDefaults,
3
+ isBigIntColumn,
4
+ normalizeContract,
5
+ validateContract,
6
+ } from '../validate';
@@ -1 +1 @@
1
- export { validateModel, validateSqlContract, validateStorage } from '../validators';
1
+ export * from '../validators';
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,39 @@
1
- import type { ContractBase } from '@prisma-next/contract/types';
1
+ import type {
2
+ ColumnDefault,
3
+ ContractBase,
4
+ ExecutionHashBase,
5
+ ExecutionSection,
6
+ ProfileHashBase,
7
+ StorageHashBase,
8
+ } from '@prisma-next/contract/types';
2
9
 
10
+ /**
11
+ * A column definition in storage.
12
+ *
13
+ * `typeParams` is optional because most columns use non-parameterized types.
14
+ * Columns with parameterized types can either inline `typeParams` or reference
15
+ * a named {@link StorageTypeInstance} via `typeRef`.
16
+ */
3
17
  export type StorageColumn = {
4
18
  readonly nativeType: string;
5
19
  readonly codecId: string;
6
20
  readonly nullable: boolean;
21
+ /**
22
+ * Opaque, codec-owned JS/type parameters.
23
+ * The codec that owns `codecId` defines the shape and semantics.
24
+ * Mutually exclusive with `typeRef`.
25
+ */
26
+ readonly typeParams?: Record<string, unknown>;
27
+ /**
28
+ * Reference to a named type instance in `storage.types`.
29
+ * Mutually exclusive with `typeParams`.
30
+ */
31
+ readonly typeRef?: string;
32
+ /**
33
+ * Default value for the column.
34
+ * Can be a literal value or database function.
35
+ */
36
+ readonly default?: ColumnDefault;
7
37
  };
8
38
 
9
39
  export type PrimaryKey = {
@@ -26,10 +56,24 @@ export type ForeignKeyReferences = {
26
56
  readonly columns: readonly string[];
27
57
  };
28
58
 
59
+ export type ReferentialAction = 'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault';
60
+
61
+ export type ForeignKeyOptions = {
62
+ readonly name?: string;
63
+ readonly onDelete?: ReferentialAction;
64
+ readonly onUpdate?: ReferentialAction;
65
+ };
66
+
29
67
  export type ForeignKey = {
30
68
  readonly columns: readonly string[];
31
69
  readonly references: ForeignKeyReferences;
32
70
  readonly name?: string;
71
+ readonly onDelete?: ReferentialAction;
72
+ readonly onUpdate?: ReferentialAction;
73
+ /** Whether to emit FK constraint DDL (ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY). */
74
+ readonly constraint: boolean;
75
+ /** Whether to emit a backing index for the FK columns. */
76
+ readonly index: boolean;
33
77
  };
34
78
 
35
79
  export type StorageTable = {
@@ -40,8 +84,29 @@ export type StorageTable = {
40
84
  readonly foreignKeys: ReadonlyArray<ForeignKey>;
41
85
  };
42
86
 
87
+ /**
88
+ * A named, parameterized type instance.
89
+ * These are registered in `storage.types` for reuse across columns
90
+ * and to enable ergonomic schema surfaces like `schema.types.MyType`.
91
+ *
92
+ * Unlike {@link StorageColumn}, `typeParams` is required here because
93
+ * `StorageTypeInstance` exists specifically to define reusable parameterized types.
94
+ * A type instance without parameters would be redundant—columns can reference
95
+ * the codec directly via `codecId`.
96
+ */
97
+ export type StorageTypeInstance = {
98
+ readonly codecId: string;
99
+ readonly nativeType: string;
100
+ readonly typeParams: Record<string, unknown>;
101
+ };
102
+
43
103
  export type SqlStorage = {
44
104
  readonly tables: Record<string, StorageTable>;
105
+ /**
106
+ * Named type instances for parameterized/custom types.
107
+ * Columns can reference these via `typeRef`.
108
+ */
109
+ readonly types?: Record<string, StorageTypeInstance>;
45
110
  };
46
111
 
47
112
  export type ModelField = {
@@ -67,17 +132,38 @@ export type SqlMappings = {
67
132
  readonly operationTypes: Record<string, Record<string, unknown>>;
68
133
  };
69
134
 
135
+ export const DEFAULT_FK_CONSTRAINT = true;
136
+ export const DEFAULT_FK_INDEX = true;
137
+
138
+ /**
139
+ * Resolves foreign key `constraint` and `index` fields to their effective boolean values,
140
+ * falling back through optional override defaults, then to the global defaults.
141
+ */
142
+ export function applyFkDefaults(
143
+ fk: { constraint?: boolean | undefined; index?: boolean | undefined },
144
+ overrideDefaults?: { constraint?: boolean | undefined; index?: boolean | undefined },
145
+ ): { constraint: boolean; index: boolean } {
146
+ return {
147
+ constraint: fk.constraint ?? overrideDefaults?.constraint ?? DEFAULT_FK_CONSTRAINT,
148
+ index: fk.index ?? overrideDefaults?.index ?? DEFAULT_FK_INDEX,
149
+ };
150
+ }
151
+
70
152
  export type SqlContract<
71
153
  S extends SqlStorage = SqlStorage,
72
154
  M extends Record<string, unknown> = Record<string, unknown>,
73
155
  R extends Record<string, unknown> = Record<string, unknown>,
74
156
  Map extends SqlMappings = SqlMappings,
75
- > = ContractBase & {
157
+ TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,
158
+ TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,
159
+ TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,
160
+ > = ContractBase<TStorageHash, TExecutionHash, TProfileHash> & {
76
161
  readonly targetFamily: string;
77
162
  readonly storage: S;
78
163
  readonly models: M;
79
164
  readonly relations: R;
80
165
  readonly mappings: Map;
166
+ readonly execution?: ExecutionSection;
81
167
  };
82
168
 
83
169
  export type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> =