@pothos/plugin-drizzle 0.3.0 → 0.4.1
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 +2 -1
- package/dts/drizzle-field-builder.d.ts.map +1 -1
- package/dts/global-types.d.ts +8 -2
- package/dts/global-types.d.ts.map +1 -1
- package/dts/node-ref.d.ts +7 -1
- package/dts/node-ref.d.ts.map +1 -1
- package/dts/types.d.ts +3 -3
- package/dts/types.d.ts.map +1 -1
- package/dts/utils/cursors.d.ts +4 -1
- package/dts/utils/cursors.d.ts.map +1 -1
- package/esm/drizzle-field-builder.d.ts +2 -1
- package/esm/drizzle-field-builder.d.ts.map +1 -1
- package/esm/drizzle-field-builder.js +18 -28
- package/esm/drizzle-field-builder.js.map +1 -1
- package/esm/global-types.d.ts +8 -2
- package/esm/global-types.d.ts.map +1 -1
- package/esm/global-types.js.map +1 -1
- package/esm/interface-ref.js +1 -3
- package/esm/interface-ref.js.map +1 -1
- package/esm/node-ref.d.ts +7 -1
- package/esm/node-ref.d.ts.map +1 -1
- package/esm/node-ref.js +20 -0
- 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 +6 -4
- package/esm/schema-builder.js.map +1 -1
- package/esm/types.d.ts +3 -3
- package/esm/types.d.ts.map +1 -1
- package/esm/utils/cursors.d.ts +4 -1
- package/esm/utils/cursors.d.ts.map +1 -1
- package/esm/utils/cursors.js +80 -22
- package/esm/utils/cursors.js.map +1 -1
- package/lib/drizzle-field-builder.js +18 -28
- 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 +30 -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 +5 -3
- package/lib/schema-builder.js.map +1 -1
- package/lib/utils/cursors.js +90 -25
- package/lib/utils/cursors.js.map +1 -1
- package/package.json +7 -7
- package/src/drizzle-field-builder.ts +16 -19
- package/src/global-types.ts +11 -2
- package/src/node-ref.ts +18 -1
- package/src/schema-builder.ts +15 -5
- package/src/types.ts +5 -4
- package/src/utils/cursors.ts +98 -22
package/src/utils/cursors.ts
CHANGED
|
@@ -43,6 +43,35 @@ export function formatDrizzleCursor(record: Record<string, unknown>, fields: Col
|
|
|
43
43
|
return getCursorFormatter(fields)(record);
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
export function formatIDChunk(value: unknown) {
|
|
47
|
+
if (value instanceof Date) {
|
|
48
|
+
return `${String(Number(value))}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
switch (typeof value) {
|
|
52
|
+
case 'number':
|
|
53
|
+
case 'string':
|
|
54
|
+
case 'bigint':
|
|
55
|
+
return `${value}`;
|
|
56
|
+
default:
|
|
57
|
+
throw new PothosValidationError(`Unsupported ID type ${typeof value}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function getIDSerializer(fields: Column[]) {
|
|
62
|
+
if (fields.length === 0) {
|
|
63
|
+
throw new PothosValidationError('Column serializer must have at least one field');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return (value: Record<string, unknown>) => {
|
|
67
|
+
if (fields.length > 1) {
|
|
68
|
+
return `${JSON.stringify(fields.map((col) => value[col.name]))}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return `${formatIDChunk(value[fields[0].name])}`;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
46
75
|
export function getColumnSerializer(fields: Column[]) {
|
|
47
76
|
if (fields.length === 0) {
|
|
48
77
|
throw new PothosValidationError('Column serializer must have at least one field');
|
|
@@ -113,32 +142,73 @@ export function parseSerializedDrizzleColumn(value: unknown) {
|
|
|
113
142
|
}
|
|
114
143
|
}
|
|
115
144
|
|
|
116
|
-
export function
|
|
145
|
+
export function parseSerializedIDColumn(id: string, field: Column): unknown {
|
|
117
146
|
if (!id) {
|
|
118
147
|
return id;
|
|
119
148
|
}
|
|
120
149
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
150
|
+
try {
|
|
151
|
+
switch (field.dataType) {
|
|
152
|
+
case 'date':
|
|
153
|
+
return new Date(id);
|
|
154
|
+
case 'string':
|
|
155
|
+
return id;
|
|
156
|
+
case 'number':
|
|
157
|
+
return Number.parseInt(id, 10);
|
|
158
|
+
case 'bigint':
|
|
159
|
+
return BigInt(id);
|
|
160
|
+
default:
|
|
161
|
+
throw new PothosValidationError(`Unsupported ID type ${field.dataType}`);
|
|
162
|
+
}
|
|
163
|
+
} catch (error: unknown) {
|
|
164
|
+
if (error instanceof PothosValidationError) {
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
throw new PothosValidationError(`Invalid serialized ID: ${id}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function getIDParser(fields: readonly Column[]) {
|
|
173
|
+
if (fields.length === 0) {
|
|
174
|
+
throw new PothosValidationError('Column parser must have at least one field');
|
|
141
175
|
}
|
|
176
|
+
|
|
177
|
+
return (value: string) => {
|
|
178
|
+
if (fields.length === 1) {
|
|
179
|
+
return { [fields[0].name]: parseSerializedIDColumn(value, fields[0]) };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
const parsed = JSON.parse(value) as unknown[];
|
|
184
|
+
|
|
185
|
+
if (!Array.isArray(parsed)) {
|
|
186
|
+
throw new PothosValidationError(
|
|
187
|
+
`Expected compound ID to contain an array, but got ${value}`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (parsed.length !== fields.length) {
|
|
192
|
+
throw new PothosValidationError(
|
|
193
|
+
`Expected compound ID to contain ${fields.length} elements, but got ${parsed.length}`,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const record: Record<string, unknown> = {};
|
|
198
|
+
|
|
199
|
+
fields.forEach((field, i) => {
|
|
200
|
+
record[field.name] = parsed[i];
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
return record;
|
|
204
|
+
} catch (error: unknown) {
|
|
205
|
+
if (error instanceof PothosValidationError) {
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
throw new PothosValidationError(`Invalid serialized ID: ${value}`);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
142
212
|
}
|
|
143
213
|
|
|
144
214
|
export function getColumnParser(fields: readonly Column[]) {
|
|
@@ -159,6 +229,12 @@ export function getColumnParser(fields: readonly Column[]) {
|
|
|
159
229
|
);
|
|
160
230
|
}
|
|
161
231
|
|
|
232
|
+
if (parsed.length !== fields.length) {
|
|
233
|
+
throw new PothosValidationError(
|
|
234
|
+
`Expected compound cursor to contain ${fields.length} elements, but got ${parsed.length}`,
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
162
238
|
const record: Record<string, unknown> = {};
|
|
163
239
|
|
|
164
240
|
fields.forEach((field, i) => {
|
|
@@ -404,7 +480,7 @@ export async function resolveDrizzleCursorConnection<T extends {}>(
|
|
|
404
480
|
) {
|
|
405
481
|
let query: DBQueryConfig<'many', false>;
|
|
406
482
|
let formatter: (node: Record<string, unknown>) => string;
|
|
407
|
-
const results = await resolve((q) => {
|
|
483
|
+
const results = await resolve((q = {}) => {
|
|
408
484
|
const { cursorColumns, ...connectionQuery } = drizzleCursorConnectionQuery({
|
|
409
485
|
...options,
|
|
410
486
|
orderBy: q.orderBy
|