@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.
- package/CHANGELOG.md +12 -0
- package/dts/drizzle-field-builder.d.ts.map +1 -1
- package/dts/utils/config.d.ts +2 -1
- package/dts/utils/config.d.ts.map +1 -1
- package/dts/utils/cursors.d.ts +4 -4
- package/dts/utils/cursors.d.ts.map +1 -1
- package/esm/drizzle-field-builder.d.ts.map +1 -1
- package/esm/drizzle-field-builder.js +19 -29
- package/esm/drizzle-field-builder.js.map +1 -1
- package/esm/interface-ref.js +1 -3
- package/esm/interface-ref.js.map +1 -1
- package/esm/node-ref.js +1 -3
- package/esm/node-ref.js.map +1 -1
- package/esm/object-ref.js +1 -4
- package/esm/object-ref.js.map +1 -1
- package/esm/schema-builder.js +3 -2
- package/esm/schema-builder.js.map +1 -1
- package/esm/utils/config.d.ts +2 -1
- package/esm/utils/config.d.ts.map +1 -1
- package/esm/utils/config.js +17 -1
- package/esm/utils/config.js.map +1 -1
- package/esm/utils/cursors.d.ts +4 -4
- package/esm/utils/cursors.d.ts.map +1 -1
- package/esm/utils/cursors.js +11 -11
- package/esm/utils/cursors.js.map +1 -1
- package/lib/drizzle-field-builder.js +19 -29
- package/lib/drizzle-field-builder.js.map +1 -1
- package/lib/interface-ref.js +1 -3
- package/lib/interface-ref.js.map +1 -1
- package/lib/node-ref.js +1 -3
- package/lib/node-ref.js.map +1 -1
- package/lib/object-ref.js +1 -4
- package/lib/object-ref.js.map +1 -1
- package/lib/schema-builder.js +3 -2
- package/lib/schema-builder.js.map +1 -1
- package/lib/utils/config.js +16 -0
- package/lib/utils/config.js.map +1 -1
- package/lib/utils/cursors.js +11 -11
- package/lib/utils/cursors.js.map +1 -1
- package/package.json +7 -7
- package/src/drizzle-field-builder.ts +12 -15
- package/src/schema-builder.ts +3 -2
- package/src/utils/config.ts +28 -0
- package/src/utils/cursors.ts +15 -11
package/src/utils/config.ts
CHANGED
|
@@ -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
|
},
|
package/src/utils/cursors.ts
CHANGED
|
@@ -39,8 +39,12 @@ export function formatCursorChunk(value: unknown) {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
export function formatDrizzleCursor(
|
|
43
|
-
|
|
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
|
|
72
|
+
return `${JSON.stringify(fields.map((col) => value[config.columnToTsName(col)]))}`;
|
|
69
73
|
}
|
|
70
74
|
|
|
71
|
-
return `${formatIDChunk(value[fields[0]
|
|
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
|
|
86
|
+
return `J:${JSON.stringify(fields.map((col) => value[config.columnToTsName(col)]))}`;
|
|
83
87
|
}
|
|
84
88
|
|
|
85
|
-
return `${formatCursorChunk(value[fields[0]
|
|
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
|
|