@prisma-next/sql-contract 0.3.0-dev.4 → 0.3.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 (46) hide show
  1. package/README.md +40 -10
  2. package/dist/factories.d.mts +49 -0
  3. package/dist/factories.d.mts.map +1 -0
  4. package/dist/factories.mjs +82 -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-DTFobApb.d.mts +137 -0
  10. package/dist/types-DTFobApb.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 +242 -0
  18. package/dist/validate.mjs.map +1 -0
  19. package/dist/validators-DG6QQnb9.mjs +162 -0
  20. package/dist/validators-DG6QQnb9.mjs.map +1 -0
  21. package/dist/{exports/validators.d.ts → validators.d.mts} +17 -4
  22. package/dist/validators.d.mts.map +1 -0
  23. package/dist/validators.mjs +3 -0
  24. package/package.json +27 -29
  25. package/src/exports/factories.ts +11 -0
  26. package/src/exports/pack-types.ts +1 -0
  27. package/src/exports/types.ts +20 -0
  28. package/src/exports/validate.ts +6 -0
  29. package/src/exports/validators.ts +1 -0
  30. package/src/factories.ts +162 -0
  31. package/src/index.ts +4 -0
  32. package/src/pack-types.ts +9 -0
  33. package/src/types.ts +163 -0
  34. package/src/validate.ts +443 -0
  35. package/src/validators.ts +227 -0
  36. package/dist/exports/factories.d.ts +0 -41
  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 -11
  40. package/dist/exports/pack-types.js +0 -1
  41. package/dist/exports/pack-types.js.map +0 -1
  42. package/dist/exports/types.d.ts +0 -70
  43. package/dist/exports/types.js +0 -1
  44. package/dist/exports/types.js.map +0 -1
  45. package/dist/exports/validators.js +0 -96
  46. package/dist/exports/validators.js.map +0 -1
