@pothos/plugin-drizzle 0.4.1 → 0.4.3

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 (45) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dts/global-types.d.ts +1 -1
  3. package/dts/global-types.d.ts.map +1 -1
  4. package/dts/types.d.ts +1 -3
  5. package/dts/types.d.ts.map +1 -1
  6. package/dts/utils/config.d.ts +2 -1
  7. package/dts/utils/config.d.ts.map +1 -1
  8. package/dts/utils/cursors.d.ts +4 -4
  9. package/dts/utils/cursors.d.ts.map +1 -1
  10. package/esm/drizzle-field-builder.js +1 -1
  11. package/esm/drizzle-field-builder.js.map +1 -1
  12. package/esm/field-builder.js +3 -3
  13. package/esm/field-builder.js.map +1 -1
  14. package/esm/global-types.d.ts +2 -2
  15. package/esm/global-types.d.ts.map +1 -1
  16. package/esm/schema-builder.js +3 -2
  17. package/esm/schema-builder.js.map +1 -1
  18. package/esm/types.d.ts +1 -3
  19. package/esm/types.d.ts.map +1 -1
  20. package/esm/utils/config.d.ts +2 -1
  21. package/esm/utils/config.d.ts.map +1 -1
  22. package/esm/utils/config.js +17 -1
  23. package/esm/utils/config.js.map +1 -1
  24. package/esm/utils/cursors.d.ts +4 -4
  25. package/esm/utils/cursors.d.ts.map +1 -1
  26. package/esm/utils/cursors.js +11 -11
  27. package/esm/utils/cursors.js.map +1 -1
  28. package/lib/drizzle-field-builder.js +1 -1
  29. package/lib/drizzle-field-builder.js.map +1 -1
  30. package/lib/field-builder.js +6 -6
  31. package/lib/field-builder.js.map +1 -1
  32. package/lib/schema-builder.js +3 -2
  33. package/lib/schema-builder.js.map +1 -1
  34. package/lib/utils/config.js +16 -0
  35. package/lib/utils/config.js.map +1 -1
  36. package/lib/utils/cursors.js +11 -11
  37. package/lib/utils/cursors.js.map +1 -1
  38. package/package.json +2 -2
  39. package/src/drizzle-field-builder.ts +1 -1
  40. package/src/field-builder.ts +14 -7
  41. package/src/global-types.ts +2 -2
  42. package/src/schema-builder.ts +3 -2
  43. package/src/types.ts +1 -12
  44. package/src/utils/config.ts +28 -0
  45. package/src/utils/cursors.ts +15 -11
