@pothos/plugin-drizzle 0.13.0 → 0.15.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 (49) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +2 -2
  3. package/dts/drizzle-field-builder.d.ts.map +1 -1
  4. package/dts/global-types.d.ts.map +1 -1
  5. package/dts/utils/config.d.ts.map +1 -1
  6. package/dts/utils/drizzle-graphql.d.ts +1 -0
  7. package/dts/utils/drizzle-graphql.d.ts.map +1 -1
  8. package/dts/utils/map-query.d.ts.map +1 -1
  9. package/dts/utils/usage.d.ts +1 -1
  10. package/esm/drizzle-field-builder.d.ts.map +1 -1
  11. package/esm/drizzle-field-builder.js +2 -1
  12. package/esm/drizzle-field-builder.js.map +1 -1
  13. package/esm/global-types.d.ts.map +1 -1
  14. package/esm/model-loader.js +2 -2
  15. package/esm/model-loader.js.map +1 -1
  16. package/esm/utils/config.d.ts.map +1 -1
  17. package/esm/utils/config.js +8 -12
  18. package/esm/utils/config.js.map +1 -1
  19. package/esm/utils/cursors.js +2 -2
  20. package/esm/utils/cursors.js.map +1 -1
  21. package/esm/utils/drizzle-graphql.d.ts +1 -0
  22. package/esm/utils/drizzle-graphql.d.ts.map +1 -1
  23. package/esm/utils/drizzle-graphql.js +1 -1
  24. package/esm/utils/drizzle-graphql.js.map +1 -1
  25. package/esm/utils/map-query.d.ts.map +1 -1
  26. package/esm/utils/map-query.js +26 -8
  27. package/esm/utils/map-query.js.map +1 -1
  28. package/esm/utils/usage.d.ts +1 -1
  29. package/lib/drizzle-field-builder.js +2 -1
  30. package/lib/drizzle-field-builder.js.map +1 -1
  31. package/lib/model-loader.js +1 -1
  32. package/lib/model-loader.js.map +1 -1
  33. package/lib/utils/config.js +7 -11
  34. package/lib/utils/config.js.map +1 -1
  35. package/lib/utils/cursors.js +1 -1
  36. package/lib/utils/cursors.js.map +1 -1
  37. package/lib/utils/drizzle-graphql.js +233 -230
  38. package/lib/utils/drizzle-graphql.js.map +1 -1
  39. package/lib/utils/map-query.js +25 -8
  40. package/lib/utils/map-query.js.map +1 -1
  41. package/package.json +8 -8
  42. package/src/drizzle-field-builder.ts +2 -1
  43. package/src/global-types.ts +0 -4
  44. package/src/model-loader.ts +2 -2
  45. package/src/utils/config.ts +8 -12
  46. package/src/utils/cursors.ts +2 -2
  47. package/src/utils/drizzle-graphql.ts +1 -0
  48. package/src/utils/map-query.ts +27 -7
  49. package/src/utils/usage.ts +1 -1
@@ -37,10 +37,6 @@ import type {
37
37
  ShapeFromIdColumns,
38
38
  } from './types';
39
39
 
