@prisma-next/contract-authoring 0.3.0-dev.3 → 0.3.0-dev.30

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.
@@ -0,0 +1,46 @@
1
+ import type { ColumnBuilderState, ModelBuilderState, RelationDefinition, TableBuilderState } from './builder-state';
2
+ export type BuildStorageColumn<Nullable extends boolean, Type extends string> = {
3
+ readonly nativeType: string;
4
+ readonly codecId: Type;
5
+ readonly nullable: Nullable;
6
+ };
7
+ export type ExtractColumns<T extends TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>> = T extends TableBuilderState<string, infer C, readonly string[] | undefined> ? C : never;
8
+ export type ExtractPrimaryKey<T extends TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>> = T extends TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, infer PK> ? PK : never;
9
+ export type BuildStorage<Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>> = {
10
+ readonly tables: {
11
+ readonly [K in keyof Tables]: {
12
+ readonly columns: {
13
+ readonly [ColK in keyof ExtractColumns<Tables[K]>]: ExtractColumns<Tables[K]>[ColK] extends ColumnBuilderState<string, infer Null, infer TType> ? BuildStorageColumn<Null & boolean, TType> : never;
14
+ };
15
+ };
16
+ };
17
+ };
18
+ export type BuildStorageTables<Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>> = {
19
+ readonly [K in keyof Tables]: {
20
+ readonly columns: {
21
+ readonly [ColK in keyof ExtractColumns<Tables[K]>]: ExtractColumns<Tables[K]>[ColK] extends ColumnBuilderState<string, infer Null, infer TType> ? BuildStorageColumn<Null & boolean, TType> : never;
22
+ };
23
+ };
24
+ };
25
+ export type Mutable<T> = {
26
+ -readonly [K in keyof T]-?: T[K];
27
+ };
28
+ export type BuildModelFields<Fields extends Record<string, string>> = {
29
+ readonly [K in keyof Fields]: {
30
+ readonly column: Fields[K];
31
+ };
32
+ };
33
+ export type ExtractModelFields<T extends ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>> = T extends ModelBuilderState<string, string, infer F, Record<string, RelationDefinition>> ? F : never;
34
+ export type ExtractModelRelations<T extends ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>> = T extends ModelBuilderState<string, string, Record<string, string>, infer R> ? R : never;
35
+ export type BuildModels<Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>>> = {
36
+ readonly [K in keyof Models]: {
37
+ readonly storage: {
38
+ readonly table: Models[K]['table'];
39
+ };
40
+ readonly fields: BuildModelFields<ExtractModelFields<Models[K]>>;
41
+ };
42
+ };
43
+ export type BuildRelations<Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>>> = {
44
+ readonly [K in keyof Models as Models[K]['table']]: ExtractModelRelations<Models[K]>;
45
+ };
46
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,iBAAiB,CAAC;AAEzB,MAAM,MAAM,kBAAkB,CAAC,QAAQ,SAAS,OAAO,EAAE,IAAI,SAAS,MAAM,IAAI;IAC9E,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,cAAc,CACxB,CAAC,SAAS,iBAAiB,CACzB,MAAM,EACN,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC,EAC3D,SAAS,MAAM,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contract-authoring",
3
- "version": "0.3.0-dev.3",
3
+ "version": "0.3.0-dev.30",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "Target-agnostic contract authoring builder core for Prisma Next",
7
7
  "dependencies": {
8
8
  "ts-toolbelt": "^9.6.0",
9
- "@prisma-next/contract": "0.3.0-dev.3"
9
+ "@prisma-next/contract": "0.3.0-dev.30"
10
10
  },
11
11
  "devDependencies": {
12
- "@vitest/coverage-v8": "^4.0.0",
13
- "tsup": "^8.3.0",
14
- "typescript": "^5.9.3",
15
- "vitest": "^4.0.16"
12
+ "tsup": "8.5.1",
13
+ "typescript": "5.9.3",
14
+ "vitest": "4.0.16",
15
+ "@prisma-next/tsconfig": "0.0.0"
16
16
  },
17
17
  "files": [
18
- "dist"
18
+ "dist",
19
+ "src"
19
20
  ],
20
21
  "exports": {
21
22
  ".": {
@@ -24,13 +25,13 @@
24
25
  }
25
26
  },
