@pothos/plugin-drizzle 0.11.1 → 0.12.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.
Files changed (92) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dts/connection-helpers.d.ts +7 -5
  3. package/dts/connection-helpers.d.ts.map +1 -1
  4. package/dts/drizzle-field-builder.d.ts +3 -3
  5. package/dts/drizzle-field-builder.d.ts.map +1 -1
  6. package/dts/global-types.d.ts +30 -25
  7. package/dts/global-types.d.ts.map +1 -1
  8. package/dts/index.d.ts.map +1 -1
  9. package/dts/interface-ref.d.ts +3 -3
  10. package/dts/interface-ref.d.ts.map +1 -1
  11. package/dts/model-loader.d.ts.map +1 -1
  12. package/dts/node-ref.d.ts +1 -1
  13. package/dts/node-ref.d.ts.map +1 -1
  14. package/dts/object-ref.d.ts +2 -2
  15. package/dts/object-ref.d.ts.map +1 -1
  16. package/dts/types.d.ts +41 -29
  17. package/dts/types.d.ts.map +1 -1
  18. package/dts/utils/config.d.ts.map +1 -1
  19. package/dts/utils/cursors.d.ts +3 -3
  20. package/dts/utils/cursors.d.ts.map +1 -1
  21. package/dts/utils/selections.d.ts +1 -1
  22. package/dts/utils/selections.d.ts.map +1 -1
  23. package/esm/connection-helpers.d.ts +7 -5
  24. package/esm/connection-helpers.d.ts.map +1 -1
  25. package/esm/connection-helpers.js +2 -2
  26. package/esm/connection-helpers.js.map +1 -1
  27. package/esm/drizzle-field-builder.d.ts +2 -2
  28. package/esm/drizzle-field-builder.d.ts.map +1 -1
  29. package/esm/drizzle-field-builder.js +14 -18
  30. package/esm/drizzle-field-builder.js.map +1 -1
  31. package/esm/global-types.d.ts +41 -36
  32. package/esm/global-types.d.ts.map +1 -1
  33. package/esm/global-types.js.map +1 -1
  34. package/esm/index.d.ts.map +1 -1
  35. package/esm/index.js +7 -1
  36. package/esm/index.js.map +1 -1
  37. package/esm/interface-ref.d.ts +3 -3
  38. package/esm/interface-ref.d.ts.map +1 -1
  39. package/esm/model-loader.d.ts.map +1 -1
  40. package/esm/model-loader.js +3 -3
  41. package/esm/model-loader.js.map +1 -1
  42. package/esm/node-ref.d.ts +1 -1
  43. package/esm/node-ref.d.ts.map +1 -1
  44. package/esm/object-ref.d.ts +2 -2
  45. package/esm/object-ref.d.ts.map +1 -1
  46. package/esm/schema-builder.js +16 -10
  47. package/esm/schema-builder.js.map +1 -1
  48. package/esm/types.d.ts +41 -29
  49. package/esm/types.d.ts.map +1 -1
  50. package/esm/types.js.map +1 -1
  51. package/esm/utils/config.d.ts.map +1 -1
  52. package/esm/utils/config.js +17 -21
  53. package/esm/utils/config.js.map +1 -1
  54. package/esm/utils/cursors.d.ts +3 -3
  55. package/esm/utils/cursors.d.ts.map +1 -1
  56. package/esm/utils/cursors.js +17 -14
  57. package/esm/utils/cursors.js.map +1 -1
  58. package/esm/utils/selections.d.ts +1 -1
  59. package/esm/utils/selections.d.ts.map +1 -1
  60. package/esm/utils/selections.js +11 -16
  61. package/esm/utils/selections.js.map +1 -1
  62. package/lib/connection-helpers.js +2 -2
  63. package/lib/connection-helpers.js.map +1 -1
  64. package/lib/drizzle-field-builder.js +13 -17
  65. package/lib/drizzle-field-builder.js.map +1 -1
  66. package/lib/index.js +7 -1
  67. package/lib/index.js.map +1 -1
  68. package/lib/model-loader.js +2 -2
  69. package/lib/model-loader.js.map +1 -1
  70. package/lib/schema-builder.js +16 -10
  71. package/lib/schema-builder.js.map +1 -1
  72. package/lib/types.js.map +1 -1
  73. package/lib/utils/config.js +16 -20
  74. package/lib/utils/config.js.map +1 -1
  75. package/lib/utils/cursors.js +17 -14
  76. package/lib/utils/cursors.js.map +1 -1
  77. package/lib/utils/selections.js +11 -15
  78. package/lib/utils/selections.js.map +1 -1
  79. package/package.json +7 -7
  80. package/src/connection-helpers.ts +13 -12
  81. package/src/drizzle-field-builder.ts +16 -24
  82. package/src/global-types.ts +62 -88
  83. package/src/index.ts +9 -4
  84. package/src/interface-ref.ts +3 -3
  85. package/src/model-loader.ts +4 -3
  86. package/src/node-ref.ts +1 -1
  87. package/src/object-ref.ts +2 -2
  88. package/src/schema-builder.ts +12 -10
  89. package/src/types.ts +49 -48
  90. package/src/utils/config.ts +21 -32
  91. package/src/utils/cursors.ts +31 -18
  92. package/src/utils/selections.ts +10 -20
