@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 +335 -236
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +89 -3
- package/dist/index.d.ts +89 -3
- package/dist/index.js +339 -238
- package/dist/index.js.map +1 -1
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
asc,
|
|
18
18
|
desc,
|
|
19
19
|
eq,
|
|
20
|
+
extractExtendedColumnType as extractExtendedColumnType2,
|
|
20
21
|
getColumns,
|
|
21
22
|
getTableAsAliasSQL,
|
|
22
23
|
gt,
|
|
@@ -104,200 +105,29 @@ var uncapitalize = (input) => input?.length ? `${input[0].toLocaleLowerCase()}${
|
|
|
104
105
|
var capitalize = (input) => input?.length ? `${input[0].toLocaleUpperCase()}${input.length > 1 ? input.slice(1, input.length) : ""}` : input;
|
|
105
106
|
|
|
106
107
|
// src/util/data-mappers/index.ts
|
|
107
|
-
import { getTableColumns, is, One } from "drizzle-orm";
|
|
108
|
-
import { GraphQLError } from "graphql";
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const remapped = remapToGraphQLSingleOutput(
|
|
128
|
-
value,
|
|
129
|
-
rel.targetTableName,
|
|
130
|
-
rel.relation?.targetTable ?? rel.relation?.referencedTable,
|
|
131
|
-
relationMap
|
|
132
|
-
);
|
|
133
|
-
return remapped;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (!column) {
|
|
137
|
-
return value;
|
|
138
|
-
}
|
|
139
|
-
if (isJsonColumn(column)) {
|
|
140
|
-
return value;
|
|
141
|
-
}
|
|
142
|
-
if (value instanceof Date) {
|
|
143
|
-
return value.toISOString();
|
|
144
|
-
}
|
|
145
|
-
if (value instanceof Buffer) {
|
|
146
|
-
return Array.from(value);
|
|
147
|
-
}
|
|
148
|
-
if (typeof value === "bigint") {
|
|
149
|
-
return value.toString();
|
|
150
|
-
}
|
|
151
|
-
if (Array.isArray(value)) {
|
|
152
|
-
if (column.columnType === "PgGeometry" || column.columnType === "PgVector") {
|
|
153
|
-
return value;
|
|
154
|
-
}
|
|
155
|
-
return value.map((arrVal) => remapToGraphQLCore(key, arrVal, tableName, column, relationMap));
|
|
156
|
-
}
|
|
157
|
-
if (typeof value === "object" && value !== null) {
|
|
158
|
-
if (column.columnType === "PgGeometryObject") {
|
|
159
|
-
return value;
|
|
160
|
-
}
|
|
161
|
-
return JSON.stringify(value);
|
|
162
|
-
}
|
|
163
|
-
return value;
|
|
164
|
-
};
|
|
165
|
-
var remapToGraphQLSingleOutput = (queryOutput, tableName, table, relationMap) => {
|
|
166
|
-
for (const [key, value] of Object.entries(queryOutput)) {
|
|
167
|
-
if (value === void 0 || value === null) {
|
|
168
|
-
const relEntry = value === null ? relationMap?.[tableName]?.[key] : void 0;
|
|
169
|
-
if (relEntry && is(relEntry.relation, One)) {
|
|
170
|
-
queryOutput[key] = null;
|
|
171
|
-
continue;
|
|
172
|
-
}
|
|
173
|
-
delete queryOutput[key];
|
|
174
|
-
continue;
|
|
175
|
-
}
|
|
176
|
-
const column = table[key];
|
|
177
|
-
if (value === 0n && column && column.columnType === "SQLiteBigInt" && !column.notNull) {
|
|
178
|
-
delete queryOutput[key];
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
queryOutput[key] = remapToGraphQLCore(key, value, tableName, column, relationMap);
|
|
182
|
-
}
|
|
183
|
-
return queryOutput;
|
|
184
|
-
};
|
|
185
|
-
var remapToGraphQLArrayOutput = (queryOutput, tableName, table, relationMap) => {
|
|
186
|
-
for (const entry of queryOutput) {
|
|
187
|
-
remapToGraphQLSingleOutput(entry, tableName, table, relationMap);
|
|
188
|
-
}
|
|
189
|
-
return queryOutput;
|
|
190
|
-
};
|
|
191
|
-
var remapFromGraphQLCore = (value, column, columnName) => {
|
|
192
|
-
const dataType = column.dataType ?? "";
|
|
193
|
-
const columnType = column.columnType ?? "";
|
|
194
|
-
const isTimestampColumn = columnType === "SQLiteTimestamp" || columnType === "SQLiteTimestampMs" || columnType === "MySqlTimestamp" || columnType === "MySqlDateTime" || columnType === "PgTimestamp" || columnType === "PgTimestampString";
|
|
195
|
-
if (isTimestampColumn) {
|
|
196
|
-
const formatted = new Date(value);
|
|
197
|
-
if (Number.isNaN(formatted.getTime())) {
|
|
198
|
-
throw new GraphQLError(`Field '${columnName}' is not a valid date!`);
|
|
199
|
-
}
|
|
200
|
-
return formatted;
|
|
201
|
-
}
|
|
202
|
-
const isDateOnlyColumn = columnType === "MySqlDate" || columnType === "PgDate";
|
|
203
|
-
if (isDateOnlyColumn && typeof value === "string") {
|
|
204
|
-
const dateOnly = value.includes("T") ? value.split("T")[0] : value;
|
|
205
|
-
const check = new Date(dateOnly);
|
|
206
|
-
if (Number.isNaN(check.getTime())) {
|
|
207
|
-
throw new GraphQLError(`Field '${columnName}' is not a valid date!`);
|
|
208
|
-
}
|
|
209
|
-
return dateOnly;
|
|
210
|
-
}
|
|
211
|
-
if (dataType.includes("bigint")) {
|
|
212
|
-
try {
|
|
213
|
-
return BigInt(value);
|
|
214
|
-
} catch {
|
|
215
|
-
throw new GraphQLError(`Field '${columnName}' is not a BigInt!`);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
if (dataType.includes("json") && column.columnType !== "PgGeometryObject") {
|
|
219
|
-
return value;
|
|
220
|
-
}
|
|
221
|
-
switch (dataType) {
|
|
222
|
-
case "date": {
|
|
223
|
-
const formatted = new Date(value);
|
|
224
|
-
if (Number.isNaN(formatted.getTime())) {
|
|
225
|
-
throw new GraphQLError(`Field '${columnName}' is not a valid date!`);
|
|
226
|
-
}
|
|
227
|
-
return formatted;
|
|
228
|
-
}
|
|
229
|
-
case "buffer": {
|
|
230
|
-
if (!Array.isArray(value)) {
|
|
231
|
-
throw new GraphQLError(`Field '${columnName}' is not an array!`);
|
|
232
|
-
}
|
|
233
|
-
return Buffer.from(value);
|
|
234
|
-
}
|
|
235
|
-
case "json": {
|
|
236
|
-
if (column.columnType === "PgGeometryObject") {
|
|
237
|
-
return value;
|
|
238
|
-
}
|
|
239
|
-
try {
|
|
240
|
-
return JSON.parse(value);
|
|
241
|
-
} catch (e) {
|
|
242
|
-
throw new GraphQLError(
|
|
243
|
-
`Invalid JSON in field '${columnName}':
|
|
244
|
-
${e instanceof Error ? e.message : "Unknown error"}`
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
case "array": {
|
|
249
|
-
if (!Array.isArray(value)) {
|
|
250
|
-
throw new GraphQLError(`Field '${columnName}' is not an array!`);
|
|
251
|
-
}
|
|
252
|
-
if (column.columnType === "PgGeometry" && value.length !== 2) {
|
|
253
|
-
throw new GraphQLError(
|
|
254
|
-
`Invalid float tuple in field '${columnName}': expected array with length of 2, received ${value.length}`
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
return value;
|
|
258
|
-
}
|
|
259
|
-
case "bigint": {
|
|
260
|
-
try {
|
|
261
|
-
return BigInt(value);
|
|
262
|
-
} catch (_error) {
|
|
263
|
-
throw new GraphQLError(`Field '${columnName}' is not a BigInt!`);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
default: {
|
|
267
|
-
if (typeof value === "object" && value !== null && Object.getPrototypeOf(value) === null) {
|
|
268
|
-
return Object.assign({}, value);
|
|
269
|
-
}
|
|
270
|
-
return value;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
};
|
|
274
|
-
var remapFromGraphQLSingleInput = (queryInput, table) => {
|
|
275
|
-
for (const [key, value] of Object.entries(queryInput)) {
|
|
276
|
-
if (value === void 0) {
|
|
277
|
-
delete queryInput[key];
|
|
278
|
-
} else {
|
|
279
|
-
const column = getTableColumns(table)[key];
|
|
280
|
-
if (!column) {
|
|
281
|
-
throw new GraphQLError(`Unknown column: ${key}`);
|
|
282
|
-
}
|
|
283
|
-
if (value === null && column.notNull) {
|
|
284
|
-
delete queryInput[key];
|
|
285
|
-
continue;
|
|
286
|
-
}
|
|
287
|
-
queryInput[key] = remapFromGraphQLCore(value, column, key);
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
return queryInput;
|
|
291
|
-
};
|
|
292
|
-
var remapFromGraphQLArrayInput = (queryInput, table) => {
|
|
293
|
-
for (const entry of queryInput) {
|
|
294
|
-
remapFromGraphQLSingleInput(entry, table);
|
|
295
|
-
}
|
|
296
|
-
return queryInput;
|
|
297
|
-
};
|
|
108
|
+
import { getTableColumns as getTableColumns2, is as is2, One } from "drizzle-orm";
|
|
109
|
+
import { GraphQLError as GraphQLError2 } from "graphql";
|
|
110
|
+
|
|
111
|
+
// src/util/type-converter/index.ts
|
|
112
|
+
import { extractExtendedColumnType, getTableColumns, is } from "drizzle-orm";
|
|
113
|
+
import { MySqlInt, MySqlSerial } from "drizzle-orm/mysql-core";
|
|
114
|
+
import { PgDate, PgDateString, PgInteger, PgSerial, PgTimestamp, PgTimestampString, PgUUID } from "drizzle-orm/pg-core";
|
|
115
|
+
import { SQLiteInteger } from "drizzle-orm/sqlite-core";
|
|
116
|
+
import {
|
|
117
|
+
GraphQLBoolean,
|
|
118
|
+
GraphQLEnumType,
|
|
119
|
+
GraphQLFloat,
|
|
120
|
+
GraphQLInputObjectType,
|
|
121
|
+
GraphQLInt,
|
|
122
|
+
GraphQLList,
|
|
123
|
+
GraphQLNonNull,
|
|
124
|
+
GraphQLObjectType,
|
|
125
|
+
GraphQLScalarType as GraphQLScalarType2,
|
|
126
|
+
GraphQLString
|
|
127
|
+
} from "graphql";
|
|
298
128
|
|
|
299
129
|
// src/util/scalars/index.ts
|
|
300
|
-
import { GraphQLError
|
|
130
|
+
import { GraphQLError, GraphQLScalarType, Kind } from "graphql";
|
|
301
131
|
import { GraphQLDate, GraphQLDateTime, GraphQLJSON, GraphQLUUID } from "graphql-scalars";
|
|
302
132
|
var asDecimalString = (value) => {
|
|
303
133
|
if (typeof value === "bigint") {
|
|
@@ -305,10 +135,10 @@ var asDecimalString = (value) => {
|
|
|
305
135
|
}
|
|
306
136
|
if (typeof value === "number") {
|
|
307
137
|
if (!Number.isInteger(value)) {
|
|
308
|
-
throw new
|
|
138
|
+
throw new GraphQLError(`BigInt cannot represent non-integer value: ${value}`);
|
|
309
139
|
}
|
|
310
140
|
if (!Number.isSafeInteger(value)) {
|
|
311
|
-
throw new
|
|
141
|
+
throw new GraphQLError(
|
|
312
142
|
`BigInt cannot represent the number ${value} without precision loss \u2014 pass it as a string instead`
|
|
313
143
|
);
|
|
314
144
|
}
|
|
@@ -316,11 +146,11 @@ var asDecimalString = (value) => {
|
|
|
316
146
|
}
|
|
317
147
|
if (typeof value === "string") {
|
|
318
148
|
if (!/^-?\d+$/.test(value)) {
|
|
319
|
-
throw new
|
|
149
|
+
throw new GraphQLError(`BigInt cannot represent non-integer value: "${value}"`);
|
|
320
150
|
}
|
|
321
151
|
return value;
|
|
322
152
|
}
|
|
323
|
-
throw new
|
|
153
|
+
throw new GraphQLError(`BigInt cannot represent value: ${JSON.stringify(value)}`);
|
|
324
154
|
};
|
|
325
155
|
var GraphQLBigIntString = new GraphQLScalarType({
|
|
326
156
|
name: "BigInt",
|
|
@@ -329,7 +159,7 @@ var GraphQLBigIntString = new GraphQLScalarType({
|
|
|
329
159
|
parseValue: asDecimalString,
|
|
330
160
|
parseLiteral: (ast) => {
|
|
331
161
|
if (ast.kind !== Kind.STRING && ast.kind !== Kind.INT) {
|
|
332
|
-
throw new
|
|
162
|
+
throw new GraphQLError(`BigInt cannot represent a ${ast.kind}`, { nodes: ast });
|
|
333
163
|
}
|
|
334
164
|
return asDecimalString(ast.value);
|
|
335
165
|
}
|
|
@@ -341,17 +171,17 @@ var asNumericString = (value) => {
|
|
|
341
171
|
}
|
|
342
172
|
if (typeof value === "number") {
|
|
343
173
|
if (!Number.isFinite(value)) {
|
|
344
|
-
throw new
|
|
174
|
+
throw new GraphQLError(`Decimal cannot represent non-finite value: ${value}`);
|
|
345
175
|
}
|
|
346
176
|
return String(value);
|
|
347
177
|
}
|
|
348
178
|
if (typeof value === "string") {
|
|
349
179
|
if (!numericStringPattern.test(value)) {
|
|
350
|
-
throw new
|
|
180
|
+
throw new GraphQLError(`Decimal cannot represent non-numeric value: "${value}"`);
|
|
351
181
|
}
|
|
352
182
|
return value;
|
|
353
183
|
}
|
|
354
|
-
throw new
|
|
184
|
+
throw new GraphQLError(`Decimal cannot represent value: ${JSON.stringify(value)}`);
|
|
355
185
|
};
|
|
356
186
|
var GraphQLDecimalString = new GraphQLScalarType({
|
|
357
187
|
name: "Decimal",
|
|
@@ -360,28 +190,13 @@ var GraphQLDecimalString = new GraphQLScalarType({
|
|
|
360
190
|
parseValue: asNumericString,
|
|
361
191
|
parseLiteral: (ast) => {
|
|
362
192
|
if (ast.kind !== Kind.STRING && ast.kind !== Kind.INT && ast.kind !== Kind.FLOAT) {
|
|
363
|
-
throw new
|
|
193
|
+
throw new GraphQLError(`Decimal cannot represent a ${ast.kind}`, { nodes: ast });
|
|
364
194
|
}
|
|
365
195
|
return asNumericString(ast.value);
|
|
366
196
|
}
|
|
367
197
|
});
|
|
368
198
|
|
|
369
199
|
// src/util/type-converter/index.ts
|
|
370
|
-
import { extractExtendedColumnType, is as is2 } from "drizzle-orm";
|
|
371
|
-
import { MySqlInt, MySqlSerial } from "drizzle-orm/mysql-core";
|
|
372
|
-
import { PgDate, PgDateString, PgInteger, PgSerial, PgTimestamp, PgTimestampString, PgUUID } from "drizzle-orm/pg-core";
|
|
373
|
-
import { SQLiteInteger } from "drizzle-orm/sqlite-core";
|
|
374
|
-
import {
|
|
375
|
-
GraphQLBoolean,
|
|
376
|
-
GraphQLEnumType,
|
|
377
|
-
GraphQLFloat,
|
|
378
|
-
GraphQLInputObjectType,
|
|
379
|
-
GraphQLInt,
|
|
380
|
-
GraphQLList,
|
|
381
|
-
GraphQLNonNull,
|
|
382
|
-
GraphQLObjectType,
|
|
383
|
-
GraphQLString
|
|
384
|
-
} from "graphql";
|
|
385
200
|
var allowedNameChars = /^[a-zA-Z0-9_]+$/;
|
|
386
201
|
var enumMap = /* @__PURE__ */ new WeakMap();
|
|
387
202
|
var generateEnumCached = (column, columnName, tableName) => {
|
|
@@ -468,7 +283,7 @@ var columnBaseToGraphQLCore = (column, columnName, tableName, isInput) => {
|
|
|
468
283
|
case "bigint":
|
|
469
284
|
return { type: GraphQLBigIntString, description: "BigInt" };
|
|
470
285
|
case "number": {
|
|
471
|
-
return
|
|
286
|
+
return is(column, PgInteger) || is(column, PgSerial) || is(column, MySqlInt) || is(column, MySqlSerial) || is(column, SQLiteInteger) ? { type: GraphQLInt, description: "Integer" } : { type: GraphQLFloat, description: "Float" };
|
|
472
287
|
}
|
|
473
288
|
case "array": {
|
|
474
289
|
if (column.columnType === "PgVector") {
|
|
@@ -498,12 +313,65 @@ var columnBaseToGraphQLCore = (column, columnName, tableName, isInput) => {
|
|
|
498
313
|
throw new Error(`Drizzle-GraphQL Error: Type ${column.dataType} is not implemented!`);
|
|
499
314
|
}
|
|
500
315
|
};
|
|
316
|
+
var columnScalarOverrides = /* @__PURE__ */ new WeakMap();
|
|
317
|
+
var getColumnScalarOverride = (column, isInput) => {
|
|
318
|
+
const override = columnScalarOverrides.get(column);
|
|
319
|
+
return override ? isInput ? override.input : override.output : void 0;
|
|
320
|
+
};
|
|
321
|
+
var normalizeScalarOverride = (override, tableName, columnName) => {
|
|
322
|
+
const pair = override instanceof GraphQLScalarType2 ? { output: override, input: override } : { output: override.output, input: override.input };
|
|
323
|
+
for (const scalar of [pair.output, pair.input]) {
|
|
324
|
+
if (scalar !== void 0 && !(scalar instanceof GraphQLScalarType2)) {
|
|
325
|
+
throw new Error(
|
|
326
|
+
`Drizzle-GraphQL Error: Scalar override for column '${tableName}.${columnName}' must be a GraphQLScalarType!`
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return pair;
|
|
331
|
+
};
|
|
332
|
+
var registerScalarOverrides = (tables, config) => {
|
|
333
|
+
for (const [tableName, table] of Object.entries(tables)) {
|
|
334
|
+
for (const [columnName, column] of Object.entries(getTableColumns(table))) {
|
|
335
|
+
const declared = config.scalars?.[tableName]?.[columnName];
|
|
336
|
+
let resolved;
|
|
337
|
+
if (declared !== void 0) {
|
|
338
|
+
resolved = normalizeScalarOverride(declared, tableName, columnName);
|
|
339
|
+
} else if (config.mapColumnType) {
|
|
340
|
+
let defaultType;
|
|
341
|
+
let defaultInputType;
|
|
342
|
+
try {
|
|
343
|
+
defaultType = columnToGraphQLCore(column, columnName, tableName, false).type;
|
|
344
|
+
} catch {
|
|
345
|
+
}
|
|
346
|
+
try {
|
|
347
|
+
defaultInputType = columnToGraphQLCore(column, columnName, tableName, true).type;
|
|
348
|
+
} catch {
|
|
349
|
+
}
|
|
350
|
+
const mapped = config.mapColumnType(column, { tableName, columnName, defaultType, defaultInputType });
|
|
351
|
+
if (mapped !== void 0) {
|
|
352
|
+
resolved = normalizeScalarOverride(mapped, tableName, columnName);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (resolved && (resolved.output !== void 0 || resolved.input !== void 0)) {
|
|
356
|
+
columnScalarOverrides.set(column, resolved);
|
|
357
|
+
} else {
|
|
358
|
+
columnScalarOverrides.delete(column);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
};
|
|
501
363
|
var drizzleColumnToGraphQLType = (column, columnName, tableName, forceNullable = false, defaultIsNullable = false, isInput = false) => {
|
|
502
|
-
const
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
364
|
+
const override = getColumnScalarOverride(column, isInput);
|
|
365
|
+
let typeDesc;
|
|
366
|
+
if (override) {
|
|
367
|
+
typeDesc = { type: override, description: override.name };
|
|
368
|
+
} else {
|
|
369
|
+
typeDesc = columnToGraphQLCore(column, columnName, tableName, isInput);
|
|
370
|
+
const noDesc = ["string", "boolean", "number"];
|
|
371
|
+
const { type: baseType } = extractExtendedColumnType(column);
|
|
372
|
+
if (noDesc.find((e) => e === baseType)) {
|
|
373
|
+
delete typeDesc.description;
|
|
374
|
+
}
|
|
507
375
|
}
|
|
508
376
|
if (forceNullable) {
|
|
509
377
|
return typeDesc;
|
|
@@ -517,6 +385,206 @@ var drizzleColumnToGraphQLType = (column, columnName, tableName, forceNullable =
|
|
|
517
385
|
return typeDesc;
|
|
518
386
|
};
|
|
519
387
|
|
|
388
|
+
// src/util/data-mappers/index.ts
|
|
389
|
+
var isJsonColumn = (column) => (column.dataType ?? "").includes("json") && column.columnType !== "PgGeometryObject";
|
|
390
|
+
var remapToGraphQLCore = (key, value, tableName, column, relationMap) => {
|
|
391
|
+
if (Array.isArray(value)) {
|
|
392
|
+
const relations = relationMap?.[tableName];
|
|
393
|
+
if (relations?.[key]) {
|
|
394
|
+
const rel = relations[key];
|
|
395
|
+
return remapToGraphQLArrayOutput(
|
|
396
|
+
value,
|
|
397
|
+
rel.targetTableName,
|
|
398
|
+
rel.relation?.targetTable ?? rel.relation?.referencedTable,
|
|
399
|
+
relationMap
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (typeof value === "object" && value !== null) {
|
|
404
|
+
const relations = relationMap?.[tableName];
|
|
405
|
+
if (relations?.[key]) {
|
|
406
|
+
const rel = relations[key];
|
|
407
|
+
const remapped = remapToGraphQLSingleOutput(
|
|
408
|
+
value,
|
|
409
|
+
rel.targetTableName,
|
|
410
|
+
rel.relation?.targetTable ?? rel.relation?.referencedTable,
|
|
411
|
+
relationMap
|
|
412
|
+
);
|
|
413
|
+
return remapped;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (!column) {
|
|
417
|
+
return value;
|
|
418
|
+
}
|
|
419
|
+
if (getColumnScalarOverride(column, false)) {
|
|
420
|
+
return value;
|
|
421
|
+
}
|
|
422
|
+
if (isJsonColumn(column)) {
|
|
423
|
+
return value;
|
|
424
|
+
}
|
|
425
|
+
if (value instanceof Date) {
|
|
426
|
+
return value.toISOString();
|
|
427
|
+
}
|
|
428
|
+
if (value instanceof Buffer) {
|
|
429
|
+
return Array.from(value);
|
|
430
|
+
}
|
|
431
|
+
if (typeof value === "bigint") {
|
|
432
|
+
return value.toString();
|
|
433
|
+
}
|
|
434
|
+
if (Array.isArray(value)) {
|
|
435
|
+
if (column.columnType === "PgGeometry" || column.columnType === "PgVector") {
|
|
436
|
+
return value;
|
|
437
|
+
}
|
|
438
|
+
return value.map((arrVal) => remapToGraphQLCore(key, arrVal, tableName, column, relationMap));
|
|
439
|
+
}
|
|
440
|
+
if (typeof value === "object" && value !== null) {
|
|
441
|
+
if (column.columnType === "PgGeometryObject") {
|
|
442
|
+
return value;
|
|
443
|
+
}
|
|
444
|
+
return JSON.stringify(value);
|
|
445
|
+
}
|
|
446
|
+
return value;
|
|
447
|
+
};
|
|
448
|
+
var remapToGraphQLSingleOutput = (queryOutput, tableName, table, relationMap) => {
|
|
449
|
+
for (const [key, value] of Object.entries(queryOutput)) {
|
|
450
|
+
if (value === void 0 || value === null) {
|
|
451
|
+
const relEntry = value === null ? relationMap?.[tableName]?.[key] : void 0;
|
|
452
|
+
if (relEntry && is2(relEntry.relation, One)) {
|
|
453
|
+
queryOutput[key] = null;
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
delete queryOutput[key];
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
const column = table[key];
|
|
460
|
+
if (value === 0n && column && column.columnType === "SQLiteBigInt" && !column.notNull) {
|
|
461
|
+
delete queryOutput[key];
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
queryOutput[key] = remapToGraphQLCore(key, value, tableName, column, relationMap);
|
|
465
|
+
}
|
|
466
|
+
return queryOutput;
|
|
467
|
+
};
|
|
468
|
+
var remapToGraphQLArrayOutput = (queryOutput, tableName, table, relationMap) => {
|
|
469
|
+
for (const entry of queryOutput) {
|
|
470
|
+
remapToGraphQLSingleOutput(entry, tableName, table, relationMap);
|
|
471
|
+
}
|
|
472
|
+
return queryOutput;
|
|
473
|
+
};
|
|
474
|
+
var remapFromGraphQLCore = (value, column, columnName) => {
|
|
475
|
+
if (getColumnScalarOverride(column, true)) {
|
|
476
|
+
if (typeof value === "object" && value !== null && Object.getPrototypeOf(value) === null) {
|
|
477
|
+
return Object.assign({}, value);
|
|
478
|
+
}
|
|
479
|
+
return value;
|
|
480
|
+
}
|
|
481
|
+
const dataType = column.dataType ?? "";
|
|
482
|
+
const columnType = column.columnType ?? "";
|
|
483
|
+
const isTimestampColumn = columnType === "SQLiteTimestamp" || columnType === "SQLiteTimestampMs" || columnType === "MySqlTimestamp" || columnType === "MySqlDateTime" || columnType === "PgTimestamp" || columnType === "PgTimestampString";
|
|
484
|
+
if (isTimestampColumn) {
|
|
485
|
+
const formatted = new Date(value);
|
|
486
|
+
if (Number.isNaN(formatted.getTime())) {
|
|
487
|
+
throw new GraphQLError2(`Field '${columnName}' is not a valid date!`);
|
|
488
|
+
}
|
|
489
|
+
return formatted;
|
|
490
|
+
}
|
|
491
|
+
const isDateOnlyColumn = columnType === "MySqlDate" || columnType === "PgDate";
|
|
492
|
+
if (isDateOnlyColumn && typeof value === "string") {
|
|
493
|
+
const dateOnly = value.includes("T") ? value.split("T")[0] : value;
|
|
494
|
+
const check = new Date(dateOnly);
|
|
495
|
+
if (Number.isNaN(check.getTime())) {
|
|
496
|
+
throw new GraphQLError2(`Field '${columnName}' is not a valid date!`);
|
|
497
|
+
}
|
|
498
|
+
return dateOnly;
|
|
499
|
+
}
|
|
500
|
+
if (dataType.includes("bigint")) {
|
|
501
|
+
try {
|
|
502
|
+
return BigInt(value);
|
|
503
|
+
} catch {
|
|
504
|
+
throw new GraphQLError2(`Field '${columnName}' is not a BigInt!`);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
if (dataType.includes("json") && column.columnType !== "PgGeometryObject") {
|
|
508
|
+
return value;
|
|
509
|
+
}
|
|
510
|
+
switch (dataType) {
|
|
511
|
+
case "date": {
|
|
512
|
+
const formatted = new Date(value);
|
|
513
|
+
if (Number.isNaN(formatted.getTime())) {
|
|
514
|
+
throw new GraphQLError2(`Field '${columnName}' is not a valid date!`);
|
|
515
|
+
}
|
|
516
|
+
return formatted;
|
|
517
|
+
}
|
|
518
|
+
case "buffer": {
|
|
519
|
+
if (!Array.isArray(value)) {
|
|
520
|
+
throw new GraphQLError2(`Field '${columnName}' is not an array!`);
|
|
521
|
+
}
|
|
522
|
+
return Buffer.from(value);
|
|
523
|
+
}
|
|
524
|
+
case "json": {
|
|
525
|
+
if (column.columnType === "PgGeometryObject") {
|
|
526
|
+
return value;
|
|
527
|
+
}
|
|
528
|
+
try {
|
|
529
|
+
return JSON.parse(value);
|
|
530
|
+
} catch (e) {
|
|
531
|
+
throw new GraphQLError2(
|
|
532
|
+
`Invalid JSON in field '${columnName}':
|
|
533
|
+
${e instanceof Error ? e.message : "Unknown error"}`
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
case "array": {
|
|
538
|
+
if (!Array.isArray(value)) {
|
|
539
|
+
throw new GraphQLError2(`Field '${columnName}' is not an array!`);
|
|
540
|
+
}
|
|
541
|
+
if (column.columnType === "PgGeometry" && value.length !== 2) {
|
|
542
|
+
throw new GraphQLError2(
|
|
543
|
+
`Invalid float tuple in field '${columnName}': expected array with length of 2, received ${value.length}`
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
return value;
|
|
547
|
+
}
|
|
548
|
+
case "bigint": {
|
|
549
|
+
try {
|
|
550
|
+
return BigInt(value);
|
|
551
|
+
} catch (_error) {
|
|
552
|
+
throw new GraphQLError2(`Field '${columnName}' is not a BigInt!`);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
default: {
|
|
556
|
+
if (typeof value === "object" && value !== null && Object.getPrototypeOf(value) === null) {
|
|
557
|
+
return Object.assign({}, value);
|
|
558
|
+
}
|
|
559
|
+
return value;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
var remapFromGraphQLSingleInput = (queryInput, table) => {
|
|
564
|
+
for (const [key, value] of Object.entries(queryInput)) {
|
|
565
|
+
if (value === void 0) {
|
|
566
|
+
delete queryInput[key];
|
|
567
|
+
} else {
|
|
568
|
+
const column = getTableColumns2(table)[key];
|
|
569
|
+
if (!column) {
|
|
570
|
+
throw new GraphQLError2(`Unknown column: ${key}`);
|
|
571
|
+
}
|
|
572
|
+
if (value === null && column.notNull) {
|
|
573
|
+
delete queryInput[key];
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
queryInput[key] = remapFromGraphQLCore(value, column, key);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return queryInput;
|
|
580
|
+
};
|
|
581
|
+
var remapFromGraphQLArrayInput = (queryInput, table) => {
|
|
582
|
+
for (const entry of queryInput) {
|
|
583
|
+
remapFromGraphQLSingleInput(entry, table);
|
|
584
|
+
}
|
|
585
|
+
return queryInput;
|
|
586
|
+
};
|
|
587
|
+
|
|
520
588
|
// src/util/builders/common.ts
|
|
521
589
|
var rqbCrashTypes = ["SQLiteBigInt", "SQLiteBlobJson", "SQLiteBlobBuffer"];
|
|
522
590
|
var resolveTypeName = (name, typeNameMapper) => {
|
|
@@ -1095,9 +1163,31 @@ var arrayFilterFields = (colType, colArr, colDesc) => {
|
|
|
1095
1163
|
};
|
|
1096
1164
|
};
|
|
1097
1165
|
var FILTERS_WITHOUT_STRING_OPS = /* @__PURE__ */ new Set(["Id", "Int", "Float", "BigInt", "Decimal"]);
|
|
1166
|
+
var BUILTIN_FILTER_SCALARS = /* @__PURE__ */ new Set([
|
|
1167
|
+
GraphQLString2,
|
|
1168
|
+
GraphQLInt2,
|
|
1169
|
+
GraphQLFloat2,
|
|
1170
|
+
GraphQLBoolean2,
|
|
1171
|
+
GraphQLBigIntString,
|
|
1172
|
+
GraphQLDecimalString,
|
|
1173
|
+
GraphQLUUID,
|
|
1174
|
+
GraphQLJSON,
|
|
1175
|
+
GraphQLDate,
|
|
1176
|
+
GraphQLDateTime
|
|
1177
|
+
]);
|
|
1178
|
+
var resolveOverrideFilterDescriptor = (scalar) => {
|
|
1179
|
+
if (scalar === GraphQLJSON) {
|
|
1180
|
+
return { name: "JSON", kind: "json" };
|
|
1181
|
+
}
|
|
1182
|
+
if (scalar === GraphQLUUID) {
|
|
1183
|
+
return { name: "Id", kind: "scalar" };
|
|
1184
|
+
}
|
|
1185
|
+
return { name: scalar.name, kind: "scalar" };
|
|
1186
|
+
};
|
|
1098
1187
|
var generateColumnFilterValues = (column, tableName, columnName, cacheCtx) => {
|
|
1099
1188
|
const columnGraphQLType = drizzleColumnToGraphQLType(column, columnName, tableName, true, false, true);
|
|
1100
|
-
const
|
|
1189
|
+
const inputOverride = getColumnScalarOverride(column, true);
|
|
1190
|
+
const { name: genericName, kind } = inputOverride ? resolveOverrideFilterDescriptor(inputOverride) : resolveGenericFilterDescriptor(column, columnGraphQLType);
|
|
1101
1191
|
const cached = cacheCtx.genericFilterCache.get(genericName);
|
|
1102
1192
|
if (cached) {
|
|
1103
1193
|
return cached;
|
|
@@ -1105,7 +1195,9 @@ var generateColumnFilterValues = (column, tableName, columnName, cacheCtx) => {
|
|
|
1105
1195
|
const colType = columnGraphQLType.type;
|
|
1106
1196
|
const colDesc = columnGraphQLType.description;
|
|
1107
1197
|
const colArr = new GraphQLList2(new GraphQLNonNull2(colType));
|
|
1108
|
-
const
|
|
1198
|
+
const customOverrideFilter = inputOverride !== void 0 && !BUILTIN_FILTER_SCALARS.has(inputOverride);
|
|
1199
|
+
const underlying = extractExtendedColumnType2(column);
|
|
1200
|
+
const omitStringOps = customOverrideFilter ? underlying.type !== "string" || underlying.constraint === "numeric" : FILTERS_WITHOUT_STRING_OPS.has(genericName);
|
|
1109
1201
|
const baseFields = kind === "json" ? jsonFilterFields(column, colType) : kind === "array" ? arrayFilterFields(colType, colArr, colDesc) : {
|
|
1110
1202
|
eq: { type: colType, description: colDesc },
|
|
1111
1203
|
ne: { type: colType, description: colDesc },
|
|
@@ -1186,10 +1278,9 @@ var generateTableOrderCached = (table) => {
|
|
|
1186
1278
|
}
|
|
1187
1279
|
return remapped;
|
|
1188
1280
|
};
|
|
1189
|
-
var filterMap = /* @__PURE__ */ new WeakMap();
|
|
1190
1281
|
var generateTableFilterValuesCached = (table, tableName, cacheCtx) => {
|
|
1191
|
-
if (
|
|
1192
|
-
return
|
|
1282
|
+
if (cacheCtx.filterFieldCache.has(table)) {
|
|
1283
|
+
return cacheCtx.filterFieldCache.get(table);
|
|
1193
1284
|
}
|
|
1194
1285
|
const columns = getColumns(table);
|
|
1195
1286
|
const columnEntries = Object.entries(columns);
|
|
@@ -1201,13 +1292,12 @@ var generateTableFilterValuesCached = (table, tableName, cacheCtx) => {
|
|
|
1201
1292
|
}
|
|
1202
1293
|
])
|
|
1203
1294
|
);
|
|
1204
|
-
|
|
1295
|
+
cacheCtx.filterFieldCache.set(table, remapped);
|
|
1205
1296
|
return remapped;
|
|
1206
1297
|
};
|
|
1207
|
-
var
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
return fieldMap.get(table);
|
|
1298
|
+
var generateTableSelectTypeFieldsCached = (table, tableName, cacheCtx) => {
|
|
1299
|
+
if (cacheCtx.selectFieldCache.has(table)) {
|
|
1300
|
+
return cacheCtx.selectFieldCache.get(table);
|
|
1211
1301
|
}
|
|
1212
1302
|
const columns = getColumns(table);
|
|
1213
1303
|
const columnEntries = Object.entries(columns);
|
|
@@ -1224,7 +1314,7 @@ var generateTableSelectTypeFieldsCached = (table, tableName) => {
|
|
|
1224
1314
|
resolve: rowCursorResolver
|
|
1225
1315
|
};
|
|
1226
1316
|
}
|
|
1227
|
-
|
|
1317
|
+
cacheCtx.selectFieldCache.set(table, remapped);
|
|
1228
1318
|
return remapped;
|
|
1229
1319
|
};
|
|
1230
1320
|
var isFilterableRelation = (relation) => !relation.through;
|
|
@@ -1374,7 +1464,7 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
1374
1464
|
const table = tables[tableName];
|
|
1375
1465
|
const order = withOrder ? generateTableOrderTypeCached(table, tableName, typeNameMapper, cacheCtx, relationMap, tables) : void 0;
|
|
1376
1466
|
const filters = generateTableFilterTypeCached(table, tableName, cacheCtx, typeNameMapper, relationMap, tables);
|
|
1377
|
-
const tableFields = generateTableSelectTypeFieldsCached(table, tableName);
|
|
1467
|
+
const tableFields = generateTableSelectTypeFieldsCached(table, tableName, cacheCtx);
|
|
1378
1468
|
const relationsForTable = relationMap[tableName];
|
|
1379
1469
|
const relationEntries = relationsForTable ? Object.entries(relationsForTable) : [];
|
|
1380
1470
|
if (relationsDepthLimit !== void 0 && currentDepth >= relationsDepthLimit) {
|
|
@@ -1443,7 +1533,7 @@ var generateSelectFields = (tables, tableName, relationMap, fromTableName, fromR
|
|
|
1443
1533
|
let relType = cacheCtx.objectTypeCache.get(targetTableName);
|
|
1444
1534
|
if (!relType) {
|
|
1445
1535
|
const targetTable = tables[targetTableName];
|
|
1446
|
-
const targetTableFields = generateTableSelectTypeFieldsCached(targetTable, targetTableName);
|
|
1536
|
+
const targetTableFields = generateTableSelectTypeFieldsCached(targetTable, targetTableName, cacheCtx);
|
|
1447
1537
|
let targetContainer = cacheCtx.relationFieldContainers.get(targetTableName);
|
|
1448
1538
|
if (!targetContainer) {
|
|
1449
1539
|
targetContainer = { fields: {} };
|
|
@@ -2706,7 +2796,7 @@ import {
|
|
|
2706
2796
|
count,
|
|
2707
2797
|
countDistinct,
|
|
2708
2798
|
eq as eq2,
|
|
2709
|
-
extractExtendedColumnType as
|
|
2799
|
+
extractExtendedColumnType as extractExtendedColumnType3,
|
|
2710
2800
|
getColumns as getColumns2,
|
|
2711
2801
|
gt as gt2,
|
|
2712
2802
|
gte as gte2,
|
|
@@ -2750,7 +2840,7 @@ var classifyAggregateColumns = (table, tableName) => {
|
|
|
2750
2840
|
if (gqlType instanceof GraphQLList3 || gqlType instanceof GraphQLObjectType3) {
|
|
2751
2841
|
continue;
|
|
2752
2842
|
}
|
|
2753
|
-
const { type: dataType, constraint } =
|
|
2843
|
+
const { type: dataType, constraint } = extractExtendedColumnType3(column);
|
|
2754
2844
|
if (dataType === "boolean" || dataType === "array" || dataType === "custom") {
|
|
2755
2845
|
continue;
|
|
2756
2846
|
}
|
|
@@ -2991,7 +3081,7 @@ var groupableColumns = (table, tableName) => {
|
|
|
2991
3081
|
if (groupable[columnName]) {
|
|
2992
3082
|
continue;
|
|
2993
3083
|
}
|
|
2994
|
-
const { type: dataType } =
|
|
3084
|
+
const { type: dataType } = extractExtendedColumnType3(column);
|
|
2995
3085
|
if (dataType !== "boolean") {
|
|
2996
3086
|
continue;
|
|
2997
3087
|
}
|
|
@@ -3458,6 +3548,7 @@ var generateSchemaData = (db, schema, relations, options) => {
|
|
|
3458
3548
|
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
3459
3549
|
);
|
|
3460
3550
|
}
|
|
3551
|
+
registerScalarOverrides(tables, options);
|
|
3461
3552
|
const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
|
|
3462
3553
|
attachTargetPrimaryKeys(namedRelations, tables, mysqlPrimaryKeyPropNames);
|
|
3463
3554
|
const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
|
|
@@ -3469,6 +3560,8 @@ var generateSchemaData = (db, schema, relations, options) => {
|
|
|
3469
3560
|
relationFieldContainers: /* @__PURE__ */ new Map(),
|
|
3470
3561
|
fullyBuiltTables: /* @__PURE__ */ new Set(),
|
|
3471
3562
|
relationTypeCache: /* @__PURE__ */ new Map(),
|
|
3563
|
+
selectFieldCache: /* @__PURE__ */ new WeakMap(),
|
|
3564
|
+
filterFieldCache: /* @__PURE__ */ new WeakMap(),
|
|
3472
3565
|
orderTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
3473
3566
|
filterTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
3474
3567
|
listRelationFilterCache: /* @__PURE__ */ new Map(),
|
|
@@ -4297,6 +4390,7 @@ function generateSchemaData2(db, schema, relations, options) {
|
|
|
4297
4390
|
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
4298
4391
|
);
|
|
4299
4392
|
}
|
|
4393
|
+
registerScalarOverrides(tables, options);
|
|
4300
4394
|
const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
|
|
4301
4395
|
attachTargetPrimaryKeys(namedRelations, tables, pgPrimaryKeyPropNames);
|
|
4302
4396
|
const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
|
|
@@ -4308,6 +4402,8 @@ function generateSchemaData2(db, schema, relations, options) {
|
|
|
4308
4402
|
relationFieldContainers: /* @__PURE__ */ new Map(),
|
|
4309
4403
|
fullyBuiltTables: /* @__PURE__ */ new Set(),
|
|
4310
4404
|
relationTypeCache: /* @__PURE__ */ new Map(),
|
|
4405
|
+
selectFieldCache: /* @__PURE__ */ new WeakMap(),
|
|
4406
|
+
filterFieldCache: /* @__PURE__ */ new WeakMap(),
|
|
4311
4407
|
orderTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
4312
4408
|
filterTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
4313
4409
|
listRelationFilterCache: /* @__PURE__ */ new Map(),
|
|
@@ -5128,6 +5224,7 @@ var generateSchemaData3 = (db, schema, relations, options) => {
|
|
|
5128
5224
|
"Drizzle-GraphQL Error: No tables detected in Drizzle-ORM's database instance. Did you forget to pass schema to drizzle constructor?"
|
|
5129
5225
|
);
|
|
5130
5226
|
}
|
|
5227
|
+
registerScalarOverrides(tables, options);
|
|
5131
5228
|
const namedRelations = buildNamedRelations(relations ?? {}, tableEntries);
|
|
5132
5229
|
attachTargetPrimaryKeys(namedRelations, tables, sqlitePrimaryKeyPropNames);
|
|
5133
5230
|
const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
|
|
@@ -5139,6 +5236,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
|
|
|
5139
5236
|
relationFieldContainers: /* @__PURE__ */ new Map(),
|
|
5140
5237
|
fullyBuiltTables: /* @__PURE__ */ new Set(),
|
|
5141
5238
|
relationTypeCache: /* @__PURE__ */ new Map(),
|
|
5239
|
+
selectFieldCache: /* @__PURE__ */ new WeakMap(),
|
|
5240
|
+
filterFieldCache: /* @__PURE__ */ new WeakMap(),
|
|
5142
5241
|
orderTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
5143
5242
|
filterTypeCache: /* @__PURE__ */ new WeakMap(),
|
|
5144
5243
|
listRelationFilterCache: /* @__PURE__ */ new Map(),
|
|
@@ -5584,6 +5683,8 @@ var buildSchema = (db, config) => {
|
|
|
5584
5683
|
shouldEagerLoad,
|
|
5585
5684
|
features,
|
|
5586
5685
|
complexity,
|
|
5686
|
+
scalars: config?.scalars,
|
|
5687
|
+
mapColumnType: config?.mapColumnType,
|
|
5587
5688
|
transactions
|
|
5588
5689
|
};
|
|
5589
5690
|
let generatorOutput;
|