@@ -97,11 +97,12 @@ schemaBuilderProto.drizzleNode = function drizzleNode(
97
97
  Column
98
98
  >,
99
99
  ) {
100
- const tableConfig = getSchemaConfig(this).schema![table];
100
+ const schemaConfig = getSchemaConfig(this);
101
+ const tableConfig = schemaConfig.schema![table];
101
102
  const idColumn = typeof column === 'function' ? column(tableConfig.columns) : column;
102
103
  const idColumns = Array.isArray(idColumn) ? idColumn : [idColumn];
103
104
  const interfaceRef = this.nodeInterfaceRef?.();
104
- const resolve = getIDSerializer(idColumns);
105
+ const resolve = getIDSerializer(idColumns, schemaConfig);
105
106
  const idParser = getIDParser(idColumns);
106
107
  const typeName = variant ?? name ?? table;
107
108
  const nodeRef = new DrizzleNodeRef(typeName, table, {
package/src/types.ts CHANGED
@@ -223,18 +223,7 @@ export type DrizzleFieldWithInputOptions<
223
223
  >,
224
224
  'args'
225
225
  > &
226
- PothosSchemaTypes.FieldWithInputBaseOptions<
227
- Types,
228
- Args & {
229
- [K in InputName]: ArgumentRef<
230
- Types,
231
- InputShapeFromFields<Fields> | (true extends ArgRequired ? never : null | undefined)
232
- >;
233
- },
234
- Fields,
235
- InputName,
236
- ArgRequired
237
- >;
226
+ PothosSchemaTypes.FieldWithInputBaseOptions<Types, Args, Fields, InputName, ArgRequired>;
238
227
 
239
228
  export type DrizzleObjectFieldOptions<
240
229
  Types extends SchemaTypes,
@@ -1,15 +1,18 @@
1
1
  import { type SchemaTypes, createContextCache } from '@pothos/core';
2
2
  import {
3
+ type Column,
3
4
  type RelationalSchemaConfig,
4
5
  type TableRelationalConfig,
5
6
  type TablesRelationalConfig,
6
7
  createTableRelationsHelpers,
7
8
  extractTablesRelationalConfig,
9
+ getTableName,
8
10
  } from 'drizzle-orm';
9
11
  import type { DrizzleClient } from '../types';
10
12
 
11
13
  export interface PothosDrizzleSchemaConfig extends RelationalSchemaConfig<TablesRelationalConfig> {
12
14
  dbToSchema: Record<string, TableRelationalConfig>;
15
+ columnToTsName: (column: Column) => string;
13
16
  }
14
17
  const configCache = createContextCache(
15
18
  (builder: PothosSchemaTypes.SchemaBuilder<SchemaTypes>): PothosDrizzleSchemaConfig => {
@@ -37,8 +40,33 @@ const configCache = createContextCache(
37
40
  {},
38
41
  );
39
42
 
43
+ const columnMappings = Object.values(dbToSchema).reduce<Record<string, Record<string, string>>>(
44
+ (acc, table) => {
45
+ acc[table.dbName] = Object.entries(table.columns).reduce<Record<string, string>>(
46
+ (acc, [name, column]) => {
47
+ acc[column.name] = name;
48
+ return acc;
49
+ },
50
+ {},
51
+ );
52
+ return acc;
53
+ },
54
+ {},
55
+ );
56
+
40
57
  return {
41
58
  dbToSchema,
59
+ columnToTsName: (column) => {
60
+ const tableName = getTableName(column.table);
61
+ const table = columnMappings[tableName];
62
+ const columnName = table?.[column.name];
63
+
64
+ if (!columnName) {
65
+ throw new Error(`Could not find column mapping for ${tableName}.${column.name}`);
66
+ }
67
+
68
+ return columnName;
69
+ },
42
70
  ...config,
43
71
  };
44
72
  },
@@ -39,8 +39,12 @@ export function formatCursorChunk(value: unknown) {
39
39
  }
40
40
  }
41
41
 
42
- export function formatDrizzleCursor(record: Record<string, unknown>, fields: Column[]) {
43
- return getCursorFormatter(fields)(record);
42
+ export function formatDrizzleCursor(
43
+ record: Record<string, unknown>,
44
+ fields: Column[],
45
+ config: PothosDrizzleSchemaConfig,
46
+ ) {
47
+ return getCursorFormatter(fields, config)(record);
44
48
  }
45
49
 
46
50
  export function formatIDChunk(value: unknown) {
@@ -58,40 +62,40 @@ export function formatIDChunk(value: unknown) {
58
62
  }
59
63
  }
60
64
 
61
- export function getIDSerializer(fields: Column[]) {
65
+ export function getIDSerializer(fields: Column[], config: PothosDrizzleSchemaConfig) {
62
66
  if (fields.length === 0) {
63
67
  throw new PothosValidationError('Column serializer must have at least one field');
64
68
  }
65
69
 
66
70
  return (value: Record<string, unknown>) => {
67
71
  if (fields.length > 1) {
68
- return `${JSON.stringify(fields.map((col) => value[col.name]))}`;
72
+ return `${JSON.stringify(fields.map((col) => value[config.columnToTsName(col)]))}`;
69
73
  }
70
74
 
71
- return `${formatIDChunk(value[fields[0].name])}`;
75
+ return `${formatIDChunk(value[config.columnToTsName(fields[0])])}`;
72
76
  };
73
77
  }
74
78
 
75
- export function getColumnSerializer(fields: Column[]) {
79
+ export function getColumnSerializer(fields: Column[], config: PothosDrizzleSchemaConfig) {
76
80
  if (fields.length === 0) {
77
81
  throw new PothosValidationError('Column serializer must have at least one field');
78
82
  }
79
83
 
80
84
  return (value: Record<string, unknown>) => {
81
85
  if (fields.length > 1) {
82
- return `J:${JSON.stringify(fields.map((col) => value[col.name]))}`;
86
+ return `J:${JSON.stringify(fields.map((col) => value[config.columnToTsName(col)]))}`;
83
87
  }
84
88
 
85
- return `${formatCursorChunk(value[fields[0].name])}`;
89
+ return `${formatCursorChunk(value[config.columnToTsName(fields[0])])}`;
86
90
  };
87
91
  }
88
92
 
89
- export function getCursorFormatter(fields: Column[]) {
93
+ export function getCursorFormatter(fields: Column[], config: PothosDrizzleSchemaConfig) {
90
94
  if (fields.length === 0) {
91
95
  throw new PothosValidationError('Cursor must have at least one field');
92
96
  }
93
97
 
94
- const serializer = getColumnSerializer(fields);
98
+ const serializer = getColumnSerializer(fields, config);
95
99
 
96
100
  return (value: Record<string, unknown>) => {
97
101
  return encodeBase64(`DC:${serializer(value)}`);
@@ -489,7 +493,7 @@ export async function resolveDrizzleCursorConnection<T extends {}>(
489
493
  : q.orderBy
490
494
  : table.primaryKey,
491
495
  });
492
- formatter = getCursorFormatter(cursorColumns);
496
+ formatter = getCursorFormatter(cursorColumns, config);
493
497
 
494
498
  const where = typeof q.where === 'function' ? q.where(table.columns, getOperators()) : q.where;
495
499