@pothos/plugin-drizzle 0.11.2 → 0.12.1
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 +12 -0
- package/dts/connection-helpers.d.ts +7 -5
- package/dts/connection-helpers.d.ts.map +1 -1
- package/dts/drizzle-field-builder.d.ts +2 -2
- package/dts/drizzle-field-builder.d.ts.map +1 -1
- package/dts/global-types.d.ts +23 -24
- package/dts/global-types.d.ts.map +1 -1
- package/dts/interface-ref.d.ts +3 -3
- package/dts/interface-ref.d.ts.map +1 -1
- 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 +35 -31
- package/dts/types.d.ts.map +1 -1
- package/dts/utils/config.d.ts.map +1 -1
- package/dts/utils/cursors.d.ts +3 -3
- package/dts/utils/cursors.d.ts.map +1 -1
- package/dts/utils/selections.d.ts +1 -1
- package/dts/utils/selections.d.ts.map +1 -1
- package/esm/connection-helpers.d.ts +7 -5
- package/esm/connection-helpers.d.ts.map +1 -1
- package/esm/connection-helpers.js +2 -2
- package/esm/connection-helpers.js.map +1 -1
- package/esm/drizzle-field-builder.d.ts +2 -2
- package/esm/drizzle-field-builder.d.ts.map +1 -1
- package/esm/drizzle-field-builder.js +13 -19
- package/esm/drizzle-field-builder.js.map +1 -1
- package/esm/global-types.d.ts +34 -35
- package/esm/global-types.d.ts.map +1 -1
- package/esm/global-types.js.map +1 -1
- package/esm/interface-ref.d.ts +3 -3
- package/esm/interface-ref.d.ts.map +1 -1
- package/esm/model-loader.d.ts.map +1 -1
- package/esm/model-loader.js +3 -3
- 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 +8 -8
- package/esm/schema-builder.js.map +1 -1
- package/esm/types.d.ts +35 -31
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js.map +1 -1
- package/esm/utils/config.d.ts.map +1 -1
- package/esm/utils/config.js +17 -21
- package/esm/utils/config.js.map +1 -1
- package/esm/utils/cursors.d.ts +3 -3
- package/esm/utils/cursors.d.ts.map +1 -1
- package/esm/utils/cursors.js +17 -14
- package/esm/utils/cursors.js.map +1 -1
- package/esm/utils/selections.d.ts +1 -1
- package/esm/utils/selections.d.ts.map +1 -1
- package/esm/utils/selections.js +6 -13
- package/esm/utils/selections.js.map +1 -1
- package/lib/connection-helpers.js +2 -2
- package/lib/connection-helpers.js.map +1 -1
- package/lib/drizzle-field-builder.js +12 -18
- package/lib/drizzle-field-builder.js.map +1 -1
- package/lib/model-loader.js +2 -2
- package/lib/model-loader.js.map +1 -1
- package/lib/schema-builder.js +8 -8
- package/lib/schema-builder.js.map +1 -1
- package/lib/types.js.map +1 -1
- package/lib/utils/config.js +16 -20
- package/lib/utils/config.js.map +1 -1
- package/lib/utils/cursors.js +17 -14
- package/lib/utils/cursors.js.map +1 -1
- package/lib/utils/selections.js +6 -12
- package/lib/utils/selections.js.map +1 -1
- package/package.json +7 -7
- package/src/connection-helpers.ts +13 -12
- package/src/drizzle-field-builder.ts +14 -30
- package/src/global-types.ts +59 -85
- package/src/interface-ref.ts +3 -3
- package/src/model-loader.ts +4 -3
- package/src/node-ref.ts +1 -1
- package/src/object-ref.ts +2 -2
- package/src/schema-builder.ts +8 -8
- package/src/types.ts +45 -47
- package/src/utils/config.ts +21 -32
- package/src/utils/cursors.ts +31 -18
- package/src/utils/selections.ts +7 -17
package/src/types.ts
CHANGED
|
@@ -75,7 +75,7 @@ export type DrizzlePluginOptions<Types extends SchemaTypes> = {
|
|
|
75
75
|
}[];
|
|
76
76
|
readonly columns: Column[];
|
|
77
77
|
};
|
|
78
|
-
relations
|
|
78
|
+
relations: Types['DrizzleRelations'];
|
|
79
79
|
}
|
|
80
80
|
);
|
|
81
81
|
|
|
@@ -93,7 +93,7 @@ type NameOrVariant =
|
|
|
93
93
|
|
|
94
94
|
export type DrizzleObjectOptions<
|
|
95
95
|
Types extends SchemaTypes,
|
|
96
|
-
Table extends keyof Types['
|
|
96
|
+
Table extends keyof Types['DrizzleRelations'],
|
|
97
97
|
Shape,
|
|
98
98
|
Selection,
|
|
99
99
|
Interfaces extends InterfaceParam<Types>[],
|
|
@@ -103,7 +103,7 @@ export type DrizzleObjectOptions<
|
|
|
103
103
|
fields?: (
|
|
104
104
|
t: DrizzleObjectFieldBuilder<
|
|
105
105
|
Types,
|
|
106
|
-
Types['
|
|
106
|
+
Types['DrizzleRelations'][Table],
|
|
107
107
|
Shape & { [drizzleTableName]?: Table }
|
|
108
108
|
>,
|
|
109
109
|
) => FieldMap;
|
|
@@ -111,7 +111,7 @@ export type DrizzleObjectOptions<
|
|
|
111
111
|
|
|
112
112
|
export type DrizzleInterfaceOptions<
|
|
113
113
|
Types extends SchemaTypes,
|
|
114
|
-
Table extends keyof Types['
|
|
114
|
+
Table extends keyof Types['DrizzleRelations'],
|
|
115
115
|
Shape,
|
|
116
116
|
Selection,
|
|
117
117
|
Interfaces extends InterfaceParam<Types>[],
|
|
@@ -121,7 +121,7 @@ export type DrizzleInterfaceOptions<
|
|
|
121
121
|
fields?: (
|
|
122
122
|
t: DrizzleObjectFieldBuilder<
|
|
123
123
|
Types,
|
|
124
|
-
Types['
|
|
124
|
+
Types['DrizzleRelations'][Table],
|
|
125
125
|
Shape & { [drizzleTableName]?: Table }
|
|
126
126
|
>,
|
|
127
127
|
) => FieldMap;
|
|
@@ -129,7 +129,7 @@ export type DrizzleInterfaceOptions<
|
|
|
129
129
|
|
|
130
130
|
export type DrizzleNodeOptions<
|
|
131
131
|
Types extends SchemaTypes,
|
|
132
|
-
Table extends keyof Types['
|
|
132
|
+
Table extends keyof Types['DrizzleRelations'],
|
|
133
133
|
Shape,
|
|
134
134
|
Selection,
|
|
135
135
|
Interfaces extends InterfaceParam<Types>[],
|
|
@@ -157,14 +157,14 @@ export type DrizzleNodeOptions<
|
|
|
157
157
|
(
|
|
158
158
|
| Column
|
|
159
159
|
| Column[]
|
|
160
|
-
| ((columns: Types['
|
|
160
|
+
| ((columns: Types['DrizzleRelations'][Table]['table']) => Column | Column[])
|
|
161
161
|
);
|
|
162
162
|
};
|
|
163
163
|
select?: Selection;
|
|
164
164
|
fields?: (
|
|
165
165
|
t: DrizzleObjectFieldBuilder<
|
|
166
166
|
Types,
|
|
167
|
-
Types['
|
|
167
|
+
Types['DrizzleRelations'][Table],
|
|
168
168
|
Shape & { [drizzleTableName]?: Table }
|
|
169
169
|
>,
|
|
170
170
|
) => FieldMap;
|
|
@@ -172,7 +172,7 @@ export type DrizzleNodeOptions<
|
|
|
172
172
|
|
|
173
173
|
export type ShapeFromIdColumns<
|
|
174
174
|
Types extends SchemaTypes,
|
|
175
|
-
Table extends keyof Types['
|
|
175
|
+
Table extends keyof Types['DrizzleRelations'],
|
|
176
176
|
IDColumns,
|
|
177
177
|
> = IDColumns extends Column
|
|
178
178
|
? IDColumns['_']['data']
|
|
@@ -191,7 +191,7 @@ export type ShapeFromIdColumns<
|
|
|
191
191
|
export type DrizzleFieldOptions<
|
|
192
192
|
Types extends SchemaTypes,
|
|
193
193
|
ParentShape,
|
|
194
|
-
Table extends keyof Types['
|
|
194
|
+
Table extends keyof Types['DrizzleRelations'],
|
|
195
195
|
Type extends TypeParam<Types>,
|
|
196
196
|
Nullable extends FieldNullability<Type>,
|
|
197
197
|
Args extends InputFieldMap,
|
|
@@ -214,7 +214,9 @@ export type DrizzleFieldOptions<
|
|
|
214
214
|
> & {
|
|
215
215
|
type: Param;
|
|
216
216
|
resolve: (
|
|
217
|
-
query: <T extends QueryForDrizzleField<Types, Param, Table>>(
|
|
217
|
+
query: <T extends QueryForDrizzleField<Types, Param, Table>>(
|
|
218
|
+
selection?: T,
|
|
219
|
+
) => Omit<T, 'columns'>,
|
|
218
220
|
parent: ParentShape,
|
|
219
221
|
args: InputShapeFromFields<Args>,
|
|
220
222
|
ctx: Types['Context'],
|
|
@@ -225,7 +227,7 @@ export type DrizzleFieldOptions<
|
|
|
225
227
|
export type DrizzleFieldWithInputOptions<
|
|
226
228
|
Types extends SchemaTypes,
|
|
227
229
|
ParentShape,
|
|
228
|
-
Table extends keyof Types['
|
|
230
|
+
Table extends keyof Types['DrizzleRelations'],
|
|
229
231
|
Type extends TypeParam<Types>,
|
|
230
232
|
Nullable extends FieldNullability<Type>,
|
|
231
233
|
Args extends InputFieldMap,
|
|
@@ -271,7 +273,7 @@ export type DrizzleObjectFieldOptions<
|
|
|
271
273
|
unknown extends Select
|
|
272
274
|
? ParentShape
|
|
273
275
|
: BuildQueryResult<
|
|
274
|
-
Types['
|
|
276
|
+
Types['DrizzleRelations'],
|
|
275
277
|
ExtractTable<Types, ParentShape>,
|
|
276
278
|
Record<string, unknown> &
|
|
277
279
|
// biome-ignore lint/suspicious/noExplicitAny: this is fine
|
|
@@ -302,7 +304,7 @@ export type DrizzleObjectFieldOptions<
|
|
|
302
304
|
> & {
|
|
303
305
|
select?: Select &
|
|
304
306
|
(
|
|
305
|
-
| DBQueryConfig<'one', Types['
|
|
307
|
+
| DBQueryConfig<'one', Types['DrizzleRelations'], ExtractTable<Types, ParentShape>>
|
|
306
308
|
| ((
|
|
307
309
|
args: InputShapeFromFields<Args>,
|
|
308
310
|
ctx: Types['Context'],
|
|
@@ -310,11 +312,7 @@ export type DrizzleObjectFieldOptions<
|
|
|
310
312
|
selection?: Selection,
|
|
311
313
|
path?: string[],
|
|
312
314
|
) => Selection,
|
|
313
|
-
) => DBQueryConfig<
|
|
314
|
-
'one',
|
|
315
|
-
Types['DrizzleRelationsConfig'],
|
|
316
|
-
ExtractTable<Types, ParentShape>
|
|
317
|
-
>)
|
|
315
|
+
) => DBQueryConfig<'one', Types['DrizzleRelations'], ExtractTable<Types, ParentShape>>)
|
|
318
316
|
);
|
|
319
317
|
};
|
|
320
318
|
|
|
@@ -335,9 +333,9 @@ export type DrizzleFieldSelection =
|
|
|
335
333
|
) => SelectionMap);
|
|
336
334
|
|
|
337
335
|
export type ExtractTable<Types extends SchemaTypes, Shape> = Shape extends {
|
|
338
|
-
[drizzleTableName]?: keyof Types['
|
|
336
|
+
[drizzleTableName]?: keyof Types['DrizzleRelations'];
|
|
339
337
|
}
|
|
340
|
-
? Types['
|
|
338
|
+
? Types['DrizzleRelations'][NonNullable<Shape[typeof drizzleTableName]>]
|
|
341
339
|
: never;
|
|
342
340
|
|
|
343
341
|
export type RelatedFieldOptions<
|
|
@@ -371,7 +369,7 @@ export type RelatedFieldOptions<
|
|
|
371
369
|
|
|
372
370
|
export type VariantFieldOptions<
|
|
373
371
|
Types extends SchemaTypes,
|
|
374
|
-
Table extends keyof Types['
|
|
372
|
+
Table extends keyof Types['DrizzleRelations'],
|
|
375
373
|
// biome-ignore lint/suspicious/noExplicitAny: this is fine
|
|
376
374
|
Variant extends DrizzleRef<any, Table> | Table,
|
|
377
375
|
Args extends InputFieldMap,
|
|
@@ -400,7 +398,7 @@ export type VariantFieldOptions<
|
|
|
400
398
|
unknown extends ResolveShape
|
|
401
399
|
? Shape
|
|
402
400
|
: BuildQueryResult<
|
|
403
|
-
Types['
|
|
401
|
+
Types['DrizzleRelations'],
|
|
404
402
|
ExtractTable<Types, Shape>,
|
|
405
403
|
Record<string, unknown> &
|
|
406
404
|
// biome-ignore lint/suspicious/noExplicitAny: this is fine
|
|
@@ -425,8 +423,8 @@ export type RefForRelation<Types extends SchemaTypes, Rel extends Relation> = Re
|
|
|
425
423
|
: [ObjectRef<Types, TypesForRelation<Types, Rel>>];
|
|
426
424
|
|
|
427
425
|
export type TypesForRelation<Types extends SchemaTypes, Rel extends Relation> = BuildQueryResult<
|
|
428
|
-
Types['
|
|
429
|
-
Types['
|
|
426
|
+
Types['DrizzleRelations'],
|
|
427
|
+
Types['DrizzleRelations'][Rel['targetTable']['_']['name']],
|
|
430
428
|
true
|
|
431
429
|
>;
|
|
432
430
|
|
|
@@ -441,16 +439,16 @@ export type QueryForField<
|
|
|
441
439
|
? Omit<
|
|
442
440
|
DBQueryConfig<
|
|
443
441
|
'one',
|
|
444
|
-
Types['
|
|
445
|
-
Types['
|
|
442
|
+
Types['DrizzleRelations'],
|
|
443
|
+
Types['DrizzleRelations'][Rel['targetTable']['_']['name']]
|
|
446
444
|
>,
|
|
447
445
|
'columns' | 'extra' | 'with'
|
|
448
446
|
>
|
|
449
447
|
: Omit<
|
|
450
448
|
DBQueryConfig<
|
|
451
449
|
'many',
|
|
452
|
-
Types['
|
|
453
|
-
Types['
|
|
450
|
+
Types['DrizzleRelations'],
|
|
451
|
+
Types['DrizzleRelations'][Rel['targetTable']['_']['name']]
|
|
454
452
|
>,
|
|
455
453
|
'columns' | 'extra' | 'with'
|
|
456
454
|
>
|
|
@@ -461,14 +459,9 @@ export type QueryForField<
|
|
|
461
459
|
export type QueryForDrizzleField<
|
|
462
460
|
Types extends SchemaTypes,
|
|
463
461
|
Param,
|
|
464
|
-
Table extends keyof Types['
|
|
462
|
+
Table extends keyof Types['DrizzleRelations'],
|
|
465
463
|
> = Omit<
|
|
466
|
-
DBQueryConfig<
|
|
467
|
-
'many',
|
|
468
|
-
Types['DrizzleRelationsConfig'],
|
|
469
|
-
Types['DrizzleRelationsConfig'][Table],
|
|
470
|
-
true
|
|
471
|
-
>,
|
|
464
|
+
DBQueryConfig<'many', Types['DrizzleRelations'], Types['DrizzleRelations'][Table]>,
|
|
472
465
|
Param extends [unknown] ? never : 'limit'
|
|
473
466
|
>;
|
|
474
467
|
|
|
@@ -477,33 +470,33 @@ export type QueryForRelatedConnection<
|
|
|
477
470
|
Table extends TableRelationalConfig,
|
|
478
471
|
Args,
|
|
479
472
|
> = Omit<
|
|
480
|
-
DBQueryConfig<'many', Types['
|
|
473
|
+
DBQueryConfig<'many', Types['DrizzleRelations'], Table>,
|
|
481
474
|
'limit' | 'offset' | 'columns' | 'extra' | 'with' | 'orderBy'
|
|
482
475
|
> & {
|
|
483
|
-
orderBy?: ConnectionOrderBy<Table> | ((
|
|
476
|
+
orderBy?: ConnectionOrderBy<Table> | ((table: Table) => ConnectionOrderBy<Table>);
|
|
484
477
|
} extends infer QueryConfig
|
|
485
478
|
? QueryConfig | ((args: Args, context: Types['Context']) => QueryConfig)
|
|
486
479
|
: never;
|
|
487
480
|
|
|
488
481
|
export type QueryForDrizzleConnection<
|
|
489
482
|
Types extends SchemaTypes,
|
|
490
|
-
|
|
483
|
+
TableConfig extends TableRelationalConfig,
|
|
491
484
|
> = Omit<
|
|
492
|
-
DBQueryConfig<'many', Types['
|
|
485
|
+
DBQueryConfig<'many', Types['DrizzleRelations'], TableConfig>,
|
|
493
486
|
'limit' | 'offset' | 'orderBy'
|
|
494
487
|
> & {
|
|
495
|
-
orderBy?: ConnectionOrderBy<
|
|
488
|
+
orderBy?: ConnectionOrderBy<TableConfig> | ((table: Table) => ConnectionOrderBy<TableConfig>);
|
|
496
489
|
};
|
|
497
490
|
|
|
498
491
|
export type ListRelation<T extends TableRelationalConfig> = {
|
|
499
|
-
[K in keyof T['relations']]: T['relations'][K] extends Many<string
|
|
492
|
+
[K in keyof T['relations']]: T['relations'][K] extends Many<string> ? K : never;
|
|
500
493
|
}[keyof T['relations']];
|
|
501
494
|
|
|
502
495
|
export type DrizzleConnectionFieldOptions<
|
|
503
496
|
Types extends SchemaTypes,
|
|
504
497
|
ParentShape,
|
|
505
498
|
Type extends // biome-ignore lint/suspicious/noExplicitAny: this is fine
|
|
506
|
-
DrizzleRef<any, keyof Types['
|
|
499
|
+
DrizzleRef<any, keyof Types['DrizzleRelations']> | keyof Types['DrizzleRelations'],
|
|
507
500
|
TableConfig extends TableRelationalConfig,
|
|
508
501
|
Param extends OutputType<Types>,
|
|
509
502
|
Nullable extends boolean,
|
|
@@ -547,9 +540,11 @@ export type DrizzleConnectionFieldOptions<
|
|
|
547
540
|
resolve: (
|
|
548
541
|
query: <T extends QueryForRelatedConnection<Types, TableConfig, ConnectionArgs>>(
|
|
549
542
|
selection?: T,
|
|
550
|
-
) => Omit<T, 'orderBy'> & {
|
|
543
|
+
) => Omit<T, 'orderBy' | 'columns'> & {
|
|
551
544
|
orderBy: {
|
|
552
|
-
[K in
|
|
545
|
+
[K in TableConfig['table']['_'] extends { columns: infer Columns }
|
|
546
|
+
? keyof Columns
|
|
547
|
+
: never]?: 'asc' | 'desc' | undefined;
|
|
553
548
|
};
|
|
554
549
|
},
|
|
555
550
|
parent: ParentShape,
|
|
@@ -572,7 +567,7 @@ export type RelatedConnectionOptions<
|
|
|
572
567
|
Nullable extends boolean,
|
|
573
568
|
Args extends InputFieldMap,
|
|
574
569
|
NodeTable extends
|
|
575
|
-
TableRelationalConfig = Types['
|
|
570
|
+
TableRelationalConfig = Types['DrizzleRelations'][Table['relations'][Field]['targetTable']['_']['name']],
|
|
576
571
|
> = Omit<
|
|
577
572
|
PothosSchemaTypes.ObjectFieldOptions<
|
|
578
573
|
Types,
|
|
@@ -627,7 +622,10 @@ export type ConnectionOrderBy<T extends TableRelationalConfig> =
|
|
|
627
622
|
| Column
|
|
628
623
|
| Column[]
|
|
629
624
|
| {
|
|
630
|
-
[K in
|
|
625
|
+
[K in T['table']['_'] extends { columns: infer Columns } ? keyof Columns : never]?:
|
|
626
|
+
| 'asc'
|
|
627
|
+
| 'desc'
|
|
628
|
+
| undefined;
|
|
631
629
|
};
|
|
632
630
|
|
|
633
631
|
export type ShapeFromConnection<T> = T extends { shape: unknown } ? T['shape'] : never;
|
package/src/utils/config.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createContextCache, type SchemaTypes } from '@pothos/core';
|
|
2
|
-
import { type AnyRelations, type Column,
|
|
2
|
+
import { type AnyRelations, type Column, getTableColumns, isTable, type Table } from 'drizzle-orm';
|
|
3
3
|
import type { DrizzleClient } from '../types';
|
|
4
4
|
|
|
5
5
|
export interface PothosDrizzleSchemaConfig {
|
|
@@ -17,46 +17,35 @@ const configCache = createContextCache(
|
|
|
17
17
|
relations = (builder.options.drizzle.client as DrizzleClient)._.relations;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const columnMappings = Object.values(dbToSchema).reduce<Record<string, Record<string, string>>>(
|
|
32
|
-
(acc, table) => {
|
|
33
|
-
acc[getTableName(table)] = Object.entries(table).reduce<Record<string, string>>(
|
|
34
|
-
(acc, [name, column]) => {
|
|
35
|
-
acc[(column as Column).name] = name;
|
|
36
|
-
return acc;
|
|
37
|
-
},
|
|
38
|
-
{},
|
|
39
|
-
);
|
|
40
|
-
return acc;
|
|
41
|
-
},
|
|
42
|
-
{},
|
|
43
|
-
);
|
|
20
|
+
const columnNameMappings: Record<string, string> = {};
|
|
21
|
+
|
|
22
|
+
Object.values(relations).forEach(({ table }) => {
|
|
23
|
+
if (isTable(table)) {
|
|
24
|
+
Object.entries(getTableColumns(table)).forEach(([tsName, col]) => {
|
|
25
|
+
if (col.uniqueName) {
|
|
26
|
+
columnNameMappings[col.uniqueName] = tsName;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
});
|
|
44
31
|
|
|
45
32
|
return {
|
|
46
33
|
skipDeferredFragments: builder.options.drizzle.skipDeferredFragments ?? true,
|
|
47
34
|
columnToTsName: (column) => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
35
|
+
if (!column.uniqueName) {
|
|
36
|
+
throw new Error(`Column ${String(column.name)} has no uniqueName`);
|
|
37
|
+
}
|
|
51
38
|
|
|
52
|
-
if (!
|
|
53
|
-
throw new Error(`
|
|
39
|
+
if (!(column.uniqueName in columnNameMappings)) {
|
|
40
|
+
throw new Error(`Typescript name not found for ${column.uniqueName}`);
|
|
54
41
|
}
|
|
55
42
|
|
|
56
|
-
return
|
|
43
|
+
return columnNameMappings[column.uniqueName];
|
|
57
44
|
},
|
|
58
45
|
getPrimaryKey: (tableName) => {
|
|
59
|
-
const tableConfig = builder.options.drizzle.getTableConfig(
|
|
46
|
+
const tableConfig = builder.options.drizzle.getTableConfig(
|
|
47
|
+
relations[tableName].table as Table,
|
|
48
|
+
);
|
|
60
49
|
|
|
61
50
|
const primaryKey = tableConfig.columns.find((column) => column.primary);
|
|
62
51
|
|
package/src/utils/cursors.ts
CHANGED
|
@@ -5,7 +5,14 @@ import {
|
|
|
5
5
|
PothosValidationError,
|
|
6
6
|
type SchemaTypes,
|
|
7
7
|
} from '@pothos/core';
|
|
8
|
-
import
|
|
8
|
+
import {
|
|
9
|
+
type Column,
|
|
10
|
+
type DBQueryConfig,
|
|
11
|
+
getTableColumns,
|
|
12
|
+
type SQL,
|
|
13
|
+
type Table,
|
|
14
|
+
type TableRelationalConfig,
|
|
15
|
+
} from 'drizzle-orm';
|
|
9
16
|
import type { GraphQLResolveInfo } from 'graphql';
|
|
10
17
|
import type { ConnectionOrderBy, QueryForDrizzleConnection } from '../types';
|
|
11
18
|
import type { PothosDrizzleSchemaConfig } from './config';
|
|
@@ -150,18 +157,23 @@ export function parseSerializedIDColumn(id: string, field: Column): unknown {
|
|
|
150
157
|
}
|
|
151
158
|
|
|
152
159
|
try {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return new Date(id);
|
|
156
|
-
case 'string':
|
|
157
|
-
return id;
|
|
158
|
-
case 'number':
|
|
159
|
-
return Number.parseInt(id, 10);
|
|
160
|
-
case 'bigint':
|
|
161
|
-
return BigInt(id);
|
|
162
|
-
default:
|
|
163
|
-
throw new PothosValidationError(`Unsupported ID type ${field.dataType}`);
|
|
160
|
+
if (field.dataType.startsWith('number')) {
|
|
161
|
+
return Number.parseInt(id, 10);
|
|
164
162
|
}
|
|
163
|
+
|
|
164
|
+
if (field.dataType.startsWith('bigint')) {
|
|
165
|
+
return BigInt(id);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (field.dataType.startsWith('string')) {
|
|
169
|
+
return id;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (field.dataType === 'object date') {
|
|
173
|
+
return new Date(id);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
throw new PothosValidationError(`Unsupported ID type ${field.dataType}`);
|
|
165
177
|
} catch (error: unknown) {
|
|
166
178
|
if (error instanceof PothosValidationError) {
|
|
167
179
|
throw error;
|
|
@@ -283,12 +295,12 @@ export interface DrizzleCursorConnectionQueryOptions {
|
|
|
283
295
|
orderBy: ConnectionOrderBy<TableRelationalConfig>;
|
|
284
296
|
where?: SQL;
|
|
285
297
|
config: PothosDrizzleSchemaConfig;
|
|
286
|
-
table: TableRelationalConfig
|
|
298
|
+
table: TableRelationalConfig;
|
|
287
299
|
}
|
|
288
300
|
|
|
289
301
|
function parseOrderBy(
|
|
290
302
|
config: PothosDrizzleSchemaConfig,
|
|
291
|
-
table: TableRelationalConfig
|
|
303
|
+
table: TableRelationalConfig,
|
|
292
304
|
orderBy: ConnectionOrderBy<TableRelationalConfig>,
|
|
293
305
|
invert: boolean,
|
|
294
306
|
) {
|
|
@@ -310,13 +322,14 @@ function parseOrderBy(
|
|
|
310
322
|
normalized.push({ direction: 'asc', column: field });
|
|
311
323
|
}
|
|
312
324
|
} else {
|
|
325
|
+
const tableColumns = getTableColumns(table.table as Table);
|
|
313
326
|
Object.entries(
|
|
314
327
|
orderBy as {
|
|
315
328
|
[k: string]: 'asc' | 'desc' | undefined;
|
|
316
329
|
},
|
|
317
330
|
).forEach(([name, direction]) => {
|
|
318
331
|
if (direction) {
|
|
319
|
-
const column =
|
|
332
|
+
const column = tableColumns[name];
|
|
320
333
|
columns.push(column);
|
|
321
334
|
entries.push([name, direction === 'asc' ? asc : desc]);
|
|
322
335
|
normalized.push({ direction: direction, column });
|
|
@@ -506,7 +519,7 @@ export async function resolveDrizzleCursorConnection<T extends {}>(
|
|
|
506
519
|
) => MaybePromise<readonly T[]>,
|
|
507
520
|
parent: unknown,
|
|
508
521
|
) {
|
|
509
|
-
const table = config.relations
|
|
522
|
+
const table = config.relations[tableName];
|
|
510
523
|
let query: DBQueryConfig<'many'>;
|
|
511
524
|
let formatter: (node: Record<string, unknown>) => string;
|
|
512
525
|
const results = await resolve((q = {}) => {
|
|
@@ -514,8 +527,8 @@ export async function resolveDrizzleCursorConnection<T extends {}>(
|
|
|
514
527
|
...options,
|
|
515
528
|
config,
|
|
516
529
|
orderBy:
|
|
517
|
-
(typeof q.orderBy === 'function' ? q.orderBy(table.
|
|
518
|
-
config.getPrimaryKey(table.
|
|
530
|
+
(typeof q.orderBy === 'function' ? q.orderBy(table.table as Table) : q.orderBy) ??
|
|
531
|
+
config.getPrimaryKey(table.name),
|
|
519
532
|
table,
|
|
520
533
|
});
|
|
521
534
|
formatter = getCursorFormatter(cursorColumns, config);
|
package/src/utils/selections.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
import { PothosValidationError } from '@pothos/core';
|
|
2
|
-
import {
|
|
3
|
-
type DBQueryConfig,
|
|
4
|
-
getTableUniqueName,
|
|
5
|
-
type Table,
|
|
6
|
-
type TableRelationalConfig,
|
|
7
|
-
} from 'drizzle-orm';
|
|
2
|
+
import type { DBQueryConfig, TableRelationalConfig } from 'drizzle-orm';
|
|
8
3
|
import type { PothosDrizzleSchemaConfig } from './config';
|
|
9
4
|
import { deepEqual } from './deep-equal';
|
|
10
5
|
import type { LoaderMappings } from './loader-map';
|
|
@@ -132,12 +127,10 @@ export function mergeSelection(
|
|
|
132
127
|
const relation = state.table.relations[key];
|
|
133
128
|
|
|
134
129
|
if (!relation) {
|
|
135
|
-
throw new PothosValidationError(`Relation ${key} does not exist on ${state.table.
|
|
130
|
+
throw new PothosValidationError(`Relation ${key} does not exist on ${state.table.name}`);
|
|
136
131
|
}
|
|
137
132
|
|
|
138
|
-
|
|
139
|
-
const tableName = config.relations.tableNamesMap[tableUniqueName];
|
|
140
|
-
merge(config.relations.tablesConfig[tableName], key, value as SelectionMap | boolean);
|
|
133
|
+
merge(config.relations[relation.targetTableName], key, value as SelectionMap | boolean);
|
|
141
134
|
}
|
|
142
135
|
}
|
|
143
136
|
|
|
@@ -184,21 +177,18 @@ export function selectionToQuery(
|
|
|
184
177
|
): SelectionMap {
|
|
185
178
|
const query: SelectionMap & { extras: Record<string, unknown> } = {
|
|
186
179
|
...state.query,
|
|
180
|
+
columns: undefined,
|
|
187
181
|
with: {},
|
|
188
|
-
columns: {},
|
|
189
182
|
extras: {},
|
|
190
183
|
};
|
|
191
184
|
|
|
192
|
-
if (state.allColumns) {
|
|
193
|
-
|
|
194
|
-
query.columns![key] = true;
|
|
195
|
-
}
|
|
196
|
-
} else {
|
|
185
|
+
if (!state.allColumns) {
|
|
186
|
+
query.columns = {};
|
|
197
187
|
for (const key of state.columns) {
|
|
198
188
|
query.columns![key] = true;
|
|
199
189
|
}
|
|
200
190
|
|
|
201
|
-
for (const column of config.getPrimaryKey(state.table.
|
|
191
|
+
for (const column of config.getPrimaryKey(state.table.name)) {
|
|
202
192
|
const tsName = config.columnToTsName(column);
|
|
203
193
|
query.columns![tsName] = true;
|
|
204
194
|
}
|