@prisma-next/contract-authoring 0.3.0-dev.4 → 0.3.0-dev.6

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,60 @@
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
+ export interface ColumnBuilderState<Name extends string, Nullable extends boolean, Type extends string> {
10
+ readonly name: Name;
11
+ readonly nullable: Nullable;
12
+ readonly type: Type;
13
+ readonly nativeType: string;
14
+ }
15
+ export interface TableBuilderState<Name extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>>, PrimaryKey extends readonly string[] | undefined> {
16
+ readonly name: Name;
17
+ readonly columns: Columns;
18
+ readonly primaryKey?: PrimaryKey;
19
+ }
20
+ export type RelationDefinition = {
21
+ readonly to: string;
22
+ readonly cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';
23
+ readonly on: {
24
+ readonly parentCols: readonly string[];
25
+ readonly childCols: readonly string[];
26
+ };
27
+ readonly through?: {
28
+ readonly table: string;
29
+ readonly parentCols: readonly string[];
30
+ readonly childCols: readonly string[];
31
+ };
32
+ };
33
+ export interface ModelBuilderState<Name extends string, Table extends string, Fields extends Record<string, string>, Relations extends Record<string, RelationDefinition>> {
34
+ readonly name: Name;
35
+ readonly table: Table;
36
+ readonly fields: Fields;
37
+ readonly relations: Relations;
38
+ }
39
+ export interface ContractBuilderState<Target extends string | undefined = string | undefined, Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>> = Record<never, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>, Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>> = Record<never, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>>, CoreHash extends string | undefined = string | undefined, ExtensionPacks extends Record<string, unknown> | undefined = undefined, Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined> {
40
+ readonly target?: Target;
41
+ readonly tables: Tables;
42
+ readonly models: Models;
43
+ readonly coreHash?: CoreHash;
44
+ readonly extensionPacks?: ExtensionPacks;
45
+ readonly capabilities?: Capabilities;
46
+ /**
47
+ * Array of extension pack namespace identifiers (e.g., ['pgvector', 'postgis']).
48
+ * Populated when extension packs are registered during contract building.
49
+ * Used to track which extension packs are included in the contract.
50
+ * Can be undefined or empty if no extension packs are registered.
51
+ * Namespace format matches the extension pack ID (e.g., 'pgvector', not 'pgvector@1.0.0').
52
+ */
53
+ readonly extensionNamespaces?: readonly string[];
54
+ }
55
+ export interface ColumnBuilder<Name extends string, Nullable extends boolean, Type extends string> {
56
+ nullable<Value extends boolean>(value?: Value): ColumnBuilder<Name, Value, Type>;
57
+ type<Id extends string>(id: Id): ColumnBuilder<Name, Nullable, Id>;
58
+ build(): ColumnBuilderState<Name, Nullable, Type>;
59
+ }
60
+ //# sourceMappingURL=builder-state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builder-state.d.ts","sourceRoot":"","sources":["../src/builder-state.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF,MAAM,WAAW,kBAAkB,CACjC,IAAI,SAAS,MAAM,EACnB,QAAQ,SAAS,OAAO,EACxB,IAAI,SAAS,MAAM;IAEnB,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IACpB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IACpB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED,MAAM,WAAW,iBAAi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AAC,EAAE,cAAc,CAAC;IACzC,QAAQ,CAAC,YAAY,CAAC,EAAE,YAAY,CAAC;IACrC;;;;;;OAMG;IACH,QAAQ,CAAC,mBAAmB,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAClD;AAED,MAAM,WAAW,aAAa,CAAC,IAAI,SAAS,MAAM,EAAE,QAAQ,SAAS,OAAO,EAAE,IAAI,SAAS,MAAM;IAC/F,QAAQ,CAAC,KAAK,SAAS,OAAO,EAAE,KAAK,CAAC,EAAE,KAAK,GAAG,aAAa,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IACjF,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,EAAE,EAAE,EAAE,GAAG,aAAa,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,CAAC,CAAC;IACnE,KAAK,IAAI,kBAAkB,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;CACnD"}
@@ -0,0 +1,15 @@
1
+ import type { TargetPackRef } from '@prisma-next/contract/framework-components';
2
+ import type { ColumnBuilderState, ContractBuilderState, ModelBuilderState, RelationDefinition, TableBuilderState } from './builder-state';
3
+ import { ModelBuilder } from './model-builder';
4
+ import { TableBuilder } from './table-builder';
5
+ export declare class ContractBuilder<Target extends string | undefined = undefined, Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>> = Record<never, never>, Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>> = Record<never, never>, CoreHash extends string | undefined = undefined, ExtensionPacks extends Record<string, unknown> | undefined = undefined, Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined> {
6
+ protected readonly state: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>;
7
+ constructor(state?: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>);
8
+ target<T extends string>(packRef: TargetPackRef<string, T>): ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities>;
9
+ capabilities<C extends Record<string, Record<string, boolean>>>(capabilities: C): ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C>;
10
+ table<TableName extends string, T extends TableBuilder<TableName, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>(name: TableName, callback: (t: TableBuilder<TableName>) => T | undefined): ContractBuilder<Target, Tables & Record<TableName, ReturnType<T['build']>>, Models, CoreHash, ExtensionPacks, Capabilities>;
11
+ model<ModelName extends string, TableName extends string, M extends ModelBuilder<ModelName, TableName, Record<string, string>, Record<string, RelationDefinition>>>(name: ModelName, table: TableName, callback: (m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>) => M | undefined): ContractBuilder<Target, Tables, Models & Record<ModelName, ReturnType<M['build']>>, CoreHash, ExtensionPacks, Capabilities>;
12
+ coreHash<H extends string>(hash: H): ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities>;
13
+ }
14
+ export declare function defineContract(): ContractBuilder;
15
+ //# sourceMappingURL=contract-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract-builder.d.ts","sourceRoot":"","sources":["../src/contract-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4CAA4C,CAAC;AAChF,OAAO,KAAK,EACV,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EAClB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,qBAAa,eAAe,CAC1B,MAAM,SAAS,MAAM,GAAG,SAAS,GAAG,SAAS,EAC7C,MAAM,SAAS,MAAM,CACnB,MAAM,EACN,iBAAiB,CACf,MAAM,EACN,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM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/index.d.ts CHANGED
@@ -1,175 +1,6 @@
1
- import { TargetPackRef } from '@prisma-next/contract/framework-components';
2
-
3
- /**
4
- * Column type descriptor containing both codec ID and native type.
5
- * Used when defining columns with descriptor objects instead of string IDs.
6
- */
7
- type ColumnTypeDescriptor = {
8
- readonly codecId: string;
9
- readonly nativeType: string;
10
- };
11
- interface ColumnBuilderState<Name extends string, Nullable extends boolean, Type extends string> {
12
- readonly name: Name;
13
- readonly nullable: Nullable;
14
- readonly type: Type;
15
- readonly nativeType: string;
16
- }
17
- interface TableBuilderState<Name extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>>, PrimaryKey extends readonly string[] | undefined> {
18
- readonly name: Name;
19
- readonly columns: Columns;
20
- readonly primaryKey?: PrimaryKey;
21
- }
22
- type RelationDefinition = {
23
- readonly to: string;
24
- readonly cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';
25
- readonly on: {
26
- readonly parentCols: readonly string[];
27
- readonly childCols: readonly string[];
28
- };
29
- readonly through?: {
30
- readonly table: string;
31
- readonly parentCols: readonly string[];
32
- readonly childCols: readonly string[];
33
- };
34
- };
35
- interface ModelBuilderState<Name extends string, Table extends string, Fields extends Record<string, string>, Relations extends Record<string, RelationDefinition>> {
36
- readonly name: Name;
37
- readonly table: Table;
38
- readonly fields: Fields;
39
- readonly relations: Relations;
40
- }
41
- interface ContractBuilderState<Target extends string | undefined = string | undefined, Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>> = Record<never, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>, Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>> = Record<never, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>>, CoreHash extends string | undefined = string | undefined, ExtensionPacks extends Record<string, unknown> | undefined = undefined, Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined> {
42
- readonly target?: Target;
43
- readonly tables: Tables;
44
- readonly models: Models;
45
- readonly coreHash?: CoreHash;
46
- readonly extensionPacks?: ExtensionPacks;
47
- readonly capabilities?: Capabilities;
48
- /**
49
- * Array of extension pack namespace identifiers (e.g., ['pgvector', 'postgis']).
50
- * Populated when extension packs are registered during contract building.
51
- * Used to track which extension packs are included in the contract.
52
- * Can be undefined or empty if no extension packs are registered.
53
- * Namespace format matches the extension pack ID (e.g., 'pgvector', not 'pgvector@1.0.0').
54
- */
55
- readonly extensionNamespaces?: readonly string[];
56
- }
57
- interface ColumnBuilder<Name extends string, Nullable extends boolean, Type extends string> {
58
- nullable<Value extends boolean>(value?: Value): ColumnBuilder<Name, Value, Type>;
59
- type<Id extends string>(id: Id): ColumnBuilder<Name, Nullable, Id>;
60
- build(): ColumnBuilderState<Name, Nullable, Type>;
61
- }
62
-
63
- declare class ModelBuilder<Name extends string, Table extends string, Fields extends Record<string, string> = Record<never, never>, Relations extends Record<string, RelationDefinition> = Record<never, never>> {
64
- private readonly _name;
65
- private readonly _table;
66
- private readonly _fields;
67
- private readonly _relations;
68
- constructor(name: Name, table: Table, fields?: Fields, relations?: Relations);
69
- field<FieldName extends string, ColumnName extends string>(fieldName: FieldName, columnName: ColumnName): ModelBuilder<Name, Table, Fields & Record<FieldName, ColumnName>, Relations>;
70
- relation<RelationName extends string, ToModel extends string, ToTable extends string>(name: RelationName, options: {
71
- toModel: ToModel;
72
- toTable: ToTable;
73
- cardinality: '1:1' | '1:N' | 'N:1';
74
- on: {
75
- parentTable: Table;
76
- parentColumns: readonly string[];
77
- childTable: ToTable;
78
- childColumns: readonly string[];
79
- };
80
- }): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;
81
- relation<RelationName extends string, ToModel extends string, ToTable extends string, JunctionTable extends string>(name: RelationName, options: {
82
- toModel: ToModel;
83
- toTable: ToTable;
84
- cardinality: 'N:M';
85
- through: {
86
- table: JunctionTable;
87
- parentColumns: readonly string[];
88
- childColumns: readonly string[];
89
- };
90
- on: {
91
- parentTable: Table;
92
- parentColumns: readonly string[];
93
- childTable: JunctionTable;
94
- childColumns: readonly string[];
95
- };
96
- }): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;
97
- build(): ModelBuilderState<Name, Table, Fields, Relations>;
98
- }
99
-
100
- declare class TableBuilder<Name extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<never, ColumnBuilderState<string, boolean, string>>, PrimaryKey extends readonly string[] | undefined = undefined> {
101
- private readonly _name;
102
- private readonly _columns;
103
- private readonly _primaryKey;
104
- constructor(name: Name, columns?: Columns, primaryKey?: PrimaryKey);
105
- column<ColName extends string, Descriptor extends ColumnTypeDescriptor, Nullable extends boolean | undefined = undefined>(name: ColName, options: {
106
- type: Descriptor;
107
- nullable?: Nullable;
108
- }): TableBuilder<Name, Columns & Record<ColName, ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>>, PrimaryKey>;
109
- primaryKey<PK extends readonly string[]>(columns: PK, _name?: string): TableBuilder<Name, Columns, PK>;
110
- unique(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey>;
111
- index(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey>;
112
- foreignKey(_columns: readonly string[], _references: {
113
- table: string;
114
- columns: readonly string[];
115
- }, _name?: string): TableBuilder<Name, Columns, PrimaryKey>;
116
- build(): TableBuilderState<Name, Columns, PrimaryKey>;
117
- }
118
-
119
- declare class ContractBuilder<Target extends string | undefined = undefined, Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>> = Record<never, never>, Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>> = Record<never, never>, CoreHash extends string | undefined = undefined, ExtensionPacks extends Record<string, unknown> | undefined = undefined, Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined> {
120
- protected readonly state: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>;
121
- constructor(state?: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>);
122
- target<T extends string>(packRef: TargetPackRef<string, T>): ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities>;
123
- capabilities<C extends Record<string, Record<string, boolean>>>(capabilities: C): ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C>;
124
- table<TableName extends string, T extends TableBuilder<TableName, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>(name: TableName, callback: (t: TableBuilder<TableName>) => T | undefined): ContractBuilder<Target, Tables & Record<TableName, ReturnType<T['build']>>, Models, CoreHash, ExtensionPacks, Capabilities>;
125
- model<ModelName extends string, TableName extends string, M extends ModelBuilder<ModelName, TableName, Record<string, string>, Record<string, RelationDefinition>>>(name: ModelName, table: TableName, callback: (m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>) => M | undefined): ContractBuilder<Target, Tables, Models & Record<ModelName, ReturnType<M['build']>>, CoreHash, ExtensionPacks, Capabilities>;
126
- coreHash<H extends string>(hash: H): ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities>;
127
- }
128
- declare function defineContract(): ContractBuilder;
129
-
130
- type BuildStorageColumn<Nullable extends boolean, Type extends string> = {
131
- readonly nativeType: string;
132
- readonly codecId: Type;
133
- readonly nullable: Nullable;
134
- };
135
- 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;
136
- 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;
137
- type BuildStorage<Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>> = {
138
- readonly tables: {
139
- readonly [K in keyof Tables]: {
140
- readonly columns: {
141
- readonly [ColK in keyof ExtractColumns<Tables[K]>]: ExtractColumns<Tables[K]>[ColK] extends ColumnBuilderState<string, infer Null, infer TType> ? BuildStorageColumn<Null & boolean, TType> : never;
142
- };
143
- };
144
- };
145
- };
146
- type BuildStorageTables<Tables extends Record<string, TableBuilderState<string, Record<string, ColumnBuilderState<string, boolean, string>>, readonly string[] | undefined>>> = {
147
- readonly [K in keyof Tables]: {
148
- readonly columns: {
149
- readonly [ColK in keyof ExtractColumns<Tables[K]>]: ExtractColumns<Tables[K]>[ColK] extends ColumnBuilderState<string, infer Null, infer TType> ? BuildStorageColumn<Null & boolean, TType> : never;
150
- };
151
- };
152
- };
153
- type Mutable<T> = {
154
- -readonly [K in keyof T]-?: T[K];
155
- };
156
- type BuildModelFields<Fields extends Record<string, string>> = {
157
- readonly [K in keyof Fields]: {
158
- readonly column: Fields[K];
159
- };
160
- };
161
- 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;
162
- 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;
163
- type BuildModels<Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>>> = {
164
- readonly [K in keyof Models]: {
165
- readonly storage: {
166
- readonly table: Models[K]['table'];
167
- };
168
- readonly fields: BuildModelFields<ExtractModelFields<Models[K]>>;
169
- };
170
- };
171
- type BuildRelations<Models extends Record<string, ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>>> = {
172
- readonly [K in keyof Models as Models[K]['table']]: ExtractModelRelations<Models[K]>;
173
- };
174
-
175
- export { type BuildModelFields, type BuildModels, type BuildRelations, type BuildStorage, type BuildStorageColumn, type BuildStorageTables, type ColumnBuilder, type ColumnBuilderState, type ColumnTypeDescriptor, ContractBuilder, type ContractBuilderState, type ExtractColumns, type ExtractModelFields, type ExtractModelRelations, type ExtractPrimaryKey, ModelBuilder, type ModelBuilderState, type Mutable, type RelationDefinition, TableBuilder, type TableBuilderState, defineContract };
1
+ export type { ColumnBuilder, ColumnBuilderState, ColumnTypeDescriptor, ContractBuilderState, ModelBuilderState, RelationDefinition, TableBuilderState, } from './builder-state';
2
+ export { ContractBuilder, defineContract } from './contract-builder';
3
+ export { ModelBuilder } from './model-builder';
4
+ export { TableBuilder } from './table-builder';
5
+ export type { BuildModelFields, BuildModels, BuildRelations, BuildStorage, BuildStorageColumn, BuildStorageTables, ExtractColumns, ExtractModelFields, ExtractModelRelations, ExtractPrimaryKey, Mutable, } from './types';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,aAAa,EACb,kBAAkB,EAClB,oBAAoB,EACpB,oBAAoB,EACpB,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,YAAY,EACV,gBAAgB,EAChB,WAAW,EACX,cAAc,EACd,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,cAAc,EACd,kBAAkB,EAClB,qBAAqB,EACrB,iBAAiB,EACjB,OAAO,GACR,MAAM,SAAS,CAAC"}
@@ -0,0 +1,38 @@
1
+ import type { ModelBuilderState, RelationDefinition } from './builder-state';
2
+ export declare class ModelBuilder<Name extends string, Table extends string, Fields extends Record<string, string> = Record<never, never>, Relations extends Record<string, RelationDefinition> = Record<never, never>> {
3
+ private readonly _name;
4
+ private readonly _table;
5
+ private readonly _fields;
6
+ private readonly _relations;
7
+ constructor(name: Name, table: Table, fields?: Fields, relations?: Relations);
8
+ field<FieldName extends string, ColumnName extends string>(fieldName: FieldName, columnName: ColumnName): ModelBuilder<Name, Table, Fields & Record<FieldName, ColumnName>, Relations>;
9
+ relation<RelationName extends string, ToModel extends string, ToTable extends string>(name: RelationName, options: {
10
+ toModel: ToModel;
11
+ toTable: ToTable;
12
+ cardinality: '1:1' | '1:N' | 'N:1';
13
+ on: {
14
+ parentTable: Table;
15
+ parentColumns: readonly string[];
16
+ childTable: ToTable;
17
+ childColumns: readonly string[];
18
+ };
19
+ }): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;
20
+ relation<RelationName extends string, ToModel extends string, ToTable extends string, JunctionTable extends string>(name: RelationName, options: {
21
+ toModel: ToModel;
22
+ toTable: ToTable;
23
+ cardinality: 'N:M';
24
+ through: {
25
+ table: JunctionTable;
26
+ parentColumns: readonly string[];
27
+ childColumns: readonly string[];
28
+ };
29
+ on: {
30
+ parentTable: Table;
31
+ parentColumns: readonly string[];
32
+ childTable: JunctionTable;
33
+ childColumns: readonly string[];
34
+ };
35
+ }): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;
36
+ build(): ModelBuilderState<Name, Table, Fields, Relations>;
37
+ }
38
+ //# sourceMappingURL=model-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model-builder.d.ts","sourceRoot":"","sources":["../src/model-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAE7E,qBAAa,YAAY,CACvB,IAAI,SAAS,MAAM,EACnB,KAAK,SAAS,MAAM,EACpB,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,EAC5D,SAAS,SAAS,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,GAAG,MAAM,CAAC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C;QACjB,OAAO,EAAE,OAAO,CAAC;QACjB,WAAW,EAAE,KAAK,CAAC;QACnB,OAAO,EAAE;YACP,KAAK,EAAE,aAAa,CAAC;YACrB,aAAa,EAAE,SAAS,MAAM,EAAE,CAAC;YACjC,YAAY,EAAE,SAAS,MAAM,EAAE,CAAC;SACjC,CAAC;QACF,EAAE,EAAE;YACF,WAAW,EAAE,KAAK,CAAC;YACnB,aAAa,EAAE,SAAS,MAAM,EAAE,CAAC;YACjC,UAAU,EAAE,aAAa,CAAC;YAC1B,YAAY,EAAE,SAAS,MAAM,EAAE,CAAC;SACjC,CAAC;KACH,GACA,YAAY,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,GAAG,MAAM,CAAC,YAAY,EAAE,kBAAkB,CAAC,CAAC;IA0E1F,KAAK,IAAI,iBAAiB,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC;CAQ3D"}
@@ -0,0 +1,20 @@
1
+ import type { ColumnBuilderState, ColumnTypeDescriptor, TableBuilderState } from './builder-state';
2
+ export declare class TableBuilder<Name extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<never, ColumnBuilderState<string, boolean, string>>, PrimaryKey extends readonly string[] | undefined = undefined> {
3
+ private readonly _name;
4
+ private readonly _columns;
5
+ private readonly _primaryKey;
6
+ constructor(name: Name, columns?: Columns, primaryKey?: PrimaryKey);
7
+ column<ColName extends string, Descriptor extends ColumnTypeDescriptor, Nullable extends boolean | undefined = undefined>(name: ColName, options: {
8
+ type: Descriptor;
9
+ nullable?: Nullable;
10
+ }): TableBuilder<Name, Columns & Record<ColName, ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>>, PrimaryKey>;
11
+ primaryKey<PK extends readonly string[]>(columns: PK, _name?: string): TableBuilder<Name, Columns, PK>;
12
+ unique(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey>;
13
+ index(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey>;
14
+ foreignKey(_columns: readonly string[], _references: {
15
+ table: string;
16
+ columns: readonly string[];
17
+ }, _name?: string): TableBuilder<Name, Columns, PrimaryKey>;
18
+ build(): TableBuilderState<Name, Columns, PrimaryKey>;
19
+ }
20
+ //# sourceMappingURL=table-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table-builder.d.ts","sourceRoot":"","sources":["../src/table-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEnG,qBAAa,YAAY,CACvB,IAAI,SAAS,MAAM,EACnB,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC,GAAG,MAAM,CAClF,KAAK,EACL,kBAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAC5C,EACD,UAAU,SAAS,SAAS,MAAM,EAAE,GAAG,SAAS,GAAG,SAAS;IAE5D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAO;IAC7B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAU;IACnC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAa;gBAE7B,IAAI,EAAE,IAAI,EAAE,OAAO,GAAE,OAAuB,EAAE,UAAU,CAAC,EAAE,UAAU;IAMjF,MAAM,CACJ,OAAO,SAAS,MAAM,EACtB,UAAU,SAAS,oBAAoB,EACvC,QAAQ,SAAS,OAAO,GAAG,SAAS,GAAG,SAAS,EAEhD,IAAI,EAAE,OAAO,EACb,OAAO,EAAE;QACP,IAAI,EAAE,UAAU,CAAC;QACjB,QAAQ,CAAC,EAAE,QAAQ,CAAC;KACrB,GACA,YAAY,CACb,IAAI,EACJ,OAAO,GACL,MAAM,CACJ,OAAO,EACP,kBAAkB,CAAC,OAAO,EAAE,QAAQ,SAAS,IAAI,GAAG,IAAI,GAAG,KAAK,EAAE,UAAU,CAAC,SAAS,CAAC,CAAC,CACzF,EACH,UAAU,CACX;IAqBD,UAAU,CAAC,EAAE,SAAS,SAAS,MAAM,EAAE,EACrC,OAAO,EAAE,EAAE,EACX,KAAK,CAAC,EAAE,MAAM,GACb,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,CAAC;IAIlC,MAAM,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC;IAI5F,KAAK,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC;IAI3F,UAAU,CACR,QAAQ,EAAE,SAAS,MAAM,EAAE,EAC3B,WAAW,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,EAC1D,KAAK,CAAC,EAAE,MAAM,GACb,YAAY,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC;IAI1C,KAAK,IAAI,iBAAiB,CAAC,IAAI,EAAE,OAAO,EAAE,UAAU,CAAC;CAOtD"}
@@ -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,EAAE,GAAG,SAAS,CAC9B,IACC,CAAC,SAAS,iBAAiB,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;AAE5F,MAAM,MAAM,iBAAiB,CAC3B,CAAC,SAAS,iBAAiB,CACzB,MAAM,EACN,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC,EAC3D,SAAS,MAAM,EAAE,GAAG,SAAS,CAC9B,IACC,CAAC,SAAS,iBAAiB,CAC7B,MAAM,EACN,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC,EAC3D,MAAM,EAAE,CACT,GACG,EAAE,GACF,KAAK,CAAC;AAEV,MAAM,MAAM,YAAY,CACtB,M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,MAAM,GAAG;QAC5B,QAAQ,CAAC,OAAO,EAAE;YAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;SAAE,CAAC;QACzD,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAC,kBAAkB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KAClE;CACF,CAAC;AAEF,MAAM,MAAM,cAAc,CACxB,MAAM,SAAS,MAAM,CACnB,MAAM,EACN,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAC9F,IACC;IACF,QAAQ,EAAE,CAAC,IAAI,MAAM,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,qBAAqB,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;CACrF,CAAC"}
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@prisma-next/contract-authoring",
3
- "version": "0.3.0-dev.4",
3
+ "version": "0.3.0-dev.6",
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.4"
9
+ "@prisma-next/contract": "0.3.0-dev.6"
10
10
  },
11
11
  "devDependencies": {
12
12
  "@vitest/coverage-v8": "4.0.16",
@@ -15,7 +15,8 @@
15
15
  "vitest": "4.0.16"
16
16
  },
17
17
  "files": [
18
- "dist"
18
+ "dist",
19
+ "src"
19
20
  ],
20
21
  "exports": {
21
22
  ".": {
@@ -24,7 +25,7 @@
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",
@@ -0,0 +1,105 @@
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
+ export interface TableBuilderState<
22
+ Name extends string,
23
+ Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,
24
+ PrimaryKey extends readonly string[] | undefined,
25
+ > {
26
+ readonly name: Name;
27
+ readonly columns: Columns;
28
+ readonly primaryKey?: PrimaryKey;
29
+ }
30
+
31
+ export type RelationDefinition = {
32
+ readonly to: string;
33
+ readonly cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';
34
+ readonly on: {
35
+ readonly parentCols: readonly string[];
36
+ readonly childCols: readonly string[];
37
+ };
38
+ readonly through?: {
39
+ readonly table: string;
40
+ readonly parentCols: readonly string[];
41
+ readonly childCols: readonly string[];
42
+ };
43
+ };
44
+
45
+ export interface ModelBuilderState<
46
+ Name extends string,
47
+ Table extends string,
48
+ Fields extends Record<string, string>,
49
+ Relations extends Record<string, RelationDefinition>,
50
+ > {
51
+ readonly name: Name;
52
+ readonly table: Table;
53
+ readonly fields: Fields;
54
+ readonly relations: Relations;
55
+ }
56
+
57
+ export interface ContractBuilderState<
58
+ Target extends string | undefined = string | undefined,
59
+ Tables extends Record<
60
+ string,
61
+ TableBuilderState<
62
+ string,
63
+ Record<string, ColumnBuilderState<string, boolean, string>>,
64
+ readonly string[] | undefined
65
+ >
66
+ > = Record<
67
+ never,
68
+ TableBuilderState<
69
+ string,
70
+ Record<string, ColumnBuilderState<string, boolean, string>>,
71
+ readonly string[] | undefined
72
+ >
73
+ >,
74
+ Models extends Record<
75
+ string,
76
+ ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
77
+ > = Record<
78
+ never,
79
+ ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
80
+ >,
81
+ CoreHash extends string | undefined = string | undefined,
82
+ ExtensionPacks extends Record<string, unknown> | undefined = undefined,
83
+ Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined,
84
+ > {
85
+ readonly target?: Target;
86
+ readonly tables: Tables;
87
+ readonly models: Models;
88
+ readonly coreHash?: CoreHash;
89
+ readonly extensionPacks?: ExtensionPacks;
90
+ readonly capabilities?: Capabilities;
91
+ /**
92
+ * Array of extension pack namespace identifiers (e.g., ['pgvector', 'postgis']).
93
+ * Populated when extension packs are registered during contract building.
94
+ * Used to track which extension packs are included in the contract.
95
+ * Can be undefined or empty if no extension packs are registered.
96
+ * Namespace format matches the extension pack ID (e.g., 'pgvector', not 'pgvector@1.0.0').
97
+ */
98
+ readonly extensionNamespaces?: readonly string[];
99
+ }
100
+
101
+ export interface ColumnBuilder<Name extends string, Nullable extends boolean, Type extends string> {
102
+ nullable<Value extends boolean>(value?: Value): ColumnBuilder<Name, Value, Type>;
103
+ type<Id extends string>(id: Id): ColumnBuilder<Name, Nullable, Id>;
104
+ build(): ColumnBuilderState<Name, Nullable, Type>;
105
+ }
@@ -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 { 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 = new TableBuilder<TableName>(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,27 @@
1
+ export type {
2
+ ColumnBuilder,
3
+ ColumnBuilderState,
4
+ ColumnTypeDescriptor,
5
+ ContractBuilderState,
6
+ ModelBuilderState,
7
+ RelationDefinition,
8
+ TableBuilderState,
9
+ } from './builder-state';
10
+
11
+ export { ContractBuilder, defineContract } from './contract-builder';
12
+ export { ModelBuilder } from './model-builder';
13
+ export { TableBuilder } from './table-builder';
14
+
15
+ export type {
16
+ BuildModelFields,
17
+ BuildModels,
18
+ BuildRelations,
19
+ BuildStorage,
20
+ BuildStorageColumn,
21
+ BuildStorageTables,
22
+ ExtractColumns,
23
+ ExtractModelFields,
24
+ ExtractModelRelations,
25
+ ExtractPrimaryKey,
26
+ Mutable,
27
+ } 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
+ }
@@ -0,0 +1,90 @@
1
+ import type { ColumnBuilderState, ColumnTypeDescriptor, TableBuilderState } from './builder-state';
2
+
3
+ export class TableBuilder<
4
+ Name extends string,
5
+ Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<
6
+ never,
7
+ ColumnBuilderState<string, boolean, string>
8
+ >,
9
+ PrimaryKey extends readonly string[] | undefined = undefined,
10
+ > {
11
+ private readonly _name: Name;
12
+ private readonly _columns: Columns;
13
+ private readonly _primaryKey: PrimaryKey;
14
+
15
+ constructor(name: Name, columns: Columns = {} as Columns, primaryKey?: PrimaryKey) {
16
+ this._name = name;
17
+ this._columns = columns;
18
+ this._primaryKey = primaryKey as PrimaryKey;
19
+ }
20
+
21
+ column<
22
+ ColName extends string,
23
+ Descriptor extends ColumnTypeDescriptor,
24
+ Nullable extends boolean | undefined = undefined,
25
+ >(
26
+ name: ColName,
27
+ options: {
28
+ type: Descriptor;
29
+ nullable?: Nullable;
30
+ },
31
+ ): TableBuilder<
32
+ Name,
33
+ Columns &
34
+ Record<
35
+ ColName,
36
+ ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>
37
+ >,
38
+ PrimaryKey
39
+ > {
40
+ const nullable = (options.nullable ?? false) as Nullable extends true ? true : false;
41
+ const { codecId, nativeType } = options.type;
42
+
43
+ const columnState = {
44
+ name,
45
+ nullable,
46
+ type: codecId,
47
+ nativeType,
48
+ } as ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>;
49
+ return new TableBuilder(
50
+ this._name,
51
+ { ...this._columns, [name]: columnState } as Columns &
52
+ Record<
53
+ ColName,
54
+ ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>
55
+ >,
56
+ this._primaryKey,
57
+ );
58
+ }
59
+
60
+ primaryKey<PK extends readonly string[]>(
61
+ columns: PK,
62
+ _name?: string,
63
+ ): TableBuilder<Name, Columns, PK> {
64
+ return new TableBuilder(this._name, this._columns, columns);
65
+ }
66
+
67
+ unique(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey> {
68
+ return this;
69
+ }
70
+
71
+ index(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey> {
72
+ return this;
73
+ }
74
+
75
+ foreignKey(
76
+ _columns: readonly string[],
77
+ _references: { table: string; columns: readonly string[] },
78
+ _name?: string,
79
+ ): TableBuilder<Name, Columns, PrimaryKey> {
80
+ return this;
81
+ }
82
+
83
+ build(): TableBuilderState<Name, Columns, PrimaryKey> {
84
+ return {
85
+ name: this._name,
86
+ columns: this._columns,
87
+ ...(this._primaryKey !== undefined ? { primaryKey: this._primaryKey } : {}),
88
+ } as TableBuilderState<Name, Columns, PrimaryKey>;
89
+ }
90
+ }
package/src/types.ts ADDED
@@ -0,0 +1,127 @@
1
+ import type {
2
+ ColumnBuilderState,
3
+ ModelBuilderState,
4
+ RelationDefinition,
5
+ TableBuilderState,
6
+ } from './builder-state';
7
+
8
+ export type BuildStorageColumn<Nullable extends boolean, Type extends string> = {
9
+ readonly nativeType: string;
10
+ readonly codecId: Type;
11
+ readonly nullable: Nullable;
12
+ };
13
+
14
+ export type ExtractColumns<
15
+ T extends TableBuilderState<
16
+ string,
17
+ Record<string, ColumnBuilderState<string, boolean, string>>,
18
+ readonly string[] | undefined
19
+ >,
20
+ > = T extends TableBuilderState<string, infer C, readonly string[] | undefined> ? C : never;
21
+
22
+ export type ExtractPrimaryKey<
23
+ T extends TableBuilderState<
24
+ string,
25
+ Record<string, ColumnBuilderState<string, boolean, string>>,
26
+ readonly string[] | undefined
27
+ >,
28
+ > = T extends TableBuilderState<
29
+ string,
30
+ Record<string, ColumnBuilderState<string, boolean, string>>,
31
+ infer PK
32
+ >
33
+ ? PK
34
+ : never;
35
+
36
+ export type BuildStorage<
37
+ Tables extends Record<
38
+ string,
39
+ TableBuilderState<
40
+ string,
41
+ Record<string, ColumnBuilderState<string, boolean, string>>,
42
+ readonly string[] | undefined
43
+ >
44
+ >,
45
+ > = {
46
+ readonly tables: {
47
+ readonly [K in keyof Tables]: {
48
+ readonly columns: {
49
+ readonly [ColK in keyof ExtractColumns<Tables[K]>]: ExtractColumns<
50
+ Tables[K]
51
+ >[ColK] extends ColumnBuilderState<string, infer Null, infer TType>
52
+ ? BuildStorageColumn<Null & boolean, TType>
53
+ : never;
54
+ };
55
+ };
56
+ };
57
+ };
58
+
59
+ export type BuildStorageTables<
60
+ Tables extends Record<
61
+ string,
62
+ TableBuilderState<
63
+ string,
64
+ Record<string, ColumnBuilderState<string, boolean, string>>,
65
+ readonly string[] | undefined
66
+ >
67
+ >,
68
+ > = {
69
+ readonly [K in keyof Tables]: {
70
+ readonly columns: {
71
+ readonly [ColK in keyof ExtractColumns<Tables[K]>]: ExtractColumns<
72
+ Tables[K]
73
+ >[ColK] extends ColumnBuilderState<string, infer Null, infer TType>
74
+ ? BuildStorageColumn<Null & boolean, TType>
75
+ : never;
76
+ };
77
+ };
78
+ };
79
+
80
+ export type Mutable<T> = {
81
+ -readonly [K in keyof T]-?: T[K];
82
+ };
83
+
84
+ export type BuildModelFields<Fields extends Record<string, string>> = {
85
+ readonly [K in keyof Fields]: { readonly column: Fields[K] };
86
+ };
87
+
88
+ export type ExtractModelFields<
89
+ T extends ModelBuilderState<
90
+ string,
91
+ string,
92
+ Record<string, string>,
93
+ Record<string, RelationDefinition>
94
+ >,
95
+ > = T extends ModelBuilderState<string, string, infer F, Record<string, RelationDefinition>>
96
+ ? F
97
+ : never;
98
+
99
+ export type ExtractModelRelations<
100
+ T extends ModelBuilderState<
101
+ string,
102
+ string,
103
+ Record<string, string>,
104
+ Record<string, RelationDefinition>
105
+ >,
106
+ > = T extends ModelBuilderState<string, string, Record<string, string>, infer R> ? R : never;
107
+
108
+ export type BuildModels<
109
+ Models extends Record<
110
+ string,
111
+ ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
112
+ >,
113
+ > = {
114
+ readonly [K in keyof Models]: {
115
+ readonly storage: { readonly table: Models[K]['table'] };
116
+ readonly fields: BuildModelFields<ExtractModelFields<Models[K]>>;
117
+ };
118
+ };
119
+
120
+ export type BuildRelations<
121
+ Models extends Record<
122
+ string,
123
+ ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
124
+ >,
125
+ > = {
126
+ readonly [K in keyof Models as Models[K]['table']]: ExtractModelRelations<Models[K]>;
127
+ };