@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.
- package/CHANGELOG.md +12 -0
- package/dts/global-types.d.ts +1 -1
- package/dts/global-types.d.ts.map +1 -1
- package/dts/types.d.ts +1 -3
- package/dts/types.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.js +1 -1
- package/esm/drizzle-field-builder.js.map +1 -1
- package/esm/field-builder.js +3 -3
- package/esm/field-builder.js.map +1 -1
- package/esm/global-types.d.ts +2 -2
- package/esm/global-types.d.ts.map +1 -1
- package/esm/schema-builder.js +3 -2
- package/esm/schema-builder.js.map +1 -1
- package/esm/types.d.ts +1 -3
- package/esm/types.d.ts.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 +1 -1
- package/lib/drizzle-field-builder.js.map +1 -1
- package/lib/field-builder.js +6 -6
- package/lib/field-builder.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 +2 -2
- package/src/drizzle-field-builder.ts +1 -1
- package/src/field-builder.ts +14 -7
- package/src/global-types.ts +2 -2
- package/src/schema-builder.ts +3 -2
- package/src/types.ts +1 -12
- package/src/utils/config.ts +28 -0
- package/src/utils/cursors.ts +15 -11
package/src/schema-builder.ts
CHANGED
|
@@ -97,11 +97,12 @@ schemaBuilderProto.drizzleNode = function drizzleNode(
|
|
|
97
97
|
Column
|
|
98
98
|
>,
|
|
99
99
|
) {
|
|
100
|
-
const
|
|
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,
|
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
|
|