@@ -36,8 +36,9 @@ schemaBuilderProto.drizzleObject = function drizzleObject(table, { select, field
36
36
  extensions: {
37
37
  ...options.extensions,
38
38
  pothosDrizzleModel: table,
39
- pothosDrizzleTable: getSchemaConfig(this).relations.tablesConfig[table],
40
- pothosDrizzleSelect: select ?? true,
39
+ pothosDrizzleTable: getSchemaConfig(this).relations[table],
40
+ pothosDrizzleSelect:
41
+ typeof select === 'object' ? { columns: {}, ...select } : (select ?? true),
41
42
  pothosDrizzleLoader: ModelLoader.forModel(table, this),
42
43
  },
43
44
  name,
@@ -64,8 +65,9 @@ schemaBuilderProto.drizzleInterface = function drizzleInterface(
64
65
  extensions: {
65
66
  ...options.extensions,
66
67
  pothosDrizzleModel: table,
67
- pothosDrizzleTable: getSchemaConfig(this).relations.tablesConfig[table],
68
- pothosDrizzleSelect: select ?? true,
68
+ pothosDrizzleTable: getSchemaConfig(this).relations[table],
69
+ pothosDrizzleSelect:
70
+ typeof select === 'object' ? { columns: {}, ...select } : (select ?? true),
69
71
  pothosDrizzleLoader: ModelLoader.forModel(table, this),
70
72
  },
71
73
  name,
@@ -97,8 +99,8 @@ schemaBuilderProto.drizzleNode = function drizzleNode(
97
99
  >,
98
100
  ) {
99
101
  const schemaConfig = getSchemaConfig(this);
100
- const tableConfig = schemaConfig.relations.tablesConfig[table];
101
- const idColumn = typeof column === 'function' ? column(tableConfig.columns) : column;
102
+ const tableConfig = schemaConfig.relations[table];
103
+ const idColumn = typeof column === 'function' ? column(tableConfig.table) : column;
102
104
  const idColumns = Array.isArray(idColumn) ? idColumn : [idColumn];
103
105
  const interfaceRef = this.nodeInterfaceRef?.();
104
106
  const resolve = getIDSerializer(idColumns, schemaConfig);
@@ -230,7 +232,7 @@ schemaBuilderProto.drizzleInterfaceFields = function drizzleInterfaceFields(type
230
232
  // drizzleGraphQL: {
231
233
  // inputType: 'orderBy',
232
234
  // table,
233
- // tableConfig: getSchemaConfig(this).relations.tablesConfig[table],
235
+ // tableConfig: getSchemaConfig(this).relations[table],
234
236
  // } satisfies DrizzleGraphQLInputExtensions,
235
237
  // },
236
238
  // });
@@ -256,7 +258,7 @@ schemaBuilderProto.drizzleInterfaceFields = function drizzleInterfaceFields(type
256
258
  // drizzleGraphQL: {
257
259
  // inputType: 'filters',
258
260
  // table,
259
- // tableConfig: getSchemaConfig(this).relations.tablesConfig[table],
261
+ // tableConfig: getSchemaConfig(this).relations[table],
260
262
  // } satisfies DrizzleGraphQLInputExtensions,
261
263
  // },
262
264
  // });
@@ -282,7 +284,7 @@ schemaBuilderProto.drizzleInterfaceFields = function drizzleInterfaceFields(type
282
284
  // drizzleGraphQL: {
283
285
  // inputType: 'insert',
284
286
  // table,
285
- // tableConfig: getSchemaConfig(this).relations.tablesConfig[table],
287
+ // tableConfig: getSchemaConfig(this).relations[table],
286
288
  // } satisfies DrizzleGraphQLInputExtensions,
287
289
  // },
288
290
  // });
@@ -308,7 +310,7 @@ schemaBuilderProto.drizzleInterfaceFields = function drizzleInterfaceFields(type
308
310
  // drizzleGraphQL: {
309
311
  // inputType: 'update',
310
312
  // table,
311
- // tableConfig: getSchemaConfig(this).relations.tablesConfig[table],
313
+ // tableConfig: getSchemaConfig(this).relations[table],
312
314
  // } satisfies DrizzleGraphQLInputExtensions,
313
315
  // },
314
316
  // });
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?: Types['DrizzleRelations'];
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['DrizzleRelationsConfig'],
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['DrizzleRelationsConfig'][Table],
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['DrizzleRelationsConfig'],
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['DrizzleRelationsConfig'][Table],
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['DrizzleRelationsConfig'],
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['DrizzleRelationsConfig'][Table]['columns']) => Column | Column[])
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['DrizzleRelationsConfig'][Table],
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['DrizzleRelationsConfig'],
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['DrizzleRelationsConfig'],
194
+ Table extends keyof Types['DrizzleRelations'],
195
195
  Type extends TypeParam<Types>,
196
196
  Nullable extends FieldNullability<Type>,
197
197
  Args extends InputFieldMap,
@@ -225,7 +225,7 @@ export type DrizzleFieldOptions<
225
225
  export type DrizzleFieldWithInputOptions<
226
226
  Types extends SchemaTypes,
227
227
  ParentShape,
228
- Table extends keyof Types['DrizzleRelationsConfig'],
228
+ Table extends keyof Types['DrizzleRelations'],
229
229
  Type extends TypeParam<Types>,
230
230
  Nullable extends FieldNullability<Type>,
231
231
  Args extends InputFieldMap,
@@ -271,10 +271,13 @@ export type DrizzleObjectFieldOptions<
271
271
  unknown extends Select
272
272
  ? ParentShape
273
273
  : BuildQueryResult<
274
- Types['DrizzleRelationsConfig'],
274
+ Types['DrizzleRelations'],
275
275
  ExtractTable<Types, ParentShape>,
276
- // biome-ignore lint/suspicious/noExplicitAny: this is fine
277
- Record<string, unknown> & (Select extends (...args: any[]) => infer R ? R : Select)
276
+ Record<string, unknown> &
277
+ // biome-ignore lint/suspicious/noExplicitAny: this is fine
278
+ Select extends (...args: any[]) => infer R
279
+ ? R & { columns: {} }
280
+ : Select & { columns: {} }
278
281
  > &
279
282
  ParentShape,
280
283
  typeof drizzleTableName
@@ -299,7 +302,7 @@ export type DrizzleObjectFieldOptions<
299
302
  > & {
300
303
  select?: Select &
301
304
  (
302
- | DBQueryConfig<'one', Types['DrizzleRelationsConfig'], ExtractTable<Types, ParentShape>>
305
+ | DBQueryConfig<'one', Types['DrizzleRelations'], ExtractTable<Types, ParentShape>>
303
306
  | ((
304
307
  args: InputShapeFromFields<Args>,
305
308
  ctx: Types['Context'],
@@ -307,11 +310,7 @@ export type DrizzleObjectFieldOptions<
307
310
  selection?: Selection,
308
311
  path?: string[],
309
312
  ) => Selection,
310
- ) => DBQueryConfig<
311
- 'one',
312
- Types['DrizzleRelationsConfig'],
313
- ExtractTable<Types, ParentShape>
314
- >)
313
+ ) => DBQueryConfig<'one', Types['DrizzleRelations'], ExtractTable<Types, ParentShape>>)
315
314
  );
316
315
  };
317
316
 
@@ -332,9 +331,9 @@ export type DrizzleFieldSelection =
332
331
  ) => SelectionMap);
333
332
 
334
333
  export type ExtractTable<Types extends SchemaTypes, Shape> = Shape extends {
335
- [drizzleTableName]?: keyof Types['DrizzleRelationsConfig'];
334
+ [drizzleTableName]?: keyof Types['DrizzleRelations'];
336
335
  }
337
- ? Types['DrizzleRelationsConfig'][NonNullable<Shape[typeof drizzleTableName]>]
336
+ ? Types['DrizzleRelations'][NonNullable<Shape[typeof drizzleTableName]>]
338
337
  : never;
339
338
 
340
339
  export type RelatedFieldOptions<
@@ -368,7 +367,7 @@ export type RelatedFieldOptions<
368
367
 
369
368
  export type VariantFieldOptions<
370
369
  Types extends SchemaTypes,
371
- Table extends keyof Types['DrizzleRelationsConfig'],
370
+ Table extends keyof Types['DrizzleRelations'],
372
371
  // biome-ignore lint/suspicious/noExplicitAny: this is fine
373
372
  Variant extends DrizzleRef<any, Table> | Table,
374
373
  Args extends InputFieldMap,
@@ -397,11 +396,13 @@ export type VariantFieldOptions<
397
396
  unknown extends ResolveShape
398
397
  ? Shape
399
398
  : BuildQueryResult<
400
- Types['DrizzleRelationsConfig'],
399
+ Types['DrizzleRelations'],
401
400
  ExtractTable<Types, Shape>,
402
401
  Record<string, unknown> &
403
402
  // biome-ignore lint/suspicious/noExplicitAny: this is fine
404
- (ResolveShape extends (...args: any[]) => infer R ? R : ResolveShape)
403
+ ResolveShape extends (...args: any[]) => infer R
404
+ ? R & { columns: {} }
405
+ : ResolveShape & { columns: {} }
405
406
  > &
406
407
  Shape,
407
408
  typeof drizzleTableName
@@ -420,8 +421,8 @@ export type RefForRelation<Types extends SchemaTypes, Rel extends Relation> = Re
420
421
  : [ObjectRef<Types, TypesForRelation<Types, Rel>>];
421
422
 
422
423
  export type TypesForRelation<Types extends SchemaTypes, Rel extends Relation> = BuildQueryResult<
423
- Types['DrizzleRelationsConfig'],
424
- Types['DrizzleRelationsConfig'][Rel['targetTable']['_']['name']],
424
+ Types['DrizzleRelations'],
425
+ Types['DrizzleRelations'][Rel['targetTable']['_']['name']],
425
426
  true
426
427
  >;
427
428
 
@@ -436,16 +437,16 @@ export type QueryForField<
436
437
  ? Omit<
437
438
  DBQueryConfig<
438
439
  'one',
439
- Types['DrizzleRelationsConfig'],
440
- Types['DrizzleRelationsConfig'][Rel['targetTable']['_']['name']]
440
+ Types['DrizzleRelations'],
441
+ Types['DrizzleRelations'][Rel['targetTable']['_']['name']]
441
442
  >,
442
443
  'columns' | 'extra' | 'with'
443
444
  >
444
445
  : Omit<
445
446
  DBQueryConfig<
446
447
  'many',
447
- Types['DrizzleRelationsConfig'],
448
- Types['DrizzleRelationsConfig'][Rel['targetTable']['_']['name']]
448
+ Types['DrizzleRelations'],
449
+ Types['DrizzleRelations'][Rel['targetTable']['_']['name']]
449
450
  >,
450
451
  'columns' | 'extra' | 'with'
451
452
  >
@@ -456,14 +457,9 @@ export type QueryForField<
456
457
  export type QueryForDrizzleField<
457
458
  Types extends SchemaTypes,
458
459
  Param,
459
- Table extends keyof Types['DrizzleRelationsConfig'],
460
+ Table extends keyof Types['DrizzleRelations'],
460
461
  > = Omit<
461
- DBQueryConfig<
462
- 'many',
463
- Types['DrizzleRelationsConfig'],
464
- Types['DrizzleRelationsConfig'][Table],
465
- true
466
- >,
462
+ DBQueryConfig<'many', Types['DrizzleRelations'], Types['DrizzleRelations'][Table]>,
467
463
  Param extends [unknown] ? never : 'limit'
468
464
  >;
469
465
 
@@ -472,33 +468,33 @@ export type QueryForRelatedConnection<
472
468
  Table extends TableRelationalConfig,
473
469
  Args,
474
470
  > = Omit<
475
- DBQueryConfig<'many', Types['DrizzleRelationsConfig'], Table>,
471
+ DBQueryConfig<'many', Types['DrizzleRelations'], Table>,
476
472
  'limit' | 'offset' | 'columns' | 'extra' | 'with' | 'orderBy'
477
473
  > & {
478
- orderBy?: ConnectionOrderBy<Table> | ((columns: Table['columns']) => ConnectionOrderBy<Table>);
474
+ orderBy?: ConnectionOrderBy<Table> | ((table: Table) => ConnectionOrderBy<Table>);
479
475
  } extends infer QueryConfig
480
476
  ? QueryConfig | ((args: Args, context: Types['Context']) => QueryConfig)
481
477
  : never;
482
478
 
483
479
  export type QueryForDrizzleConnection<
484
480
  Types extends SchemaTypes,
485
- Table extends TableRelationalConfig,
481
+ TableConfig extends TableRelationalConfig,
486
482
  > = Omit<
487
- DBQueryConfig<'many', Types['DrizzleRelationsConfig'], Table>,
483
+ DBQueryConfig<'many', Types['DrizzleRelations'], TableConfig>,
488
484
  'limit' | 'offset' | 'orderBy'
489
485
  > & {
490
- orderBy?: ConnectionOrderBy<Table> | ((columns: Table['columns']) => ConnectionOrderBy<Table>);
486
+ orderBy?: ConnectionOrderBy<TableConfig> | ((table: Table) => ConnectionOrderBy<TableConfig>);
491
487
  };
492
488
 
493
489
  export type ListRelation<T extends TableRelationalConfig> = {
494
- [K in keyof T['relations']]: T['relations'][K] extends Many<string, string> ? K : never;
490
+ [K in keyof T['relations']]: T['relations'][K] extends Many<string> ? K : never;
495
491
  }[keyof T['relations']];
496
492
 
497
493
  export type DrizzleConnectionFieldOptions<
498
494
  Types extends SchemaTypes,
499
495
  ParentShape,
500
496
  Type extends // biome-ignore lint/suspicious/noExplicitAny: this is fine
501
- DrizzleRef<any, keyof Types['DrizzleRelationsConfig']> | keyof Types['DrizzleRelationsConfig'],
497
+ DrizzleRef<any, keyof Types['DrizzleRelations']> | keyof Types['DrizzleRelations'],
502
498
  TableConfig extends TableRelationalConfig,
503
499
  Param extends OutputType<Types>,
504
500
  Nullable extends boolean,
@@ -544,7 +540,9 @@ export type DrizzleConnectionFieldOptions<
544
540
  selection?: T,
545
541
  ) => Omit<T, 'orderBy'> & {
546
542
  orderBy: {
547
- [K in keyof TableConfig['columns']]?: 'asc' | 'desc' | undefined;
543
+ [K in TableConfig['table']['_'] extends { columns: infer Columns }
544
+ ? keyof Columns
545
+ : never]?: 'asc' | 'desc' | undefined;
548
546
  };
549
547
  },
