@prisma-next/contract-authoring 0.3.0-dev.2 → 0.3.0-dev.21

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,195 @@
1
+ import type {
2
+ ColumnBuilderState,
3
+ ColumnTypeDescriptor,
4
+ ForeignKeyDef,
5
+ IndexDef,
6
+ TableBuilderState,
7
+ UniqueConstraintDef,
8
+ } from './builder-state';
9
+
10
+ interface TableBuilderInternalState<
11
+ Name extends string,
12
+ Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,
13
+ PrimaryKey extends readonly string[] | undefined,
14
+ > {
15
+ readonly name: Name;
16
+ readonly columns: Columns;
17
+ readonly primaryKey: PrimaryKey;
18
+ readonly primaryKeyName: string | undefined;
19
+ readonly uniques: readonly UniqueConstraintDef[];
20
+ readonly indexes: readonly IndexDef[];
21
+ readonly foreignKeys: readonly ForeignKeyDef[];
22
+ }
23
+
24
+ /**
25
+ * Creates a new table builder with the given name.
26
+ * This is the preferred way to create a TableBuilder - it ensures
27
+ * type parameters are inferred correctly without unsafe casts.
28
+ */
29
+ export function createTable<Name extends string>(name: Name): TableBuilder<Name> {
30
+ return new TableBuilder(name, {}, undefined, undefined, [], [], []);
31
+ }
32
+
33
+ /**
34
+ * Builder for defining table structure with type-safe chaining.
35
+ * Use `createTable(name)` to create instances.
36
+ */
37
+ export class TableBuilder<
38
+ Name extends string,
39
+ Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<
40
+ never,
41
+ ColumnBuilderState<string, boolean, string>
42
+ >,
43
+ PrimaryKey extends readonly string[] | undefined = undefined,
44
+ > {
45
+ private readonly _state: TableBuilderInternalState<Name, Columns, PrimaryKey>;
46
+
47
+ /** @internal Use createTable() instead */
48
+ constructor(
49
+ name: Name,
50
+ columns: Columns,
51
+ primaryKey: PrimaryKey,
52
+ primaryKeyName: string | undefined,
53
+ uniques: readonly UniqueConstraintDef[],
54
+ indexes: readonly IndexDef[],
55
+ foreignKeys: readonly ForeignKeyDef[],
56
+ ) {
57
+ this._state = {
58
+ name,
59
+ columns,
60
+ primaryKey,
61
+ primaryKeyName,
62
+ uniques,
63
+ indexes,
64
+ foreignKeys,
65
+ };
66
+ }
67
+
68
+ private get _name(): Name {
69
+ return this._state.name;
70
+ }
71
+
72
+ private get _columns(): Columns {
73
+ return this._state.columns;
74
+ }
75
+
76
+ private get _primaryKey(): PrimaryKey {
77
+ return this._state.primaryKey;
78
+ }
79
+
80
+ column<
81
+ ColName extends string,
82
+ Descriptor extends ColumnTypeDescriptor,
83
+ Nullable extends boolean | undefined = undefined,
84
+ >(
85
+ name: ColName,
86
+ options: {
87
+ type: Descriptor;
88
+ nullable?: Nullable;
89
+ },
90
+ ): TableBuilder<
91
+ Name,
92
+ Columns &
93
+ Record<
94
+ ColName,
95
+ ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>
96
+ >,
97
+ PrimaryKey
98
+ > {
99
+ const nullable = (options.nullable ?? false) as Nullable extends true ? true : false;
100
+ const { codecId, nativeType } = options.type;
101
+
102
+ const columnState = {
103
+ name,
104
+ nullable,
105
+ type: codecId,
106
+ nativeType,
107
+ } as ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>;
108
+ const newColumns = { ...this._columns, [name]: columnState } as Columns &
109
+ Record<
110
+ ColName,
111
+ ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>
112
+ >;
113
+ return new TableBuilder(
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,
121
+ );
122
+ }
123
+
124
+ primaryKey<PK extends readonly string[]>(
125
+ columns: PK,
126
+ name?: string,
127
+ ): TableBuilder<Name, Columns, PK> {
128
+ return new TableBuilder(
129
+ this._state.name,
130
+ this._state.columns,
131
+ columns,
132
+ name,
133
+ this._state.uniques,
134
+ this._state.indexes,
135
+ this._state.foreignKeys,
136
+ );
137
+ }
138
+
139
+ unique(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {
140
+ const constraint: UniqueConstraintDef = name ? { columns, name } : { columns };
141
+ return new TableBuilder(
142
+ this._state.name,
143
+ this._state.columns,
144
+ this._state.primaryKey,
145
+ this._state.primaryKeyName,
146
+ [...this._state.uniques, constraint],
147
+ this._state.indexes,
148
+ this._state.foreignKeys,
149
+ );
150
+ }
151
+
152
+ index(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {
153
+ const indexDef: IndexDef = name ? { columns, name } : { columns };
154
+ return new TableBuilder(
155
+ this._state.name,
156
+ this._state.columns,
157
+ this._state.primaryKey,
158
+ this._state.primaryKeyName,
159
+ this._state.uniques,
160
+ [...this._state.indexes, indexDef],
161
+ this._state.foreignKeys,
162
+ );
163
+ }
164
+
165
+ foreignKey(
166
+ columns: readonly string[],
167
+ references: { table: string; columns: readonly string[] },
168
+ name?: string,
169
+ ): TableBuilder<Name, Columns, PrimaryKey> {
170
+ const fkDef: ForeignKeyDef = name ? { columns, references, name } : { columns, references };
171
+ return new TableBuilder(
172
+ this._state.name,
173
+ this._state.columns,
174
+ this._state.primaryKey,
175
+ this._state.primaryKeyName,
176
+ this._state.uniques,
177
+ this._state.indexes,
178
+ [...this._state.foreignKeys, fkDef],
179
+ );
180
+ }
181
+
182
+ build(): TableBuilderState<Name, Columns, PrimaryKey> {
183
+ return {
184
+ name: this._name,
185
+ columns: this._columns,
186
+ ...(this._primaryKey !== undefined ? { primaryKey: this._primaryKey } : {}),
187
+ ...(this._state.primaryKeyName !== undefined
188
+ ? { primaryKeyName: this._state.primaryKeyName }
189
+ : {}),
190
+ uniques: this._state.uniques,
191
+ indexes: this._state.indexes,
192
+ foreignKeys: this._state.foreignKeys,
193
+ } as TableBuilderState<Name, Columns, PrimaryKey>;
194
+ }
195
+ }
package/src/types.ts ADDED
@@ -0,0 +1,129 @@
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
+ > =
29
+ T extends TableBuilderState<
30
+ string,
31
+ Record<string, ColumnBuilderState<string, boolean, string>>,
32
+ infer PK
33
+ >
34
+ ? PK
35
+ : never;
36
+
37
+ export type BuildStorage<
38
+ Tables extends Record<
39
+ string,
40
+ TableBuilderState<
41
+ string,
42
+ Record<string, ColumnBuilderState<string, boolean, string>>,
43
+ readonly string[] | undefined
44
+ >
45
+ >,
46
+ > = {
47
+ readonly tables: {
48
+ readonly [K in keyof Tables]: {
49
+ readonly columns: {
50
+ readonly [ColK in keyof ExtractColumns<Tables[K]>]: ExtractColumns<
51
+ Tables[K]
52
+ >[ColK] extends ColumnBuilderState<string, infer Null, infer TType>
53
+ ? BuildStorageColumn<Null & boolean, TType>
54
+ : never;
55
+ };
56
+ };
57
+ };
58
+ };
59
+
60
+ export type BuildStorageTables<
61
+ Tables extends Record<
62
+ string,
63
+ TableBuilderState<
64
+ string,
65
+ Record<string, ColumnBuilderState<string, boolean, string>>,
66
+ readonly string[] | undefined
67
+ >
68
+ >,
69
+ > = {
70
+ readonly [K in keyof Tables]: {
71
+ readonly columns: {
72
+ readonly [ColK in keyof ExtractColumns<Tables[K]>]: ExtractColumns<
73
+ Tables[K]
74
+ >[ColK] extends ColumnBuilderState<string, infer Null, infer TType>
75
+ ? BuildStorageColumn<Null & boolean, TType>
76
+ : never;
77
+ };
78
+ };
79
+ };
80
+
81
+ export type Mutable<T> = {
82
+ -readonly [K in keyof T]-?: T[K];
83
+ };
84
+
85
+ export type BuildModelFields<Fields extends Record<string, string>> = {
86
+ readonly [K in keyof Fields]: { readonly column: Fields[K] };
87
+ };
88
+
89
+ export type ExtractModelFields<
90
+ T extends ModelBuilderState<
91
+ string,
92
+ string,
93
+ Record<string, string>,
94
+ Record<string, RelationDefinition>
95
+ >,
96
+ > =
97
+ T extends ModelBuilderState<string, string, infer F, Record<string, RelationDefinition>>
98
+ ? F
99
+ : never;
100
+
101
+ export type ExtractModelRelations<
102
+ T extends ModelBuilderState<
103
+ string,
104
+ string,
105
+ Record<string, string>,
106
+ Record<string, RelationDefinition>
107
+ >,
108
+ > = T extends ModelBuilderState<string, string, Record<string, string>, infer R> ? R : never;
109
+
110
+ export type BuildModels<
111
+ Models extends Record<
112
+ string,
113
+ ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
114
+ >,
115
+ > = {
116
+ readonly [K in keyof Models]: {
117
+ readonly storage: { readonly table: Models[K]['table'] };
118
+ readonly fields: BuildModelFields<ExtractModelFields<Models[K]>>;
119
+ };
120
+ };
121
+
122
+ export type BuildRelations<
123
+ Models extends Record<
124
+ string,
125
+ ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
126
+ >,
127
+ > = {
128
+ readonly [K in keyof Models as Models[K]['table']]: ExtractModelRelations<Models[K]>;
129
+ };