@vantreeseba/drizzle-graphql 5.5.0 → 5.6.0

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/dist/index.cjs CHANGED
@@ -107,8 +107,276 @@ var uncapitalize = (input) => input?.length ? `${input[0].toLocaleLowerCase()}${
107
107
  var capitalize = (input) => input?.length ? `${input[0].toLocaleUpperCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
108
108
 
109
109
  // src/util/data-mappers/index.ts
110
+ var import_drizzle_orm2 = require("drizzle-orm");
111
+ var import_graphql3 = require("graphql");
112
+
113
+ // src/util/type-converter/index.ts
110
114
  var import_drizzle_orm = require("drizzle-orm");
115
+ var import_mysql_core = require("drizzle-orm/mysql-core");
116
+ var import_pg_core = require("drizzle-orm/pg-core");
117
+ var import_sqlite_core = require("drizzle-orm/sqlite-core");
118
+ var import_graphql2 = require("graphql");
119
+
120
+ // src/util/scalars/index.ts
111
121
  var import_graphql = require("graphql");
122
+ var import_graphql_scalars = require("graphql-scalars");
123
+ var asDecimalString = (value) => {
124
+ if (typeof value === "bigint") {
125
+ return value.toString();
126
+ }
127
+ if (typeof value === "number") {
128
+ if (!Number.isInteger(value)) {
129
+ throw new import_graphql.GraphQLError(`BigInt cannot represent non-integer value: ${value}`);
130
+ }
131
+ if (!Number.isSafeInteger(value)) {
132
+ throw new import_graphql.GraphQLError(
133
+ `BigInt cannot represent the number ${value} without precision loss \u2014 pass it as a string instead`
134
+ );
135
+ }
136
+ return String(value);
137
+ }
138
+ if (typeof value === "string") {
139
+ if (!/^-?\d+$/.test(value)) {
140
+ throw new import_graphql.GraphQLError(`BigInt cannot represent non-integer value: "${value}"`);
141
+ }
142
+ return value;
143
+ }
144
+ throw new import_graphql.GraphQLError(`BigInt cannot represent value: ${JSON.stringify(value)}`);
145
+ };
146
+ var GraphQLBigIntString = new import_graphql.GraphQLScalarType({
147
+ name: "BigInt",
148
+ description: "A 64-bit integer, transported as a decimal string so that values beyond JavaScript's safe integer range survive the round-trip intact.",
149
+ serialize: asDecimalString,
150
+ parseValue: asDecimalString,
151
+ parseLiteral: (ast) => {
152
+ if (ast.kind !== import_graphql.Kind.STRING && ast.kind !== import_graphql.Kind.INT) {
153
+ throw new import_graphql.GraphQLError(`BigInt cannot represent a ${ast.kind}`, { nodes: ast });
154
+ }
155
+ return asDecimalString(ast.value);
156
+ }
157
+ });
158
+ var numericStringPattern = /^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/;
159
+ var asNumericString = (value) => {
160
+ if (typeof value === "bigint") {
161
+ return value.toString();
162
+ }
163
+ if (typeof value === "number") {
164
+ if (!Number.isFinite(value)) {
165
+ throw new import_graphql.GraphQLError(`Decimal cannot represent non-finite value: ${value}`);
166
+ }
167
+ return String(value);
168
+ }
169
+ if (typeof value === "string") {
170
+ if (!numericStringPattern.test(value)) {
171
+ throw new import_graphql.GraphQLError(`Decimal cannot represent non-numeric value: "${value}"`);
172
+ }
173
+ return value;
174
+ }
175
+ throw new import_graphql.GraphQLError(`Decimal cannot represent value: ${JSON.stringify(value)}`);
176
+ };
177
+ var GraphQLDecimalString = new import_graphql.GraphQLScalarType({
178
+ name: "Decimal",
179
+ description: "An arbitrary-precision decimal, transported as a numeric string so that values beyond JavaScript's double-precision range survive the round-trip intact.",
180
+ serialize: asNumericString,
181
+ parseValue: asNumericString,
182
+ parseLiteral: (ast) => {
183
+ if (ast.kind !== import_graphql.Kind.STRING && ast.kind !== import_graphql.Kind.INT && ast.kind !== import_graphql.Kind.FLOAT) {
184
+ throw new import_graphql.GraphQLError(`Decimal cannot represent a ${ast.kind}`, { nodes: ast });
185
+ }
186
+ return asNumericString(ast.value);
187
+ }
188
+ });
189
+
190
+ // src/util/type-converter/index.ts
191
+ var allowedNameChars = /^[a-zA-Z0-9_]+$/;
192
+ var enumMap = /* @__PURE__ */ new WeakMap();
193
+ var generateEnumCached = (column, columnName, tableName) => {
194
+ if (enumMap.has(column)) {
195
+ return enumMap.get(column);
196
+ }
197
+ const gqlEnum = new import_graphql2.GraphQLEnumType({
198
+ name: `${capitalize(tableName)}${capitalize(columnName)}Enum`,
199
+ values: Object.fromEntries(
200
+ column.enumValues.map((e, index) => [
201
+ allowedNameChars.test(e) ? e : `Option${index}`,
202
+ {
203
+ value: e,
204
+ description: `Value: ${e}`
205
+ }
206
+ ])
207
+ )
208
+ });
209
+ enumMap.set(column, gqlEnum);
210
+ return gqlEnum;
211
+ };
212
+ var geoXyType = new import_graphql2.GraphQLObjectType({
213
+ name: "PgGeometryObject",
214
+ fields: {
215
+ x: { type: import_graphql2.GraphQLFloat },
216
+ y: { type: import_graphql2.GraphQLFloat }
217
+ }
218
+ });
219
+ var geoXyInputType = new import_graphql2.GraphQLInputObjectType({
220
+ name: "PgGeometryObjectInput",
221
+ fields: {
222
+ x: { type: import_graphql2.GraphQLFloat },
223
+ y: { type: import_graphql2.GraphQLFloat }
224
+ }
225
+ });
226
+ var columnToGraphQLCore = (column, columnName, tableName, isInput) => {
227
+ const dimensions = column.dimensions;
228
+ if (dimensions !== void 0 && dimensions > 0) {
229
+ const inner = columnBaseToGraphQLCore(column, columnName, tableName, isInput);
230
+ const innerDesc = inner.description ?? inner.type.name;
231
+ let type = inner.type;
232
+ let description = innerDesc;
233
+ for (let i = 0; i < dimensions; i++) {
234
+ type = new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(type));
235
+ description = `Array<${description}>`;
236
+ }
237
+ return { type, description };
238
+ }
239
+ return columnBaseToGraphQLCore(column, columnName, tableName, isInput);
240
+ };
241
+ var columnBaseToGraphQLCore = (column, columnName, tableName, isInput) => {
242
+ const { type: baseType, constraint } = (0, import_drizzle_orm.extractExtendedColumnType)(column);
243
+ switch (baseType) {
244
+ case "boolean":
245
+ return { type: import_graphql2.GraphQLBoolean, description: "Boolean" };
246
+ case "object":
247
+ if (column instanceof import_pg_core.PgTimestamp || column instanceof import_pg_core.PgDate) {
248
+ return { type: import_graphql_scalars.GraphQLDateTime, description: "DateTime" };
249
+ }
250
+ return column.columnType === "PgGeometryObject" ? {
251
+ type: isInput ? geoXyInputType : geoXyType,
252
+ description: "Geometry points XY"
253
+ } : column.columnType === "PgBytea" ? {
254
+ type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(import_graphql2.GraphQLInt)),
255
+ description: "Buffer"
256
+ } : { type: import_graphql_scalars.GraphQLJSON, description: "JSON" };
257
+ case "string":
258
+ if (column.enumValues?.length) {
259
+ return { type: generateEnumCached(column, columnName, tableName) };
260
+ }
261
+ if (constraint === "numeric" && !column.dimensions) {
262
+ return { type: GraphQLDecimalString, description: "Decimal" };
263
+ }
264
+ if (column instanceof import_pg_core.PgTimestamp || column instanceof import_pg_core.PgTimestampString) {
265
+ return { type: import_graphql_scalars.GraphQLDateTime, description: "DateTime" };
266
+ }
267
+ if (column instanceof import_pg_core.PgUUID) {
268
+ return { type: import_graphql_scalars.GraphQLUUID, description: "UUID" };
269
+ }
270
+ if (column instanceof import_pg_core.PgDateString) {
271
+ return isInput ? { type: import_graphql2.GraphQLString, description: "Date" } : { type: import_graphql_scalars.GraphQLDate, description: "Date" };
272
+ }
273
+ return { type: import_graphql2.GraphQLString, description: "String" };
274
+ case "bigint":
275
+ return { type: GraphQLBigIntString, description: "BigInt" };
276
+ case "number": {
277
+ return (0, import_drizzle_orm.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm.is)(column, import_pg_core.PgSerial) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlInt) || (0, import_drizzle_orm.is)(column, import_mysql_core.MySqlSerial) || (0, import_drizzle_orm.is)(column, import_sqlite_core.SQLiteInteger) ? { type: import_graphql2.GraphQLInt, description: "Integer" } : { type: import_graphql2.GraphQLFloat, description: "Float" };
278
+ }
279
+ case "array": {
280
+ if (column.columnType === "PgVector") {
281
+ return {
282
+ type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(import_graphql2.GraphQLFloat)),
283
+ description: "Array<Float>"
284
+ };
285
+ }
286
+ if (column.columnType === "PgGeometry") {
287
+ return {
288
+ type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(import_graphql2.GraphQLFloat)),
289
+ description: "Tuple<[Float, Float]>"
290
+ };
291
+ }
292
+ const innerType = columnToGraphQLCore(
293
+ column.baseColumn,
294
+ columnName,
295
+ tableName,
296
+ isInput
297
+ );
298
+ return {
299
+ type: new import_graphql2.GraphQLList(new import_graphql2.GraphQLNonNull(innerType.type)),
300
+ description: `Array<${innerType.description}>`
301
+ };
302
+ }
303
+ default:
304
+ throw new Error(`Drizzle-GraphQL Error: Type ${column.dataType} is not implemented!`);
305
+ }
306
+ };
307
+ var columnScalarOverrides = /* @__PURE__ */ new WeakMap();
308
+ var getColumnScalarOverride = (column, isInput) => {
309
+ const override = columnScalarOverrides.get(column);
310
+ return override ? isInput ? override.input : override.output : void 0;
311
+ };
312
+ var normalizeScalarOverride = (override, tableName, columnName) => {
313
+ const pair = override instanceof import_graphql2.GraphQLScalarType ? { output: override, input: override } : { output: override.output, input: override.input };
314
+ for (const scalar of [pair.output, pair.input]) {
315
+ if (scalar !== void 0 && !(scalar instanceof import_graphql2.GraphQLScalarType)) {
316
+ throw new Error(
317
+ `Drizzle-GraphQL Error: Scalar override for column '${tableName}.${columnName}' must be a GraphQLScalarType!`
318
+ );
319
+ }
320
+ }
321
+ return pair;
322
+ };
323
+ var registerScalarOverrides = (tables, config) => {
324
+ for (const [tableName, table] of Object.entries(tables)) {
325
+ for (const [columnName, column] of Object.entries((0, import_drizzle_orm.getTableColumns)(table))) {
326
+ const declared = config.scalars?.[tableName]?.[columnName];
327
+ let resolved;
328
+ if (declared !== void 0) {
329
+ resolved = normalizeScalarOverride(declared, tableName, columnName);
330
+ } else if (config.mapColumnType) {
331
+ let defaultType;
332
+ let defaultInputType;
333
+ try {
334
+ defaultType = columnToGraphQLCore(column, columnName, tableName, false).type;
335
+ } catch {
336
+ }
337
+ try {
338
+ defaultInputType = columnToGraphQLCore(column, columnName, tableName, true).type;
339
+ } catch {
340
+ }
341
+ const mapped = config.mapColumnType(column, { tableName, columnName, defaultType, defaultInputType });
342
+ if (mapped !== void 0) {
343
+ resolved = normalizeScalarOverride(mapped, tableName, columnName);
344
+ }
345
+ }
346
+ if (resolved && (resolved.output !== void 0 || resolved.input !== void 0)) {
347
+ columnScalarOverrides.set(column, resolved);
348
+ } else {
349
+ columnScalarOverrides.delete(column);
350
+ }
351
+ }
352
+ }
353
+ };
354
+ var drizzleColumnToGraphQLType = (column, columnName, tableName, forceNullable = false, defaultIsNullable = false, isInput = false) => {
355
+ const override = getColumnScalarOverride(column, isInput);
356
+ let typeDesc;
357
+ if (override) {
358
+ typeDesc = { type: override, description: override.name };
359
+ } else {
360
+ typeDesc = columnToGraphQLCore(column, columnName, tableName, isInput);
361
+ const noDesc = ["string", "boolean", "number"];
362
+ const { type: baseType } = (0, import_drizzle_orm.extractExtendedColumnType)(column);
363
+ if (noDesc.find((e) => e === baseType)) {
364
+ delete typeDesc.description;
365
+ }
366
+ }
367
+ if (forceNullable) {
368
+ return typeDesc;
369
+ }
370
+ if (column.notNull && !(defaultIsNullable && (column.hasDefault || column.defaultFn))) {
371
+ return {
372
+ type: new import_graphql2.GraphQLNonNull(typeDesc.type),
373
+ description: typeDesc.description
374
+ };
375
+ }
376
+ return typeDesc;
377
+ };
378
+
379
+ // src/util/data-mappers/index.ts
112
380
  var isJsonColumn = (column) => (column.dataType ?? "").includes("json") && column.columnType !== "PgGeometryObject";
