@pothos/plugin-drizzle 0.8.1 → 0.10.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 (122) hide show
  1. package/CHANGELOG.md +102 -0
  2. package/TODO.md +25 -4
  3. package/dts/connection-helpers.d.ts +34 -0
  4. package/dts/connection-helpers.d.ts.map +1 -0
  5. package/dts/drizzle-field-builder.d.ts +18 -53
  6. package/dts/drizzle-field-builder.d.ts.map +1 -1
  7. package/dts/global-types.d.ts +22 -41
  8. package/dts/global-types.d.ts.map +1 -1
  9. package/dts/index.d.ts +5 -2
  10. package/dts/index.d.ts.map +1 -1
  11. package/dts/interface-ref.d.ts +3 -2
  12. package/dts/interface-ref.d.ts.map +1 -1
  13. package/dts/model-loader.d.ts +3 -2
  14. package/dts/model-loader.d.ts.map +1 -1
  15. package/dts/node-ref.d.ts +1 -1
  16. package/dts/node-ref.d.ts.map +1 -1
  17. package/dts/object-ref.d.ts +2 -2
  18. package/dts/object-ref.d.ts.map +1 -1
  19. package/dts/types.d.ts +61 -41
  20. package/dts/types.d.ts.map +1 -1
  21. package/dts/utils/config.d.ts +4 -3
  22. package/dts/utils/config.d.ts.map +1 -1
  23. package/dts/utils/cursors.d.ts +13 -7
  24. package/dts/utils/cursors.d.ts.map +1 -1
  25. package/dts/utils/drizzle-graphql.d.ts +0 -17
  26. package/dts/utils/drizzle-graphql.d.ts.map +1 -1
  27. package/dts/utils/map-query.d.ts.map +1 -1
  28. package/dts/utils/selections.d.ts +5 -5
  29. package/dts/utils/selections.d.ts.map +1 -1
  30. package/esm/connection-helpers.d.ts +36 -0
  31. package/esm/connection-helpers.d.ts.map +1 -0
  32. package/esm/connection-helpers.js +82 -0
  33. package/esm/connection-helpers.js.map +1 -0
  34. package/esm/drizzle-field-builder.d.ts +18 -73
  35. package/esm/drizzle-field-builder.d.ts.map +1 -1
  36. package/esm/drizzle-field-builder.js +25 -14
  37. package/esm/drizzle-field-builder.js.map +1 -1
  38. package/esm/field-builder.js +2 -3
  39. package/esm/field-builder.js.map +1 -1
  40. package/esm/global-types.d.ts +35 -60
  41. package/esm/global-types.d.ts.map +1 -1
  42. package/esm/global-types.js.map +1 -1
  43. package/esm/index.d.ts +5 -2
  44. package/esm/index.d.ts.map +1 -1
  45. package/esm/index.js +71 -61
  46. package/esm/index.js.map +1 -1
  47. package/esm/interface-ref.d.ts +3 -2
  48. package/esm/interface-ref.d.ts.map +1 -1
  49. package/esm/interface-ref.js.map +1 -1
  50. package/esm/model-loader.d.ts +3 -2
  51. package/esm/model-loader.d.ts.map +1 -1
  52. package/esm/model-loader.js +17 -10
  53. package/esm/model-loader.js.map +1 -1
  54. package/esm/node-ref.d.ts +1 -1
  55. package/esm/node-ref.d.ts.map +1 -1
  56. package/esm/object-ref.d.ts +2 -2
  57. package/esm/object-ref.d.ts.map +1 -1
  58. package/esm/schema-builder.js +113 -67
  59. package/esm/schema-builder.js.map +1 -1
  60. package/esm/types.d.ts +61 -41
  61. package/esm/types.d.ts.map +1 -1
  62. package/esm/types.js.map +1 -1
  63. package/esm/utils/config.d.ts +4 -3
  64. package/esm/utils/config.d.ts.map +1 -1
  65. package/esm/utils/config.js +32 -15
  66. package/esm/utils/config.js.map +1 -1
  67. package/esm/utils/cursors.d.ts +13 -7
  68. package/esm/utils/cursors.d.ts.map +1 -1
  69. package/esm/utils/cursors.js +137 -63
  70. package/esm/utils/cursors.js.map +1 -1
  71. package/esm/utils/drizzle-graphql.d.ts +0 -17
  72. package/esm/utils/drizzle-graphql.d.ts.map +1 -1
  73. package/esm/utils/drizzle-graphql.js +230 -198
  74. package/esm/utils/drizzle-graphql.js.map +1 -1
  75. package/esm/utils/map-query.d.ts.map +1 -1
  76. package/esm/utils/map-query.js +10 -6
  77. package/esm/utils/map-query.js.map +1 -1
  78. package/esm/utils/selections.d.ts +5 -5
  79. package/esm/utils/selections.d.ts.map +1 -1
  80. package/esm/utils/selections.js +19 -18
  81. package/esm/utils/selections.js.map +1 -1
  82. package/lib/connection-helpers.js +93 -0
  83. package/lib/connection-helpers.js.map +1 -0
  84. package/lib/drizzle-field-builder.js +24 -13
  85. package/lib/drizzle-field-builder.js.map +1 -1
  86. package/lib/field-builder.js +2 -3
  87. package/lib/field-builder.js.map +1 -1
  88. package/lib/index.js +79 -60
  89. package/lib/index.js.map +1 -1
  90. package/lib/interface-ref.js.map +1 -1
  91. package/lib/model-loader.js +17 -10
  92. package/lib/model-loader.js.map +1 -1
  93. package/lib/schema-builder.js +113 -67
  94. package/lib/schema-builder.js.map +1 -1
  95. package/lib/types.js.map +1 -1
  96. package/lib/utils/config.js +31 -14
  97. package/lib/utils/config.js.map +1 -1
  98. package/lib/utils/cursors.js +135 -63
  99. package/lib/utils/cursors.js.map +1 -1
  100. package/lib/utils/drizzle-graphql.js +230 -221
  101. package/lib/utils/drizzle-graphql.js.map +1 -1
  102. package/lib/utils/map-query.js +10 -6
  103. package/lib/utils/map-query.js.map +1 -1
  104. package/lib/utils/selections.js +18 -17
  105. package/lib/utils/selections.js.map +1 -1
  106. package/package.json +14 -14
  107. package/src/connection-helpers.ts +206 -0
  108. package/src/drizzle-field-builder.ts +30 -26
  109. package/src/field-builder.ts +4 -7
  110. package/src/global-types.ts +112 -119
  111. package/src/index.ts +91 -70
  112. package/src/interface-ref.ts +7 -3
  113. package/src/model-loader.ts +32 -17
  114. package/src/node-ref.ts +1 -1
  115. package/src/object-ref.ts +2 -2
  116. package/src/schema-builder.ts +116 -113
  117. package/src/types.ts +95 -62
  118. package/src/utils/config.ts +39 -30
  119. package/src/utils/cursors.ts +134 -101
  120. package/src/utils/drizzle-graphql.ts +273 -274
  121. package/src/utils/map-query.ts +20 -8
  122. 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
