@pothos/plugin-drizzle 0.8.0 → 0.9.0
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/CHANGELOG.md +98 -0
- package/TODO.md +25 -4
- package/dts/connection-helpers.d.ts +32 -0
- package/dts/connection-helpers.d.ts.map +1 -0
- package/dts/drizzle-field-builder.d.ts +18 -53
- package/dts/drizzle-field-builder.d.ts.map +1 -1
- package/dts/global-types.d.ts +22 -41
- package/dts/global-types.d.ts.map +1 -1
- package/dts/index.d.ts +4 -2
- package/dts/index.d.ts.map +1 -1
- package/dts/interface-ref.d.ts +3 -2
- package/dts/interface-ref.d.ts.map +1 -1
- package/dts/model-loader.d.ts +3 -2
- package/dts/model-loader.d.ts.map +1 -1
- package/dts/node-ref.d.ts +1 -1
- package/dts/node-ref.d.ts.map +1 -1
- package/dts/object-ref.d.ts +2 -2
- package/dts/object-ref.d.ts.map +1 -1
- package/dts/types.d.ts +61 -41
- package/dts/types.d.ts.map +1 -1
- package/dts/utils/config.d.ts +4 -3
- package/dts/utils/config.d.ts.map +1 -1
- package/dts/utils/cursors.d.ts +13 -7
- package/dts/utils/cursors.d.ts.map +1 -1
- package/dts/utils/drizzle-graphql.d.ts +0 -17
- package/dts/utils/drizzle-graphql.d.ts.map +1 -1
- package/dts/utils/map-query.d.ts.map +1 -1
- package/dts/utils/selections.d.ts +5 -5
- package/dts/utils/selections.d.ts.map +1 -1
- package/esm/connection-helpers.d.ts +34 -0
- package/esm/connection-helpers.d.ts.map +1 -0
- package/esm/connection-helpers.js +74 -0
- package/esm/connection-helpers.js.map +1 -0
- package/esm/drizzle-field-builder.d.ts +18 -73
- package/esm/drizzle-field-builder.d.ts.map +1 -1
- package/esm/drizzle-field-builder.js +25 -14
- package/esm/drizzle-field-builder.js.map +1 -1
- package/esm/field-builder.js +2 -3
- package/esm/field-builder.js.map +1 -1
- package/esm/global-types.d.ts +35 -60
- package/esm/global-types.d.ts.map +1 -1
- package/esm/global-types.js.map +1 -1
- package/esm/index.d.ts +4 -2
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +70 -61
- package/esm/index.js.map +1 -1
- package/esm/interface-ref.d.ts +3 -2
- package/esm/interface-ref.d.ts.map +1 -1
- package/esm/interface-ref.js.map +1 -1
- package/esm/model-loader.d.ts +3 -2
- package/esm/model-loader.d.ts.map +1 -1
- package/esm/model-loader.js +18 -10
- package/esm/model-loader.js.map +1 -1
- package/esm/node-ref.d.ts +1 -1
- package/esm/node-ref.d.ts.map +1 -1
- package/esm/object-ref.d.ts +2 -2
- package/esm/object-ref.d.ts.map +1 -1
- package/esm/schema-builder.js +111 -65
- package/esm/schema-builder.js.map +1 -1
- package/esm/types.d.ts +61 -41
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js.map +1 -1
- package/esm/utils/config.d.ts +4 -3
- package/esm/utils/config.d.ts.map +1 -1
- package/esm/utils/config.js +32 -15
- package/esm/utils/config.js.map +1 -1
- package/esm/utils/cursors.d.ts +13 -7
- package/esm/utils/cursors.d.ts.map +1 -1
- package/esm/utils/cursors.js +137 -63
- package/esm/utils/cursors.js.map +1 -1
- package/esm/utils/drizzle-graphql.d.ts +0 -17
- package/esm/utils/drizzle-graphql.d.ts.map +1 -1
- package/esm/utils/drizzle-graphql.js +230 -198
- package/esm/utils/drizzle-graphql.js.map +1 -1
- package/esm/utils/map-query.d.ts.map +1 -1
- package/esm/utils/map-query.js +9 -5
- package/esm/utils/map-query.js.map +1 -1
- package/esm/utils/selections.d.ts +5 -5
- package/esm/utils/selections.d.ts.map +1 -1
- package/esm/utils/selections.js +19 -18
- package/esm/utils/selections.js.map +1 -1
- package/lib/connection-helpers.js +85 -0
- package/lib/connection-helpers.js.map +1 -0
- package/lib/drizzle-field-builder.js +24 -13
- package/lib/drizzle-field-builder.js.map +1 -1
- package/lib/field-builder.js +2 -3
- package/lib/field-builder.js.map +1 -1
- package/lib/index.js +75 -60
- package/lib/index.js.map +1 -1
- package/lib/interface-ref.js.map +1 -1
- package/lib/model-loader.js +18 -10
- package/lib/model-loader.js.map +1 -1
- package/lib/schema-builder.js +111 -65
- package/lib/schema-builder.js.map +1 -1
- package/lib/types.js.map +1 -1
- package/lib/utils/config.js +31 -14
- package/lib/utils/config.js.map +1 -1
- package/lib/utils/cursors.js +135 -63
- package/lib/utils/cursors.js.map +1 -1
- package/lib/utils/drizzle-graphql.js +230 -221
- package/lib/utils/drizzle-graphql.js.map +1 -1
- package/lib/utils/map-query.js +9 -5
- package/lib/utils/map-query.js.map +1 -1
- package/lib/utils/selections.js +18 -17
- package/lib/utils/selections.js.map +1 -1
- package/package.json +14 -14
- package/src/connection-helpers.ts +184 -0
- package/src/drizzle-field-builder.ts +30 -26
- package/src/field-builder.ts +4 -7
- package/src/global-types.ts +112 -119
- package/src/index.ts +90 -68
- package/src/interface-ref.ts +7 -3
- package/src/model-loader.ts +33 -17
- package/src/node-ref.ts +1 -1
- package/src/object-ref.ts +2 -2
- package/src/schema-builder.ts +114 -111
- package/src/types.ts +95 -62
- package/src/utils/config.ts +39 -30
- package/src/utils/cursors.ts +134 -101
- package/src/utils/drizzle-graphql.ts +273 -274
- package/src/utils/map-query.ts +19 -7
- package/src/utils/selections.ts +34 -28
package/src/types.ts
CHANGED
|
@@ -25,15 +25,14 @@ import {
|
|
|
25
25
|
typeBrandKey,
|
|
26
26
|
} from '@pothos/core';
|
|
27
27
|
import type {
|
|
28
|
+
AnyRelations,
|
|
28
29
|
BuildQueryResult,
|
|
29
30
|
Column,
|
|
30
31
|
DBQueryConfig,
|
|
31
32
|
Many,
|
|
32
33
|
Relation,
|
|
33
|
-
|
|
34
|
-
SQL,
|
|
34
|
+
Table,
|
|
35
35
|
TableRelationalConfig,
|
|
36
|
-
TablesRelationalConfig,
|
|
37
36
|
} from 'drizzle-orm';
|
|
38
37
|
import type { FieldNode, GraphQLResolveInfo } from 'graphql';
|
|
39
38
|
import type { DrizzleObjectFieldBuilder } from './drizzle-field-builder';
|
|
@@ -42,7 +41,12 @@ import type { IndirectInclude } from './utils/map-query';
|
|
|
42
41
|
import type { SelectionMap } from './utils/selections';
|
|
43
42
|
|
|
44
43
|
export type DrizzleClient = {
|
|
45
|
-
_:
|
|
44
|
+
readonly _: {
|
|
45
|
+
readonly schema: unknown;
|
|
46
|
+
readonly fullSchema: Record<string, unknown>;
|
|
47
|
+
readonly tableNamesMap: Record<string, string>;
|
|
48
|
+
readonly relations: AnyRelations;
|
|
49
|
+
};
|
|
46
50
|
query: {};
|
|
47
51
|
};
|
|
48
52
|
|
|
@@ -53,11 +57,23 @@ export type DrizzlePluginOptions<Types extends SchemaTypes> = {
|
|
|
53
57
|
} & (
|
|
54
58
|
| {
|
|
55
59
|
client: DrizzleClient;
|
|
56
|
-
|
|
60
|
+
getTableConfig: (table: Table) => {
|
|
61
|
+
primaryKeys: {
|
|
62
|
+
readonly columns: Column[];
|
|
63
|
+
}[];
|
|
64
|
+
readonly columns: Column[];
|
|
65
|
+
};
|
|
66
|
+
relations?: Types['DrizzleRelations'];
|
|
57
67
|
}
|
|
58
68
|
| {
|
|
59
69
|
client: (ctx: Types['Context']) => DrizzleClient;
|
|
60
|
-
|
|
70
|
+
getTableConfig: (table: Table) => {
|
|
71
|
+
primaryKeys: {
|
|
72
|
+
readonly columns: Column[];
|
|
73
|
+
}[];
|
|
74
|
+
readonly columns: Column[];
|
|
75
|
+
};
|
|
76
|
+
relations?: Types['DrizzleRelations'];
|
|
61
77
|
}
|
|
62
78
|
);
|
|
63
79
|
|
|
@@ -75,7 +91,7 @@ type NameOrVariant =
|
|
|
75
91
|
|
|
76
92
|
export type DrizzleObjectOptions<
|
|
77
93
|
Types extends SchemaTypes,
|
|
78
|
-
Table extends keyof Types['
|
|
94
|
+
Table extends keyof Types['DrizzleRelationsConfig'],
|
|
79
95
|
Shape,
|
|
80
96
|
Selection,
|
|
81
97
|
Interfaces extends InterfaceParam<Types>[],
|
|
@@ -85,7 +101,7 @@ export type DrizzleObjectOptions<
|
|
|
85
101
|
fields?: (
|
|
86
102
|
t: DrizzleObjectFieldBuilder<
|
|
87
103
|
Types,
|
|
88
|
-
Types['
|
|
104
|
+
Types['DrizzleRelationsConfig'][Table],
|
|
89
105
|
Shape & { [drizzleTableName]?: Table }
|
|
90
106
|
>,
|
|
91
107
|
) => FieldMap;
|
|
@@ -93,7 +109,7 @@ export type DrizzleObjectOptions<
|
|
|
93
109
|
|
|
94
110
|
export type DrizzleInterfaceOptions<
|
|
95
111
|
Types extends SchemaTypes,
|
|
96
|
-
Table extends keyof Types['
|
|
112
|
+
Table extends keyof Types['DrizzleRelationsConfig'],
|
|
97
113
|
Shape,
|
|
98
114
|
Selection,
|
|
99
115
|
Interfaces extends InterfaceParam<Types>[],
|
|
@@ -108,7 +124,7 @@ export type DrizzleInterfaceOptions<
|
|
|
108
124
|
fields?: (
|
|
109
125
|
t: DrizzleObjectFieldBuilder<
|
|
110
126
|
Types,
|
|
111
|
-
Types['
|
|
127
|
+
Types['DrizzleRelationsConfig'][Table],
|
|
112
128
|
Shape & { [drizzleTableName]?: Table }
|
|
113
129
|
>,
|
|
114
130
|
) => FieldMap;
|
|
@@ -116,11 +132,11 @@ export type DrizzleInterfaceOptions<
|
|
|
116
132
|
|
|
117
133
|
export type DrizzleNodeOptions<
|
|
118
134
|
Types extends SchemaTypes,
|
|
119
|
-
Table extends keyof Types['
|
|
135
|
+
Table extends keyof Types['DrizzleRelationsConfig'],
|
|
120
136
|
Shape,
|
|
121
137
|
Selection,
|
|
122
138
|
Interfaces extends InterfaceParam<Types>[],
|
|
123
|
-
|
|
139
|
+
IDColumns,
|
|
124
140
|
> = NameOrVariant &
|
|
125
141
|
Omit<
|
|
126
142
|
| PothosSchemaTypes.ObjectTypeOptions<Types, Shape>
|
|
@@ -140,26 +156,46 @@ export type DrizzleNodeOptions<
|
|
|
140
156
|
>,
|
|
141
157
|
'args' | 'nullable' | 'type' | InferredFieldOptionKeys
|
|
142
158
|
> & {
|
|
143
|
-
column:
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
159
|
+
column: IDColumns &
|
|
160
|
+
(
|
|
161
|
+
| Column
|
|
162
|
+
| Column[]
|
|
163
|
+
| ((columns: Types['DrizzleRelationsConfig'][Table]['columns']) => Column | Column[])
|
|
164
|
+
);
|
|
147
165
|
};
|
|
148
166
|
name: string;
|
|
149
167
|
select?: Selection;
|
|
150
168
|
fields?: (
|
|
151
169
|
t: DrizzleObjectFieldBuilder<
|
|
152
170
|
Types,
|
|
153
|
-
Types['
|
|
171
|
+
Types['DrizzleRelationsConfig'][Table],
|
|
154
172
|
Shape & { [drizzleTableName]?: Table }
|
|
155
173
|
>,
|
|
156
174
|
) => FieldMap;
|
|
157
175
|
};
|
|
158
176
|
|
|
177
|
+
export type ShapeFromIdColumns<
|
|
178
|
+
Types extends SchemaTypes,
|
|
179
|
+
Table extends keyof Types['DrizzleRelationsConfig'],
|
|
180
|
+
IDColumns,
|
|
181
|
+
> = IDColumns extends Column
|
|
182
|
+
? IDColumns['_']['data']
|
|
183
|
+
: IDColumns extends Column[]
|
|
184
|
+
? {
|
|
185
|
+
[K in IDColumns[number]['_']['name']]: Extract<
|
|
186
|
+
IDColumns[number],
|
|
187
|
+
{ _: { name: K } }
|
|
188
|
+
>['_']['data'];
|
|
189
|
+
}
|
|
190
|
+
: // biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
191
|
+
IDColumns extends ((...args: any[]) => infer R extends Column | Column[])
|
|
192
|
+
? ShapeFromIdColumns<Types, Table, R>
|
|
193
|
+
: never;
|
|
194
|
+
|
|
159
195
|
export type DrizzleFieldOptions<
|
|
160
196
|
Types extends SchemaTypes,
|
|
161
197
|
ParentShape,
|
|
162
|
-
Table extends keyof Types['
|
|
198
|
+
Table extends keyof Types['DrizzleRelationsConfig'],
|
|
163
199
|
Type extends TypeParam<Types>,
|
|
164
200
|
Nullable extends FieldNullability<Type>,
|
|
165
201
|
Args extends InputFieldMap,
|
|
@@ -193,7 +229,7 @@ export type DrizzleFieldOptions<
|
|
|
193
229
|
export type DrizzleFieldWithInputOptions<
|
|
194
230
|
Types extends SchemaTypes,
|
|
195
231
|
ParentShape,
|
|
196
|
-
Table extends keyof Types['
|
|
232
|
+
Table extends keyof Types['DrizzleRelationsConfig'],
|
|
197
233
|
Type extends TypeParam<Types>,
|
|
198
234
|
Nullable extends FieldNullability<Type>,
|
|
199
235
|
Args extends InputFieldMap,
|
|
@@ -239,7 +275,7 @@ export type DrizzleObjectFieldOptions<
|
|
|
239
275
|
unknown extends Select
|
|
240
276
|
? ParentShape
|
|
241
277
|
: BuildQueryResult<
|
|
242
|
-
Types['
|
|
278
|
+
Types['DrizzleRelationsConfig'],
|
|
243
279
|
ExtractTable<Types, ParentShape>,
|
|
244
280
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
245
281
|
Record<string, unknown> & (Select extends (...args: any[]) => infer R ? R : Select)
|
|
@@ -267,12 +303,7 @@ export type DrizzleObjectFieldOptions<
|
|
|
267
303
|
> & {
|
|
268
304
|
select?: Select &
|
|
269
305
|
(
|
|
270
|
-
| DBQueryConfig<
|
|
271
|
-
'one',
|
|
272
|
-
false,
|
|
273
|
-
Types['DrizzleRelationSchema'],
|
|
274
|
-
ExtractTable<Types, ParentShape>
|
|
275
|
-
>
|
|
306
|
+
| DBQueryConfig<'one', Types['DrizzleRelationsConfig'], ExtractTable<Types, ParentShape>>
|
|
276
307
|
| ((
|
|
277
308
|
args: InputShapeFromFields<Args>,
|
|
278
309
|
ctx: Types['Context'],
|
|
@@ -282,15 +313,14 @@ export type DrizzleObjectFieldOptions<
|
|
|
282
313
|
) => Selection,
|
|
283
314
|
) => DBQueryConfig<
|
|
284
315
|
'one',
|
|
285
|
-
|
|
286
|
-
Types['DrizzleRelationSchema'],
|
|
316
|
+
Types['DrizzleRelationsConfig'],
|
|
287
317
|
ExtractTable<Types, ParentShape>
|
|
288
318
|
>)
|
|
289
319
|
);
|
|
290
320
|
};
|
|
291
321
|
|
|
292
322
|
export type DrizzleFieldSelection =
|
|
293
|
-
| DBQueryConfig<'one'
|
|
323
|
+
| DBQueryConfig<'one'>
|
|
294
324
|
| ((
|
|
295
325
|
args: {},
|
|
296
326
|
ctx: object,
|
|
@@ -298,17 +328,17 @@ export type DrizzleFieldSelection =
|
|
|
298
328
|
selection:
|
|
299
329
|
| SelectionMap
|
|
300
330
|
| boolean
|
|
301
|
-
| ((args: object, context: object) => DBQueryConfig<'one'
|
|
331
|
+
| ((args: object, context: object) => DBQueryConfig<'one'>),
|
|
302
332
|
path?: IndirectInclude | string[],
|
|
303
333
|
type?: string,
|
|
304
|
-
) => DBQueryConfig<'one'
|
|
334
|
+
) => DBQueryConfig<'one'> | boolean,
|
|
305
335
|
resolveSelection: (path: string[]) => FieldNode | null,
|
|
306
336
|
) => SelectionMap);
|
|
307
337
|
|
|
308
338
|
export type ExtractTable<Types extends SchemaTypes, Shape> = Shape extends {
|
|
309
|
-
[drizzleTableName]?: keyof Types['
|
|
339
|
+
[drizzleTableName]?: keyof Types['DrizzleRelationsConfig'];
|
|
310
340
|
}
|
|
311
|
-
? Types['
|
|
341
|
+
? Types['DrizzleRelationsConfig'][NonNullable<Shape[typeof drizzleTableName]>]
|
|
312
342
|
: never;
|
|
313
343
|
|
|
314
344
|
export type RelatedFieldOptions<
|
|
@@ -341,7 +371,7 @@ export type RelatedFieldOptions<
|
|
|
341
371
|
|
|
342
372
|
export type VariantFieldOptions<
|
|
343
373
|
Types extends SchemaTypes,
|
|
344
|
-
Table extends keyof Types['
|
|
374
|
+
Table extends keyof Types['DrizzleRelationsConfig'],
|
|
345
375
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
346
376
|
Variant extends DrizzleRef<any, Table> | Table,
|
|
347
377
|
Args extends InputFieldMap,
|
|
@@ -370,7 +400,7 @@ export type VariantFieldOptions<
|
|
|
370
400
|
unknown extends ResolveShape
|
|
371
401
|
? Shape
|
|
372
402
|
: BuildQueryResult<
|
|
373
|
-
Types['
|
|
403
|
+
Types['DrizzleRelationsConfig'],
|
|
374
404
|
ExtractTable<Types, Shape>,
|
|
375
405
|
Record<string, unknown> &
|
|
376
406
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
@@ -393,8 +423,8 @@ export type RefForRelation<Types extends SchemaTypes, Rel extends Relation> = Re
|
|
|
393
423
|
: [ObjectRef<Types, TypesForRelation<Types, Rel>>];
|
|
394
424
|
|
|
395
425
|
export type TypesForRelation<Types extends SchemaTypes, Rel extends Relation> = BuildQueryResult<
|
|
396
|
-
Types['
|
|
397
|
-
Types['
|
|
426
|
+
Types['DrizzleRelationsConfig'],
|
|
427
|
+
Types['DrizzleRelationsConfig'][Rel['targetTable']['_']['name']],
|
|
398
428
|
true
|
|
399
429
|
>;
|
|
400
430
|
|
|
@@ -409,18 +439,16 @@ export type QueryForField<
|
|
|
409
439
|
? Omit<
|
|
410
440
|
DBQueryConfig<
|
|
411
441
|
'one',
|
|
412
|
-
|
|
413
|
-
Types['
|
|
414
|
-
Types['DrizzleRelationSchemaByDbName'][Rel['referencedTableName']]
|
|
442
|
+
Types['DrizzleRelationsConfig'],
|
|
443
|
+
Types['DrizzleRelationsConfig'][Rel['targetTable']['_']['name']]
|
|
415
444
|
>,
|
|
416
445
|
'columns' | 'extra' | 'with'
|
|
417
446
|
>
|
|
418
447
|
: Omit<
|
|
419
448
|
DBQueryConfig<
|
|
420
449
|
'many',
|
|
421
|
-
|
|
422
|
-
Types['
|
|
423
|
-
Types['DrizzleRelationSchemaByDbName'][Rel['referencedTableName']]
|
|
450
|
+
Types['DrizzleRelationsConfig'],
|
|
451
|
+
Types['DrizzleRelationsConfig'][Rel['targetTable']['_']['name']]
|
|
424
452
|
>,
|
|
425
453
|
'columns' | 'extra' | 'with'
|
|
426
454
|
>
|
|
@@ -431,13 +459,13 @@ export type QueryForField<
|
|
|
431
459
|
export type QueryForDrizzleField<
|
|
432
460
|
Types extends SchemaTypes,
|
|
433
461
|
Param,
|
|
434
|
-
Table extends keyof Types['
|
|
462
|
+
Table extends keyof Types['DrizzleRelationsConfig'],
|
|
435
463
|
> = Omit<
|
|
436
464
|
DBQueryConfig<
|
|
437
465
|
'many',
|
|
438
|
-
|
|
439
|
-
Types['
|
|
440
|
-
|
|
466
|
+
Types['DrizzleRelationsConfig'],
|
|
467
|
+
Types['DrizzleRelationsConfig'][Table],
|
|
468
|
+
true
|
|
441
469
|
>,
|
|
442
470
|
Param extends [unknown] ? never : 'limit'
|
|
443
471
|
>;
|
|
@@ -447,10 +475,10 @@ export type QueryForRelatedConnection<
|
|
|
447
475
|
Table extends TableRelationalConfig,
|
|
448
476
|
Args,
|
|
449
477
|
> = Omit<
|
|
450
|
-
DBQueryConfig<'many',
|
|
451
|
-
'
|
|
478
|
+
DBQueryConfig<'many', Types['DrizzleRelationsConfig'], Table>,
|
|
479
|
+
'limit' | 'offset' | 'columns' | 'extra' | 'with' | 'orderBy'
|
|
452
480
|
> & {
|
|
453
|
-
orderBy?: ConnectionOrderBy | ((columns: Table['columns']) => ConnectionOrderBy);
|
|
481
|
+
orderBy?: ConnectionOrderBy<Table> | ((columns: Table['columns']) => ConnectionOrderBy<Table>);
|
|
454
482
|
} extends infer QueryConfig
|
|
455
483
|
? QueryConfig | ((args: Args, context: Types['Context']) => QueryConfig)
|
|
456
484
|
: never;
|
|
@@ -459,21 +487,21 @@ export type QueryForDrizzleConnection<
|
|
|
459
487
|
Types extends SchemaTypes,
|
|
460
488
|
Table extends TableRelationalConfig,
|
|
461
489
|
> = Omit<
|
|
462
|
-
DBQueryConfig<'many',
|
|
463
|
-
'
|
|
490
|
+
DBQueryConfig<'many', Types['DrizzleRelationsConfig'], Table>,
|
|
491
|
+
'limit' | 'offset' | 'orderBy'
|
|
464
492
|
> & {
|
|
465
|
-
orderBy?: ConnectionOrderBy | ((columns: Table['columns']) => ConnectionOrderBy);
|
|
493
|
+
orderBy?: ConnectionOrderBy<Table> | ((columns: Table['columns']) => ConnectionOrderBy<Table>);
|
|
466
494
|
};
|
|
467
495
|
|
|
468
496
|
export type ListRelation<T extends TableRelationalConfig> = {
|
|
469
|
-
[K in keyof T['relations']]: T['relations'][K] extends Many<string> ? K : never;
|
|
497
|
+
[K in keyof T['relations']]: T['relations'][K] extends Many<string, string> ? K : never;
|
|
470
498
|
}[keyof T['relations']];
|
|
471
499
|
|
|
472
500
|
export type DrizzleConnectionFieldOptions<
|
|
473
501
|
Types extends SchemaTypes,
|
|
474
502
|
ParentShape,
|
|
475
503
|
Type extends // biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
476
|
-
DrizzleRef<any, keyof Types['
|
|
504
|
+
DrizzleRef<any, keyof Types['DrizzleRelationsConfig']> | keyof Types['DrizzleRelationsConfig'],
|
|
477
505
|
TableConfig extends TableRelationalConfig,
|
|
478
506
|
Param extends OutputType<Types>,
|
|
479
507
|
Nullable extends boolean,
|
|
@@ -517,7 +545,11 @@ export type DrizzleConnectionFieldOptions<
|
|
|
517
545
|
resolve: (
|
|
518
546
|
query: <T extends QueryForRelatedConnection<Types, TableConfig, ConnectionArgs>>(
|
|
519
547
|
selection?: T,
|
|
520
|
-
) => Omit<T, 'orderBy'> & {
|
|
548
|
+
) => Omit<T, 'orderBy'> & {
|
|
549
|
+
orderBy: {
|
|
550
|
+
[K in keyof TableConfig['columns']]?: 'asc' | 'desc' | undefined;
|
|
551
|
+
};
|
|
552
|
+
},
|
|
521
553
|
parent: ParentShape,
|
|
522
554
|
args: ConnectionArgs,
|
|
523
555
|
context: Types['Context'],
|
|
@@ -538,7 +570,7 @@ export type RelatedConnectionOptions<
|
|
|
538
570
|
Nullable extends boolean,
|
|
539
571
|
Args extends InputFieldMap,
|
|
540
572
|
NodeTable extends
|
|
541
|
-
TableRelationalConfig = Types['
|
|
573
|
+
TableRelationalConfig = Types['DrizzleRelationsConfig'][Table['relations'][Field]['targetTable']['_']['name']],
|
|
542
574
|
> = Omit<
|
|
543
575
|
PothosSchemaTypes.ObjectFieldOptions<
|
|
544
576
|
Types,
|
|
@@ -581,7 +613,7 @@ export type RelatedConnectionOptions<
|
|
|
581
613
|
>;
|
|
582
614
|
query?: QueryForRelatedConnection<Types, NodeTable, ConnectionArgs>;
|
|
583
615
|
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
584
|
-
type?: DrizzleRef<any, Table['relations'][Field]['
|
|
616
|
+
type?: DrizzleRef<any, Table['relations'][Field]['targetTable']['_']['name']>;
|
|
585
617
|
|
|
586
618
|
defaultSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
|
|
587
619
|
maxSize?: number | ((args: ConnectionArgs, ctx: Types['Context']) => number);
|
|
@@ -589,11 +621,12 @@ export type RelatedConnectionOptions<
|
|
|
589
621
|
}
|
|
590
622
|
: never);
|
|
591
623
|
|
|
592
|
-
export type ConnectionOrderBy =
|
|
624
|
+
export type ConnectionOrderBy<T extends TableRelationalConfig> =
|
|
593
625
|
| Column
|
|
594
|
-
|
|
|
595
|
-
| {
|
|
596
|
-
|
|
626
|
+
| Column[]
|
|
627
|
+
| {
|
|
628
|
+
[K in keyof T['columns']]?: 'asc' | 'desc' | undefined;
|
|
629
|
+
};
|
|
597
630
|
|
|
598
631
|
export type ShapeFromConnection<T> = T extends { shape: unknown } ? T['shape'] : never;
|
|
599
632
|
|
package/src/utils/config.ts
CHANGED
|
@@ -1,41 +1,28 @@
|
|
|
1
1
|
import { type SchemaTypes, createContextCache } from '@pothos/core';
|
|
2
|
-
import {
|
|
3
|
-
type Column,
|
|
4
|
-
type RelationalSchemaConfig,
|
|
5
|
-
type TableRelationalConfig,
|
|
6
|
-
type TablesRelationalConfig,
|
|
7
|
-
createTableRelationsHelpers,
|
|
8
|
-
extractTablesRelationalConfig,
|
|
9
|
-
getTableName,
|
|
10
|
-
} from 'drizzle-orm';
|
|
2
|
+
import { type AnyRelations, type Column, type Table, getTableName } from 'drizzle-orm';
|
|
11
3
|
import type { DrizzleClient } from '../types';
|
|
12
4
|
|
|
13
|
-
export interface PothosDrizzleSchemaConfig
|
|
5
|
+
export interface PothosDrizzleSchemaConfig {
|
|
14
6
|
skipDeferredFragments: boolean;
|
|
15
|
-
|
|
7
|
+
relations: AnyRelations;
|
|
8
|
+
getPrimaryKey: (tableName: string) => Column[];
|
|
16
9
|
columnToTsName: (column: Column) => string;
|
|
17
10
|
}
|
|
18
11
|
const configCache = createContextCache(
|
|
19
12
|
(builder: PothosSchemaTypes.SchemaBuilder<SchemaTypes>): PothosDrizzleSchemaConfig => {
|
|
20
|
-
let
|
|
21
|
-
if (builder.options.drizzle.
|
|
22
|
-
|
|
23
|
-
builder.options.drizzle.schema,
|
|
24
|
-
createTableRelationsHelpers,
|
|
25
|
-
);
|
|
26
|
-
config = {
|
|
27
|
-
fullSchema: builder.options.drizzle.schema,
|
|
28
|
-
schema: tablesConfig.tables,
|
|
29
|
-
tableNamesMap: tablesConfig.tableNamesMap,
|
|
30
|
-
};
|
|
13
|
+
let relations: AnyRelations;
|
|
14
|
+
if (builder.options.drizzle.relations) {
|
|
15
|
+
relations = builder.options.drizzle.relations;
|
|
31
16
|
} else {
|
|
32
|
-
|
|
33
|
-
._ as RelationalSchemaConfig<TablesRelationalConfig>;
|
|
17
|
+
relations = (builder.options.drizzle.client as DrizzleClient)._.relations;
|
|
34
18
|
}
|
|
35
19
|
|
|
36
|
-
const dbToSchema = Object.values(
|
|
20
|
+
const dbToSchema = Object.values(relations.tables).reduce<Record<string, Table>>(
|
|
37
21
|
(acc, table) => {
|
|
38
|
-
|
|
22
|
+
const tableName = getTableName(table);
|
|
23
|
+
if (tableName) {
|
|
24
|
+
acc[tableName] = table;
|
|
25
|
+
}
|
|
39
26
|
return acc;
|
|
40
27
|
},
|
|
41
28
|
{},
|
|
@@ -43,9 +30,9 @@ const configCache = createContextCache(
|
|
|
43
30
|
|
|
44
31
|
const columnMappings = Object.values(dbToSchema).reduce<Record<string, Record<string, string>>>(
|
|
45
32
|
(acc, table) => {
|
|
46
|
-
acc[table
|
|
33
|
+
acc[getTableName(table)] = Object.entries(table).reduce<Record<string, string>>(
|
|
47
34
|
(acc, [name, column]) => {
|
|
48
|
-
acc[column.name] = name;
|
|
35
|
+
acc[(column as Column).name] = name;
|
|
49
36
|
return acc;
|
|
50
37
|
},
|
|
51
38
|
{},
|
|
@@ -57,7 +44,6 @@ const configCache = createContextCache(
|
|
|
57
44
|
|
|
58
45
|
return {
|
|
59
46
|
skipDeferredFragments: builder.options.drizzle.skipDeferredFragments ?? true,
|
|
60
|
-
dbToSchema,
|
|
61
47
|
columnToTsName: (column) => {
|
|
62
48
|
const tableName = getTableName(column.table);
|
|
63
49
|
const table = columnMappings[tableName];
|
|
@@ -69,7 +55,30 @@ const configCache = createContextCache(
|
|
|
69
55
|
|
|
70
56
|
return columnName;
|
|
71
57
|
},
|
|
72
|
-
|
|
58
|
+
getPrimaryKey: (tableName) => {
|
|
59
|
+
const tableConfig = builder.options.drizzle.getTableConfig(relations.tables[tableName]);
|
|
60
|
+
|
|
61
|
+
const primaryKey = tableConfig.columns.find((column) => column.primary);
|
|
62
|
+
|
|
63
|
+
if (primaryKey) {
|
|
64
|
+
return [primaryKey];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const primaryKeys = tableConfig.primaryKeys.find((key) => key.columns.length > 0);
|
|
68
|
+
|
|
69
|
+
if (primaryKeys) {
|
|
70
|
+
return primaryKeys.columns;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const uniqueColumn = tableConfig.columns.find((column) => column.isUnique);
|
|
74
|
+
|
|
75
|
+
if (uniqueColumn) {
|
|
76
|
+
return [uniqueColumn];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
throw new Error(`Could not find primary key for table ${tableName}`);
|
|
80
|
+
},
|
|
81
|
+
relations,
|
|
73
82
|
};
|
|
74
83
|
},
|
|
75
84
|
);
|