@@ -0,0 +1,162 @@
1
+ import type {
2
+ ExecutionHashBase,
3
+ ProfileHashBase,
4
+ StorageHashBase,
5
+ } from '@prisma-next/contract/types';
6
+ import type {
7
+ ForeignKey,
8
+ ForeignKeyReferences,
9
+ Index,
10
+ ModelDefinition,
11
+ ModelField,
12
+ ModelStorage,
13
+ PrimaryKey,
14
+ SqlContract,
15
+ SqlMappings,
16
+ SqlStorage,
17
+ StorageColumn,
18
+ StorageTable,
19
+ UniqueConstraint,
20
+ } from './types';
21
+ import { applyFkDefaults } from './types';
22
+
23
+ /**
24
+ * Creates a StorageColumn with nativeType and codecId.
25
+ *
26
+ * @param nativeType - Native database type identifier (e.g., 'int4', 'text', 'vector')
27
+ * @param codecId - Codec identifier (e.g., 'pg/int4@1', 'pg/text@1')
28
+ * @param nullable - Whether the column is nullable (default: false)
29
+ * @returns StorageColumn with nativeType and codecId
30
+ */
31
+ export function col(nativeType: string, codecId: string, nullable = false): StorageColumn {
32
+ return {
33
+ nativeType,
34
+ codecId,
35
+ nullable,
36
+ };
37
+ }
38
+
39
+ export function pk(...columns: readonly string[]): PrimaryKey {
40
+ return {
41
+ columns,
42
+ };
43
+ }
44
+
45
+ export function unique(...columns: readonly string[]): UniqueConstraint {
46
+ return {
47
+ columns,
48
+ };
49
+ }
50
+
51
+ export function index(...columns: readonly string[]): Index {
52
+ return {
53
+ columns,
54
+ };
55
+ }
56
+
57
+ export function fk(
58
+ columns: readonly string[],
59
+ refTable: string,
60
+ refColumns: readonly string[],
61
+ opts?: { name?: string; constraint?: boolean; index?: boolean },
62
+ ): ForeignKey {
63
+ const references: ForeignKeyReferences = {
64
+ table: refTable,
65
+ columns: refColumns,
66
+ };
67
+ return {
68
+ columns,
69
+ references,
70
+ ...applyFkDefaults({ constraint: opts?.constraint, index: opts?.index }),
71
+ ...(opts?.name !== undefined && { name: opts.name }),
72
+ };
73
+ }
74
+
75
+ export function table(
76
+ columns: Record<string, StorageColumn>,
77
+ opts?: {
78
+ pk?: PrimaryKey;
79
+ uniques?: readonly UniqueConstraint[];
80
+ indexes?: readonly Index[];
81
+ fks?: readonly ForeignKey[];
82
+ },
83
+ ): StorageTable {
84
+ return {
85
+ columns,
86
+ ...(opts?.pk !== undefined && { primaryKey: opts.pk }),
87
+ uniques: opts?.uniques ?? [],
88
+ indexes: opts?.indexes ?? [],
89
+ foreignKeys: opts?.fks ?? [],
90
+ };
91
+ }
92
+
93
+ export function model(
94
+ table: string,
95
+ fields: Record<string, ModelField>,
96
+ relations: Record<string, unknown> = {},
97
+ ): ModelDefinition {
98
+ const storage: ModelStorage = { table };
99
+ return {
100
+ storage,
101
+ fields,
102
+ relations,
103
+ };
104
+ }
105
+
106
+ export function storage(tables: Record<string, StorageTable>): SqlStorage {
107
+ return { tables };
108
+ }
109
+
110
+ export function contract<
111
+ TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,
112
+ TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,
113
+ TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,
114
+ >(opts: {
115
+ target: string;
116
+ storageHash: TStorageHash;
117
+ executionHash?: TExecutionHash;
118
+ storage: SqlStorage;
119
+ models?: Record<string, ModelDefinition>;
120
+ relations?: Record<string, unknown>;
121
+ mappings?: Partial<SqlMappings>;
122
+ schemaVersion?: '1';
123
+ targetFamily?: 'sql';
124
+ profileHash?: TProfileHash;
125
+ capabilities?: Record<string, Record<string, boolean>>;
126
+ extensionPacks?: Record<string, unknown>;
127
+ meta?: Record<string, unknown>;
128
+ sources?: Record<string, unknown>;
129
+ }): SqlContract<
130
+ SqlStorage,
131
+ Record<string, unknown>,
132
+ Record<string, unknown>,
133
+ SqlMappings,
134
+ TStorageHash,
135
+ TExecutionHash,
136
+ TProfileHash
137
+ > {
138
+ return {
139
+ schemaVersion: opts.schemaVersion ?? '1',
140
+ target: opts.target,
141
+ targetFamily: opts.targetFamily ?? 'sql',
142
+ storageHash: opts.storageHash,
143
+ ...(opts.executionHash !== undefined && { executionHash: opts.executionHash }),
144
+ storage: opts.storage,
145
+ models: opts.models ?? {},
146
+ relations: opts.relations ?? {},
147
+ mappings: (opts.mappings ?? {}) as SqlMappings,
148
+ ...(opts.profileHash !== undefined && { profileHash: opts.profileHash }),
149
+ ...(opts.capabilities !== undefined && { capabilities: opts.capabilities }),
150
+ ...(opts.extensionPacks !== undefined && { extensionPacks: opts.extensionPacks }),
151
+ ...(opts.meta !== undefined && { meta: opts.meta }),
152
+ ...(opts.sources !== undefined && { sources: opts.sources as Record<string, unknown> }),
153
+ } as SqlContract<
154
+ SqlStorage,
155
+ Record<string, unknown>,
156
+ Record<string, unknown>,
157
+ SqlMappings,
158
+ TStorageHash,
159
+ TExecutionHash,
160
+ TProfileHash
161
+ >;
162
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './exports/factories';
2
+ export * from './exports/types';
3
+ export * from './exports/validate';
4
+ export * from './exports/validators';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Storage type metadata for pack refs.
3
+ */
4
+ export interface StorageTypeMetadata {
5
+ readonly typeId: string;
6
+ readonly familyId: string;
7
+ readonly targetId: string;
8
+ readonly nativeType?: string;
9
+ }
package/src/types.ts ADDED
@@ -0,0 +1,163 @@
1
+ import type {
2
+ ColumnDefault,
3
+ ContractBase,
4
+ ExecutionHashBase,
5
+ ExecutionSection,
6
+ ProfileHashBase,
7
+ StorageHashBase,
8
+ } from '@prisma-next/contract/types';
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
+ */
17
+ export type StorageColumn = {
18
+ readonly nativeType: string;
19
+ readonly codecId: string;
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;
37
+ };
38
+
39
+ export type PrimaryKey = {
40
+ readonly columns: readonly string[];
41
+ readonly name?: string;
42
+ };
43
+
44
+ export type UniqueConstraint = {
45
+ readonly columns: readonly string[];
46
+ readonly name?: string;
47
+ };
48
+
49
+ export type Index = {
50
+ readonly columns: readonly string[];
51
+ readonly name?: string;
52
+ };
53
+
54
+ export type ForeignKeyReferences = {
55
+ readonly table: string;
56
+ readonly columns: readonly string[];
57
+ };
58
+
59
+ export type ForeignKey = {
60
+ readonly columns: readonly string[];
61
+ readonly references: ForeignKeyReferences;
62
+ readonly name?: string;
63
+ /** Whether to emit FK constraint DDL (ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY). */
64
+ readonly constraint: boolean;
65
+ /** Whether to emit a backing index for the FK columns. */
66
+ readonly index: boolean;
67
+ };
68
+
69
+ export type StorageTable = {
70
+ readonly columns: Record<string, StorageColumn>;
71
+ readonly primaryKey?: PrimaryKey;
72
+ readonly uniques: ReadonlyArray<UniqueConstraint>;
73
+ readonly indexes: ReadonlyArray<Index>;
74
+ readonly foreignKeys: ReadonlyArray<ForeignKey>;
75
+ };
76
+
77
+ /**
78
+ * A named, parameterized type instance.
79
+ * These are registered in `storage.types` for reuse across columns
80
+ * and to enable ergonomic schema surfaces like `schema.types.MyType`.
81
+ *
82
+ * Unlike {@link StorageColumn}, `typeParams` is required here because
83
+ * `StorageTypeInstance` exists specifically to define reusable parameterized types.
84
+ * A type instance without parameters would be redundant—columns can reference
85
+ * the codec directly via `codecId`.
86
+ */
87
+ export type StorageTypeInstance = {
88
+ readonly codecId: string;
89
+ readonly nativeType: string;
90
+ readonly typeParams: Record<string, unknown>;
91
+ };
92
+
93
+ export type SqlStorage = {
94
+ readonly tables: Record<string, StorageTable>;
95
+ /**
96
+ * Named type instances for parameterized/custom types.
97
+ * Columns can reference these via `typeRef`.
98
+ */
99
+ readonly types?: Record<string, StorageTypeInstance>;
100
+ };
101
+
102
+ export type ModelField = {
103
+ readonly column: string;
104
+ };
105
+
106
+ export type ModelStorage = {
107
+ readonly table: string;
108
+ };
109
+
110
+ export type ModelDefinition = {
111
+ readonly storage: ModelStorage;
112
+ readonly fields: Record<string, ModelField>;
113
+ readonly relations: Record<string, unknown>;
114
+ };
115
+
116
+ export type SqlMappings = {
117
+ readonly modelToTable?: Record<string, string>;
118
+ readonly tableToModel?: Record<string, string>;
119
+ readonly fieldToColumn?: Record<string, Record<string, string>>;
120
+ readonly columnToField?: Record<string, Record<string, string>>;
121
+ readonly codecTypes: Record<string, { readonly output: unknown }>;
122
+ readonly operationTypes: Record<string, Record<string, unknown>>;
123
+ };
124
+
125
+ export const DEFAULT_FK_CONSTRAINT = true;
126
+ export const DEFAULT_FK_INDEX = true;
127
+
128
+ /**
129
+ * Resolves foreign key `constraint` and `index` fields to their effective boolean values,
130
+ * falling back through optional override defaults, then to the global defaults.
131
+ */
132
+ export function applyFkDefaults(
133
+ fk: { constraint?: boolean | undefined; index?: boolean | undefined },
134
+ overrideDefaults?: { constraint?: boolean | undefined; index?: boolean | undefined },
135
+ ): { constraint: boolean; index: boolean } {
136
+ return {
137
+ constraint: fk.constraint ?? overrideDefaults?.constraint ?? DEFAULT_FK_CONSTRAINT,
138
+ index: fk.index ?? overrideDefaults?.index ?? DEFAULT_FK_INDEX,
139
+ };
140
+ }
141
+
142
+ export type SqlContract<
143
+ S extends SqlStorage = SqlStorage,
144
+ M extends Record<string, unknown> = Record<string, unknown>,
145
+ R extends Record<string, unknown> = Record<string, unknown>,
146
+ Map extends SqlMappings = SqlMappings,
147
+ TStorageHash extends StorageHashBase<string> = StorageHashBase<string>,
148
+ TExecutionHash extends ExecutionHashBase<string> = ExecutionHashBase<string>,
149
+ TProfileHash extends ProfileHashBase<string> = ProfileHashBase<string>,
150
+ > = ContractBase<TStorageHash, TExecutionHash, TProfileHash> & {
151
+ readonly targetFamily: string;
152
+ readonly storage: S;
153
+ readonly models: M;
154
+ readonly relations: R;
155
+ readonly mappings: Map;
156
+ readonly execution?: ExecutionSection;
157
+ };
158
+
159
+ export type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> =
160
+ TContract['mappings']['codecTypes'];
161
+
162
+ export type ExtractOperationTypes<TContract extends SqlContract<SqlStorage>> =
163
+ TContract['mappings']['operationTypes'];