550
548
  parent: ParentShape,
@@ -567,7 +565,7 @@ export type RelatedConnectionOptions<
567
565
  Nullable extends boolean,
568
566
  Args extends InputFieldMap,
569
567
  NodeTable extends
570
- TableRelationalConfig = Types['DrizzleRelationsConfig'][Table['relations'][Field]['targetTable']['_']['name']],
568
+ TableRelationalConfig = Types['DrizzleRelations'][Table['relations'][Field]['targetTable']['_']['name']],
571
569
  > = Omit<
572
570
  PothosSchemaTypes.ObjectFieldOptions<
573
571
  Types,
@@ -622,7 +620,10 @@ export type ConnectionOrderBy<T extends TableRelationalConfig> =
622
620
  | Column
623
621
  | Column[]
624
622
  | {
625
- [K in keyof T['columns']]?: 'asc' | 'desc' | undefined;
623
+ [K in T['table']['_'] extends { columns: infer Columns } ? keyof Columns : never]?:
624
+ | 'asc'
625
+ | 'desc'
626
+ | undefined;
626
627
  };
627
628
 
628
629
  export type ShapeFromConnection<T> = T extends { shape: unknown } ? T['shape'] : never;
@@ -1,5 +1,5 @@
1
1
  import { createContextCache, type SchemaTypes } from '@pothos/core';
