@pothos/plugin-drizzle 0.4.0 → 0.4.2

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 (44) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dts/drizzle-field-builder.d.ts.map +1 -1
  3. package/dts/utils/config.d.ts +2 -1
  4. package/dts/utils/config.d.ts.map +1 -1
  5. package/dts/utils/cursors.d.ts +4 -4
  6. package/dts/utils/cursors.d.ts.map +1 -1
  7. package/esm/drizzle-field-builder.d.ts.map +1 -1
  8. package/esm/drizzle-field-builder.js +19 -29
  9. package/esm/drizzle-field-builder.js.map +1 -1
  10. package/esm/interface-ref.js +1 -3
  11. package/esm/interface-ref.js.map +1 -1
  12. package/esm/node-ref.js +1 -3
  13. package/esm/node-ref.js.map +1 -1
  14. package/esm/object-ref.js +1 -4
  15. package/esm/object-ref.js.map +1 -1
  16. package/esm/schema-builder.js +3 -2
  17. package/esm/schema-builder.js.map +1 -1
  18. package/esm/utils/config.d.ts +2 -1
  19. package/esm/utils/config.d.ts.map +1 -1
  20. package/esm/utils/config.js +17 -1
  21. package/esm/utils/config.js.map +1 -1
  22. package/esm/utils/cursors.d.ts +4 -4
  23. package/esm/utils/cursors.d.ts.map +1 -1
  24. package/esm/utils/cursors.js +11 -11
  25. package/esm/utils/cursors.js.map +1 -1
  26. package/lib/drizzle-field-builder.js +19 -29
  27. package/lib/drizzle-field-builder.js.map +1 -1
  28. package/lib/interface-ref.js +1 -3
  29. package/lib/interface-ref.js.map +1 -1
  30. package/lib/node-ref.js +1 -3
  31. package/lib/node-ref.js.map +1 -1
  32. package/lib/object-ref.js +1 -4
  33. package/lib/object-ref.js.map +1 -1
  34. package/lib/schema-builder.js +3 -2
  35. package/lib/schema-builder.js.map +1 -1
  36. package/lib/utils/config.js +16 -0
  37. package/lib/utils/config.js.map +1 -1
  38. package/lib/utils/cursors.js +11 -11
  39. package/lib/utils/cursors.js.map +1 -1
  40. package/package.json +7 -7
  41. package/src/drizzle-field-builder.ts +12 -15
  42. package/src/schema-builder.ts +3 -2
  43. package/src/utils/config.ts +28 -0
  44. package/src/utils/cursors.ts +15 -11
@@ -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