- RelationalSchemaConfig,
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
- _: Partial<RelationalSchemaConfig<TablesRelationalConfig>>;
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
- schema?: Record<string, unknown>;
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
- schema: Record<string, unknown>;
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['DrizzleRelationSchema'],
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['DrizzleRelationSchema'][Table],
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['DrizzleRelationSchema'],
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['DrizzleRelationSchema'][Table],
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['DrizzleRelationSchema'],
135
+ Table extends keyof Types['DrizzleRelationsConfig'],
120
136
  Shape,
121
137
  Selection,
122
138
  Interfaces extends InterfaceParam<Types>[],
123
- IDColumn extends Column,
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
- | IDColumn
145
- | IDColumn[]
146
- | ((columns: Types['DrizzleRelationSchema'][Table]['columns']) => IDColumn | IDColumn[]);
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['DrizzleRelationSchema'][Table],
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['DrizzleRelationSchema'],
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['DrizzleRelationSchema'],
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['DrizzleRelationSchema'],
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
- false,
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', false>
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', false>),
331
+ | ((args: object, context: object) => DBQueryConfig<'one'>),
302
332
  path?: IndirectInclude | string[],
303
333
  type?: string,
304
- ) => DBQueryConfig<'one', false> | boolean,
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['DrizzleRelationSchema'];
339
+ [drizzleTableName]?: keyof Types['DrizzleRelationsConfig'];
310
340
  }
311
- ? Types['DrizzleRelationSchema'][NonNullable<Shape[typeof drizzleTableName]>]
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['DrizzleRelationSchema'],
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['DrizzleRelationSchema'],
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['DrizzleRelationSchema'],
397
- Types['DrizzleRelationSchemaByDbName'][Rel['referencedTableName']],
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
- false,
413
- Types['DrizzleRelationSchema'],
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
- false,
422
- Types['DrizzleRelationSchema'],
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['DrizzleRelationSchema'],
462
+ Table extends keyof Types['DrizzleRelationsConfig'],
435
463
  > = Omit<
436
464
  DBQueryConfig<
437
465
  'many',
438
- true,
439
- Types['DrizzleRelationSchema'],
440
- Types['DrizzleRelationSchema'][Table]
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', false, Types['DrizzleRelationSchema'], Table>,
451
- 'orderBy' | 'limit' | 'offset' | 'columns' | 'extra' | 'with'
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', false, Types['DrizzleRelationSchema'], Table>,
463
- 'orderBy' | 'limit' | 'offset'
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['DrizzleRelationSchema']> | keyof Types['DrizzleRelationSchema'],
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'> & { orderBy: SQL },
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['DrizzleRelationSchemaByDbName'][Table['relations'][Field]['referencedTableName']],
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]['referencedTableName']>;
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
- | { asc: Column }
595
- | { desc: Column }
596
- | (Column | { asc: Column } | { desc: Column })[];
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
 
@@ -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 extends RelationalSchemaConfig<TablesRelationalConfig> {
5
+ export interface PothosDrizzleSchemaConfig {
14
6
  skipDeferredFragments: boolean;
15
- dbToSchema: Record<string, TableRelationalConfig>;
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 config: RelationalSchemaConfig<TablesRelationalConfig>;
21
- if (builder.options.drizzle.schema) {
22
- const tablesConfig = extractTablesRelationalConfig(
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
- config = (builder.options.drizzle.client as DrizzleClient)
33
- ._ as RelationalSchemaConfig<TablesRelationalConfig>;
17
+ relations = (builder.options.drizzle.client as DrizzleClient)._.relations;
34
18
  }
35
19
 
36
- const dbToSchema = Object.values(config.schema).reduce<Record<string, TableRelationalConfig>>(
20
+ const dbToSchema = Object.values(relations.tables).reduce<Record<string, Table>>(
37
21
  (acc, table) => {
38
- acc[table.dbName] = table;
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.dbName] = Object.entries(table.columns).reduce<Record<string, string>>(
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
- ...config,
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
  );