@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.
Files changed (53) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dts/drizzle-field-builder.d.ts +2 -1
  3. package/dts/drizzle-field-builder.d.ts.map +1 -1
  4. package/dts/global-types.d.ts +8 -2
  5. package/dts/global-types.d.ts.map +1 -1
  6. package/dts/node-ref.d.ts +7 -1
  7. package/dts/node-ref.d.ts.map +1 -1
  8. package/dts/types.d.ts +3 -3
  9. package/dts/types.d.ts.map +1 -1
  10. package/dts/utils/cursors.d.ts +4 -1
  11. package/dts/utils/cursors.d.ts.map +1 -1
  12. package/esm/drizzle-field-builder.d.ts +2 -1
  13. package/esm/drizzle-field-builder.d.ts.map +1 -1
  14. package/esm/drizzle-field-builder.js +18 -28
  15. package/esm/drizzle-field-builder.js.map +1 -1
  16. package/esm/global-types.d.ts +8 -2
  17. package/esm/global-types.d.ts.map +1 -1
  18. package/esm/global-types.js.map +1 -1
  19. package/esm/interface-ref.js +1 -3
  20. package/esm/interface-ref.js.map +1 -1
  21. package/esm/node-ref.d.ts +7 -1
  22. package/esm/node-ref.d.ts.map +1 -1
  23. package/esm/node-ref.js +20 -0
  24. package/esm/node-ref.js.map +1 -1
  25. package/esm/object-ref.js +1 -4
  26. package/esm/object-ref.js.map +1 -1
  27. package/esm/schema-builder.js +6 -4
  28. package/esm/schema-builder.js.map +1 -1
  29. package/esm/types.d.ts +3 -3
  30. package/esm/types.d.ts.map +1 -1
  31. package/esm/utils/cursors.d.ts +4 -1
  32. package/esm/utils/cursors.d.ts.map +1 -1
  33. package/esm/utils/cursors.js +80 -22
  34. package/esm/utils/cursors.js.map +1 -1
  35. package/lib/drizzle-field-builder.js +18 -28
  36. package/lib/drizzle-field-builder.js.map +1 -1
  37. package/lib/interface-ref.js +1 -3
  38. package/lib/interface-ref.js.map +1 -1
  39. package/lib/node-ref.js +30 -3
  40. package/lib/node-ref.js.map +1 -1
  41. package/lib/object-ref.js +1 -4
  42. package/lib/object-ref.js.map +1 -1
  43. package/lib/schema-builder.js +5 -3
  44. package/lib/schema-builder.js.map +1 -1
  45. package/lib/utils/cursors.js +90 -25
  46. package/lib/utils/cursors.js.map +1 -1
  47. package/package.json +7 -7
  48. package/src/drizzle-field-builder.ts +16 -19
  49. package/src/global-types.ts +11 -2
  50. package/src/node-ref.ts +18 -1
  51. package/src/schema-builder.ts +15 -5
  52. package/src/types.ts +5 -4
  53. package/src/utils/cursors.ts +98 -22
@@ -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 parseID(id: string, dataType: string): unknown {
145
+ export function parseSerializedIDColumn(id: string, field: Column): unknown {
117
146
  if (!id) {
118
147
  return id;
119
148
  }
120
149
 
121
- switch (dataType) {
122
- case 'String':
123
- return id;
124
- case 'Int':
125
- return Number.parseInt(id, 10);
126
- case 'BigInt':
127
- return BigInt(id);
128
- case 'Boolean':
129
- return id !== 'false';
130
- case 'Float':
131
- case 'Decimal':
132
- return Number.parseFloat(id);
133
- case 'DateTime':
134
- return new Date(id);
135
- case 'Json':
136
- return JSON.parse(id) as unknown;
137
- case 'Byte':
138
- return Buffer.from(id, 'base64');
139
- default:
140
- return id;
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