@prisma-next/contract-authoring 0.3.0-dev.16 → 0.3.0-dev.160

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.
@@ -1,160 +0,0 @@
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
- }
@@ -1,90 +0,0 @@
1
- import type { ColumnBuilderState, ColumnTypeDescriptor, TableBuilderState } from './builder-state';
2
-
3
- export class TableBuilder<
4
- Name extends string,
5
- Columns extends Record<string, ColumnBuilderState<string, boolean, string>> = Record<
6
- never,
7
- ColumnBuilderState<string, boolean, string>
8
- >,
9
- PrimaryKey extends readonly string[] | undefined = undefined,
10
- > {
11
- private readonly _name: Name;
12
- private readonly _columns: Columns;
13
- private readonly _primaryKey: PrimaryKey;
14
-
15
- constructor(name: Name, columns: Columns = {} as Columns, primaryKey?: PrimaryKey) {
16
- this._name = name;
17
- this._columns = columns;
18
- this._primaryKey = primaryKey as PrimaryKey;
19
- }
20
-
21
- column<
22
- ColName extends string,
23
- Descriptor extends ColumnTypeDescriptor,
24
- Nullable extends boolean | undefined = undefined,
25
- >(
26
- name: ColName,
27
- options: {
28
- type: Descriptor;
29
- nullable?: Nullable;
30
- },
31
- ): TableBuilder<
32
- Name,
33
- Columns &
34
- Record<
35
- ColName,
36
- ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>
37
- >,
38
- PrimaryKey
39
- > {
40
- const nullable = (options.nullable ?? false) as Nullable extends true ? true : false;
41
- const { codecId, nativeType } = options.type;
42
-
43
- const columnState = {
44
- name,
45
- nullable,
46
- type: codecId,
47
- nativeType,
48
- } as ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>;
49
- return new TableBuilder(
50
- this._name,
51
- { ...this._columns, [name]: columnState } as Columns &
52
- Record<
53
- ColName,
54
- ColumnBuilderState<ColName, Nullable extends true ? true : false, Descriptor['codecId']>
55
- >,
56
- this._primaryKey,
57
- );
58
- }
59
-
60
- primaryKey<PK extends readonly string[]>(
61
- columns: PK,
62
- _name?: string,
63
- ): TableBuilder<Name, Columns, PK> {
64
- return new TableBuilder(this._name, this._columns, columns);
65
- }
66
-
67
- unique(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey> {
68
- return this;
69
- }
70
-
71
- index(_columns: readonly string[], _name?: string): TableBuilder<Name, Columns, PrimaryKey> {
72
- return this;
73
- }
74
-
75
- foreignKey(
76
- _columns: readonly string[],
77
- _references: { table: string; columns: readonly string[] },
78
- _name?: string,
79
- ): TableBuilder<Name, Columns, PrimaryKey> {
80
- return this;
81
- }
82
-
83
- build(): TableBuilderState<Name, Columns, PrimaryKey> {
84
- return {
85
- name: this._name,
86
- columns: this._columns,
87
- ...(this._primaryKey !== undefined ? { primaryKey: this._primaryKey } : {}),
88
- } as TableBuilderState<Name, Columns, PrimaryKey>;
89
- }
90
- }
package/src/types.ts DELETED
@@ -1,129 +0,0 @@
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
- };