@prisma-next/contract-authoring 0.3.0-dev.135 → 0.3.0-dev.146
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/README.md +17 -23
- package/dist/index.d.mts +2 -293
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -237
- package/package.json +5 -9
- package/src/descriptors.ts +18 -0
- package/src/index.ts +1 -26
- package/dist/index.mjs.map +0 -1
- package/src/builder-state.ts +0 -201
- package/src/contract-builder.ts +0 -169
- package/src/model-builder.ts +0 -160
- package/src/table-builder.ts +0 -330
- package/src/types.ts +0 -119
package/src/table-builder.ts
DELETED
|
@@ -1,330 +0,0 @@
|
|
|
1
|
-
import type { ColumnDefault, ExecutionMutationDefaultValue } from '@prisma-next/contract/types';
|
|
2
|
-
import { ifDefined } from '@prisma-next/utils/defined';
|
|
3
|
-
import type {
|
|
4
|
-
ColumnBuilderState,
|
|
5
|
-
ColumnTypeDescriptor,
|
|
6
|
-
ForeignKeyDef,
|
|
7
|
-
ForeignKeyOptions,
|
|
8
|
-
IndexDef,
|
|
9
|
-
TableBuilderState,
|
|
10
|
-
UniqueConstraintDef,
|
|
11
|
-
} from './builder-state';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Column options for nullable columns.
|
|
15
|
-
*/
|
|
16
|
-
interface NullableColumnOptions<Descriptor extends ColumnTypeDescriptor> {
|
|
17
|
-
type: Descriptor;
|
|
18
|
-
nullable: true;
|
|
19
|
-
typeParams?: Record<string, unknown>;
|
|
20
|
-
default?: ColumnDefault;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Column options for non-nullable columns.
|
|
25
|
-
* Non-nullable columns can optionally have a default value.
|
|
26
|
-
*/
|
|
27
|
-
interface NonNullableColumnOptions<Descriptor extends ColumnTypeDescriptor> {
|
|
28
|
-
type: Descriptor;
|
|
29
|
-
nullable?: false;
|
|
30
|
-
typeParams?: Record<string, unknown>;
|
|
31
|
-
default?: ColumnDefault;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
type GeneratedColumnOptions<Descriptor extends ColumnTypeDescriptor> = Omit<
|
|
35
|
-
NonNullableColumnOptions<Descriptor>,
|
|
36
|
-
'default' | 'nullable'
|
|
37
|
-
> & {
|
|
38
|
-
/**
|
|
39
|
-
* Generated columns are always non-nullable and use mutation-time defaults
|
|
40
|
-
* that the runtime injects when the column is omitted from insert input.
|
|
41
|
-
*/
|
|
42
|
-
nullable?: false;
|
|
43
|
-
generated: ExecutionMutationDefaultValue;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/** Column options for any column nullability. */
|
|
47
|
-
type ColumnOptions<Descriptor extends ColumnTypeDescriptor> =
|
|
48
|
-
| NullableColumnOptions<Descriptor>
|
|
49
|
-
| NonNullableColumnOptions<Descriptor>;
|
|
50
|
-
|
|
51
|
-
type NullableFromOptions<TOptions> = TOptions extends { nullable: true } ? true : false;
|
|
52
|
-
type IndexOptions = {
|
|
53
|
-
readonly name?: string;
|
|
54
|
-
readonly using?: string;
|
|
55
|
-
readonly config?: Record<string, unknown>;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
function isIndexDef(value: readonly string[] | IndexDef): value is IndexDef {
|
|
59
|
-
return !Array.isArray(value);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
interface TableBuilderInternalState<
|
|
63
|
-
Name extends string,
|
|
64
|
-
Columns extends Record<string, ColumnBuilderState<string, boolean, string>>,
|
|
65
|
-
PrimaryKey extends readonly string[] | undefined,
|
|
66
|
-
> {
|
|
67
|
-
readonly name: Name;
|
|
68
|
-
readonly columns: Columns;
|
|
69
|
-
readonly primaryKey: PrimaryKey;
|
|
70
|
-
readonly primaryKeyName: string | undefined;
|
|
71
|
-
readonly uniques: readonly UniqueConstraintDef[];
|
|
72
|
-
readonly indexes: readonly IndexDef[];
|
|
73
|
-
readonly foreignKeys: readonly ForeignKeyDef[];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Creates a new table builder with the given name.
|
|
78
|
-
* This is the preferred way to create a TableBuilder - it ensures
|
|
79
|
-
* type parameters are inferred correctly without unsafe casts.
|
|
80
|
-
*/
|
|
81
|
-
export function createTable<Name extends string>(name: Name): TableBuilder<Name> {
|
|
82
|
-
return new TableBuilder(name, {}, undefined, undefined, [], [], []);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Builder for defining table structure with type-safe chaining.
|
|
87
|
-
* Use `createTable(name)` to create instances.
|
|
88
|
-
*/
|
|
89
|
-
export class TableBuilder<
|
|
90
|
-
Name extends string,
|
|
91
|
-
Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<
|
|
92
|
-
never,
|
|
93
|
-
ColumnBuilderState<string, boolean, string>
|
|
94
|
-
>,
|
|
95
|
-
PrimaryKey extends readonly string[] | undefined = undefined,
|
|
96
|
-
> {
|
|
97
|
-
private readonly _state: TableBuilderInternalState<Name, Columns, PrimaryKey>;
|
|
98
|
-
|
|
99
|
-
/** @internal Use createTable() instead */
|
|
100
|
-
constructor(
|
|
101
|
-
name: Name,
|
|
102
|
-
columns: Columns,
|
|
103
|
-
primaryKey: PrimaryKey,
|
|
104
|
-
primaryKeyName: string | undefined,
|
|
105
|
-
uniques: readonly UniqueConstraintDef[],
|
|
106
|
-
indexes: readonly IndexDef[],
|
|
107
|
-
foreignKeys: readonly ForeignKeyDef[],
|
|
108
|
-
) {
|
|
109
|
-
this._state = {
|
|
110
|
-
name,
|
|
111
|
-
columns,
|
|
112
|
-
primaryKey,
|
|
113
|
-
primaryKeyName,
|
|
114
|
-
uniques,
|
|
115
|
-
indexes,
|
|
116
|
-
foreignKeys,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
private get _name(): Name {
|
|
121
|
-
return this._state.name;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
private get _columns(): Columns {
|
|
125
|
-
return this._state.columns;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
private get _primaryKey(): PrimaryKey {
|
|
129
|
-
return this._state.primaryKey;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** Add a nullable column to the table. */
|
|
133
|
-
column<ColName extends string, Descriptor extends ColumnTypeDescriptor>(
|
|
134
|
-
name: ColName,
|
|
135
|
-
options: NullableColumnOptions<Descriptor>,
|
|
136
|
-
): TableBuilder<
|
|
137
|
-
Name,
|
|
138
|
-
Columns & Record<ColName, ColumnBuilderState<ColName, true, Descriptor['codecId']>>,
|
|
139
|
-
PrimaryKey
|
|
140
|
-
>;
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Add a non-nullable column to the table.
|
|
144
|
-
* Non-nullable columns can optionally have a default value.
|
|
145
|
-
*/
|
|
146
|
-
column<ColName extends string, Descriptor extends ColumnTypeDescriptor>(
|
|
147
|
-
name: ColName,
|
|
148
|
-
options: NonNullableColumnOptions<Descriptor>,
|
|
149
|
-
): TableBuilder<
|
|
150
|
-
Name,
|
|
151
|
-
Columns & Record<ColName, ColumnBuilderState<ColName, false, Descriptor['codecId']>>,
|
|
152
|
-
PrimaryKey
|
|
153
|
-
>;
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Implementation of the column method.
|
|
157
|
-
*/
|
|
158
|
-
column<ColName extends string, Descriptor extends ColumnTypeDescriptor>(
|
|
159
|
-
name: ColName,
|
|
160
|
-
options: ColumnOptions<Descriptor>,
|
|
161
|
-
): TableBuilder<
|
|
162
|
-
Name,
|
|
163
|
-
Columns & Record<ColName, ColumnBuilderState<ColName, boolean, Descriptor['codecId']>>,
|
|
164
|
-
PrimaryKey
|
|
165
|
-
> {
|
|
166
|
-
return this.columnInternal(name, options);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
generated<ColName extends string, Descriptor extends ColumnTypeDescriptor>(
|
|
170
|
-
name: ColName,
|
|
171
|
-
options: GeneratedColumnOptions<Descriptor>,
|
|
172
|
-
): TableBuilder<
|
|
173
|
-
Name,
|
|
174
|
-
Columns & Record<ColName, ColumnBuilderState<ColName, false, Descriptor['codecId']>>,
|
|
175
|
-
PrimaryKey
|
|
176
|
-
> {
|
|
177
|
-
const { generated, ...columnOptions } = options;
|
|
178
|
-
return this.columnInternal(name, columnOptions, generated);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
private columnInternal<
|
|
182
|
-
ColName extends string,
|
|
183
|
-
Descriptor extends ColumnTypeDescriptor,
|
|
184
|
-
Options extends ColumnOptions<Descriptor>,
|
|
185
|
-
>(
|
|
186
|
-
name: ColName,
|
|
187
|
-
options: Options,
|
|
188
|
-
executionDefault?: ExecutionMutationDefaultValue,
|
|
189
|
-
): TableBuilder<
|
|
190
|
-
Name,
|
|
191
|
-
Columns &
|
|
192
|
-
Record<
|
|
193
|
-
ColName,
|
|
194
|
-
ColumnBuilderState<ColName, NullableFromOptions<Options>, Descriptor['codecId']>
|
|
195
|
-
>,
|
|
196
|
-
PrimaryKey
|
|
197
|
-
> {
|
|
198
|
-
const nullable = options.nullable ?? false;
|
|
199
|
-
const { codecId, nativeType, typeParams: descriptorTypeParams, typeRef } = options.type;
|
|
200
|
-
const typeParams = options.typeParams ?? descriptorTypeParams;
|
|
201
|
-
|
|
202
|
-
const columnState = {
|
|
203
|
-
name,
|
|
204
|
-
nullable,
|
|
205
|
-
type: codecId,
|
|
206
|
-
nativeType,
|
|
207
|
-
...ifDefined('typeParams', typeParams),
|
|
208
|
-
...ifDefined('typeRef', typeRef),
|
|
209
|
-
...ifDefined('default', 'default' in options ? options.default : undefined),
|
|
210
|
-
...ifDefined('executionDefault', executionDefault),
|
|
211
|
-
} as ColumnBuilderState<ColName, NullableFromOptions<Options>, Descriptor['codecId']>;
|
|
212
|
-
const newColumns = { ...this._columns, [name]: columnState } as Columns &
|
|
213
|
-
Record<
|
|
214
|
-
ColName,
|
|
215
|
-
ColumnBuilderState<ColName, NullableFromOptions<Options>, Descriptor['codecId']>
|
|
216
|
-
>;
|
|
217
|
-
return new TableBuilder(
|
|
218
|
-
this._state.name,
|
|
219
|
-
newColumns,
|
|
220
|
-
this._state.primaryKey,
|
|
221
|
-
this._state.primaryKeyName,
|
|
222
|
-
this._state.uniques,
|
|
223
|
-
this._state.indexes,
|
|
224
|
-
this._state.foreignKeys,
|
|
225
|
-
);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
primaryKey<PK extends readonly string[]>(
|
|
229
|
-
columns: PK,
|
|
230
|
-
name?: string,
|
|
231
|
-
): TableBuilder<Name, Columns, PK> {
|
|
232
|
-
return new TableBuilder(
|
|
233
|
-
this._state.name,
|
|
234
|
-
this._state.columns,
|
|
235
|
-
columns,
|
|
236
|
-
name,
|
|
237
|
-
this._state.uniques,
|
|
238
|
-
this._state.indexes,
|
|
239
|
-
this._state.foreignKeys,
|
|
240
|
-
);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
unique(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey> {
|
|
244
|
-
const constraint: UniqueConstraintDef = name ? { columns, name } : { columns };
|
|
245
|
-
return new TableBuilder(
|
|
246
|
-
this._state.name,
|
|
247
|
-
this._state.columns,
|
|
248
|
-
this._state.primaryKey,
|
|
249
|
-
this._state.primaryKeyName,
|
|
250
|
-
[...this._state.uniques, constraint],
|
|
251
|
-
this._state.indexes,
|
|
252
|
-
this._state.foreignKeys,
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
index(columns: readonly string[], name?: string): TableBuilder<Name, Columns, PrimaryKey>;
|
|
257
|
-
index(
|
|
258
|
-
columns: readonly string[],
|
|
259
|
-
options?: IndexOptions,
|
|
260
|
-
): TableBuilder<Name, Columns, PrimaryKey>;
|
|
261
|
-
index(indexDef: IndexDef): TableBuilder<Name, Columns, PrimaryKey>;
|
|
262
|
-
index(
|
|
263
|
-
columnsOrIndexDef: readonly string[] | IndexDef,
|
|
264
|
-
nameOrOptions?: string | IndexOptions,
|
|
265
|
-
): TableBuilder<Name, Columns, PrimaryKey> {
|
|
266
|
-
const indexDef: IndexDef = isIndexDef(columnsOrIndexDef)
|
|
267
|
-
? columnsOrIndexDef
|
|
268
|
-
: {
|
|
269
|
-
columns: columnsOrIndexDef,
|
|
270
|
-
...(typeof nameOrOptions === 'string' ? { name: nameOrOptions } : {}),
|
|
271
|
-
...(typeof nameOrOptions === 'object' && nameOrOptions !== null
|
|
272
|
-
? {
|
|
273
|
-
...(nameOrOptions.name !== undefined ? { name: nameOrOptions.name } : {}),
|
|
274
|
-
...(nameOrOptions.using !== undefined ? { using: nameOrOptions.using } : {}),
|
|
275
|
-
...(nameOrOptions.config !== undefined ? { config: nameOrOptions.config } : {}),
|
|
276
|
-
}
|
|
277
|
-
: {}),
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
return new TableBuilder(
|
|
281
|
-
this._state.name,
|
|
282
|
-
this._state.columns,
|
|
283
|
-
this._state.primaryKey,
|
|
284
|
-
this._state.primaryKeyName,
|
|
285
|
-
this._state.uniques,
|
|
286
|
-
[...this._state.indexes, indexDef],
|
|
287
|
-
this._state.foreignKeys,
|
|
288
|
-
);
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
foreignKey(
|
|
292
|
-
columns: readonly string[],
|
|
293
|
-
references: { table: string; columns: readonly string[] },
|
|
294
|
-
opts?: string | (ForeignKeyOptions & { constraint?: boolean; index?: boolean }),
|
|
295
|
-
): TableBuilder<Name, Columns, PrimaryKey> {
|
|
296
|
-
const resolved = typeof opts === 'string' ? { name: opts } : opts;
|
|
297
|
-
const fkDef: ForeignKeyDef = {
|
|
298
|
-
columns,
|
|
299
|
-
references,
|
|
300
|
-
...ifDefined('name', resolved?.name),
|
|
301
|
-
...ifDefined('onDelete', resolved?.onDelete),
|
|
302
|
-
...ifDefined('onUpdate', resolved?.onUpdate),
|
|
303
|
-
...ifDefined('constraint', resolved?.constraint),
|
|
304
|
-
...ifDefined('index', resolved?.index),
|
|
305
|
-
};
|
|
306
|
-
return new TableBuilder(
|
|
307
|
-
this._state.name,
|
|
308
|
-
this._state.columns,
|
|
309
|
-
this._state.primaryKey,
|
|
310
|
-
this._state.primaryKeyName,
|
|
311
|
-
this._state.uniques,
|
|
312
|
-
this._state.indexes,
|
|
313
|
-
[...this._state.foreignKeys, fkDef],
|
|
314
|
-
);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
build(): TableBuilderState<Name, Columns, PrimaryKey> {
|
|
318
|
-
return {
|
|
319
|
-
name: this._name,
|
|
320
|
-
columns: this._columns,
|
|
321
|
-
...(this._primaryKey !== undefined ? { primaryKey: this._primaryKey } : {}),
|
|
322
|
-
...(this._state.primaryKeyName !== undefined
|
|
323
|
-
? { primaryKeyName: this._state.primaryKeyName }
|
|
324
|
-
: {}),
|
|
325
|
-
uniques: this._state.uniques,
|
|
326
|
-
indexes: this._state.indexes,
|
|
327
|
-
foreignKeys: this._state.foreignKeys,
|
|
328
|
-
} as TableBuilderState<Name, Columns, PrimaryKey>;
|
|
329
|
-
}
|
|
330
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import type { ColumnDefault, DomainField, DomainRelation } from '@prisma-next/contract/types';
|
|
2
|
-
import type {
|
|
3
|
-
ColumnBuilderState,
|
|
4
|
-
ModelBuilderState,
|
|
5
|
-
RelationDefinition,
|
|
6
|
-
TableBuilderState,
|
|
7
|
-
} from './builder-state';
|
|
8
|
-
|
|
9
|
-
export type BuildStorageColumn<Nullable extends boolean, Type extends string> = {
|
|
10
|
-
readonly nativeType: string;
|
|
11
|
-
readonly codecId: Type;
|
|
12
|
-
readonly nullable: Nullable;
|
|
13
|
-
readonly typeParams?: Record<string, unknown>;
|
|
14
|
-
readonly typeRef?: string;
|
|
15
|
-
readonly default?: ColumnDefault;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type ExtractColumns<
|
|
19
|
-
T extends TableBuilderState<
|
|
20
|
-
string,
|
|
21
|
-
Record<string, ColumnBuilderState<string, boolean, string>>,
|
|
22
|
-
readonly string[] | undefined
|
|
23
|
-
>,
|
|
24
|
-
> = T extends TableBuilderState<string, infer C, readonly string[] | undefined> ? C : never;
|
|
25
|
-
|
|
26
|
-
export type ExtractPrimaryKey<
|
|
27
|
-
T extends TableBuilderState<
|
|
28
|
-
string,
|
|
29
|
-
Record<string, ColumnBuilderState<string, boolean, string>>,
|
|
30
|
-
readonly string[] | undefined
|
|
31
|
-
>,
|
|
32
|
-
> =
|
|
33
|
-
T extends TableBuilderState<
|
|
34
|
-
string,
|
|
35
|
-
Record<string, ColumnBuilderState<string, boolean, string>>,
|
|
36
|
-
infer PK
|
|
37
|
-
>
|
|
38
|
-
? PK
|
|
39
|
-
: never;
|
|
40
|
-
|
|
41
|
-
export type BuildStorage<
|
|
42
|
-
Tables extends Record<
|
|
43
|
-
string,
|
|
44
|
-
TableBuilderState<
|
|
45
|
-
string,
|
|
46
|
-
Record<string, ColumnBuilderState<string, boolean, string>>,
|
|
47
|
-
readonly string[] | undefined
|
|
48
|
-
>
|
|
49
|
-
>,
|
|
50
|
-
> = {
|
|
51
|
-
readonly tables: {
|
|
52
|
-
readonly [K in keyof Tables]: {
|
|
53
|
-
readonly columns: {
|
|
54
|
-
readonly [ColK in keyof ExtractColumns<Tables[K]>]: ExtractColumns<
|
|
55
|
-
Tables[K]
|
|
56
|
-
>[ColK] extends ColumnBuilderState<string, infer Null, infer TType>
|
|
57
|
-
? BuildStorageColumn<Null & boolean, TType>
|
|
58
|
-
: never;
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
};
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export type BuildStorageTables<
|
|
65
|
-
Tables extends Record<
|
|
66
|
-
string,
|
|
67
|
-
TableBuilderState<
|
|
68
|
-
string,
|
|
69
|
-
Record<string, ColumnBuilderState<string, boolean, string>>,
|
|
70
|
-
readonly string[] | undefined
|
|
71
|
-
>
|
|
72
|
-
>,
|
|
73
|
-
> = {
|
|
74
|
-
readonly [K in keyof Tables]: {
|
|
75
|
-
readonly columns: {
|
|
76
|
-
readonly [ColK in keyof ExtractColumns<Tables[K]>]: ExtractColumns<
|
|
77
|
-
Tables[K]
|
|
78
|
-
>[ColK] extends ColumnBuilderState<string, infer Null, infer TType>
|
|
79
|
-
? BuildStorageColumn<Null & boolean, TType>
|
|
80
|
-
: never;
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
export type Mutable<T> = {
|
|
86
|
-
-readonly [K in keyof T]-?: T[K];
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
export type BuildModelFields<Fields extends Record<string, string>> = {
|
|
90
|
-
readonly [K in keyof Fields]: { readonly column: Fields[K] };
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
export type ExtractModelFields<
|
|
94
|
-
T extends ModelBuilderState<
|
|
95
|
-
string,
|
|
96
|
-
string,
|
|
97
|
-
Record<string, string>,
|
|
98
|
-
Record<string, RelationDefinition>
|
|
99
|
-
>,
|
|
100
|
-
> =
|
|
101
|
-
T extends ModelBuilderState<string, string, infer F, Record<string, RelationDefinition>>
|
|
102
|
-
? F
|
|
103
|
-
: never;
|
|
104
|
-
|
|
105
|
-
export type BuildModels<
|
|
106
|
-
Models extends Record<
|
|
107
|
-
string,
|
|
108
|
-
ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
|
|
109
|
-
>,
|
|
110
|
-
> = {
|
|
111
|
-
readonly [K in keyof Models]: {
|
|
112
|
-
readonly storage: {
|
|
113
|
-
readonly table: Models[K]['table'];
|
|
114
|
-
readonly fields: BuildModelFields<ExtractModelFields<Models[K]>>;
|
|
115
|
-
};
|
|
116
|
-
readonly fields: Record<string, DomainField>;
|
|
117
|
-
readonly relations: Record<string, DomainRelation>;
|
|
118
|
-
};
|
|
119
|
-
};
|