40
- type MapToDbName<T> = {
41
- [K in keyof T as T[K] extends { dbName: infer K2 extends string } ? K2 : K]: T[K];
42
- };
43
-
44
40
  declare global {
45
41
  export namespace PothosSchemaTypes {
46
42
  export interface Plugins<Types extends SchemaTypes> {
@@ -2,7 +2,7 @@ import { createContextCache, type SchemaTypes } from '@pothos/core';
2
2
  import {
3
3
  type AnyTable,
4
4
  type Column,
5
- getTableColumns,
5
+ getColumns,
6
6
  inArray,
7
7
  type SQL,
8
8
  sql,
@@ -60,7 +60,7 @@ export class ModelLoader {
60
60
  this.columns = columns ?? this.primaryKey;
61
61
  this.selectSQL = (table) => {
62
62
  const columns = this.columns.map((column) =>
63
- Object.values(getTableColumns(table)).find(
63
+ Object.values(getColumns(table)).find(
64
64
  (tblColumn) => 'columnType' in tblColumn && tblColumn.name === column.name,
65
65
  ),
66
66
  );
@@ -1,5 +1,5 @@
1
1
  import { createContextCache, type SchemaTypes } from '@pothos/core';
2
- import { type AnyRelations, type Column, getTableColumns, isTable, type Table } from 'drizzle-orm';
2
+ import { type AnyRelations, type Column, getColumns, isTable, type Table } from 'drizzle-orm';
3
3
  import type { DrizzleClient } from '../types';
4
4
 
5
5
  export interface PothosDrizzleSchemaConfig {
@@ -17,14 +17,12 @@ const configCache = createContextCache(
17
17
  relations = (builder.options.drizzle.client as DrizzleClient)._.relations;
18
18
  }
19
19
 
20
- const columnNameMappings: Record<string, string> = {};
20
+ const columnNameMappings = new Map<Column, string>();
21
21
 
22
22
  Object.values(relations).forEach(({ table }) => {
23
23
  if (isTable(table)) {
24
- Object.entries(getTableColumns(table)).forEach(([tsName, col]) => {
25
- if (col.uniqueName) {
26
- columnNameMappings[col.uniqueName] = tsName;
27
- }
24
+ Object.entries(getColumns(table)).forEach(([tsName, col]) => {
25
+ columnNameMappings.set(col, tsName);
28
26
  });
29
27
  }
30
28
  });
@@ -32,15 +30,13 @@ const configCache = createContextCache(
32
30
  return {
33
31
  skipDeferredFragments: builder.options.drizzle.skipDeferredFragments ?? true,
34
32
  columnToTsName: (column) => {
35
- if (!column.uniqueName) {
36
- throw new Error(`Column ${String(column.name)} has no uniqueName`);
37
- }
33
+ const tsName = columnNameMappings.get(column);
38
34
 
39
- if (!(column.uniqueName in columnNameMappings)) {
40
- throw new Error(`Typescript name not found for ${column.uniqueName}`);
35
+ if (!tsName) {
36
+ throw new Error(`Typescript name not found for column ${String(column.name)}`);
41
37
  }
42
38
 
43
- return columnNameMappings[column.uniqueName];
39
+ return tsName;
44
40
  },
45
41
  getPrimaryKey: (tableName) => {
46
42
  const tableConfig = builder.options.drizzle.getTableConfig(
@@ -8,7 +8,7 @@ import {
8
8
  import {
9
9
  type Column,
10
10
  type DBQueryConfig,
11
- getTableColumns,
11
+ getColumns,
12
12
  type SQL,
13
13
  type Table,
14
14
  type TableRelationalConfig,
@@ -322,7 +322,7 @@ function parseOrderBy(
322
322
  normalized.push({ direction: 'asc', column: field });
323
323
  }
324
324
  } else {
325
- const tableColumns = getTableColumns(table.table as Table);
325
+ const tableColumns = getColumns(table.table as Table);
326
326
  Object.entries(
327
327
  orderBy as {
328
328
  [k: string]: 'asc' | 'desc' | undefined;
@@ -1,3 +1,4 @@
1
+ export {};
1
2
  // import {
2
3
  // type Column,
3
4
  // type SQL,
@@ -401,15 +401,35 @@ function addFieldSelection(
401
401
 
402
402
  if (fieldSelect && selectionCompatible(state, fieldSelectionMap, true)) {
403
403
  mergeSelection(config, state, fieldSelectionMap);
404
- state.mappings[selection.alias?.value ?? selection.name.value] = {
405
- field: selection.name.value,
406
- type: type.name,
407
- mappings,
408
- indirectPath,
409
- };
404
+
405
+ state.mappings = mergeMappings(state.mappings, {
406
+ [selection.alias?.value ?? selection.name.value]: {
407
+ field: selection.name.value,
408
+ type: type.name,
409
+ mappings,
410
+ indirectPath,
411
+ },
412
+ });
410
413
  }
411
414
  }
412
415
 
416
+ function mergeMappings(existing: LoaderMappings, incoming: LoaderMappings): LoaderMappings {
417
+ const result: LoaderMappings = { ...existing };
418
+
419
+ for (const [key, value] of Object.entries(incoming)) {
420
+ if (result[key]) {
421
+ result[key] = {
422
+ ...result[key],
423
+ mappings: mergeMappings(result[key].mappings, value.mappings),
424
+ };
425
+ } else {
426
+ result[key] = value;
427
+ }
428
+ }
429
+
430
+ return result;
431
+ }
432
+
413
433
  export interface QueryFromInfoOptions<T extends SelectionMap> {
414
434
  config: PothosDrizzleSchemaConfig;
415
435
  context: object;
@@ -567,7 +587,7 @@ export function getIndirectType(type: GraphQLNamedType, info: GraphQLResolveInfo
567
587
 
568
588
  while (targetType.extensions?.pothosIndirectInclude) {
569
589
  targetType = info.schema.getType(
570
- (targetType.extensions?.pothosIndirectInclude as IndirectInclude).getType(),
590
+ (targetType.extensions.pothosIndirectInclude as IndirectInclude).getType(),
571
591
  )!;
572
592
  }
573
593
 
@@ -1,6 +1,6 @@
1
1
  export const usageSymbol = Symbol.for('Pothos.isUsed');
2
2
 
3
- export function wrapWithUsageCheck<T extends Object>(obj: T): T {
3
+ export function wrapWithUsageCheck<T extends object>(obj: T): T {
4
4
  const result = {};
5
5
  let used = true;
6
6