113
381
  var remapToGraphQLCore = (key, value, tableName, column, relationMap) => {
114
382
  if (Array.isArray(value)) {
@@ -139,6 +407,9 @@ var remapToGraphQLCore = (key, value, tableName, column, relationMap) => {
139
407
  if (!column) {
140
408
  return value;
141
409
  }
410
+ if (getColumnScalarOverride(column, false)) {
411
+ return value;
412
+ }
142
413
  if (isJsonColumn(column)) {
143
414
  return value;
144
415
  }
@@ -169,7 +440,7 @@ var remapToGraphQLSingleOutput = (queryOutput, tableName, table, relationMap) =>
169
440
  for (const [key, value] of Object.entries(queryOutput)) {
170
441
  if (value === void 0 || value === null) {
171
442
  const relEntry = value === null ? relationMap?.[tableName]?.[key] : void 0;
172
- if (relEntry && (0, import_drizzle_orm.is)(relEntry.relation, import_drizzle_orm.One)) {
443
+ if (relEntry && (0, import_drizzle_orm2.is)(relEntry.relation, import_drizzle_orm2.One)) {
173
444
  queryOutput[key] = null;
174
445
  continue;
175
446
  }
@@ -192,13 +463,19 @@ var remapToGraphQLArrayOutput = (queryOutput, tableName, table, relationMap) =>
192
463
  return queryOutput;
193
464
  };
194
465
  var remapFromGraphQLCore = (value, column, columnName) => {
466
+ if (getColumnScalarOverride(column, true)) {
467
+ if (typeof value === "object" && value !== null && Object.getPrototypeOf(value) === null) {
468
+ return Object.assign({}, value);
469
+ }
470
+ return value;
471
+ }
195
472
  const dataType = column.dataType ?? "";
196
473
  const columnType = column.columnType ?? "";
197
474
  const isTimestampColumn = columnType === "SQLiteTimestamp" || columnType === "SQLiteTimestampMs" || columnType === "MySqlTimestamp" || columnType === "MySqlDateTime" || columnType === "PgTimestamp" || columnType === "PgTimestampString";
198
475
  if (isTimestampColumn) {
199
476
  const formatted = new Date(value);
200
477
  if (Number.isNaN(formatted.getTime())) {
201
- throw new import_graphql.GraphQLError(`Field '${columnName}' is not a valid date!`);
478
+ throw new import_graphql3.GraphQLError(`Field '${columnName}' is not a valid date!`);
202
479
  }
203
480
  return formatted;
204
481
  }
@@ -207,7 +484,7 @@ var remapFromGraphQLCore = (value, column, columnName) => {
207
484
  const dateOnly = value.includes("T") ? value.split("T")[0] : value;
208
485
  const check = new Date(dateOnly);
209
486
  if (Number.isNaN(check.getTime())) {
210
- throw new import_graphql.GraphQLError(`Field '${columnName}' is not a valid date!`);
487
+ throw new import_graphql3.GraphQLError(`Field '${columnName}' is not a valid date!`);
211
488
  }
212
489
  return dateOnly;
213
490
  }
@@ -215,7 +492,7 @@ var remapFromGraphQLCore = (value, column, columnName) => {
215
492
  try {
216
493
  return BigInt(value);
217
494
  } catch {
218
- throw new import_graphql.GraphQLError(`Field '${columnName}' is not a BigInt!`);
495
+ throw new import_graphql3.GraphQLError(`Field '${columnName}' is not a BigInt!`);
219
496
  }
220
497
  }
221
498
  if (dataType.includes("json") && column.columnType !== "PgGeometryObject") {
@@ -225,13 +502,13 @@ var remapFromGraphQLCore = (value, column, columnName) => {
225
502
  case "date": {
226
503
  const formatted = new Date(value);
227
504
  if (Number.isNaN(formatted.getTime())) {
228
- throw new import_graphql.GraphQLError(`Field '${columnName}' is not a valid date!`);
505
+ throw new import_graphql3.GraphQLError(`Field '${columnName}' is not a valid date!`);
229
506
  }
230
507
  return formatted;
231
508
  }
232
509
  case "buffer": {
233
510
  if (!Array.isArray(value)) {
234
- throw new import_graphql.GraphQLError(`Field '${columnName}' is not an array!`);
511
+ throw new import_graphql3.GraphQLError(`Field '${columnName}' is not an array!`);
235
512
  }
236
513
  return Buffer.from(value);
237
514
  }
@@ -242,7 +519,7 @@ var remapFromGraphQLCore = (value, column, columnName) => {
242
519
  try {
243
520
  return JSON.parse(value);
244
521
  } catch (e) {
245
- throw new import_graphql.GraphQLError(
522
+ throw new import_graphql3.GraphQLError(
246
523
  `Invalid JSON in field '${columnName}':
247
524
  ${e instanceof Error ? e.message : "Unknown error"}`
248
525
  );
@@ -250,10 +527,10 @@ ${e instanceof Error ? e.message : "Unknown error"}`
250
527
  }
251
528
  case "array": {
252
529
  if (!Array.isArray(value)) {
253
- throw new import_graphql.GraphQLError(`Field '${columnName}' is not an array!`);
530
+ throw new import_graphql3.GraphQLError(`Field '${columnName}' is not an array!`);
254
531
  }
255
532
  if (column.columnType === "PgGeometry" && value.length !== 2) {
256
- throw new import_graphql.GraphQLError(
533
+ throw new import_graphql3.GraphQLError(
257
534
  `Invalid float tuple in field '${columnName}': expected array with length of 2, received ${value.length}`
258
535
  );
259
536
  }
@@ -263,7 +540,7 @@ ${e instanceof Error ? e.message : "Unknown error"}`
263
540
  try {
264
541
  return BigInt(value);
265
542
  } catch (_error) {
266
- throw new import_graphql.GraphQLError(`Field '${columnName}' is not a BigInt!`);
543
+ throw new import_graphql3.GraphQLError(`Field '${columnName}' is not a BigInt!`);
267
544
  }
268
545
  }
269
546
  default: {
@@ -279,9 +556,9 @@ var remapFromGraphQLSingleInput = (queryInput, table) => {
279
556
  if (value === void 0) {
280
557
  delete queryInput[key];
281
558
  } else {
282
- const column = (0, import_drizzle_orm.getTableColumns)(table)[key];
559
+ const column = (0, import_drizzle_orm2.getTableColumns)(table)[key];
283
560
  if (!column) {
284
- throw new import_graphql.GraphQLError(`Unknown column: ${key}`);
561
+ throw new import_graphql3.GraphQLError(`Unknown column: ${key}`);
285
562
  }
286
563
  if (value === null && column.notNull) {
287
564
  delete queryInput[key];
@@ -299,217 +576,6 @@ var remapFromGraphQLArrayInput = (queryInput, table) => {
299
576
  return queryInput;
300
577
  };
301
578
 
302
- // src/util/scalars/index.ts
303
- var import_graphql2 = require("graphql");
304
- var import_graphql_scalars = require("graphql-scalars");
305
- var asDecimalString = (value) => {
306
- if (typeof value === "bigint") {
307
- return value.toString();
308
- }
309
- if (typeof value === "number") {
310
- if (!Number.isInteger(value)) {
311
- throw new import_graphql2.GraphQLError(`BigInt cannot represent non-integer value: ${value}`);
312
- }
313
- if (!Number.isSafeInteger(value)) {
314
- throw new import_graphql2.GraphQLError(
315
- `BigInt cannot represent the number ${value} without precision loss \u2014 pass it as a string instead`
316
- );
317
- }
318
- return String(value);
319
- }
320
- if (typeof value === "string") {
321
- if (!/^-?\d+$/.test(value)) {
322
- throw new import_graphql2.GraphQLError(`BigInt cannot represent non-integer value: "${value}"`);
323
- }
324
- return value;
325
- }
326
- throw new import_graphql2.GraphQLError(`BigInt cannot represent value: ${JSON.stringify(value)}`);
327
- };
328
- var GraphQLBigIntString = new import_graphql2.GraphQLScalarType({
329
- name: "BigInt",
330
- description: "A 64-bit integer, transported as a decimal string so that values beyond JavaScript's safe integer range survive the round-trip intact.",
331
- serialize: asDecimalString,
332
- parseValue: asDecimalString,
333
- parseLiteral: (ast) => {
334
- if (ast.kind !== import_graphql2.Kind.STRING && ast.kind !== import_graphql2.Kind.INT) {
335
- throw new import_graphql2.GraphQLError(`BigInt cannot represent a ${ast.kind}`, { nodes: ast });
336
- }
337
- return asDecimalString(ast.value);
338
- }
339
- });
340
- var numericStringPattern = /^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/;
341
- var asNumericString = (value) => {
342
- if (typeof value === "bigint") {
343
- return value.toString();
344
- }
345
- if (typeof value === "number") {
346
- if (!Number.isFinite(value)) {
347
- throw new import_graphql2.GraphQLError(`Decimal cannot represent non-finite value: ${value}`);
348
- }
349
- return String(value);
350
- }
351
- if (typeof value === "string") {
352
- if (!numericStringPattern.test(value)) {
353
- throw new import_graphql2.GraphQLError(`Decimal cannot represent non-numeric value: "${value}"`);
354
- }
355
- return value;
356
- }
357
- throw new import_graphql2.GraphQLError(`Decimal cannot represent value: ${JSON.stringify(value)}`);
358
- };
359
- var GraphQLDecimalString = new import_graphql2.GraphQLScalarType({
360
- name: "Decimal",
361
- description: "An arbitrary-precision decimal, transported as a numeric string so that values beyond JavaScript's double-precision range survive the round-trip intact.",
362
- serialize: asNumericString,
363
- parseValue: asNumericString,
364
- parseLiteral: (ast) => {
365
- if (ast.kind !== import_graphql2.Kind.STRING && ast.kind !== import_graphql2.Kind.INT && ast.kind !== import_graphql2.Kind.FLOAT) {
366
- throw new import_graphql2.GraphQLError(`Decimal cannot represent a ${ast.kind}`, { nodes: ast });
367
- }
368
- return asNumericString(ast.value);
369
- }
370
- });
371
-
372
- // src/util/type-converter/index.ts
373
- var import_drizzle_orm2 = require("drizzle-orm");
374
- var import_mysql_core = require("drizzle-orm/mysql-core");
375
- var import_pg_core = require("drizzle-orm/pg-core");
376
- var import_sqlite_core = require("drizzle-orm/sqlite-core");
377
- var import_graphql3 = require("graphql");
378
- var allowedNameChars = /^[a-zA-Z0-9_]+$/;
379
- var enumMap = /* @__PURE__ */ new WeakMap();
380
- var generateEnumCached = (column, columnName, tableName) => {
381
- if (enumMap.has(column)) {
382
- return enumMap.get(column);
383
- }
384
- const gqlEnum = new import_graphql3.GraphQLEnumType({
385
- name: `${capitalize(tableName)}${capitalize(columnName)}Enum`,
386
- values: Object.fromEntries(
387
- column.enumValues.map((e, index) => [
388
- allowedNameChars.test(e) ? e : `Option${index}`,
389
- {
390
- value: e,
391
- description: `Value: ${e}`
392
- }
393
- ])
394
- )
395
- });
396
- enumMap.set(column, gqlEnum);
397
- return gqlEnum;
398
- };
399
- var geoXyType = new import_graphql3.GraphQLObjectType({
400
- name: "PgGeometryObject",
401
- fields: {
402
- x: { type: import_graphql3.GraphQLFloat },
403
- y: { type: import_graphql3.GraphQLFloat }
404
- }
405
- });
406
- var geoXyInputType = new import_graphql3.GraphQLInputObjectType({
407
- name: "PgGeometryObjectInput",
408
- fields: {
409
- x: { type: import_graphql3.GraphQLFloat },
410
- y: { type: import_graphql3.GraphQLFloat }
411
- }
412
- });
413
- var columnToGraphQLCore = (column, columnName, tableName, isInput) => {
414
- const dimensions = column.dimensions;
415
- if (dimensions !== void 0 && dimensions > 0) {
416
- const inner = columnBaseToGraphQLCore(column, columnName, tableName, isInput);
417
- const innerDesc = inner.description ?? inner.type.name;
418
- let type = inner.type;
419
- let description = innerDesc;
420
- for (let i = 0; i < dimensions; i++) {
421
- type = new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(type));
422
- description = `Array<${description}>`;
423
- }
424
- return { type, description };
425
- }
426
- return columnBaseToGraphQLCore(column, columnName, tableName, isInput);
427
- };
428
- var columnBaseToGraphQLCore = (column, columnName, tableName, isInput) => {
429
- const { type: baseType, constraint } = (0, import_drizzle_orm2.extractExtendedColumnType)(column);
430
- switch (baseType) {
431
- case "boolean":
432
- return { type: import_graphql3.GraphQLBoolean, description: "Boolean" };
433
- case "object":
434
- if (column instanceof import_pg_core.PgTimestamp || column instanceof import_pg_core.PgDate) {
435
- return { type: import_graphql_scalars.GraphQLDateTime, description: "DateTime" };
436
- }
437
- return column.columnType === "PgGeometryObject" ? {
438
- type: isInput ? geoXyInputType : geoXyType,
439
- description: "Geometry points XY"
440
- } : column.columnType === "PgBytea" ? {
441
- type: new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(import_graphql3.GraphQLInt)),
442
- description: "Buffer"
443
- } : { type: import_graphql_scalars.GraphQLJSON, description: "JSON" };
444
- case "string":
445
- if (column.enumValues?.length) {
446
- return { type: generateEnumCached(column, columnName, tableName) };
447
- }
448
- if (constraint === "numeric" && !column.dimensions) {
449
- return { type: GraphQLDecimalString, description: "Decimal" };
450
- }
451
- if (column instanceof import_pg_core.PgTimestamp || column instanceof import_pg_core.PgTimestampString) {
452
- return { type: import_graphql_scalars.GraphQLDateTime, description: "DateTime" };
453
- }
454
- if (column instanceof import_pg_core.PgUUID) {
455
- return { type: import_graphql_scalars.GraphQLUUID, description: "UUID" };
456
- }
457
- if (column instanceof import_pg_core.PgDateString) {
458
- return isInput ? { type: import_graphql3.GraphQLString, description: "Date" } : { type: import_graphql_scalars.GraphQLDate, description: "Date" };
459
- }
460
- return { type: import_graphql3.GraphQLString, description: "String" };
461
- case "bigint":
462
- return { type: GraphQLBigIntString, description: "BigInt" };
463
- case "number": {
464
- return (0, import_drizzle_orm2.is)(column, import_pg_core.PgInteger) || (0, import_drizzle_orm2.is)(column, import_pg_core.PgSerial) || (0, import_drizzle_orm2.is)(column, import_mysql_core.MySqlInt) || (0, import_drizzle_orm2.is)(column, import_mysql_core.MySqlSerial) || (0, import_drizzle_orm2.is)(column, import_sqlite_core.SQLiteInteger) ? { type: import_graphql3.GraphQLInt, description: "Integer" } : { type: import_graphql3.GraphQLFloat, description: "Float" };
465
- }
466
- case "array": {
467
- if (column.columnType === "PgVector") {
468
- return {
469
- type: new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(import_graphql3.GraphQLFloat)),
470
- description: "Array<Float>"
471
- };
472
- }
473
- if (column.columnType === "PgGeometry") {
474
- return {
475
- type: new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(import_graphql3.GraphQLFloat)),
476
- description: "Tuple<[Float, Float]>"
477
- };
478
- }
479
- const innerType = columnToGraphQLCore(
480
- column.baseColumn,
481
- columnName,
482
- tableName,
483
- isInput
484
- );
485
- return {
486
- type: new import_graphql3.GraphQLList(new import_graphql3.GraphQLNonNull(innerType.type)),
487
- description: `Array<${innerType.description}>`
488
- };
489
- }
490
- default:
491
- throw new Error(`Drizzle-GraphQL Error: Type ${column.dataType} is not implemented!`);
492
- }
493
- };
494
- var drizzleColumnToGraphQLType = (column, columnName, tableName, forceNullable = false, defaultIsNullable = false, isInput = false) => {
495
- const typeDesc = columnToGraphQLCore(column, columnName, tableName, isInput);
496
- const noDesc = ["string", "boolean", "number"];
497
- const { type: baseType } = (0, import_drizzle_orm2.extractExtendedColumnType)(column);
498
- if (noDesc.find((e) => e === baseType)) {
499
- delete typeDesc.description;
500
- }
501
- if (forceNullable) {
502
- return typeDesc;
503
- }
504
- if (column.notNull && !(defaultIsNullable && (column.hasDefault || column.defaultFn))) {
505
- return {
506
- type: new import_graphql3.GraphQLNonNull(typeDesc.type),
507
- description: typeDesc.description
508
- };
509
- }
510
- return typeDesc;
511
- };
512
-
513
579
  // src/util/builders/common.ts
514
580
  var rqbCrashTypes = ["SQLiteBigInt", "SQLiteBlobJson", "SQLiteBlobBuffer"];
515
581
  var resolveTypeName = (name, typeNameMapper) => {
@@ -1088,9 +1154,31 @@ var arrayFilterFields = (colType, colArr, colDesc) => {
1088
1154
  };
1089
1155
  };
1090
1156
  var FILTERS_WITHOUT_STRING_OPS = /* @__PURE__ */ new Set(["Id", "Int", "Float", "BigInt", "Decimal"]);
1157
+ var BUILTIN_FILTER_SCALARS = /* @__PURE__ */ new Set([
1158
+ import_graphql4.GraphQLString,
1159
+ import_graphql4.GraphQLInt,
1160
+ import_graphql4.GraphQLFloat,
1161
+ import_graphql4.GraphQLBoolean,
1162
+ GraphQLBigIntString,
1163
+ GraphQLDecimalString,
1164
+ import_graphql_scalars.GraphQLUUID,
1165
+ import_graphql_scalars.GraphQLJSON,
1166
+ import_graphql_scalars.GraphQLDate,
1167
+ import_graphql_scalars.GraphQLDateTime
1168
+ ]);
1169
+ var resolveOverrideFilterDescriptor = (scalar) => {
1170
+ if (scalar === import_graphql_scalars.GraphQLJSON) {
1171
+ return { name: "JSON", kind: "json" };
1172
+ }
1173
+ if (scalar === import_graphql_scalars.GraphQLUUID) {
1174
+ return { name: "Id", kind: "scalar" };
1175
+ }
1176
+ return { name: scalar.name, kind: "scalar" };
1177
+ };
1091
1178
  var generateColumnFilterValues = (column, tableName, columnName, cacheCtx) => {
1092
1179
  const columnGraphQLType = drizzleColumnToGraphQLType(column, columnName, tableName, true, false, true);
1093
- const { name: genericName, kind } = resolveGenericFilterDescriptor(column, columnGraphQLType);
1180
+ const inputOverride = getColumnScalarOverride(column, true);
1181
+ const { name: genericName, kind } = inputOverride ? resolveOverrideFilterDescriptor(inputOverride) : resolveGenericFilterDescriptor(column, columnGraphQLType);
1094
1182
  const cached = cacheCtx.genericFilterCache.get(genericName);
1095
1183
  if (cached) {
1096
1184
  return cached;
@@ -1098,7 +1186,9 @@ var generateColumnFilterValues = (column, tableName, columnName, cacheCtx) => {
1098
1186
  const colType = columnGraphQLType.type;
1099
1187
  const colDesc = columnGraphQLType.description;
1100
1188
  const colArr = new import_graphql4.GraphQLList(new import_graphql4.GraphQLNonNull(colType));
1101
- const omitStringOps = FILTERS_WITHOUT_STRING_OPS.has(genericName);
1189
+ const customOverrideFilter = inputOverride !== void 0 && !BUILTIN_FILTER_SCALARS.has(inputOverride);
1190
+ const underlying = (0, import_drizzle_orm3.extractExtendedColumnType)(column);
1191
+ const omitStringOps = customOverrideFilter ? underlying.type !== "string" || underlying.constraint === "numeric" : FILTERS_WITHOUT_STRING_OPS.has(genericName);
1102
1192
  const baseFields = kind === "json" ? jsonFilterFields(column, colType) : kind === "array" ? arrayFilterFields(colType, colArr, colDesc) : {
1103
1193
  eq: { type: colType, description: colDesc },
1104
1194
  ne: { type: colType, description: colDesc },
@@ -1179,10 +1269,9 @@ var generateTableOrderCached = (table) => {
1179
1269
  }
1180
1270
  return remapped;
1181
1271
  };
1182
- var filterMap = /* @__PURE__ */ new WeakMap();
1183
1272
  var generateTableFilterValuesCached = (table, tableName, cacheCtx) => {
1184
- if (filterMap.has(table)) {
1185
- return filterMap.get(table);
1273
+ if (cacheCtx.filterFieldCache.has(table)) {
1274
+ return cacheCtx.filterFieldCache.get(table);
1186
1275
  }
1187
1276
  const columns = (0, import_drizzle_orm3.getColumns)(table);
1188
1277
  const columnEntries = Object.entries(columns);
@@ -1194,13 +1283,12 @@ var generateTableFilterValuesCached = (table, tableName, cacheCtx) => {
1194
1283
  }
1195
1284
  ])
1196
1285
  );
1197
- filterMap.set(table, remapped);
1286
+ cacheCtx.filterFieldCache.set(table, remapped);
1198
1287
  return remapped;
1199
1288
  };
1200
- var fieldMap = /* @__PURE__ */ new WeakMap();
1201
- var generateTableSelectTypeFieldsCached = (table, tableName) => {
1202
- if (fieldMap.has(table)) {
1203
- return fieldMap.get(table);
1289
+ var generateTableSelectTypeFieldsCached = (table, tableName, cacheCtx) => {
1290
+ if (cacheCtx.selectFieldCache.has(table)) {
1291
+ return cacheCtx.selectFieldCache.get(table);
1204
1292
  }
1205
1293
  const columns = (0, import_drizzle_orm3.getColumns)(table);
1206
1294
  const columnEntries = Object.entries(columns);
@@ -1217,7 +1305,7 @@ var generateTableSelectTypeFieldsCached = (table, tableName) => {
1217
1305
  resolve: rowCursorResolver
1218
1306
  };
1219
1307
  }
1220
- fieldMap.set(table, remapped);
1308
+ cacheCtx.selectFieldCache.set(table, remapped);
1221
1309
  return remapped;
1222
1310
  };
1223
1311
  var isFilterableRelation = (relation) => !relation.through;
@@ -1367,7 +1455,7 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
1367
1455
  const table = tables[tableName];
1368
1456
  const order = withOrder ? generateTableOrderTypeCached(table, tableName, typeNameMapper, cacheCtx, relationMap, tables) : void 0;
1369
1457
  const filters = generateTableFilterTypeCached(table, tableName, cacheCtx, typeNameMapper, relationMap, tables);
1370
- const tableFields = generateTableSelectTypeFieldsCached(table, tableName);
1458
+ const tableFields = generateTableSelectTypeFieldsCached(table, tableName, cacheCtx);
1371
1459
  const relationsForTable = relationMap[tableName];
1372
1460
  const relationEntries = relationsForTable ? Object.entries(relationsForTable) : [];
1373
1461
  if (relationsDepthLimit !== void 0 && currentDepth >= relationsDepthLimit) {
@@ -1436,7 +1524,7 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
1436
1524
  let relType = cacheCtx.objectTypeCache.get(targetTableName);
1437
1525
  if (!relType) {
1438
1526
  const targetTable = tables[targetTableName];
1439
- const targetTableFields = generateTableSelectTypeFieldsCached(targetTable, targetTableName);
1527
+ const targetTableFields = generateTableSelectTypeFieldsCached(targetTable, targetTableName, cacheCtx);
1440
1528
  let targetContainer = cacheCtx.relationFieldContainers.get(targetTableName);
1441
1529
  if (!targetContainer) {
1442
1530
  targetContainer = { fields: {} };
@@ -3420,6 +3508,7 @@ var generateSchemaData = (db, schema, relations, options) => {
3420
3508
  "Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
3421
3509
  );
3422
3510
  }
3511
+ registerScalarOverrides(tables, options);
3423
3512
  const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
3424
3513
  attachTargetPrimaryKeys(namedRelations, tables, mysqlPrimaryKeyPropNames);
3425
3514
  const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
@@ -3431,6 +3520,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3431
3520
  relationFieldContainers: /* @__PURE__ */ new Map(),
3432
3521
  fullyBuiltTables: /* @__PURE__ */ new Set(),
3433
3522
  relationTypeCache: /* @__PURE__ */ new Map(),
3523
+ selectFieldCache: /* @__PURE__ */ new WeakMap(),
3524
+ filterFieldCache: /* @__PURE__ */ new WeakMap(),
3434
3525
  orderTypeCache: /* @__PURE__ */ new WeakMap(),
3435
3526
  filterTypeCache: /* @__PURE__ */ new WeakMap(),
3436
3527
  listRelationFilterCache: /* @__PURE__ */ new Map(),
@@ -4255,6 +4346,7 @@ function generateSchemaData2(db, schema, relations, options) {
4255
4346
  "Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
4256
4347
  );
4257
4348
  }
4349
+ registerScalarOverrides(tables, options);
4258
4350
  const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
4259
4351
  attachTargetPrimaryKeys(namedRelations, tables, pgPrimaryKeyPropNames);
4260
4352
  const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
@@ -4266,6 +4358,8 @@ function generateSchemaData2(db, schema, relations, options) {
4266
4358
  relationFieldContainers: /* @__PURE__ */ new Map(),
4267
4359
  fullyBuiltTables: /* @__PURE__ */ new Set(),
4268
4360
  relationTypeCache: /* @__PURE__ */ new Map(),
4361
+ selectFieldCache: /* @__PURE__ */ new WeakMap(),
4362
+ filterFieldCache: /* @__PURE__ */ new WeakMap(),
4269
4363
  orderTypeCache: /* @__PURE__ */ new WeakMap(),
4270
4364
  filterTypeCache: /* @__PURE__ */ new WeakMap(),
4271
4365
  listRelationFilterCache: /* @__PURE__ */ new Map(),
@@ -5082,6 +5176,7 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5082
5176
  "Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
5083
5177
  );
5084
5178
  }
5179
+ registerScalarOverrides(tables, options);
5085
5180
  const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
5086
5181
  attachTargetPrimaryKeys(namedRelations, tables, sqlitePrimaryKeyPropNames);
5087
5182
  const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
@@ -5093,6 +5188,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5093
5188
  relationFieldContainers: /* @__PURE__ */ new Map(),
5094
5189
  fullyBuiltTables: /* @__PURE__ */ new Set(),
5095
5190
  relationTypeCache: /* @__PURE__ */ new Map(),
5191
+ selectFieldCache: /* @__PURE__ */ new WeakMap(),
5192
+ filterFieldCache: /* @__PURE__ */ new WeakMap(),
5096
5193
  orderTypeCache: /* @__PURE__ */ new WeakMap(),
5097
5194
  filterTypeCache: /* @__PURE__ */ new WeakMap(),
5098
5195
  listRelationFilterCache: /* @__PURE__ */ new Map(),
@@ -5538,6 +5635,8 @@ var buildSchema = (db, config) => {
5538
5635
  shouldEagerLoad,
5539
5636
  features,
5540
5637
  complexity,
5638
+ scalars: config?.scalars,
5639
+ mapColumnType: config?.mapColumnType,
5541
5640
  transactions
5542
5641
  };
5543
5642
  let generatorOutput;