drizzle-graphql-plus 0.8.14 → 0.8.15
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/index.cjs +762 -8
- package/index.cjs.map +1 -1
- package/index.d.cts +57 -2
- package/index.d.ts +57 -2
- package/index.js +781 -6
- package/index.js.map +1 -1
- package/package.json +8 -4
package/index.cjs
CHANGED
|
@@ -7,11 +7,11 @@ var __export = (target, all) => {
|
|
|
7
7
|
for (var name in all)
|
|
8
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
9
|
};
|
|
10
|
-
var __copyProps = (to, from, except,
|
|
10
|
+
var __copyProps = (to, from, except, desc3) => {
|
|
11
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
12
|
for (let key of __getOwnPropNames(from))
|
|
13
13
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc3 = __getOwnPropDesc(from, key)) || desc3.enumerable });
|
|
15
15
|
}
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
@@ -20,9 +20,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
-
buildSchema: () => buildSchema
|
|
23
|
+
buildSchema: () => buildSchema,
|
|
24
|
+
buildSchemaSDL: () => buildSchemaSDL,
|
|
25
|
+
setCustomGraphQL: () => setCustomGraphQL,
|
|
26
|
+
setCustomGraphQLTypes: () => setCustomGraphQLTypes
|
|
24
27
|
});
|
|
25
28
|
module.exports = __toCommonJS(src_exports);
|
|
29
|
+
|
|
30
|
+
// src/buildSchema.ts
|
|
26
31
|
var import_drizzle_orm7 = require("drizzle-orm");
|
|
27
32
|
var import_mysql_core3 = require("drizzle-orm/mysql-core");
|
|
28
33
|
var import_pg_core3 = require("drizzle-orm/pg-core");
|
|
@@ -287,9 +292,6 @@ var extractSelectedColumnsFromTree = (tree, table) => {
|
|
|
287
292
|
fieldData.fieldsByTypeName
|
|
288
293
|
)) {
|
|
289
294
|
for (const [subFieldName, subFieldData] of Object.entries(typeFields)) {
|
|
290
|
-
console.log(
|
|
291
|
-
` Checking subField: ${subFieldName}, name: ${subFieldData.name}`
|
|
292
|
-
);
|
|
293
295
|
if (tableColumns[subFieldData.name]) {
|
|
294
296
|
selectedColumns.push([subFieldData.name, true]);
|
|
295
297
|
} else {
|
|
@@ -2146,7 +2148,7 @@ var generateSchemaData3 = (db, schema, relationsDepthLimit) => {
|
|
|
2146
2148
|
return { queries, mutations, inputs, interfaces, types: outputs };
|
|
2147
2149
|
};
|
|
2148
2150
|
|
|
2149
|
-
// src/
|
|
2151
|
+
// src/buildSchema.ts
|
|
2150
2152
|
var buildSchema = (db, config) => {
|
|
2151
2153
|
const schema = db._.fullSchema;
|
|
2152
2154
|
if (!schema) {
|
|
@@ -2193,8 +2195,760 @@ var buildSchema = (db, config) => {
|
|
|
2193
2195
|
const outputSchema = new import_graphql7.GraphQLSchema(graphQLSchemaConfig);
|
|
2194
2196
|
return { schema: outputSchema, entities: generatorOutput };
|
|
2195
2197
|
};
|
|
2198
|
+
|
|
2199
|
+
// src/buildSchemaSDL/index.ts
|
|
2200
|
+
var import_drizzle_orm11 = require("drizzle-orm");
|
|
2201
|
+
var import_sqlite_core5 = require("drizzle-orm/sqlite-core");
|
|
2202
|
+
|
|
2203
|
+
// src/buildSchemaSDL/generator/schema.ts
|
|
2204
|
+
var import_drizzle_orm8 = require("drizzle-orm");
|
|
2205
|
+
var import_mysql_core4 = require("drizzle-orm/mysql-core");
|
|
2206
|
+
var import_pg_core4 = require("drizzle-orm/pg-core");
|
|
2207
|
+
var import_sqlite_core4 = require("drizzle-orm/sqlite-core");
|
|
2208
|
+
var allowedNameChars2 = /^[a-zA-Z0-9_]+$/;
|
|
2209
|
+
var customScalars = /* @__PURE__ */ new Set();
|
|
2210
|
+
var enumDefinitions = /* @__PURE__ */ new Map();
|
|
2211
|
+
var columnToSDL = (column, columnName, tableName, forceNullable = false) => {
|
|
2212
|
+
let baseType;
|
|
2213
|
+
if (column.customGraphqlType) {
|
|
2214
|
+
baseType = column.customGraphqlType;
|
|
2215
|
+
} else {
|
|
2216
|
+
switch (column.dataType) {
|
|
2217
|
+
case "boolean":
|
|
2218
|
+
baseType = "Boolean";
|
|
2219
|
+
break;
|
|
2220
|
+
case "json":
|
|
2221
|
+
if (column.columnType === "PgGeometryObject") {
|
|
2222
|
+
baseType = "PgGeometryObject";
|
|
2223
|
+
customScalars.add("PgGeometryObject");
|
|
2224
|
+
} else {
|
|
2225
|
+
baseType = "JSON";
|
|
2226
|
+
customScalars.add("JSON");
|
|
2227
|
+
}
|
|
2228
|
+
break;
|
|
2229
|
+
case "date":
|
|
2230
|
+
baseType = "Date";
|
|
2231
|
+
customScalars.add("Date");
|
|
2232
|
+
break;
|
|
2233
|
+
case "string":
|
|
2234
|
+
if (column.enumValues?.length) {
|
|
2235
|
+
const enumName = `${capitalize(tableName)}${capitalize(
|
|
2236
|
+
columnName
|
|
2237
|
+
)}Enum`;
|
|
2238
|
+
baseType = enumName;
|
|
2239
|
+
if (!enumDefinitions.has(enumName)) {
|
|
2240
|
+
enumDefinitions.set(enumName, {
|
|
2241
|
+
name: enumName,
|
|
2242
|
+
values: column.enumValues.map(
|
|
2243
|
+
(e, index) => allowedNameChars2.test(e) ? e : `Option${index}`
|
|
2244
|
+
)
|
|
2245
|
+
});
|
|
2246
|
+
}
|
|
2247
|
+
} else {
|
|
2248
|
+
baseType = "String";
|
|
2249
|
+
}
|
|
2250
|
+
break;
|
|
2251
|
+
case "bigint":
|
|
2252
|
+
baseType = "BigInt";
|
|
2253
|
+
customScalars.add("BigInt");
|
|
2254
|
+
break;
|
|
2255
|
+
case "number":
|
|
2256
|
+
if ((0, import_drizzle_orm8.is)(column, import_pg_core4.PgInteger) || (0, import_drizzle_orm8.is)(column, import_pg_core4.PgSerial) || (0, import_drizzle_orm8.is)(column, import_mysql_core4.MySqlInt) || (0, import_drizzle_orm8.is)(column, import_mysql_core4.MySqlSerial) || (0, import_drizzle_orm8.is)(column, import_sqlite_core4.SQLiteInteger)) {
|
|
2257
|
+
baseType = "Int";
|
|
2258
|
+
} else {
|
|
2259
|
+
baseType = "Float";
|
|
2260
|
+
}
|
|
2261
|
+
break;
|
|
2262
|
+
case "buffer":
|
|
2263
|
+
baseType = "[Int!]";
|
|
2264
|
+
break;
|
|
2265
|
+
case "array":
|
|
2266
|
+
if (column.columnType === "PgVector") {
|
|
2267
|
+
baseType = "[Float!]";
|
|
2268
|
+
} else if (column.columnType === "PgGeometry") {
|
|
2269
|
+
baseType = "[Float!]";
|
|
2270
|
+
} else {
|
|
2271
|
+
const scalarName = `${capitalize(tableName)}${capitalize(
|
|
2272
|
+
columnName
|
|
2273
|
+
)}Array`;
|
|
2274
|
+
baseType = scalarName;
|
|
2275
|
+
customScalars.add(scalarName);
|
|
2276
|
+
}
|
|
2277
|
+
break;
|
|
2278
|
+
case "custom":
|
|
2279
|
+
default:
|
|
2280
|
+
if (column.columnType) {
|
|
2281
|
+
baseType = column.columnType;
|
|
2282
|
+
customScalars.add(column.columnType);
|
|
2283
|
+
} else {
|
|
2284
|
+
const customScalarName = `${capitalize(tableName)}${capitalize(
|
|
2285
|
+
columnName
|
|
2286
|
+
)}`;
|
|
2287
|
+
baseType = customScalarName;
|
|
2288
|
+
customScalars.add(customScalarName);
|
|
2289
|
+
}
|
|
2290
|
+
break;
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
if (!forceNullable && column.notNull && !column.hasDefault && !column.defaultFn) {
|
|
2294
|
+
return `${baseType}!`;
|
|
2295
|
+
}
|
|
2296
|
+
return baseType;
|
|
2297
|
+
};
|
|
2298
|
+
var generateTypes = (db, schema) => {
|
|
2299
|
+
const tables = {};
|
|
2300
|
+
const schemaEntries = Object.entries(schema);
|
|
2301
|
+
const tableEntries = [];
|
|
2302
|
+
for (const [key, value] of schemaEntries) {
|
|
2303
|
+
if (value && typeof value === "object" && "getSQL" in value) {
|
|
2304
|
+
const table = value;
|
|
2305
|
+
const tableName = (0, import_drizzle_orm8.getTableName)(table);
|
|
2306
|
+
const columns = (0, import_drizzle_orm8.getTableColumns)(table);
|
|
2307
|
+
tables[tableName] = {
|
|
2308
|
+
name: tableName,
|
|
2309
|
+
table,
|
|
2310
|
+
columns
|
|
2311
|
+
};
|
|
2312
|
+
tableEntries.push([tableName, table]);
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm8.is)(value, import_drizzle_orm8.Relations)).map(([key, value]) => [
|
|
2316
|
+
tableEntries.find(
|
|
2317
|
+
([tableName, tableValue]) => tableValue === value.table
|
|
2318
|
+
)[0],
|
|
2319
|
+
value
|
|
2320
|
+
]).map(([tableName, relValue]) => [
|
|
2321
|
+
tableName,
|
|
2322
|
+
relValue.config((0, import_drizzle_orm8.createTableRelationsHelpers)(tables[tableName].table))
|
|
2323
|
+
]);
|
|
2324
|
+
const namedRelations = Object.fromEntries(
|
|
2325
|
+
rawRelations.map(([relName, config]) => {
|
|
2326
|
+
const namedConfig = Object.fromEntries(
|
|
2327
|
+
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
2328
|
+
innerRelName,
|
|
2329
|
+
{
|
|
2330
|
+
relation: innerRelValue,
|
|
2331
|
+
targetTableName: tableEntries.find(
|
|
2332
|
+
([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
|
|
2333
|
+
)[0]
|
|
2334
|
+
}
|
|
2335
|
+
])
|
|
2336
|
+
);
|
|
2337
|
+
return [relName, namedConfig];
|
|
2338
|
+
})
|
|
2339
|
+
);
|
|
2340
|
+
return { tables, relations: namedRelations };
|
|
2341
|
+
};
|
|
2342
|
+
var generateTypeDefs = (tables, relations) => {
|
|
2343
|
+
const typeDefs = [];
|
|
2344
|
+
customScalars.clear();
|
|
2345
|
+
enumDefinitions.clear();
|
|
2346
|
+
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
2347
|
+
const typeName = capitalize(tableName);
|
|
2348
|
+
const fields = [];
|
|
2349
|
+
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
2350
|
+
const typeStr = columnToSDL(
|
|
2351
|
+
column,
|
|
2352
|
+
columnName,
|
|
2353
|
+
tableName,
|
|
2354
|
+
false
|
|
2355
|
+
);
|
|
2356
|
+
const description = column.customGraphqlDescription;
|
|
2357
|
+
if (description) {
|
|
2358
|
+
fields.push(` """${description}"""`);
|
|
2359
|
+
}
|
|
2360
|
+
fields.push(` ${columnName}: ${typeStr}`);
|
|
2361
|
+
}
|
|
2362
|
+
const tableRelations = relations[tableName];
|
|
2363
|
+
if (tableRelations) {
|
|
2364
|
+
for (const [relationName, relationInfo] of Object.entries(
|
|
2365
|
+
tableRelations
|
|
2366
|
+
)) {
|
|
2367
|
+
const isOne = (0, import_drizzle_orm8.is)(relationInfo.relation, import_drizzle_orm8.One);
|
|
2368
|
+
const targetTableName = relationInfo.targetTableName;
|
|
2369
|
+
const targetTypeName = capitalize(targetTableName);
|
|
2370
|
+
if (isOne) {
|
|
2371
|
+
fields.push(
|
|
2372
|
+
` ${relationName}(where: ${targetTypeName}Filters): ${targetTypeName}`
|
|
2373
|
+
);
|
|
2374
|
+
} else {
|
|
2375
|
+
fields.push(
|
|
2376
|
+
` ${relationName}(where: ${targetTypeName}Filters, orderBy: ${targetTypeName}OrderBy, limit: Int, offset: Int): [${targetTypeName}!]!`
|
|
2377
|
+
);
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
}
|
|
2381
|
+
typeDefs.push(`type ${typeName} {
|
|
2382
|
+
${fields.join("\n")}
|
|
2383
|
+
}`);
|
|
2384
|
+
const insertFields = [];
|
|
2385
|
+
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
2386
|
+
if (column.hasDefault || column.columnType === "SQLiteSerial") {
|
|
2387
|
+
continue;
|
|
2388
|
+
}
|
|
2389
|
+
const typeStr = columnToSDL(
|
|
2390
|
+
column,
|
|
2391
|
+
columnName,
|
|
2392
|
+
tableName,
|
|
2393
|
+
false
|
|
2394
|
+
);
|
|
2395
|
+
const nullableType = typeStr.endsWith("!") ? typeStr.slice(0, -1) : typeStr;
|
|
2396
|
+
insertFields.push(` ${columnName}: ${nullableType}`);
|
|
2397
|
+
}
|
|
2398
|
+
if (insertFields.length > 0) {
|
|
2399
|
+
typeDefs.push(
|
|
2400
|
+
`input ${typeName}InsertInput {
|
|
2401
|
+
${insertFields.join("\n")}
|
|
2402
|
+
}`
|
|
2403
|
+
);
|
|
2404
|
+
}
|
|
2405
|
+
const updateFields = [];
|
|
2406
|
+
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
2407
|
+
const typeStr = columnToSDL(
|
|
2408
|
+
column,
|
|
2409
|
+
columnName,
|
|
2410
|
+
tableName,
|
|
2411
|
+
true
|
|
2412
|
+
);
|
|
2413
|
+
updateFields.push(` ${columnName}: ${typeStr}`);
|
|
2414
|
+
}
|
|
2415
|
+
typeDefs.push(
|
|
2416
|
+
`input ${typeName}UpdateInput {
|
|
2417
|
+
${updateFields.join("\n")}
|
|
2418
|
+
}`
|
|
2419
|
+
);
|
|
2420
|
+
for (const [columnName, column] of Object.entries(tableInfo.columns)) {
|
|
2421
|
+
const typeStr = columnToSDL(
|
|
2422
|
+
column,
|
|
2423
|
+
columnName,
|
|
2424
|
+
tableName,
|
|
2425
|
+
true
|
|
2426
|
+
);
|
|
2427
|
+
const filterName = `${typeName}${capitalize(columnName)}Filters`;
|
|
2428
|
+
const filterFields = [];
|
|
2429
|
+
filterFields.push(` eq: ${typeStr}`);
|
|
2430
|
+
filterFields.push(` ne: ${typeStr}`);
|
|
2431
|
+
filterFields.push(` lt: ${typeStr}`);
|
|
2432
|
+
filterFields.push(` lte: ${typeStr}`);
|
|
2433
|
+
filterFields.push(` gt: ${typeStr}`);
|
|
2434
|
+
filterFields.push(` gte: ${typeStr}`);
|
|
2435
|
+
filterFields.push(` like: String`);
|
|
2436
|
+
filterFields.push(` notLike: String`);
|
|
2437
|
+
filterFields.push(` ilike: String`);
|
|
2438
|
+
filterFields.push(` notIlike: String`);
|
|
2439
|
+
filterFields.push(` inArray: [${typeStr}!]`);
|
|
2440
|
+
filterFields.push(` notInArray: [${typeStr}!]`);
|
|
2441
|
+
filterFields.push(` isNull: Boolean`);
|
|
2442
|
+
filterFields.push(` isNotNull: Boolean`);
|
|
2443
|
+
const orFilterName = `${typeName}${capitalize(columnName)}FiltersOr`;
|
|
2444
|
+
typeDefs.push(`input ${orFilterName} {
|
|
2445
|
+
${filterFields.join("\n")}
|
|
2446
|
+
}`);
|
|
2447
|
+
filterFields.push(` OR: [${orFilterName}!]`);
|
|
2448
|
+
typeDefs.push(`input ${filterName} {
|
|
2449
|
+
${filterFields.join("\n")}
|
|
2450
|
+
}`);
|
|
2451
|
+
}
|
|
2452
|
+
const whereFields = [];
|
|
2453
|
+
for (const columnName of Object.keys(tableInfo.columns)) {
|
|
2454
|
+
const filterName = `${typeName}${capitalize(columnName)}Filters`;
|
|
2455
|
+
whereFields.push(` ${columnName}: ${filterName}`);
|
|
2456
|
+
}
|
|
2457
|
+
whereFields.push(` OR: [${typeName}FiltersOr!]`);
|
|
2458
|
+
const filtersOrFields = [];
|
|
2459
|
+
for (const columnName of Object.keys(tableInfo.columns)) {
|
|
2460
|
+
const filterName = `${typeName}${capitalize(columnName)}Filters`;
|
|
2461
|
+
filtersOrFields.push(` ${columnName}: ${filterName}`);
|
|
2462
|
+
}
|
|
2463
|
+
typeDefs.push(
|
|
2464
|
+
`input ${typeName}FiltersOr {
|
|
2465
|
+
${filtersOrFields.join("\n")}
|
|
2466
|
+
}`
|
|
2467
|
+
);
|
|
2468
|
+
typeDefs.push(`input ${typeName}Filters {
|
|
2469
|
+
${whereFields.join("\n")}
|
|
2470
|
+
}`);
|
|
2471
|
+
const orderByFields = [];
|
|
2472
|
+
for (const columnName of Object.keys(tableInfo.columns)) {
|
|
2473
|
+
orderByFields.push(` ${columnName}: InnerOrder`);
|
|
2474
|
+
}
|
|
2475
|
+
typeDefs.push(`input ${typeName}OrderBy {
|
|
2476
|
+
${orderByFields.join("\n")}
|
|
2477
|
+
}`);
|
|
2478
|
+
}
|
|
2479
|
+
const allDefs = [];
|
|
2480
|
+
if (customScalars.size > 0) {
|
|
2481
|
+
for (const scalarName of Array.from(customScalars).sort()) {
|
|
2482
|
+
allDefs.push(`scalar ${scalarName}`);
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
if (enumDefinitions.size > 0) {
|
|
2486
|
+
for (const enumDef of enumDefinitions.values()) {
|
|
2487
|
+
const valueStrings = enumDef.values.map((v) => ` ${v}`);
|
|
2488
|
+
allDefs.push(`enum ${enumDef.name} {
|
|
2489
|
+
${valueStrings.join("\n")}
|
|
2490
|
+
}`);
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
allDefs.push(`enum OrderByDirection {
|
|
2494
|
+
asc
|
|
2495
|
+
desc
|
|
2496
|
+
}`);
|
|
2497
|
+
allDefs.push(`input InnerOrder {
|
|
2498
|
+
direction: OrderByDirection!
|
|
2499
|
+
priority: Int!
|
|
2500
|
+
}`);
|
|
2501
|
+
allDefs.push(...typeDefs);
|
|
2502
|
+
return allDefs.join("\n\n");
|
|
2503
|
+
};
|
|
2504
|
+
var generateQueryTypeDefs = (tables) => {
|
|
2505
|
+
const queryFields = [];
|
|
2506
|
+
for (const tableName of Object.keys(tables)) {
|
|
2507
|
+
const typeName = capitalize(tableName);
|
|
2508
|
+
queryFields.push(
|
|
2509
|
+
` ${tableName}(where: ${typeName}Filters, orderBy: ${typeName}OrderBy, limit: Int, offset: Int): [${typeName}!]!`
|
|
2510
|
+
);
|
|
2511
|
+
}
|
|
2512
|
+
return `type Query {
|
|
2513
|
+
${queryFields.join("\n")}
|
|
2514
|
+
}`;
|
|
2515
|
+
};
|
|
2516
|
+
var generateMutationTypeDefs = (tables) => {
|
|
2517
|
+
const mutationFields = [];
|
|
2518
|
+
for (const tableName of Object.keys(tables)) {
|
|
2519
|
+
const typeName = capitalize(tableName);
|
|
2520
|
+
mutationFields.push(
|
|
2521
|
+
` insert${typeName}(values: [${typeName}InsertInput!]!): [${typeName}!]!`
|
|
2522
|
+
);
|
|
2523
|
+
mutationFields.push(
|
|
2524
|
+
` update${typeName}(where: ${typeName}Filters, set: ${typeName}UpdateInput!): [${typeName}!]!`
|
|
2525
|
+
);
|
|
2526
|
+
mutationFields.push(
|
|
2527
|
+
` delete${typeName}(where: ${typeName}Filters): [${typeName}!]!`
|
|
2528
|
+
);
|
|
2529
|
+
}
|
|
2530
|
+
return `type Mutation {
|
|
2531
|
+
${mutationFields.join("\n")}
|
|
2532
|
+
}`;
|
|
2533
|
+
};
|
|
2534
|
+
|
|
2535
|
+
// src/buildSchemaSDL/generator/queries.ts
|
|
2536
|
+
var import_drizzle_orm9 = require("drizzle-orm");
|
|
2537
|
+
var import_graphql8 = require("graphql");
|
|
2538
|
+
var import_graphql_parse_resolve_info4 = require("graphql-parse-resolve-info");
|
|
2539
|
+
var extractFiltersColumn2 = (column, columnName, operators) => {
|
|
2540
|
+
const entries = Object.entries(operators);
|
|
2541
|
+
if (!entries.length)
|
|
2542
|
+
return void 0;
|
|
2543
|
+
if (operators.OR && operators.OR.length > 0) {
|
|
2544
|
+
if (entries.length > 1) {
|
|
2545
|
+
throw new import_graphql8.GraphQLError(
|
|
2546
|
+
`WHERE ${columnName}: Cannot specify both fields and 'OR' in column operators!`
|
|
2547
|
+
);
|
|
2548
|
+
}
|
|
2549
|
+
const variants2 = [];
|
|
2550
|
+
for (const variant of operators.OR) {
|
|
2551
|
+
const extracted = extractFiltersColumn2(column, columnName, variant);
|
|
2552
|
+
if (extracted)
|
|
2553
|
+
variants2.push(extracted);
|
|
2554
|
+
}
|
|
2555
|
+
return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm9.or)(...variants2) : variants2[0] : void 0;
|
|
2556
|
+
}
|
|
2557
|
+
const variants = [];
|
|
2558
|
+
for (const [operatorName, operatorValue] of entries) {
|
|
2559
|
+
if (operatorValue === null || operatorValue === false)
|
|
2560
|
+
continue;
|
|
2561
|
+
switch (operatorName) {
|
|
2562
|
+
case "eq":
|
|
2563
|
+
case "ne":
|
|
2564
|
+
case "gt":
|
|
2565
|
+
case "gte":
|
|
2566
|
+
case "lt":
|
|
2567
|
+
case "lte": {
|
|
2568
|
+
const singleValue = remapFromGraphQLCore(
|
|
2569
|
+
operatorValue,
|
|
2570
|
+
column,
|
|
2571
|
+
columnName
|
|
2572
|
+
);
|
|
2573
|
+
const opMap = { eq: import_drizzle_orm9.eq, ne: import_drizzle_orm9.ne, gt: import_drizzle_orm9.gt, gte: import_drizzle_orm9.gte, lt: import_drizzle_orm9.lt, lte: import_drizzle_orm9.lte };
|
|
2574
|
+
variants.push(opMap[operatorName](column, singleValue));
|
|
2575
|
+
break;
|
|
2576
|
+
}
|
|
2577
|
+
case "like":
|
|
2578
|
+
case "notLike":
|
|
2579
|
+
case "ilike":
|
|
2580
|
+
case "notIlike": {
|
|
2581
|
+
const opMap = { like: import_drizzle_orm9.like, notLike: import_drizzle_orm9.notLike, ilike: import_drizzle_orm9.ilike, notIlike: import_drizzle_orm9.notIlike };
|
|
2582
|
+
variants.push(opMap[operatorName](column, operatorValue));
|
|
2583
|
+
break;
|
|
2584
|
+
}
|
|
2585
|
+
case "inArray":
|
|
2586
|
+
case "notInArray": {
|
|
2587
|
+
if (!operatorValue.length) {
|
|
2588
|
+
throw new import_graphql8.GraphQLError(
|
|
2589
|
+
`WHERE ${columnName}: Unable to use operator ${operatorName} with an empty array!`
|
|
2590
|
+
);
|
|
2591
|
+
}
|
|
2592
|
+
const arrayValue = operatorValue.map(
|
|
2593
|
+
(val) => remapFromGraphQLCore(val, column, columnName)
|
|
2594
|
+
);
|
|
2595
|
+
const opMap = { inArray: import_drizzle_orm9.inArray, notInArray: import_drizzle_orm9.notInArray };
|
|
2596
|
+
variants.push(opMap[operatorName](column, arrayValue));
|
|
2597
|
+
break;
|
|
2598
|
+
}
|
|
2599
|
+
case "isNull":
|
|
2600
|
+
case "isNotNull": {
|
|
2601
|
+
const opMap = { isNull: import_drizzle_orm9.isNull, isNotNull: import_drizzle_orm9.isNotNull };
|
|
2602
|
+
variants.push(opMap[operatorName](column));
|
|
2603
|
+
break;
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
}
|
|
2607
|
+
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm9.and)(...variants) : variants[0] : void 0;
|
|
2608
|
+
};
|
|
2609
|
+
var buildWhereClause = (tableInfo, where) => {
|
|
2610
|
+
if (!where || Object.keys(where).length === 0) {
|
|
2611
|
+
return void 0;
|
|
2612
|
+
}
|
|
2613
|
+
if (where.OR && where.OR.length > 0) {
|
|
2614
|
+
if (Object.keys(where).length > 1) {
|
|
2615
|
+
throw new import_graphql8.GraphQLError(
|
|
2616
|
+
`WHERE ${tableInfo.name}: Cannot specify both fields and 'OR' in table filters!`
|
|
2617
|
+
);
|
|
2618
|
+
}
|
|
2619
|
+
const variants = [];
|
|
2620
|
+
for (const variant of where.OR) {
|
|
2621
|
+
const extracted = buildWhereClause(tableInfo, variant);
|
|
2622
|
+
if (extracted)
|
|
2623
|
+
variants.push(extracted);
|
|
2624
|
+
}
|
|
2625
|
+
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm9.or)(...variants) : variants[0] : void 0;
|
|
2626
|
+
}
|
|
2627
|
+
const conditions = [];
|
|
2628
|
+
for (const [columnName, operators] of Object.entries(where)) {
|
|
2629
|
+
if (columnName === "OR")
|
|
2630
|
+
continue;
|
|
2631
|
+
if (!operators || Object.keys(operators).length === 0)
|
|
2632
|
+
continue;
|
|
2633
|
+
const column = tableInfo.columns[columnName];
|
|
2634
|
+
if (!column)
|
|
2635
|
+
continue;
|
|
2636
|
+
const extracted = extractFiltersColumn2(
|
|
2637
|
+
column,
|
|
2638
|
+
columnName,
|
|
2639
|
+
operators
|
|
2640
|
+
);
|
|
2641
|
+
if (extracted)
|
|
2642
|
+
conditions.push(extracted);
|
|
2643
|
+
}
|
|
2644
|
+
if (conditions.length === 0)
|
|
2645
|
+
return void 0;
|
|
2646
|
+
if (conditions.length === 1)
|
|
2647
|
+
return conditions[0];
|
|
2648
|
+
return (0, import_drizzle_orm9.and)(...conditions);
|
|
2649
|
+
};
|
|
2650
|
+
var buildOrderByClause = (tableInfo, orderBy) => {
|
|
2651
|
+
if (!orderBy || Object.keys(orderBy).length === 0) {
|
|
2652
|
+
return void 0;
|
|
2653
|
+
}
|
|
2654
|
+
const orderEntries = Object.entries(orderBy).map(([columnName, field]) => ({
|
|
2655
|
+
columnName,
|
|
2656
|
+
...field
|
|
2657
|
+
}));
|
|
2658
|
+
orderEntries.sort((a, b) => a.priority - b.priority);
|
|
2659
|
+
const orderClauses = [];
|
|
2660
|
+
for (const entry of orderEntries) {
|
|
2661
|
+
const column = tableInfo.columns[entry.columnName];
|
|
2662
|
+
if (column) {
|
|
2663
|
+
orderClauses.push(
|
|
2664
|
+
entry.direction === "desc" ? (0, import_drizzle_orm9.desc)(column) : (0, import_drizzle_orm9.asc)(column)
|
|
2665
|
+
);
|
|
2666
|
+
}
|
|
2667
|
+
}
|
|
2668
|
+
return orderClauses.length > 0 ? orderClauses : void 0;
|
|
2669
|
+
};
|
|
2670
|
+
var extractSelectedColumns = (fields, tableInfo) => {
|
|
2671
|
+
const columns = {};
|
|
2672
|
+
for (const fieldName of Object.keys(fields)) {
|
|
2673
|
+
if (tableInfo.columns[fieldName]) {
|
|
2674
|
+
columns[fieldName] = true;
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
return Object.keys(columns).length > 0 ? columns : {};
|
|
2678
|
+
};
|
|
2679
|
+
var extractRelationsParams2 = (relationMap, tables, tableName, fields) => {
|
|
2680
|
+
const relations = relationMap[tableName];
|
|
2681
|
+
if (!relations)
|
|
2682
|
+
return void 0;
|
|
2683
|
+
const args = {};
|
|
2684
|
+
for (const [relName, { targetTableName, relation }] of Object.entries(
|
|
2685
|
+
relations
|
|
2686
|
+
)) {
|
|
2687
|
+
const relationField = fields[relName];
|
|
2688
|
+
if (!relationField)
|
|
2689
|
+
continue;
|
|
2690
|
+
const allFields = {};
|
|
2691
|
+
if (relationField.fieldsByTypeName) {
|
|
2692
|
+
for (const typeFields of Object.values(relationField.fieldsByTypeName)) {
|
|
2693
|
+
Object.assign(allFields, typeFields);
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2696
|
+
const targetTable = tables[targetTableName];
|
|
2697
|
+
if (!targetTable)
|
|
2698
|
+
continue;
|
|
2699
|
+
const thisRecord = {
|
|
2700
|
+
columns: extractSelectedColumns(allFields, targetTable)
|
|
2701
|
+
};
|
|
2702
|
+
const relationArgs = relationField.args;
|
|
2703
|
+
if (relationArgs) {
|
|
2704
|
+
if (relationArgs["where"]) {
|
|
2705
|
+
thisRecord.where = buildWhereClause(
|
|
2706
|
+
targetTable,
|
|
2707
|
+
relationArgs["where"]
|
|
2708
|
+
);
|
|
2709
|
+
}
|
|
2710
|
+
if (relationArgs["orderBy"]) {
|
|
2711
|
+
thisRecord.orderBy = buildOrderByClause(
|
|
2712
|
+
targetTable,
|
|
2713
|
+
relationArgs["orderBy"]
|
|
2714
|
+
);
|
|
2715
|
+
}
|
|
2716
|
+
if (relationArgs["limit"] !== void 0) {
|
|
2717
|
+
thisRecord.limit = relationArgs["limit"];
|
|
2718
|
+
}
|
|
2719
|
+
if (relationArgs["offset"] !== void 0) {
|
|
2720
|
+
thisRecord.offset = relationArgs["offset"];
|
|
2721
|
+
}
|
|
2722
|
+
}
|
|
2723
|
+
const nestedWith = extractRelationsParams2(
|
|
2724
|
+
relationMap,
|
|
2725
|
+
tables,
|
|
2726
|
+
targetTableName,
|
|
2727
|
+
allFields
|
|
2728
|
+
);
|
|
2729
|
+
if (nestedWith) {
|
|
2730
|
+
thisRecord.with = nestedWith;
|
|
2731
|
+
}
|
|
2732
|
+
args[relName] = thisRecord;
|
|
2733
|
+
}
|
|
2734
|
+
return Object.keys(args).length > 0 ? args : void 0;
|
|
2735
|
+
};
|
|
2736
|
+
var generateQueries = (db, tables, relations) => {
|
|
2737
|
+
const queries = {};
|
|
2738
|
+
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
2739
|
+
const queryBase = db.query[tableName];
|
|
2740
|
+
if (!queryBase) {
|
|
2741
|
+
throw new Error(
|
|
2742
|
+
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
2743
|
+
);
|
|
2744
|
+
}
|
|
2745
|
+
queries[tableName] = async (parent, args, context, info) => {
|
|
2746
|
+
try {
|
|
2747
|
+
const { where, orderBy, limit, offset } = args;
|
|
2748
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
|
|
2749
|
+
deep: true
|
|
2750
|
+
});
|
|
2751
|
+
const allFields = {};
|
|
2752
|
+
if (parsedInfo.fieldsByTypeName) {
|
|
2753
|
+
for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
|
|
2754
|
+
Object.assign(allFields, fields);
|
|
2755
|
+
}
|
|
2756
|
+
}
|
|
2757
|
+
const result = await queryBase.findMany({
|
|
2758
|
+
columns: extractSelectedColumns(allFields, tableInfo),
|
|
2759
|
+
offset,
|
|
2760
|
+
limit,
|
|
2761
|
+
orderBy: buildOrderByClause(tableInfo, orderBy),
|
|
2762
|
+
where: buildWhereClause(tableInfo, where),
|
|
2763
|
+
with: extractRelationsParams2(relations, tables, tableName, allFields)
|
|
2764
|
+
});
|
|
2765
|
+
return result;
|
|
2766
|
+
} catch (e) {
|
|
2767
|
+
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2768
|
+
throw new import_graphql8.GraphQLError(String(e.message));
|
|
2769
|
+
}
|
|
2770
|
+
throw e;
|
|
2771
|
+
}
|
|
2772
|
+
};
|
|
2773
|
+
}
|
|
2774
|
+
return queries;
|
|
2775
|
+
};
|
|
2776
|
+
|
|
2777
|
+
// src/buildSchemaSDL/generator/mutations.ts
|
|
2778
|
+
var import_drizzle_orm10 = require("drizzle-orm");
|
|
2779
|
+
var import_graphql9 = require("graphql");
|
|
2780
|
+
var buildWhereClause2 = (tableInfo, where) => {
|
|
2781
|
+
if (!where || Object.keys(where).length === 0) {
|
|
2782
|
+
return void 0;
|
|
2783
|
+
}
|
|
2784
|
+
if (where.OR && where.OR.length > 0) {
|
|
2785
|
+
if (Object.keys(where).length > 1) {
|
|
2786
|
+
throw new import_graphql9.GraphQLError(
|
|
2787
|
+
`WHERE ${tableInfo.name}: Cannot specify both fields and 'OR' in table filters!`
|
|
2788
|
+
);
|
|
2789
|
+
}
|
|
2790
|
+
const variants = [];
|
|
2791
|
+
for (const variant of where.OR) {
|
|
2792
|
+
const extracted = buildWhereClause2(tableInfo, variant);
|
|
2793
|
+
if (extracted)
|
|
2794
|
+
variants.push(extracted);
|
|
2795
|
+
}
|
|
2796
|
+
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm10.or)(...variants) : variants[0] : void 0;
|
|
2797
|
+
}
|
|
2798
|
+
const conditions = [];
|
|
2799
|
+
for (const [key, value] of Object.entries(where)) {
|
|
2800
|
+
if (key === "OR")
|
|
2801
|
+
continue;
|
|
2802
|
+
if (value === null || value === void 0)
|
|
2803
|
+
continue;
|
|
2804
|
+
const column = tableInfo.columns[key];
|
|
2805
|
+
if (column) {
|
|
2806
|
+
const filters = value;
|
|
2807
|
+
if (typeof filters === "object" && filters.eq !== void 0) {
|
|
2808
|
+
conditions.push((0, import_drizzle_orm10.eq)(column, filters.eq));
|
|
2809
|
+
} else if (typeof filters !== "object" || !Array.isArray(filters)) {
|
|
2810
|
+
conditions.push((0, import_drizzle_orm10.eq)(column, filters));
|
|
2811
|
+
}
|
|
2812
|
+
}
|
|
2813
|
+
}
|
|
2814
|
+
if (conditions.length === 0)
|
|
2815
|
+
return void 0;
|
|
2816
|
+
if (conditions.length === 1)
|
|
2817
|
+
return conditions[0];
|
|
2818
|
+
return (0, import_drizzle_orm10.and)(...conditions);
|
|
2819
|
+
};
|
|
2820
|
+
var generateMutations = (db, tables) => {
|
|
2821
|
+
const mutations = {};
|
|
2822
|
+
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
2823
|
+
const capitalizedName = capitalize(tableName);
|
|
2824
|
+
mutations[`insert${capitalizedName}`] = async (parent, args, context, info) => {
|
|
2825
|
+
try {
|
|
2826
|
+
const { values } = args;
|
|
2827
|
+
if (!values || values.length === 0) {
|
|
2828
|
+
throw new import_graphql9.GraphQLError("No values provided for insert");
|
|
2829
|
+
}
|
|
2830
|
+
const remappedValues = remapFromGraphQLArrayInput(
|
|
2831
|
+
values,
|
|
2832
|
+
tableInfo.table
|
|
2833
|
+
);
|
|
2834
|
+
const result = await db.insert(tableInfo.table).values(remappedValues).returning();
|
|
2835
|
+
return result;
|
|
2836
|
+
} catch (e) {
|
|
2837
|
+
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2838
|
+
throw new import_graphql9.GraphQLError(String(e.message));
|
|
2839
|
+
}
|
|
2840
|
+
throw e;
|
|
2841
|
+
}
|
|
2842
|
+
};
|
|
2843
|
+
mutations[`update${capitalizedName}`] = async (parent, args, context, info) => {
|
|
2844
|
+
try {
|
|
2845
|
+
const { where, set } = args;
|
|
2846
|
+
if (!set || Object.keys(set).length === 0) {
|
|
2847
|
+
throw new import_graphql9.GraphQLError("No values provided for update");
|
|
2848
|
+
}
|
|
2849
|
+
const remappedSet = remapFromGraphQLSingleInput(set, tableInfo.table);
|
|
2850
|
+
const whereClause = buildWhereClause2(tableInfo, where);
|
|
2851
|
+
let query = db.update(tableInfo.table).set(remappedSet);
|
|
2852
|
+
if (whereClause) {
|
|
2853
|
+
query = query.where(whereClause);
|
|
2854
|
+
}
|
|
2855
|
+
const result = await query.returning();
|
|
2856
|
+
return result;
|
|
2857
|
+
} catch (e) {
|
|
2858
|
+
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2859
|
+
throw new import_graphql9.GraphQLError(String(e.message));
|
|
2860
|
+
}
|
|
2861
|
+
throw e;
|
|
2862
|
+
}
|
|
2863
|
+
};
|
|
2864
|
+
mutations[`delete${capitalizedName}`] = async (parent, args, context, info) => {
|
|
2865
|
+
try {
|
|
2866
|
+
const { where } = args;
|
|
2867
|
+
const whereClause = buildWhereClause2(tableInfo, where);
|
|
2868
|
+
let deleteQuery = db.delete(tableInfo.table);
|
|
2869
|
+
if (whereClause) {
|
|
2870
|
+
deleteQuery = deleteQuery.where(whereClause);
|
|
2871
|
+
}
|
|
2872
|
+
const result = await deleteQuery.returning();
|
|
2873
|
+
return result;
|
|
2874
|
+
} catch (e) {
|
|
2875
|
+
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2876
|
+
throw new import_graphql9.GraphQLError(String(e.message));
|
|
2877
|
+
}
|
|
2878
|
+
throw e;
|
|
2879
|
+
}
|
|
2880
|
+
};
|
|
2881
|
+
}
|
|
2882
|
+
return mutations;
|
|
2883
|
+
};
|
|
2884
|
+
|
|
2885
|
+
// src/buildSchemaSDL/index.ts
|
|
2886
|
+
var buildSchemaSDL = (db, config) => {
|
|
2887
|
+
const schema = db._.fullSchema;
|
|
2888
|
+
if (!schema) {
|
|
2889
|
+
throw new Error(
|
|
2890
|
+
"Drizzle-GraphQL Error: Schema not found in drizzle instance. Make sure you're using drizzle-orm v0.30.9 or above and schema is passed to drizzle constructor!"
|
|
2891
|
+
);
|
|
2892
|
+
}
|
|
2893
|
+
if (!(0, import_drizzle_orm11.is)(db, import_sqlite_core5.BaseSQLiteDatabase)) {
|
|
2894
|
+
throw new Error(
|
|
2895
|
+
"Drizzle-GraphQL Error: buildSchemaSDL currently only supports SQLite databases"
|
|
2896
|
+
);
|
|
2897
|
+
}
|
|
2898
|
+
const { tables, relations } = generateTypes(db, schema);
|
|
2899
|
+
const typeDefsArray = [];
|
|
2900
|
+
typeDefsArray.push(generateTypeDefs(tables, relations));
|
|
2901
|
+
typeDefsArray.push(generateQueryTypeDefs(tables));
|
|
2902
|
+
if (config?.mutations !== false) {
|
|
2903
|
+
typeDefsArray.push(generateMutationTypeDefs(tables));
|
|
2904
|
+
}
|
|
2905
|
+
const typeDefs = typeDefsArray.join("\n\n");
|
|
2906
|
+
const queries = generateQueries(db, tables, relations);
|
|
2907
|
+
const resolvers = {
|
|
2908
|
+
Query: queries
|
|
2909
|
+
};
|
|
2910
|
+
if (config?.mutations !== false) {
|
|
2911
|
+
const mutations = generateMutations(db, tables);
|
|
2912
|
+
resolvers["Mutation"] = mutations;
|
|
2913
|
+
}
|
|
2914
|
+
return {
|
|
2915
|
+
typeDefs,
|
|
2916
|
+
resolvers
|
|
2917
|
+
};
|
|
2918
|
+
};
|
|
2919
|
+
|
|
2920
|
+
// src/helpers.ts
|
|
2921
|
+
var import_drizzle_orm12 = require("drizzle-orm");
|
|
2922
|
+
function setCustomGraphQL(table, columnConfig) {
|
|
2923
|
+
for (const [columnName, config] of Object.entries(columnConfig)) {
|
|
2924
|
+
const column = table[columnName];
|
|
2925
|
+
if (!column) {
|
|
2926
|
+
console.warn(
|
|
2927
|
+
`Warning: Column "${columnName}" not found in table "${(0, import_drizzle_orm12.getTableName)(
|
|
2928
|
+
table
|
|
2929
|
+
)}"`
|
|
2930
|
+
);
|
|
2931
|
+
continue;
|
|
2932
|
+
}
|
|
2933
|
+
if (typeof config === "string") {
|
|
2934
|
+
column.customGraphqlType = config;
|
|
2935
|
+
} else if (config && typeof config === "object") {
|
|
2936
|
+
const fieldConfig = config;
|
|
2937
|
+
column.customGraphqlType = fieldConfig.type;
|
|
2938
|
+
if (fieldConfig.description) {
|
|
2939
|
+
column.customGraphqlDescription = fieldConfig.description;
|
|
2940
|
+
}
|
|
2941
|
+
}
|
|
2942
|
+
}
|
|
2943
|
+
}
|
|
2944
|
+
function setCustomGraphQLTypes(table, columnTypes) {
|
|
2945
|
+
setCustomGraphQL(table, columnTypes);
|
|
2946
|
+
}
|
|
2196
2947
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2197
2948
|
0 && (module.exports = {
|
|
2198
|
-
buildSchema
|
|
2949
|
+
buildSchema,
|
|
2950
|
+
buildSchemaSDL,
|
|
2951
|
+
setCustomGraphQL,
|
|
2952
|
+
setCustomGraphQLTypes
|
|
2199
2953
|
});
|
|
2200
2954
|
//# sourceMappingURL=index.cjs.map
|