@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.
- package/dist/builder-state.d.ts +89 -0
- package/dist/builder-state.d.ts.map +1 -0
- package/dist/contract-builder.d.ts +15 -0
- package/dist/contract-builder.d.ts.map +1 -0
- package/dist/index.d.ts +6 -175
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +82 -20
- package/dist/index.js.map +1 -1
- package/dist/model-builder.d.ts +38 -0
- package/dist/model-builder.d.ts.map +1 -0
- package/dist/table-builder.d.ts +32 -0
- package/dist/table-builder.d.ts.map +1 -0
- package/dist/types.d.ts +46 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +13 -12
- package/src/builder-state.ts +137 -0
- package/src/contract-builder.ts +159 -0
- package/src/index.ts +30 -0
- package/src/model-builder.ts +160 -0
- package/src/table-builder.ts +195 -0
- package/src/types.ts +129 -0
|
@@ -0,0 +1,89 @@
|
|
|
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
|
+
/**
|
|
16
|
+
* Unique constraint definition for table builder.
|
|
17
|
+
*/
|
|
18
|
+
export interface UniqueConstraintDef {
|
|
19
|
+
readonly columns: readonly string[];
|
|
20
|
+
readonly name?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Index definition for table builder.
|
|
24
|
+
*/
|
|
25
|
+
export interface IndexDef {
|
|
26
|
+
readonly columns: readonly string[];
|
|
27
|
+
readonly name?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Foreign key definition for table builder.
|
|
31
|
+
*/
|
|
32
|
+
export interface ForeignKeyDef {
|
|
33
|
+
readonly columns: readonly string[];
|
|
34
|
+
readonly references: {
|
|
35
|
+
readonly table: string;
|
|
36
|
+
readonly columns: readonly string[];
|
|
37
|
+
};
|
|
38
|
+
readonly name?: string;
|
|
39
|
+
}
|
|
40
|
+
export interface TableBuilderState<Name extends string, Columns extends Record<string, ColumnBuilderState<string, boolean, string>>, PrimaryKey extends readonly string[] | undefined> {
|
|
41
|
+
readonly name: Name;
|
|
42
|
+
readonly columns: Columns;
|
|
43
|
+
readonly primaryKey?: PrimaryKey;
|
|
44
|
+
readonly primaryKeyName?: string;
|
|
45
|
+
readonly uniques: readonly UniqueConstraintDef[];
|
|
46
|
+
readonly indexes: readonly IndexDef[];
|
|
47
|
+
readonly foreignKeys: readonly ForeignKeyDef[];
|
|
48
|
+
}
|
|
49
|
+
export type RelationDefinition = {
|
|
50
|
+
readonly to: string;
|
|
51
|
+
readonly cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';
|
|
52
|
+
readonly on: {
|
|
53
|
+
readonly parentCols: readonly string[];
|
|
54
|
+
readonly childCols: readonly string[];
|
|
55
|
+
};
|
|
56
|
+
readonly through?: {
|
|
57
|
+
readonly table: string;
|
|
58
|
+
readonly parentCols: readonly string[];
|
|
59
|
+
readonly childCols: readonly string[];
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
export interface ModelBuilderState<Name extends string, Table extends string, Fields extends Record<string, string>, Relations extends Record<string, RelationDefinition>> {
|
|
63
|
+
readonly name: Name;
|
|
64
|
+
readonly table: Table;
|
|
65
|
+
readonly fields: Fields;
|
|
66
|
+
readonly relations: Relations;
|
|
67
|
+
}
|
|
68
|
+
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> {
|
|
69
|
+
readonly target?: Target;
|
|
70
|
+
readonly tables: Tables;
|
|
71
|
+
readonly models: Models;
|
|
72
|
+
readonly coreHash?: CoreHash;
|
|
73
|
+
readonly extensionPacks?: ExtensionPacks;
|
|
74
|
+
readonly capabilities?: Capabilities;
|
|
75
|
+
/**
|
|
76
|
+
* Array of extension pack namespace identifiers (e.g., ['pgvector', 'postgis']).
|
|
77
|
+
* Populated when extension packs are registered during contract building.
|
|
78
|
+
* Used to track which extension packs are included in the contract.
|
|
79
|
+
* Can be undefined or empty if no extension packs are registered.
|
|
80
|
+
* Namespace format matches the extension pack ID (e.g., 'pgvector', not 'pgvector@1.0.0').
|
|
81
|
+
*/
|
|
82
|
+
readonly extensionNamespaces?: readonly string[];
|
|
83
|
+
}
|
|
84
|
+
export interface ColumnBuilder<Name extends string, Nullable extends boolean, Type extends string> {
|
|
85
|
+
nullable<Value extends boolean>(value?: Value): ColumnBuilder<Name, Value, Type>;
|
|
86
|
+
type<Id extends string>(id: Id): ColumnBuilder<Name, Nullable, Id>;
|
|
87
|
+
build(): ColumnBuilderState<Name, Nullable, Type>;
|
|
88
|
+
}
|
|
89
|
+
//# 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,CAA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C,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;AAE5D,qBAAa,eAAe,CAC1B,MAAM,SAAS,MAAM,GAAG,SAAS,GAAG,SAAS,EAC7C,MAAM,SAAS,MAAM,CACnB,MAAM,EACN,i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,CAAC,EAAE,cAAc,EAAE,YAAY,CAAC;CAM5E;AAED,wBAAgB,cAAc,IAAI,eAAe,CAEhD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,175 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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, ForeignKeyDef, IndexDef, ModelBuilderState, RelationDefinition, TableBuilderState, UniqueConstraintDef, } from './builder-state';
|
|
2
|
+
export { ContractBuilder, defineContract } from './contract-builder';
|
|
3
|
+
export { ModelBuilder } from './model-builder';
|
|
4
|
+
export { createTable, 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,aAAa,EACb,QAAQ,EACR,iBAAiB,EACjB,kBAAkB,EAClB,iBAAiB,EACjB,mBAAmB,GACpB,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,WAAW,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE5D,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"}
|
package/dist/index.js
CHANGED
|
@@ -74,14 +74,31 @@ var ModelBuilder = class _ModelBuilder {
|
|
|
74
74
|
};
|
|
75
75
|
|
|
76
76
|
// src/table-builder.ts
|
|
77
|
+
function createTable(name) {
|
|
78
|
+
return new TableBuilder(name, {}, void 0, void 0, [], [], []);
|
|
79
|
+
}
|
|
77
80
|
var TableBuilder = class _TableBuilder {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
_state;
|
|
82
|
+
/** @internal Use createTable() instead */
|
|
83
|
+
constructor(name, columns, primaryKey, primaryKeyName, uniques, indexes, foreignKeys) {
|
|
84
|
+
this._state = {
|
|
85
|
+
name,
|
|
86
|
+
columns,
|
|
87
|
+
primaryKey,
|
|
88
|
+
primaryKeyName,
|
|
89
|
+
uniques,
|
|
90
|
+
indexes,
|
|
91
|
+
foreignKeys
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
get _name() {
|
|
95
|
+
return this._state.name;
|
|
96
|
+
}
|
|
97
|
+
get _columns() {
|
|
98
|
+
return this._state.columns;
|
|
99
|
+
}
|
|
100
|
+
get _primaryKey() {
|
|
101
|
+
return this._state.primaryKey;
|
|
85
102
|
}
|
|
86
103
|
column(name, options) {
|
|
87
104
|
const nullable = options.nullable ?? false;
|
|
@@ -92,29 +109,73 @@ var TableBuilder = class _TableBuilder {
|
|
|
92
109
|
type: codecId,
|
|
93
110
|
nativeType
|
|
94
111
|
};
|
|
112
|
+
const newColumns = { ...this._columns, [name]: columnState };
|
|
95
113
|
return new _TableBuilder(
|
|
96
|
-
this.
|
|
97
|
-
|
|
98
|
-
this.
|
|
114
|
+
this._state.name,
|
|
115
|
+
newColumns,
|
|
116
|
+
this._state.primaryKey,
|
|
117
|
+
this._state.primaryKeyName,
|
|
118
|
+
this._state.uniques,
|
|
119
|
+
this._state.indexes,
|
|
120
|
+
this._state.foreignKeys
|
|
99
121
|
);
|
|
100
122
|
}
|
|
101
|
-
primaryKey(columns,
|
|
102
|
-
return new _TableBuilder(
|
|
123
|
+
primaryKey(columns, name) {
|
|
124
|
+
return new _TableBuilder(
|
|
125
|
+
this._state.name,
|
|
126
|
+
this._state.columns,
|
|
127
|
+
columns,
|
|
128
|
+
name,
|
|
129
|
+
this._state.uniques,
|
|
130
|
+
this._state.indexes,
|
|
131
|
+
this._state.foreignKeys
|
|
132
|
+
);
|
|
103
133
|
}
|
|
104
|
-
unique(
|
|
105
|
-
|
|
134
|
+
unique(columns, name) {
|
|
135
|
+
const constraint = name ? { columns, name } : { columns };
|
|
136
|
+
return new _TableBuilder(
|
|
137
|
+
this._state.name,
|
|
138
|
+
this._state.columns,
|
|
139
|
+
this._state.primaryKey,
|
|
140
|
+
this._state.primaryKeyName,
|
|
141
|
+
[...this._state.uniques, constraint],
|
|
142
|
+
this._state.indexes,
|
|
143
|
+
this._state.foreignKeys
|
|
144
|
+
);
|
|
106
145
|
}
|
|
107
|
-
index(
|
|
108
|
-
|
|
146
|
+
index(columns, name) {
|
|
147
|
+
const indexDef = name ? { columns, name } : { columns };
|
|
148
|
+
return new _TableBuilder(
|
|
149
|
+
this._state.name,
|
|
150
|
+
this._state.columns,
|
|
151
|
+
this._state.primaryKey,
|
|
152
|
+
this._state.primaryKeyName,
|
|
153
|
+
this._state.uniques,
|
|
154
|
+
[...this._state.indexes, indexDef],
|
|
155
|
+
this._state.foreignKeys
|
|
156
|
+
);
|
|
109
157
|
}
|
|
110
|
-
foreignKey(
|
|
111
|
-
|
|
158
|
+
foreignKey(columns, references, name) {
|
|
159
|
+
const fkDef = name ? { columns, references, name } : { columns, references };
|
|
160
|
+
return new _TableBuilder(
|
|
161
|
+
this._state.name,
|
|
162
|
+
this._state.columns,
|
|
163
|
+
this._state.primaryKey,
|
|
164
|
+
this._state.primaryKeyName,
|
|
165
|
+
this._state.uniques,
|
|
166
|
+
this._state.indexes,
|
|
167
|
+
[...this._state.foreignKeys, fkDef]
|
|
168
|
+
);
|
|
112
169
|
}
|
|
113
170
|
build() {
|
|
114
171
|
return {
|
|
115
172
|
name: this._name,
|
|
116
173
|
columns: this._columns,
|
|
117
|
-
...this._primaryKey !== void 0 ? { primaryKey: this._primaryKey } : {}
|
|
174
|
+
...this._primaryKey !== void 0 ? { primaryKey: this._primaryKey } : {},
|
|
175
|
+
...this._state.primaryKeyName !== void 0 ? { primaryKeyName: this._state.primaryKeyName } : {},
|
|
176
|
+
uniques: this._state.uniques,
|
|
177
|
+
indexes: this._state.indexes,
|
|
178
|
+
foreignKeys: this._state.foreignKeys
|
|
118
179
|
};
|
|
119
180
|
}
|
|
120
181
|
};
|
|
@@ -141,7 +202,7 @@ var ContractBuilder = class _ContractBuilder {
|
|
|
141
202
|
});
|
|
142
203
|
}
|
|
143
204
|
table(name, callback) {
|
|
144
|
-
const tableBuilder =
|
|
205
|
+
const tableBuilder = createTable(name);
|
|
145
206
|
const result = callback(tableBuilder);
|
|
146
207
|
const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;
|
|
147
208
|
const tableState = finalBuilder.build();
|
|
@@ -174,6 +235,7 @@ export {
|
|
|
174
235
|
ContractBuilder,
|
|
175
236
|
ModelBuilder,
|
|
176
237
|
TableBuilder,
|
|
238
|
+
createTable,
|
|
177
239
|
defineContract
|
|
178
240
|
};
|
|
179
241
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/model-builder.ts","../src/table-builder.ts","../src/contract-builder.ts"],"sourcesContent":["import type { ModelBuilderState, RelationDefinition } from './builder-state';\n\nexport class ModelBuilder<\n Name extends string,\n Table extends string,\n Fields extends Record<string, string> = Record<never, never>,\n Relations extends Record<string, RelationDefinition> = Record<never, never>,\n> {\n private readonly _name: Name;\n private readonly _table: Table;\n private readonly _fields: Fields;\n private readonly _relations: Relations;\n\n constructor(\n name: Name,\n table: Table,\n fields: Fields = {} as Fields,\n relations: Relations = {} as Relations,\n ) {\n this._name = name;\n this._table = table;\n this._fields = fields;\n this._relations = relations;\n }\n\n field<FieldName extends string, ColumnName extends string>(\n fieldName: FieldName,\n columnName: ColumnName,\n ): ModelBuilder<Name, Table, Fields & Record<FieldName, ColumnName>, Relations> {\n return new ModelBuilder(\n this._name,\n this._table,\n {\n ...this._fields,\n [fieldName]: columnName,\n } as Fields & Record<FieldName, ColumnName>,\n this._relations,\n );\n }\n\n relation<RelationName extends string, ToModel extends string, ToTable extends string>(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: '1:1' | '1:N' | 'N:1';\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: ToTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;\n relation<\n RelationName extends string,\n ToModel extends string,\n ToTable extends string,\n JunctionTable extends string,\n >(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: 'N:M';\n through: {\n table: JunctionTable;\n parentColumns: readonly string[];\n childColumns: readonly string[];\n };\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: JunctionTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;\n relation<\n RelationName extends string,\n ToModel extends string,\n ToTable extends string,\n JunctionTable extends string = never,\n >(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';\n through?: {\n table: JunctionTable;\n parentColumns: readonly string[];\n childColumns: readonly string[];\n };\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: ToTable | JunctionTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>> {\n // Validate parentTable matches model's table\n if (options.on.parentTable !== this._table) {\n throw new Error(\n `Relation \"${name}\" parentTable \"${options.on.parentTable}\" does not match model table \"${this._table}\"`,\n );\n }\n\n // Validate childTable matches toTable (for non-N:M) or through.table (for N:M)\n if (options.cardinality === 'N:M') {\n if (!options.through) {\n throw new Error(`Relation \"${name}\" with cardinality \"N:M\" requires through field`);\n }\n if (options.on.childTable !== options.through.table) {\n throw new Error(\n `Relation \"${name}\" childTable \"${options.on.childTable}\" does not match through.table \"${options.through.table}\"`,\n );\n }\n } else {\n if (options.on.childTable !== options.toTable) {\n throw new Error(\n `Relation \"${name}\" childTable \"${options.on.childTable}\" does not match toTable \"${options.toTable}\"`,\n );\n }\n }\n\n const relationDef: RelationDefinition = {\n to: options.toModel,\n cardinality: options.cardinality,\n on: {\n parentCols: options.on.parentColumns,\n childCols: options.on.childColumns,\n },\n ...(options.through\n ? {\n through: {\n table: options.through.table,\n parentCols: options.through.parentColumns,\n childCols: options.through.childColumns,\n },\n }\n : undefined),\n };\n\n return new ModelBuilder(this._name, this._table, this._fields, {\n ...this._relations,\n [name]: relationDef,\n } as Relations & Record<RelationName, RelationDefinition>);\n }\n\n build(): ModelBuilderState<Name, Table, Fields, Relations> {\n return {\n name: this._name,\n table: this._table,\n fields: this._fields,\n relations: this._relations,\n };\n }\n}\n","import type { ColumnBuilderState, ColumnTypeDescriptor, TableBuilderState } from './builder-state';\n\nexport class TableBuilder<\n Name extends string,\n Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<\n never,\n ColumnBuilderState<string, boolean, string>\n >,\n PrimaryKey extends readonly string[] | undefined = undefined,\n> {\n private readonly _name: Name;\n private readonly _columns: Columns;\n private readonly _primaryKey: PrimaryKey;\n\n constructor(name: Name, columns: Columns = {} as Columns, primaryKey?: PrimaryKey) {\n this._name = name;\n this._columns = columns;\n this._primaryKey = primaryKey as PrimaryKey;\n }\n\n column<\n ColName extends string,\n Descriptor extends ColumnTypeDescriptor,\n Nullable extends boolean | undefined = undefined,\n >(\n name: ColName,\n options: {\n type: Descriptor;\n nullable?: Nullable;\n },\n ): TableBuilder<\n Name,\n Columns &\n Record<\n ColName,\n ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>\n >,\n PrimaryKey\n > {\n const nullable = (options.nullable ?? false) as Nullable extends true ? true : false;\n const { codecId, nativeType } = options.type;\n\n const columnState = {\n name,\n nullable,\n type: codecId,\n nativeType,\n } as ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>;\n return new TableBuilder(\n this._name,\n { ...this._columns, [name]: columnState } as Columns &\n Record<\n ColName,\n ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>\n >,\n this._primaryKey,\n );\n }\n\n primaryKey<PK extends readonly string[]>(\n columns: PK,\n _name?: string,\n ): TableBuilder<Name, Columns, PK> {\n return new TableBuilder(this._name, this._columns, columns);\n }\n\n unique(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey> {\n return this;\n }\n\n index(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey> {\n return this;\n }\n\n foreignKey(\n _columns: readonly string[],\n _references: { table: string; columns: readonly string[] },\n _name?: string,\n ): TableBuilder<Name, Columns, PrimaryKey> {\n return this;\n }\n\n build(): TableBuilderState<Name, Columns, PrimaryKey> {\n return {\n name: this._name,\n columns: this._columns,\n ...(this._primaryKey !== undefined ? { primaryKey: this._primaryKey } : {}),\n } as TableBuilderState<Name, Columns, PrimaryKey>;\n }\n}\n","import type { TargetPackRef } from '@prisma-next/contract/framework-components';\nimport type {\n ColumnBuilderState,\n ContractBuilderState,\n ModelBuilderState,\n RelationDefinition,\n TableBuilderState,\n} from './builder-state';\nimport { ModelBuilder } from './model-builder';\nimport { TableBuilder } from './table-builder';\n\nexport class ContractBuilder<\n Target extends string | undefined = undefined,\n Tables extends Record<\n string,\n TableBuilderState<\n string,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >\n > = Record<never, never>,\n Models extends Record<\n string,\n ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>\n > = Record<never, never>,\n CoreHash extends string | undefined = undefined,\n ExtensionPacks extends Record<string, unknown> | undefined = undefined,\n Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined,\n> {\n protected readonly state: ContractBuilderState<\n Target,\n Tables,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >;\n\n constructor(\n state?: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>,\n ) {\n this.state =\n state ??\n ({\n tables: {},\n models: {},\n } as ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>);\n }\n\n target<T extends string>(\n packRef: TargetPackRef<string, T>,\n ): ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities> {\n return new ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities>({\n ...this.state,\n target: packRef.targetId,\n });\n }\n\n capabilities<C extends Record<string, Record<string, boolean>>>(\n capabilities: C,\n ): ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C> {\n return new ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C>({\n ...this.state,\n capabilities,\n });\n }\n\n table<\n TableName extends string,\n T extends TableBuilder<\n TableName,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >,\n >(\n name: TableName,\n callback: (t: TableBuilder<TableName>) => T | undefined,\n ): ContractBuilder<\n Target,\n Tables & Record<TableName, ReturnType<T['build']>>,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n const tableBuilder = new TableBuilder<TableName>(name);\n const result = callback(tableBuilder);\n const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;\n const tableState = finalBuilder.build();\n\n return new ContractBuilder<\n Target,\n Tables & Record<TableName, ReturnType<T['build']>>,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n tables: { ...this.state.tables, [name]: tableState } as Tables &\n Record<TableName, ReturnType<T['build']>>,\n });\n }\n\n model<\n ModelName extends string,\n TableName extends string,\n M extends ModelBuilder<\n ModelName,\n TableName,\n Record<string, string>,\n Record<string, RelationDefinition>\n >,\n >(\n name: ModelName,\n table: TableName,\n callback: (\n m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>,\n ) => M | undefined,\n ): ContractBuilder<\n Target,\n Tables,\n Models & Record<ModelName, ReturnType<M['build']>>,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n const modelBuilder = new ModelBuilder<ModelName, TableName>(name, table);\n const result = callback(modelBuilder);\n const finalBuilder = result instanceof ModelBuilder ? result : modelBuilder;\n const modelState = finalBuilder.build();\n\n return new ContractBuilder<\n Target,\n Tables,\n Models & Record<ModelName, ReturnType<M['build']>>,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n models: { ...this.state.models, [name]: modelState } as Models &\n Record<ModelName, ReturnType<M['build']>>,\n });\n }\n\n coreHash<H extends string>(\n hash: H,\n ): ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities> {\n return new ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities>({\n ...this.state,\n coreHash: hash,\n });\n }\n}\n\nexport function defineContract(): ContractBuilder {\n return new ContractBuilder();\n}\n"],"mappings":";AAEO,IAAM,eAAN,MAAM,cAKX;AAAA,EACiB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEjB,YACE,MACA,OACA,SAAiB,CAAC,GAClB,YAAuB,CAAC,GACxB;AACA,SAAK,QAAQ;AACb,SAAK,SAAS;AACd,SAAK,UAAU;AACf,SAAK,aAAa;AAAA,EACpB;AAAA,EAEA,MACE,WACA,YAC8E;AAC9E,WAAO,IAAI;AAAA,MACT,KAAK;AAAA,MACL,KAAK;AAAA,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QAAQ,CAAC;AAAA,MACT,QAAQ,CAAC;AAAA,IACX;AAAA,EACJ;AAAA,EAEA,OACE,SAC4E;AAC5E,WAAO,IAAI,iBAA2E;AAAA,MACpF,GAAG,KAAK;AAAA,MACR,QAAQ,QAAQ;AAAA,IAClB,CAAC;AAAA,EACH;AAAA,EAEA,aACE,cACsE;AACtE,WAAO,IAAI,iBAAqE;AAAA,MAC9E,GAAG,KAAK;AAAA,MACR;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAQE,MACA,UAQA;AACA,UAAM,eAAe,IAAI,aAAwB,IAAI;AACrD,UAAM,SAAS,SAAS,YAAY;AACpC,UAAM,eAAe,kBAAkB,eAAe,SAAS;AAC/D,UAAM,aAAa,aAAa,MAAM;AAEtC,WAAO,IAAI,iBAOT;AAAA,MACA,GAAG,KAAK;AAAA,MACR,QAAQ,EAAE,GAAG,KAAK,MAAM,QAAQ,CAAC,IAAI,GAAG,WAAW;AAAA,IAErD,CAAC;AAAA,EACH;AAAA,EAEA,MAUE,MACA,OACA,UAUA;AACA,UAAM,eAAe,IAAI,aAAmC,MAAM,KAAK;AACvE,UAAM,SAAS,SAAS,YAAY;AACpC,UAAM,eAAe,kBAAkB,eAAe,SAAS;AAC/D,UAAM,aAAa,aAAa,MAAM;AAEtC,WAAO,IAAI,iBAOT;AAAA,MACA,GAAG,KAAK;AAAA,MACR,QAAQ,EAAE,GAAG,KAAK,MAAM,QAAQ,CAAC,IAAI,GAAG,WAAW;AAAA,IAErD,CAAC;AAAA,EACH;AAAA,EAEA,SACE,MAC0E;AAC1E,WAAO,IAAI,iBAAyE;AAAA,MAClF,GAAG,KAAK;AAAA,MACR,UAAU;AAAA,IACZ,CAAC;AAAA,EACH;AACF;AAEO,SAAS,iBAAkC;AAChD,SAAO,IAAI,gBAAgB;AAC7B;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/model-builder.ts","../src/table-builder.ts","../src/contract-builder.ts"],"sourcesContent":["import type { ModelBuilderState, RelationDefinition } from './builder-state';\n\nexport class ModelBuilder<\n Name extends string,\n Table extends string,\n Fields extends Record<string, string> = Record<never, never>,\n Relations extends Record<string, RelationDefinition> = Record<never, never>,\n> {\n private readonly _name: Name;\n private readonly _table: Table;\n private readonly _fields: Fields;\n private readonly _relations: Relations;\n\n constructor(\n name: Name,\n table: Table,\n fields: Fields = {} as Fields,\n relations: Relations = {} as Relations,\n ) {\n this._name = name;\n this._table = table;\n this._fields = fields;\n this._relations = relations;\n }\n\n field<FieldName extends string, ColumnName extends string>(\n fieldName: FieldName,\n columnName: ColumnName,\n ): ModelBuilder<Name, Table, Fields & Record<FieldName, ColumnName>, Relations> {\n return new ModelBuilder(\n this._name,\n this._table,\n {\n ...this._fields,\n [fieldName]: columnName,\n } as Fields & Record<FieldName, ColumnName>,\n this._relations,\n );\n }\n\n relation<RelationName extends string, ToModel extends string, ToTable extends string>(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: '1:1' | '1:N' | 'N:1';\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: ToTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;\n relation<\n RelationName extends string,\n ToModel extends string,\n ToTable extends string,\n JunctionTable extends string,\n >(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: 'N:M';\n through: {\n table: JunctionTable;\n parentColumns: readonly string[];\n childColumns: readonly string[];\n };\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: JunctionTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>>;\n relation<\n RelationName extends string,\n ToModel extends string,\n ToTable extends string,\n JunctionTable extends string = never,\n >(\n name: RelationName,\n options: {\n toModel: ToModel;\n toTable: ToTable;\n cardinality: '1:1' | '1:N' | 'N:1' | 'N:M';\n through?: {\n table: JunctionTable;\n parentColumns: readonly string[];\n childColumns: readonly string[];\n };\n on: {\n parentTable: Table;\n parentColumns: readonly string[];\n childTable: ToTable | JunctionTable;\n childColumns: readonly string[];\n };\n },\n ): ModelBuilder<Name, Table, Fields, Relations & Record<RelationName, RelationDefinition>> {\n // Validate parentTable matches model's table\n if (options.on.parentTable !== this._table) {\n throw new Error(\n `Relation \"${name}\" parentTable \"${options.on.parentTable}\" does not match model table \"${this._table}\"`,\n );\n }\n\n // Validate childTable matches toTable (for non-N:M) or through.table (for N:M)\n if (options.cardinality === 'N:M') {\n if (!options.through) {\n throw new Error(`Relation \"${name}\" with cardinality \"N:M\" requires through field`);\n }\n if (options.on.childTable !== options.through.table) {\n throw new Error(\n `Relation \"${name}\" childTable \"${options.on.childTable}\" does not match through.table \"${options.through.table}\"`,\n );\n }\n } else {\n if (options.on.childTable !== options.toTable) {\n throw new Error(\n `Relation \"${name}\" childTable \"${options.on.childTable}\" does not match toTable \"${options.toTable}\"`,\n );\n }\n }\n\n const relationDef: RelationDefinition = {\n to: options.toModel,\n cardinality: options.cardinality,\n on: {\n parentCols: options.on.parentColumns,\n childCols: options.on.childColumns,\n },\n ...(options.through\n ? {\n through: {\n table: options.through.table,\n parentCols: options.through.parentColumns,\n childCols: options.through.childColumns,\n },\n }\n : undefined),\n };\n\n return new ModelBuilder(this._name, this._table, this._fields, {\n ...this._relations,\n [name]: relationDef,\n } as Relations & Record<RelationName, RelationDefinition>);\n }\n\n build(): ModelBuilderState<Name, Table, Fields, Relations> {\n return {\n name: this._name,\n table: this._table,\n fields: this._fields,\n relations: this._relations,\n };\n }\n}\n","import type {\n ColumnBuilderState,\n ColumnTypeDescriptor,\n ForeignKeyDef,\n IndexDef,\n TableBuilderState,\n UniqueConstraintDef,\n} from './builder-state';\n\ninterface TableBuilderInternalState<\n Name extends string,\n Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,\n PrimaryKey extends readonly string[] | undefined,\n> {\n readonly name: Name;\n readonly columns: Columns;\n readonly primaryKey: PrimaryKey;\n readonly primaryKeyName: string | undefined;\n readonly uniques: readonly UniqueConstraintDef[];\n readonly indexes: readonly IndexDef[];\n readonly foreignKeys: readonly ForeignKeyDef[];\n}\n\n/**\n * Creates a new table builder with the given name.\n * This is the preferred way to create a TableBuilder - it ensures\n * type parameters are inferred correctly without unsafe casts.\n */\nexport function createTable<Name extends string>(name: Name): TableBuilder<Name> {\n return new TableBuilder(name, {}, undefined, undefined, [], [], []);\n}\n\n/**\n * Builder for defining table structure with type-safe chaining.\n * Use `createTable(name)` to create instances.\n */\nexport class TableBuilder<\n Name extends string,\n Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<\n never,\n ColumnBuilderState<string, boolean, string>\n >,\n PrimaryKey extends readonly string[] | undefined = undefined,\n> {\n private readonly _state: TableBuilderInternalState<Name, Columns, PrimaryKey>;\n\n /** @internal Use createTable() instead */\n constructor(\n name: Name,\n columns: Columns,\n primaryKey: PrimaryKey,\n primaryKeyName: string | undefined,\n uniques: readonly UniqueConstraintDef[],\n indexes: readonly IndexDef[],\n foreignKeys: readonly ForeignKeyDef[],\n ) {\n this._state = {\n name,\n columns,\n primaryKey,\n primaryKeyName,\n uniques,\n indexes,\n foreignKeys,\n };\n }\n\n private get _name(): Name {\n return this._state.name;\n }\n\n private get _columns(): Columns {\n return this._state.columns;\n }\n\n private get _primaryKey(): PrimaryKey {\n return this._state.primaryKey;\n }\n\n column<\n ColName extends string,\n Descriptor extends ColumnTypeDescriptor,\n Nullable extends boolean | undefined = undefined,\n >(\n name: ColName,\n options: {\n type: Descriptor;\n nullable?: Nullable;\n },\n ): TableBuilder<\n Name,\n Columns &\n Record<\n ColName,\n ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>\n >,\n PrimaryKey\n > {\n const nullable = (options.nullable ?? false) as Nullable extends true ? true : false;\n const { codecId, nativeType } = options.type;\n\n const columnState = {\n name,\n nullable,\n type: codecId,\n nativeType,\n } as ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>;\n const newColumns = { ...this._columns, [name]: columnState } as Columns &\n Record<\n ColName,\n ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>\n >;\n return new TableBuilder(\n this._state.name,\n newColumns,\n this._state.primaryKey,\n this._state.primaryKeyName,\n this._state.uniques,\n this._state.indexes,\n this._state.foreignKeys,\n );\n }\n\n primaryKey<PK extends readonly string[]>(\n columns: PK,\n name?: string,\n ): TableBuilder<Name, Columns, PK> {\n return new TableBuilder(\n this._state.name,\n this._state.columns,\n columns,\n name,\n this._state.uniques,\n this._state.indexes,\n this._state.foreignKeys,\n );\n }\n\n unique(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {\n const constraint: UniqueConstraintDef = name ? { columns, name } : { columns };\n return new TableBuilder(\n this._state.name,\n this._state.columns,\n this._state.primaryKey,\n this._state.primaryKeyName,\n [...this._state.uniques, constraint],\n this._state.indexes,\n this._state.foreignKeys,\n );\n }\n\n index(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {\n const indexDef: IndexDef = name ? { columns, name } : { columns };\n return new TableBuilder(\n this._state.name,\n this._state.columns,\n this._state.primaryKey,\n this._state.primaryKeyName,\n this._state.uniques,\n [...this._state.indexes, indexDef],\n this._state.foreignKeys,\n );\n }\n\n foreignKey(\n columns: readonly string[],\n references: { table: string; columns: readonly string[] },\n name?: string,\n ): TableBuilder<Name, Columns, PrimaryKey> {\n const fkDef: ForeignKeyDef = name ? { columns, references, name } : { columns, references };\n return new TableBuilder(\n this._state.name,\n this._state.columns,\n this._state.primaryKey,\n this._state.primaryKeyName,\n this._state.uniques,\n this._state.indexes,\n [...this._state.foreignKeys, fkDef],\n );\n }\n\n build(): TableBuilderState<Name, Columns, PrimaryKey> {\n return {\n name: this._name,\n columns: this._columns,\n ...(this._primaryKey !== undefined ? { primaryKey: this._primaryKey } : {}),\n ...(this._state.primaryKeyName !== undefined\n ? { primaryKeyName: this._state.primaryKeyName }\n : {}),\n uniques: this._state.uniques,\n indexes: this._state.indexes,\n foreignKeys: this._state.foreignKeys,\n } as TableBuilderState<Name, Columns, PrimaryKey>;\n }\n}\n","import type { TargetPackRef } from '@prisma-next/contract/framework-components';\nimport type {\n ColumnBuilderState,\n ContractBuilderState,\n ModelBuilderState,\n RelationDefinition,\n TableBuilderState,\n} from './builder-state';\nimport { ModelBuilder } from './model-builder';\nimport { createTable, TableBuilder } from './table-builder';\n\nexport class ContractBuilder<\n Target extends string | undefined = undefined,\n Tables extends Record<\n string,\n TableBuilderState<\n string,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >\n > = Record<never, never>,\n Models extends Record<\n string,\n ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>\n > = Record<never, never>,\n CoreHash extends string | undefined = undefined,\n ExtensionPacks extends Record<string, unknown> | undefined = undefined,\n Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined,\n> {\n protected readonly state: ContractBuilderState<\n Target,\n Tables,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >;\n\n constructor(\n state?: ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>,\n ) {\n this.state =\n state ??\n ({\n tables: {},\n models: {},\n } as ContractBuilderState<Target, Tables, Models, CoreHash, ExtensionPacks, Capabilities>);\n }\n\n target<T extends string>(\n packRef: TargetPackRef<string, T>,\n ): ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities> {\n return new ContractBuilder<T, Tables, Models, CoreHash, ExtensionPacks, Capabilities>({\n ...this.state,\n target: packRef.targetId,\n });\n }\n\n capabilities<C extends Record<string, Record<string, boolean>>>(\n capabilities: C,\n ): ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C> {\n return new ContractBuilder<Target, Tables, Models, CoreHash, ExtensionPacks, C>({\n ...this.state,\n capabilities,\n });\n }\n\n table<\n TableName extends string,\n T extends TableBuilder<\n TableName,\n Record<string, ColumnBuilderState<string, boolean, string>>,\n readonly string[] | undefined\n >,\n >(\n name: TableName,\n callback: (t: TableBuilder<TableName>) => T | undefined,\n ): ContractBuilder<\n Target,\n Tables & Record<TableName, ReturnType<T['build']>>,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n const tableBuilder = createTable(name);\n const result = callback(tableBuilder);\n const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;\n const tableState = finalBuilder.build();\n\n return new ContractBuilder<\n Target,\n Tables & Record<TableName, ReturnType<T['build']>>,\n Models,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n tables: { ...this.state.tables, [name]: tableState } as Tables &\n Record<TableName, ReturnType<T['build']>>,\n });\n }\n\n model<\n ModelName extends string,\n TableName extends string,\n M extends ModelBuilder<\n ModelName,\n TableName,\n Record<string, string>,\n Record<string, RelationDefinition>\n >,\n >(\n name: ModelName,\n table: TableName,\n callback: (\n m: ModelBuilder<ModelName, TableName, Record<string, string>, Record<never, never>>,\n ) => M | undefined,\n ): ContractBuilder<\n Target,\n Tables,\n Models & Record<ModelName, ReturnType<M['build']>>,\n CoreHash,\n ExtensionPacks,\n Capabilities\n > {\n const modelBuilder = new ModelBuilder<ModelName, TableName>(name, table);\n const result = callback(modelBuilder);\n const finalBuilder = result instanceof ModelBuilder ? result : modelBuilder;\n const modelState = finalBuilder.build();\n\n return new ContractBuilder<\n Target,\n Tables,\n Models & Record<ModelName, ReturnType<M['build']>>,\n CoreHash,\n ExtensionPacks,\n Capabilities\n >({\n ...this.state,\n models: { ...this.state.models, [name]: modelState } as Models &\n Record<ModelName, ReturnType<M['build']>>,\n });\n }\n\n coreHash<H extends string>(\n hash: H,\n ): ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities> {\n return new ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities>({\n ...this.state,\n coreHash: hash,\n });\n }\n}\n\nexport function defineContract(): ContractBuilder {\n return new ContractBuilder();\n}\n"],"mappings":";AAEO,IAAM,eAAN,MAAM,cAKX;AA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C;AAAA,EACH;AAAA,EAEA,MAUE,MACA,OACA,UAUA;AACA,UAAM,eAAe,IAAI,aAAmC,MAAM,KAAK;AACvE,UAAM,SAAS,SAAS,YAAY;AACpC,UAAM,eAAe,kBAAkB,eAAe,SAAS;AAC/D,UAAM,aAAa,aAAa,MAAM;AAEtC,WAAO,IAAI,iBAOT;AAAA,MACA,GAAG,KAAK;AAAA,MACR,QAAQ,EAAE,GAAG,KAAK,MAAM,QAAQ,CAAC,IAAI,GAAG,WAAW;AAAA,IAErD,CAAC;AAAA,EACH;AAAA,EAEA,SACE,MAC0E;AAC1E,WAAO,IAAI,iBAAyE;AAAA,MAClF,GAAG,KAAK;AAAA,MACR,UAAU;AAAA,IACZ,CAAC;AAAA,EACH;AACF;AAEO,SAAS,iBAAkC;AAChD,SAAO,IAAI,gBAAgB;AAC7B;","names":[]}
|
|
@@ -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,KAAK,EAAE,KAAK,CAAC;IAE3E,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAO;IAC7B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAY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AC;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,32 @@
|
|
|
1
|
+
import type { ColumnBuilderState, ColumnTypeDescriptor, ForeignKeyDef, IndexDef, TableBuilderState, UniqueConstraintDef } from './builder-state';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a new table builder with the given name.
|
|
4
|
+
* This is the preferred way to create a TableBuilder - it ensures
|
|
5
|
+
* type parameters are inferred correctly without unsafe casts.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createTable<Name extends string>(name: Name): TableBuilder<Name>;
|
|
8
|
+
/**
|
|
9
|
+
* Builder for defining table structure with type-safe chaining.
|
|
10
|
+
* Use `createTable(name)` to create instances.
|
|
11
|
+
*/
|
|
12
|
+
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> {
|
|
13
|
+
private readonly _state;
|
|
14
|
+
/** @internal Use createTable() instead */
|
|
15
|
+
constructor(name: Name, columns: Columns, primaryKey: PrimaryKey, primaryKeyName: string | undefined, uniques: readonly UniqueConstraintDef[], indexes: readonly IndexDef[], foreignKeys: readonly ForeignKeyDef[]);
|
|
16
|
+
private get _name();
|
|
17
|
+
private get _columns();
|
|
18
|
+
private get _primaryKey();
|
|
19
|
+
column<ColName extends string, Descriptor extends ColumnTypeDescriptor, Nullable extends boolean | undefined = undefined>(name: ColName, options: {
|
|
20
|
+
type: Descriptor;
|
|
21
|
+
nullable?: Nullable;
|
|
22
|
+
}): TableBuilder<Name, Columns & Record<ColName, ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>>, PrimaryKey>;
|
|
23
|
+
primaryKey<PK extends readonly string[]>(columns: PK, name?: string): TableBuilder<Name, Columns, PK>;
|
|
24
|
+
unique(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey>;
|
|
25
|
+
index(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey>;
|
|
26
|
+
foreignKey(columns: readonly string[], references: {
|
|
27
|
+
table: string;
|
|
28
|
+
columns: readonly string[];
|
|
29
|
+
}, name?: string): TableBuilder<Name, Columns, PrimaryKey>;
|
|
30
|
+
build(): TableBuilderState<Name, Columns, PrimaryKey>;
|
|
31
|
+
}
|
|
32
|
+
//# 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,EACV,kBAAkB,EAClB,oBAAoB,EACpB,aAAa,EACb,QAAQ,EACR,iBAAiB,EACjB,mBAAmB,EACpB,MAAM,iBAAiB,CAAC;AAgBzB;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,SAAS,MAAM,EAAE,IAAI,EAAE,IAAI,GAAG,YAAY,CAAC,IAAI,CAAC,CAE/E;AAED;;;GAGG;AACH,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,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