@prisma-next/contract-authoring 0.3.0-dev.1 → 0.3.0-dev.100
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/LICENSE +201 -0
- package/README.md +4 -1
- package/dist/index.d.mts +311 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +237 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +25 -16
- package/src/builder-state.ts +201 -0
- package/src/contract-builder.ts +169 -0
- package/src/index.ts +32 -0
- package/src/model-builder.ts +160 -0
- package/src/table-builder.ts +330 -0
- package/src/types.ts +133 -0
- package/dist/index.d.ts +0 -175
- package/dist/index.js +0 -179
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { TargetPackRef } from '@prisma-next/contract/framework-components';
|
|
2
|
+
import type {
|
|
3
|
+
ColumnBuilderState,
|
|
4
|
+
ContractBuilderState,
|
|
5
|
+
ForeignKeyDefaultsState,
|
|
6
|
+
ModelBuilderState,
|
|
7
|
+
RelationDefinition,
|
|
8
|
+
TableBuilderState,
|
|
9
|
+
} from './builder-state';
|
|
10
|
+
import { ModelBuilder } from './model-builder';
|
|
11
|
+
import { createTable, TableBuilder } from './table-builder';
|
|
12
|
+
|
|
13
|
+
export class ContractBuilder<
|
|
14
|
+
Target extends string | undefined = undefined,
|
|
15
|
+
Tables extends Record<
|
|
16
|
+
string,
|
|
17
|
+
TableBuilderState<
|
|
18
|
+
string,
|
|
19
|
+
Record<string, ColumnBuilderState<string, boolean, string>>,
|
|
20
|
+
readonly string[] | undefined
|
|
21
|
+
>
|
|
22
|
+
> = Record<never, never>,
|
|
23
|
+
Models extends Record<
|
|
24
|
+
string,
|
|
25
|
+
ModelBuilderState<string, string, Record<string, string>, Record<string, RelationDefinition>>
|
|
26
|
+
> = Record<never, never>,
|
|
27
|
+
StorageHash extends string | undefined = undefined,
|
|
28
|
+
ExtensionPacks extends Record<string, unknown> | undefined = undefined,
|
|
29
|
+
Capabilities extends Record<string, Record<string, boolean>> | undefined = undefined,
|
|
30
|
+
> {
|
|
31
|
+
protected readonly state: ContractBuilderState<
|
|
32
|
+
Target,
|
|
33
|
+
Tables,
|
|
34
|
+
Models,
|
|
35
|
+
StorageHash,
|
|
36
|
+
ExtensionPacks,
|
|
37
|
+
Capabilities
|
|
38
|
+
>;
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
state?: ContractBuilderState<Target, Tables, Models, StorageHash, ExtensionPacks, Capabilities>,
|
|
42
|
+
) {
|
|
43
|
+
this.state =
|
|
44
|
+
state ??
|
|
45
|
+
({
|
|
46
|
+
tables: {},
|
|
47
|
+
models: {},
|
|
48
|
+
} as ContractBuilderState<Target, Tables, Models, StorageHash, ExtensionPacks, Capabilities>);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
target<T extends string>(
|
|
52
|
+
packRef: TargetPackRef<string, T>,
|
|
53
|
+
): ContractBuilder<T, Tables, Models, StorageHash, ExtensionPacks, Capabilities> {
|
|
54
|
+
return new ContractBuilder<T, Tables, Models, StorageHash, ExtensionPacks, Capabilities>({
|
|
55
|
+
...this.state,
|
|
56
|
+
target: packRef.targetId,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
capabilities<C extends Record<string, Record<string, boolean>>>(
|
|
61
|
+
capabilities: C,
|
|
62
|
+
): ContractBuilder<Target, Tables, Models, StorageHash, ExtensionPacks, C> {
|
|
63
|
+
return new ContractBuilder<Target, Tables, Models, StorageHash, ExtensionPacks, C>({
|
|
64
|
+
...this.state,
|
|
65
|
+
capabilities,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
table<
|
|
70
|
+
TableName extends string,
|
|
71
|
+
T extends TableBuilder<
|
|
72
|
+
TableName,
|
|
73
|
+
Record<string, ColumnBuilderState<string, boolean, string>>,
|
|
74
|
+
readonly string[] | undefined
|
|
75
|
+
>,
|
|
76
|
+
>(
|
|
77
|
+
name: TableName,
|
|
78
|
+
callback: (t: TableBuilder<TableName>) => T | undefined,
|
|
79
|
+
): ContractBuilder<
|
|
80
|
+
Target,
|
|
81
|
+
Tables & Record<TableName, ReturnType<T['build']>>,
|
|
82
|
+
Models,
|
|
83
|
+
StorageHash,
|
|
84
|
+
ExtensionPacks,
|
|
85
|
+
Capabilities
|
|
86
|
+
> {
|
|
87
|
+
const tableBuilder = createTable(name);
|
|
88
|
+
const result = callback(tableBuilder);
|
|
89
|
+
const finalBuilder = result instanceof TableBuilder ? result : tableBuilder;
|
|
90
|
+
const tableState = finalBuilder.build();
|
|
91
|
+
|
|
92
|
+
return new ContractBuilder<
|
|
93
|
+
Target,
|
|
94
|
+
Tables & Record<TableName, ReturnType<T['build']>>,
|
|
95
|
+
Models,
|
|
96
|
+
StorageHash,
|
|
97
|
+
ExtensionPacks,
|
|
98
|
+
Capabilities
|
|
99
|
+
>({
|
|
100
|
+
...this.state,
|
|
101
|
+
tables: { ...this.state.tables, [name]: tableState } as Tables &
|
|
102
|
+
Record<TableName, ReturnType<T['build']>>,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
model<
|
|
107
|
+
ModelName extends string,
|
|
108
|
+
TableName extends string,
|
|
109
|
+
M extends ModelBuilder<
|
|
110
|
+
ModelName,
|
|
111
|
+
TableName,
|
|
112
|
+
Record<string, string>,
|
|
113
|
+
Record<string, RelationDefinition>
|
|
114
|
+
>,
|
|
115
|
+
>(
|
|
116
|
+
name: ModelName,
|
|
117
|
+
table: TableName,
|
|
118
|
+
callback: (
|
|
119
|
+
m: ModelBuilder<ModelName, TableName, Record<never, never>, Record<never, never>>,
|
|
120
|
+
) => M | undefined,
|
|
121
|
+
): ContractBuilder<
|
|
122
|
+
Target,
|
|
123
|
+
Tables,
|
|
124
|
+
Models & Record<ModelName, ReturnType<M['build']>>,
|
|
125
|
+
StorageHash,
|
|
126
|
+
ExtensionPacks,
|
|
127
|
+
Capabilities
|
|
128
|
+
> {
|
|
129
|
+
const modelBuilder = new ModelBuilder<ModelName, TableName>(name, table);
|
|
130
|
+
const result = callback(modelBuilder);
|
|
131
|
+
const finalBuilder = result instanceof ModelBuilder ? result : modelBuilder;
|
|
132
|
+
const modelState = finalBuilder.build();
|
|
133
|
+
|
|
134
|
+
return new ContractBuilder<
|
|
135
|
+
Target,
|
|
136
|
+
Tables,
|
|
137
|
+
Models & Record<ModelName, ReturnType<M['build']>>,
|
|
138
|
+
StorageHash,
|
|
139
|
+
ExtensionPacks,
|
|
140
|
+
Capabilities
|
|
141
|
+
>({
|
|
142
|
+
...this.state,
|
|
143
|
+
models: { ...this.state.models, [name]: modelState } as Models &
|
|
144
|
+
Record<ModelName, ReturnType<M['build']>>,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
storageHash<H extends string>(
|
|
149
|
+
hash: H,
|
|
150
|
+
): ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities> {
|
|
151
|
+
return new ContractBuilder<Target, Tables, Models, H, ExtensionPacks, Capabilities>({
|
|
152
|
+
...this.state,
|
|
153
|
+
storageHash: hash,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
foreignKeyDefaults(
|
|
158
|
+
config: ForeignKeyDefaultsState,
|
|
159
|
+
): ContractBuilder<Target, Tables, Models, StorageHash, ExtensionPacks, Capabilities> {
|
|
160
|
+
return new ContractBuilder<Target, Tables, Models, StorageHash, ExtensionPacks, Capabilities>({
|
|
161
|
+
...this.state,
|
|
162
|
+
foreignKeyDefaults: config,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function defineContract(): ContractBuilder {
|
|
168
|
+
return new ContractBuilder();
|
|
169
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ColumnBuilder,
|
|
3
|
+
ColumnBuilderState,
|
|
4
|
+
ColumnTypeDescriptor,
|
|
5
|
+
ContractBuilderState,
|
|
6
|
+
ForeignKeyDef,
|
|
7
|
+
ForeignKeyDefaultsState,
|
|
8
|
+
ForeignKeyOptions,
|
|
9
|
+
IndexDef,
|
|
10
|
+
ModelBuilderState,
|
|
11
|
+
RelationDefinition,
|
|
12
|
+
TableBuilderState,
|
|
13
|
+
UniqueConstraintDef,
|
|
14
|
+
} from './builder-state';
|
|
15
|
+
|
|
16
|
+
export { ContractBuilder, defineContract } from './contract-builder';
|
|
17
|
+
export { ModelBuilder } from './model-builder';
|
|
18
|
+
export { createTable, TableBuilder } from './table-builder';
|
|
19
|
+
|
|
20
|
+
export type {
|
|
21
|
+
BuildModelFields,
|
|
22
|
+
BuildModels,
|
|
23
|
+
BuildRelations,
|
|
24
|
+
BuildStorage,
|
|
25
|
+
BuildStorageColumn,
|
|
26
|
+
BuildStorageTables,
|
|
27
|
+
ExtractColumns,
|
|
28
|
+
ExtractModelFields,
|
|
29
|
+
ExtractModelRelations,
|
|
30
|
+
ExtractPrimaryKey,
|
|
31
|
+
Mutable,
|
|
32
|
+
} 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,330 @@
|
|
|
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
|
+
}
|