2
- import { type AnyRelations, type Column, getTableName, type Table } from 'drizzle-orm';
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 dbToSchema = Object.values(relations.tables).reduce<Record<string, Table>>(
21
- (acc, table) => {
22
- const tableName = getTableName(table);
23
- if (tableName) {
24
- acc[tableName] = table;
25
- }
26
- return acc;
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
- const tableName = getTableName(column.table);
49
- const table = columnMappings[tableName];
50
- const columnName = table?.[column.name];
35
+ if (!column.uniqueName) {
36
+ throw new Error(`Column ${String(column.name)} has no uniqueName`);
37
+ }
51
38
 
52
- if (!columnName) {
53
- throw new Error(`Could not find column mapping for ${tableName}.${column.name}`);
39
+ if (!(column.uniqueName in columnNameMappings)) {
40
+ throw new Error(`Typescript name not found for ${column.uniqueName}`);
54
41
  }
55
42
 
56
- return columnName;
43
+ return columnNameMappings[column.uniqueName];
57
44
  },
58
45
  getPrimaryKey: (tableName) => {
59
- const tableConfig = builder.options.drizzle.getTableConfig(relations.tables[tableName]);
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
 
@@ -5,7 +5,14 @@ import {
5
5
  PothosValidationError,
6
6
  type SchemaTypes,
7
7
  } from '@pothos/core';
8
- import type { Column, DBQueryConfig, SQL, TableConfig, TableRelationalConfig } from 'drizzle-orm';
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
- switch (field.dataType) {
154
- case 'date':
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 | TableConfig;
298
+ table: TableRelationalConfig;
287
299
  }
288
300
 
289
301
  function parseOrderBy(
290
302
  config: PothosDrizzleSchemaConfig,
291
- table: TableRelationalConfig | TableConfig,
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 = table.columns[name] as 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.tablesConfig[tableName];
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.columns) : q.orderBy) ??
518
- config.getPrimaryKey(table.tsName),
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);
@@ -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';
@@ -126,18 +121,16 @@ export function mergeSelection(
126
121
  extras,
127
122
  columns,
128
123
  ...query
129
- } = typeof selection === 'boolean' ? {} : selection;
124
+ } = typeof selection === 'boolean' ? (selection === true ? {} : { columns: {} }) : selection;
130
125
  if (withSelection) {
131
126
  for (const [key, value] of Object.entries(withSelection)) {
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.dbName}`);
130
+ throw new PothosValidationError(`Relation ${key} does not exist on ${state.table.name}`);
136
131
  }
137
132
 
138
- const tableUniqueName = getTableUniqueName(relation.targetTable as Table);
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
 
@@ -155,11 +148,11 @@ export function mergeSelection(
155
148
  return;
156
149
  }
157
150
 
158
- if (columns) {
151
+ if (columns && !state.allColumns) {
159
152
  for (const key of Object.keys(columns)) {
160
153
  state.columns.add(key);
161
154
  }
162
- } else if (selection === true) {
155
+ } else {
163
156
  state.allColumns = true;
164
157
  }
165
158
 
@@ -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
- for (const key of Object.keys(state.table.columns)) {
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.tsName)) {
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
  }