26
27
  "scripts": {
27
- "build": "tsup --config tsup.config.ts",
28
+ "build": "tsup --config tsup.config.ts && tsc --project tsconfig.build.json",
28
29
  "test": "vitest run",
29
30
  "test:coverage": "vitest run --coverage",
30
31
  "typecheck": "tsc --project tsconfig.json --noEmit",
31
- "lint": "biome check . --config-path ../../../../biome.json --error-on-warnings",
32
- "lint:fix": "biome check --write . --config-path ../../../../biome.json",
33
- "lint:fix:unsafe": "biome check --write --unsafe . --config-path ../../../../biome.json",
34
- "clean": "node ../../../../scripts/clean.mjs"
32
+ "lint": "biome check . --error-on-warnings",
33
+ "lint:fix": "biome check --write .",
34
+ "lint:fix:unsafe": "biome check --write --unsafe .",
35
+ "clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
35
36
  }
36
37
  }
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Column type descriptor containing both codec ID and native type.
3
+ * Used when defining columns with descriptor objects instead of string IDs.
4
+ */
5
+ export type ColumnTypeDescriptor = {
6
+ readonly codecId: string;
7
+ readonly nativeType: string;
8
+ };
9
+
10
+ export interface ColumnBuilderState<
11
+ Name extends string,
12
+ Nullable extends boolean,
13
+ Type extends string,
14
+ > {
15
+ readonly name: Name;
16
+ readonly nullable: Nullable;
17
+ readonly type: Type;
18
+ readonly nativeType: string;
19
+ }
20
+
21
+ /**
22
+ * Unique constraint definition for table builder.
23
+ */
24
+ export interface UniqueConstraintDef {
25
+ readonly columns: readonly string[];
26
+ readonly name?: string;
27
+ }
28
+
29
+ /**
30
+ * Index definition for table builder.
31
+ */
32
+ export interface IndexDef {
33
+ readonly columns: readonly string[];
34
+ readonly name?: string;
35
+ }
36
+
37
+ /**
38
+ * Foreign key definition for table builder.
39
+ */
40
+ export interface ForeignKeyDef {
41
+ readonly columns: readonly string[];
42
+ readonly references: {
43
+ readonly table: string;
44
+ readonly columns: readonly string[];
45
+ };
46
+ readonly name?: string;
47
+ }
48
+
49
+ export interface TableBuilderState<
50
+ Name extends string,
51
+ Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,
52
+ PrimaryKey extends readonly string[] | undefined,
53
+ > {
54
+ readonly name: Name;
55
+ readonly columns: Columns;
56
+ readonly primaryKey?: PrimaryKey;
57
+ readonly primaryKeyName?: string;
58
+ readonly uniques: readonly UniqueConstraintDef[];
59
+ readonly indexes: readonly IndexDef[];
60
+ readonly foreignKeys: readonly ForeignKeyDef[];
61
+ }
62
+
63
+ export type RelationDefinition = {
64
+ readonly to: string;
65
+ readonly cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';
66
+ readonly on: {
67
+ readonly parentCols: readonly string[];
68
+ readonly childCols: readonly string[];
69
+ };
70
+ readonly through?: {
71
+ readonly table: string;
72
+ readonly parentCols: readonly string[];
73
+ readonly childCols: readonly string[];
74
+ };
75
+ };
76
+
77
+ export interface ModelBuilderState<
78
+ Name extends string,
79
+ Table extends string,
80
+ Fields extends Record<string, string>,
81
+ Relations extends Record<string, RelationDefinition>,
82
+ > {
83
+ readonly name: Name;
84
+ readonly table: Table;
85
+ readonly fields: Fields;
86
+ readonly relations: Relations;
87
+ }
88
+
89
+ export interface ContractBuilderState<
90
+ Target extends string | undefined = string | undefined,
91
+ Tables extends Record<
92
+ string,
93
+ TableBuilderState<
94
+ string,
95
+ Record<string, ColumnBuilderState<string, boolean, string>>,
96
+ readonly string[] | undefined
97
+ >
98
+ > = Record<
99
+ never,
100
+ TableBuilderState<
101
+ string,
102
+ Record<string, ColumnBuilderState<string, boolean, string>>,
103
+ readonly string[] | undefined
104
+ >
105
+ >,
106
+ Models extends Record<
107
+ string,
108
+ ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
109
+ > = Record<
110
+ never,
111
+ ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
112
+ >,
113
+ CoreHash extends string | undefined = string | undefined,
114
+ ExtensionPacks extends Record<string, unknown> | undefined = undefined,
115
+ Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined,
116
+ > {
117
+ readonly target?: Target;
118
+ readonly tables: Tables;
119
+ readonly models: Models;
120
+ readonly coreHash?: CoreHash;
121
+ readonly extensionPacks?: ExtensionPacks;
122
+ readonly capabilities?: Capabilities;
123
+ /**
124
+ * Array of extension pack namespace identifiers (e.g., ['pgvector', 'postgis']).
125
+ * Populated when extension packs are registered during contract building.
126
+ * Used to track which extension packs are included in the contract.
127
+ * Can be undefined or empty if no extension packs are registered.
128
+ * Namespace format matches the extension pack ID (e.g., 'pgvector', not 'pgvector@1.0.0').
129
+ */
130
+ readonly extensionNamespaces?: readonly string[];
131
+ }
132
+
133
+ export interface ColumnBuilder<Name extends string, Nullable extends boolean, Type extends string> {
134
+ nullable<Value extends boolean>(value?: Value): ColumnBuilder<Name, Value, Type>;
135
+ type<Id extends string>(id: Id): ColumnBuilder<Name, Nullable, Id>;
136
+ build(): ColumnBuilderState<Name, Nullable, Type>;
137
+ }
@@ -0,0 +1,159 @@
1
+ import type { TargetPackRef } from '@prisma-next/contract/framework-components';
2
+ import type {
3
+ ColumnBuilderState,
4
+ ContractBuilderState,
5
+ ModelBuilderState,
6
+ RelationDefinition,
7
+ TableBuilderState,
8
+ } from './builder-state';
9
+ import { ModelBuilder } from './model-builder';
10
+ import { createTable, TableBuilder } from './table-builder';
11
+
12
+ export class ContractBuilder<
13
+ Target extends string | undefined = undefined,
14
+ Tables extends Record<
15
+ string,
16
+ TableBuilderState<
17
+ string,
18
+ Record<string, ColumnBuilderState<string, boolean, string>>,
19
+ readonly string[] | undefined
20
+ >
21
+ > = Record<never, never>,
22
+ Models extends Record<
23
+ string,
24
+ ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
25
+ > = Record<never, never>,
26
+ CoreHash extends string | undefined = undefined,
27
+ ExtensionPacks extends Record<string, unknown> | undefined = undefined,
28
+ Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined,
29
+ > {
30
+ protected readonly state: ContractBuilderState<
31
+ Target,
32
+ Tables,
33
+ Models,
34
+ CoreHash,
35
+ ExtensionPacks,
36
+ Capabilities
37
+ >;
38
+
39
+ constructor(
40
+ state?: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>,
41
+ ) {
42
+ this.state =
43
+ state ??
44
+ ({
45
+ tables: {},
46
+ models: {},
47
+ } as ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>);
48
+ }
49
+
50
+ target<T extends string>(
51
+ packRef: TargetPackRef<string, T>,
52
+ ): ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities> {
53
+ return new ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities>({
54
+ ...this.state,
55
+ target: packRef.targetId,
56
+ });
57
+ }
58
+
59
+ capabilities<C extends Record<string, Record<string, boolean>>>(
60
+ capabilities: C,
61
+ ): ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C> {
62
+ return new ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C>({
63
+ ...this.state,
64
+ capabilities,
65
+ });
66
+ }
67
+
68
+ table<
69
+ TableName extends string,
70
+ T extends TableBuilder<
71
+ TableName,
72
+ Record<string, ColumnBuilderState<string, boolean, string>>,
73
+ readonly string[] | undefined
74
+ >,
75
+ >(
76
+ name: TableName,
77
+ callback: (t: TableBuilder<TableName>) => T | undefined,
78
+ ): ContractBuilder<
79
+ Target,
80
+ Tables & Record<TableName, ReturnType<T['build']>>,
81
+ Models,
82
+ CoreHash,
83
+ ExtensionPacks,
84
+ Capabilities
85
+ > {
86
+ const tableBuilder = createTable(name);
87
+ const result = callback(tableBuilder);
88
+ const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;
89
+ const tableState = finalBuilder.build();
90
+
91
+ return new ContractBuilder<
92
+ Target,
93
+ Tables & Record<TableName, ReturnType<T['build']>>,
94
+ Models,
95
+ CoreHash,
96
+ ExtensionPacks,
97
+ Capabilities
98
+ >({
99
+ ...this.state,
100
+ tables: { ...this.state.tables, [name]: tableState } as Tables &
101
+ Record<TableName, ReturnType<T['build']>>,
102
+ });
103
+ }
104
+
105
+ model<
106
+ ModelName extends string,
107
+ TableName extends string,
108
+ M extends ModelBuilder<
109
+ ModelName,
110
+ TableName,
111
+ Record<string, string>,
112
+ Record<string, RelationDefinition>
113
+ >,
114
+ >(
115
+ name: ModelName,
116
+ table: TableName,
117
+ callback: (
118
+ m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>,
119
+ ) => M | undefined,
120
+ ): ContractBuilder<
121
+ Target,
122
+ Tables,
123
+ Models & Record<ModelName, ReturnType<M['build']>>,
124
+ CoreHash,
125
+ ExtensionPacks,
126
+ Capabilities
127
+ > {
128
+ const modelBuilder = new ModelBuilder<ModelName, TableName>(name, table);
129
+ const result = callback(modelBuilder);
130
+ const finalBuilder = result instanceof ModelBuilder ? result : modelBuilder;
131
+ const modelState = finalBuilder.build();
132
+
133
+ return new ContractBuilder<
134
+ Target,
135
+ Tables,
136
+ Models & Record<ModelName, ReturnType<M['build']>>,
137
+ CoreHash,
138
+ ExtensionPacks,
139
+ Capabilities
140
+ >({
141
+ ...this.state,
142
+ models: { ...this.state.models, [name]: modelState } as Models &
143
+ Record<ModelName, ReturnType<M['build']>>,
144
+ });
145
+ }
146
+
147
+ coreHash<H extends string>(
148
+ hash: H,
149
+ ): ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities> {
150
+ return new ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities>({
151
+ ...this.state,
152
+ coreHash: hash,
153
+ });
154
+ }
155
+ }
156
+
157
+ export function defineContract(): ContractBuilder {
158
+ return new ContractBuilder();
159
+ }
package/src/index.ts ADDED
@@ -0,0 +1,30 @@
1
+ export type {
2
+ ColumnBuilder,
3
+ ColumnBuilderState,
4
+ ColumnTypeDescriptor,
5
+ ContractBuilderState,
6
+ ForeignKeyDef,
7
+ IndexDef,
8
+ ModelBuilderState,
9
+ RelationDefinition,
10
+ TableBuilderState,
11
+ UniqueConstraintDef,
12
+ } from './builder-state';
13
+
14
+ export { ContractBuilder, defineContract } from './contract-builder';
15
+ export { ModelBuilder } from './model-builder';
16
+ export { createTable, TableBuilder } from './table-builder';
17
+
18
+ export type {
19
+ BuildModelFields,
20
+ BuildModels,
21
+ BuildRelations,
22
+ BuildStorage,
23
+ BuildStorageColumn,
24
+ BuildStorageTables,
25
+ ExtractColumns,
26
+ ExtractModelFields,
27
+ ExtractModelRelations,
28
+ ExtractPrimaryKey,
29
+ Mutable,
30
+ } from './types';
@@ -0,0 +1,160 @@
1
+ import type { ModelBuilderState, RelationDefinition } from './builder-state';
2
+
3
+ export class ModelBuilder<
4
+ Name extends string,
5
+ Table extends string,
6
+ Fields extends Record<string, string> = Record<never, never>,
7
+ Relations extends Record<string, RelationDefinition> = Record<never, never>,
8
+ > {
9
+ private readonly _name: Name;
10
+ private readonly _table: Table;
11
+ private readonly _fields: Fields;
12
+ private readonly _relations: Relations;
13
+
14
+ constructor(
15
+ name: Name,
16
+ table: Table,
17
+ fields: Fields = {} as Fields,
18
+ relations: Relations = {} as Relations,
19
+ ) {
20
+ this._name = name;
21
+ this._table = table;
22
+ this._fields = fields;
23
+ this._relations = relations;
24
+ }
25
+
26
+ field<FieldName extends string, ColumnName extends string>(
27
+ fieldName: FieldName,
28
+ columnName: ColumnName,
29
+ ): ModelBuilder<Name, Table, Fields & Record<FieldName, ColumnName>, Relations> {
30
+ return new ModelBuilder(
31
+ this._name,
32
+ this._table,
33
+ {
34
+ ...this._fields,
35
+ [fieldName]: columnName,
36
+ } as Fields & Record<FieldName, ColumnName>,
37
+ this._relations,
38
+ );
39
+ }
40
+
41
+ relation<RelationName extends string, ToModel extends string, ToTable extends string>(
42
+ name: RelationName,
43
+ options: {
44
+ toModel: ToModel;
45
+ toTable: ToTable;
46
+ cardinality: '1:1' | '1:N' | 'N:1';
47
+ on: {
48
+ parentTable: Table;
49
+ parentColumns: readonly string[];
50
+ childTable: ToTable;
51
+ childColumns: readonly string[];
52
+ };
53
+ },
54
+ ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;
55
+ relation<
56
+ RelationName extends string,
57
+ ToModel extends string,
58
+ ToTable extends string,
59
+ JunctionTable extends string,
60
+ >(
61
+ name: RelationName,
62
+ options: {
63
+ toModel: ToModel;
64
+ toTable: ToTable;
65
+ cardinality: 'N:M';
66
+ through: {
67
+ table: JunctionTable;
68
+ parentColumns: readonly string[];
69
+ childColumns: readonly string[];
70
+ };
71
+ on: {
72
+ parentTable: Table;
73
+ parentColumns: readonly string[];
74
+ childTable: JunctionTable;
75
+ childColumns: readonly string[];
76
+ };
77
+ },
78
+ ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;
79
+ relation<
80
+ RelationName extends string,
81
+ ToModel extends string,
82
+ ToTable extends string,
83
+ JunctionTable extends string = never,
84
+ >(
85
+ name: RelationName,
86
+ options: {
87
+ toModel: ToModel;
88
+ toTable: ToTable;
89
+ cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';
90
+ through?: {
91
+ table: JunctionTable;
92
+ parentColumns: readonly string[];
93
+ childColumns: readonly string[];
94
+ };
95
+ on: {
96
+ parentTable: Table;
97
+ parentColumns: readonly string[];
98
+ childTable: ToTable | JunctionTable;
99
+ childColumns: readonly string[];
100
+ };
101
+ },
102
+ ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>> {
103
+ // Validate parentTable matches model's table
104
+ if (options.on.parentTable !== this._table) {
105
+ throw new Error(
106
+ `Relation "${name}" parentTable "${options.on.parentTable}" does not match model table "${this._table}"`,
107
+ );
108
+ }
109
+
110
+ // Validate childTable matches toTable (for non-N:M) or through.table (for N:M)
111
+ if (options.cardinality === 'N:M') {
112
+ if (!options.through) {
113
+ throw new Error(`Relation "${name}" with cardinality "N:M" requires through field`);
114
+ }
115
+ if (options.on.childTable !== options.through.table) {
116
+ throw new Error(
117
+ `Relation "${name}" childTable "${options.on.childTable}" does not match through.table "${options.through.table}"`,
118
+ );
119
+ }
120
+ } else {
121
+ if (options.on.childTable !== options.toTable) {
122
+ throw new Error(
123
+ `Relation "${name}" childTable "${options.on.childTable}" does not match toTable "${options.toTable}"`,
124
+ );
125
+ }
126
+ }
127
+
128
+ const relationDef: RelationDefinition = {
129
+ to: options.toModel,
130
+ cardinality: options.cardinality,
131
+ on: {
132
+ parentCols: options.on.parentColumns,
133
+ childCols: options.on.childColumns,
134
+ },
135
+ ...(options.through
136
+ ? {
137
+ through: {
138
+ table: options.through.table,
139
+ parentCols: options.through.parentColumns,
140
+ childCols: options.through.childColumns,
141
+ },
142
+ }
143
+ : undefined),
144
+ };
145
+
146
+ return new ModelBuilder(this._name, this._table, this._fields, {
147
+ ...this._relations,
148
+ [name]: relationDef,
149
+ } as Relations & Record<RelationName, RelationDefinition>);
150
+ }
151
+
152
+ build(): ModelBuilderState<Name, Table, Fields, Relations> {
153
+ return {
154
+ name: this._name,
155
+ table: this._table,
156
+ fields: this._fields,
157
+ relations: this._relations,
158
+ };
159
+ }
160
+ }