drizzle-graphql-plus 0.8.25 → 0.8.26
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 +271 -208
- package/index.cjs.map +1 -1
- package/index.js +268 -202
- package/index.js.map +1 -1
- package/package.json +5 -2
package/index.cjs
CHANGED
|
@@ -2197,11 +2197,58 @@ var buildSchema = (db, config) => {
|
|
|
2197
2197
|
};
|
|
2198
2198
|
|
|
2199
2199
|
// src/buildSchemaSDL/index.ts
|
|
2200
|
-
var
|
|
2200
|
+
var import_drizzle_orm12 = require("drizzle-orm");
|
|
2201
2201
|
var import_sqlite_core5 = require("drizzle-orm/sqlite-core");
|
|
2202
2202
|
|
|
2203
|
-
// src/buildSchemaSDL/generator/schema.ts
|
|
2203
|
+
// src/buildSchemaSDL/generator/schema/generation.ts
|
|
2204
2204
|
var import_drizzle_orm8 = require("drizzle-orm");
|
|
2205
|
+
var generateTypes = (db, schema) => {
|
|
2206
|
+
const tables = {};
|
|
2207
|
+
const schemaEntries = Object.entries(schema);
|
|
2208
|
+
const tableEntries = [];
|
|
2209
|
+
for (const [key, value] of schemaEntries) {
|
|
2210
|
+
if (value && typeof value === "object" && "getSQL" in value) {
|
|
2211
|
+
const table = value;
|
|
2212
|
+
const tableName = key;
|
|
2213
|
+
const columns = (0, import_drizzle_orm8.getTableColumns)(table);
|
|
2214
|
+
tables[tableName] = {
|
|
2215
|
+
name: tableName,
|
|
2216
|
+
table,
|
|
2217
|
+
columns
|
|
2218
|
+
};
|
|
2219
|
+
tableEntries.push([tableName, table]);
|
|
2220
|
+
}
|
|
2221
|
+
}
|
|
2222
|
+
const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm8.is)(value, import_drizzle_orm8.Relations)).map(([key, value]) => [
|
|
2223
|
+
tableEntries.find(
|
|
2224
|
+
([tableName, tableValue]) => tableValue === value.table
|
|
2225
|
+
)[0],
|
|
2226
|
+
value
|
|
2227
|
+
]).map(([tableName, relValue]) => [
|
|
2228
|
+
tableName,
|
|
2229
|
+
relValue.config((0, import_drizzle_orm8.createTableRelationsHelpers)(tables[tableName].table))
|
|
2230
|
+
]);
|
|
2231
|
+
const namedRelations = Object.fromEntries(
|
|
2232
|
+
rawRelations.map(([relName, config]) => {
|
|
2233
|
+
const namedConfig = Object.fromEntries(
|
|
2234
|
+
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
2235
|
+
innerRelName,
|
|
2236
|
+
{
|
|
2237
|
+
relation: innerRelValue,
|
|
2238
|
+
targetTableName: tableEntries.find(
|
|
2239
|
+
([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
|
|
2240
|
+
)[0]
|
|
2241
|
+
}
|
|
2242
|
+
])
|
|
2243
|
+
);
|
|
2244
|
+
return [relName, namedConfig];
|
|
2245
|
+
})
|
|
2246
|
+
);
|
|
2247
|
+
return { tables, relations: namedRelations };
|
|
2248
|
+
};
|
|
2249
|
+
|
|
2250
|
+
// src/buildSchemaSDL/generator/schema/type-defs.ts
|
|
2251
|
+
var import_drizzle_orm9 = require("drizzle-orm");
|
|
2205
2252
|
var import_mysql_core4 = require("drizzle-orm/mysql-core");
|
|
2206
2253
|
var import_pg_core4 = require("drizzle-orm/pg-core");
|
|
2207
2254
|
var import_sqlite_core4 = require("drizzle-orm/sqlite-core");
|
|
@@ -2259,7 +2306,7 @@ var columnToSDL = (column, columnName, tableName, forceNullable = false) => {
|
|
|
2259
2306
|
customScalars.add("BigInt");
|
|
2260
2307
|
break;
|
|
2261
2308
|
case "number":
|
|
2262
|
-
if ((0,
|
|
2309
|
+
if ((0, import_drizzle_orm9.is)(column, import_pg_core4.PgInteger) || (0, import_drizzle_orm9.is)(column, import_pg_core4.PgSerial) || (0, import_drizzle_orm9.is)(column, import_mysql_core4.MySqlInt) || (0, import_drizzle_orm9.is)(column, import_mysql_core4.MySqlSerial) || (0, import_drizzle_orm9.is)(column, import_sqlite_core4.SQLiteInteger)) {
|
|
2263
2310
|
baseType = "Int";
|
|
2264
2311
|
} else {
|
|
2265
2312
|
baseType = "Float";
|
|
@@ -2302,50 +2349,6 @@ var columnToSDL = (column, columnName, tableName, forceNullable = false) => {
|
|
|
2302
2349
|
}
|
|
2303
2350
|
return baseType;
|
|
2304
2351
|
};
|
|
2305
|
-
var generateTypes = (db, schema) => {
|
|
2306
|
-
const tables = {};
|
|
2307
|
-
const schemaEntries = Object.entries(schema);
|
|
2308
|
-
const tableEntries = [];
|
|
2309
|
-
for (const [key, value] of schemaEntries) {
|
|
2310
|
-
if (value && typeof value === "object" && "getSQL" in value) {
|
|
2311
|
-
const table = value;
|
|
2312
|
-
const tableName = key;
|
|
2313
|
-
const columns = (0, import_drizzle_orm8.getTableColumns)(table);
|
|
2314
|
-
tables[tableName] = {
|
|
2315
|
-
name: tableName,
|
|
2316
|
-
table,
|
|
2317
|
-
columns
|
|
2318
|
-
};
|
|
2319
|
-
tableEntries.push([tableName, table]);
|
|
2320
|
-
}
|
|
2321
|
-
}
|
|
2322
|
-
const rawRelations = schemaEntries.filter(([key, value]) => (0, import_drizzle_orm8.is)(value, import_drizzle_orm8.Relations)).map(([key, value]) => [
|
|
2323
|
-
tableEntries.find(
|
|
2324
|
-
([tableName, tableValue]) => tableValue === value.table
|
|
2325
|
-
)[0],
|
|
2326
|
-
value
|
|
2327
|
-
]).map(([tableName, relValue]) => [
|
|
2328
|
-
tableName,
|
|
2329
|
-
relValue.config((0, import_drizzle_orm8.createTableRelationsHelpers)(tables[tableName].table))
|
|
2330
|
-
]);
|
|
2331
|
-
const namedRelations = Object.fromEntries(
|
|
2332
|
-
rawRelations.map(([relName, config]) => {
|
|
2333
|
-
const namedConfig = Object.fromEntries(
|
|
2334
|
-
Object.entries(config).map(([innerRelName, innerRelValue]) => [
|
|
2335
|
-
innerRelName,
|
|
2336
|
-
{
|
|
2337
|
-
relation: innerRelValue,
|
|
2338
|
-
targetTableName: tableEntries.find(
|
|
2339
|
-
([tableName, tableValue]) => tableValue === innerRelValue.referencedTable
|
|
2340
|
-
)[0]
|
|
2341
|
-
}
|
|
2342
|
-
])
|
|
2343
|
-
);
|
|
2344
|
-
return [relName, namedConfig];
|
|
2345
|
-
})
|
|
2346
|
-
);
|
|
2347
|
-
return { tables, relations: namedRelations };
|
|
2348
|
-
};
|
|
2349
2352
|
var generateTypeDefs = (tables, relations) => {
|
|
2350
2353
|
const typeDefs = [];
|
|
2351
2354
|
customScalars.clear();
|
|
@@ -2358,7 +2361,7 @@ var generateTypeDefs = (tables, relations) => {
|
|
|
2358
2361
|
continue;
|
|
2359
2362
|
for (const [relationName, relationInfo] of Object.entries(tableRelations)) {
|
|
2360
2363
|
const relation = relationInfo.relation;
|
|
2361
|
-
if (!(0,
|
|
2364
|
+
if (!(0, import_drizzle_orm9.is)(relation, import_drizzle_orm9.One))
|
|
2362
2365
|
continue;
|
|
2363
2366
|
const config = relation.config;
|
|
2364
2367
|
if (!config?.fields || !config?.references)
|
|
@@ -2414,13 +2417,12 @@ var generateTypeDefs = (tables, relations) => {
|
|
|
2414
2417
|
}
|
|
2415
2418
|
fields.push(` ${columnName}: ${typeStr}`);
|
|
2416
2419
|
}
|
|
2417
|
-
fields.push(` _operation: OPERATION!`);
|
|
2418
2420
|
const tableRelations = relations[tableName];
|
|
2419
2421
|
if (tableRelations) {
|
|
2420
2422
|
for (const [relationName, relationInfo] of Object.entries(
|
|
2421
2423
|
tableRelations
|
|
2422
2424
|
)) {
|
|
2423
|
-
const isOne = (0,
|
|
2425
|
+
const isOne = (0, import_drizzle_orm9.is)(relationInfo.relation, import_drizzle_orm9.One);
|
|
2424
2426
|
const targetTableName = relationInfo.targetTableName;
|
|
2425
2427
|
const targetTypeName = capitalize(targetTableName);
|
|
2426
2428
|
if (isOne) {
|
|
@@ -2495,15 +2497,13 @@ ${whereFields.join("\n")}
|
|
|
2495
2497
|
}
|
|
2496
2498
|
typeDefs.push(`input ${typeName}OrderBy {
|
|
2497
2499
|
${orderByFields.join("\n")}
|
|
2500
|
+
}`);
|
|
2501
|
+
typeDefs.push(`type ${typeName}DeleteResult {
|
|
2502
|
+
deletedItems: [DeletedItem!]!
|
|
2503
|
+
${tableName}FindMany(where: ${typeName}Filters, orderBy: ${typeName}OrderBy, limit: Int, offset: Int): [${typeName}!]!
|
|
2498
2504
|
}`);
|
|
2499
2505
|
}
|
|
2500
2506
|
const allDefs = [];
|
|
2501
|
-
allDefs.push(`enum OPERATION {
|
|
2502
|
-
READ
|
|
2503
|
-
INSERTED
|
|
2504
|
-
UPDATED
|
|
2505
|
-
DELETED
|
|
2506
|
-
}`);
|
|
2507
2507
|
if (customScalars.size > 0) {
|
|
2508
2508
|
for (const scalarName of Array.from(customScalars).sort()) {
|
|
2509
2509
|
allDefs.push(`scalar ${scalarName}`);
|
|
@@ -2520,6 +2520,9 @@ ${valueStrings.join("\n")}
|
|
|
2520
2520
|
allDefs.push(`enum OrderByDirection {
|
|
2521
2521
|
asc
|
|
2522
2522
|
desc
|
|
2523
|
+
}`);
|
|
2524
|
+
allDefs.push(`type DeletedItem {
|
|
2525
|
+
id: ID!
|
|
2523
2526
|
}`);
|
|
2524
2527
|
allDefs.push(`input InnerOrder {
|
|
2525
2528
|
direction: OrderByDirection!
|
|
@@ -2562,6 +2565,9 @@ var generateQueryTypeDefs = (tables) => {
|
|
|
2562
2565
|
queryFields.push(
|
|
2563
2566
|
` ${tableName}FindMany(where: ${typeName}Filters, orderBy: ${typeName}OrderBy, limit: Int, offset: Int): [${typeName}!]!`
|
|
2564
2567
|
);
|
|
2568
|
+
queryFields.push(
|
|
2569
|
+
` ${tableName}FindFirst(where: ${typeName}Filters, orderBy: ${typeName}OrderBy): ${typeName}`
|
|
2570
|
+
);
|
|
2565
2571
|
}
|
|
2566
2572
|
return `type Query {
|
|
2567
2573
|
${queryFields.join("\n")}
|
|
@@ -2578,7 +2584,7 @@ var generateMutationTypeDefs = (tables) => {
|
|
|
2578
2584
|
` ${tableName}UpdateMany(where: ${typeName}Filters, set: ${typeName}UpdateInput!): [${typeName}!]!`
|
|
2579
2585
|
);
|
|
2580
2586
|
mutationFields.push(
|
|
2581
|
-
` ${tableName}DeleteMany(where: ${typeName}Filters):
|
|
2587
|
+
` ${tableName}DeleteMany(where: ${typeName}Filters): ${typeName}DeleteResult!`
|
|
2582
2588
|
);
|
|
2583
2589
|
}
|
|
2584
2590
|
return `type Mutation {
|
|
@@ -2586,10 +2592,16 @@ ${mutationFields.join("\n")}
|
|
|
2586
2592
|
}`;
|
|
2587
2593
|
};
|
|
2588
2594
|
|
|
2589
|
-
// src/buildSchemaSDL/generator/queries.ts
|
|
2590
|
-
var
|
|
2591
|
-
var import_graphql8 = require("graphql");
|
|
2595
|
+
// src/buildSchemaSDL/generator/queries/resolvers.ts
|
|
2596
|
+
var import_graphql9 = require("graphql");
|
|
2592
2597
|
var import_graphql_parse_resolve_info4 = require("graphql-parse-resolve-info");
|
|
2598
|
+
|
|
2599
|
+
// src/buildSchemaSDL/generator/utils/selection.ts
|
|
2600
|
+
var import_drizzle_orm11 = require("drizzle-orm");
|
|
2601
|
+
|
|
2602
|
+
// src/buildSchemaSDL/generator/utils/filters.ts
|
|
2603
|
+
var import_drizzle_orm10 = require("drizzle-orm");
|
|
2604
|
+
var import_graphql8 = require("graphql");
|
|
2593
2605
|
var extractFiltersColumn2 = (column, columnName, operators) => {
|
|
2594
2606
|
const entries = Object.entries(operators);
|
|
2595
2607
|
if (!entries.length)
|
|
@@ -2606,7 +2618,7 @@ var extractFiltersColumn2 = (column, columnName, operators) => {
|
|
|
2606
2618
|
if (extracted)
|
|
2607
2619
|
variants2.push(extracted);
|
|
2608
2620
|
}
|
|
2609
|
-
return variants2.length ? variants2.length > 1 ? (0,
|
|
2621
|
+
return variants2.length ? variants2.length > 1 ? (0, import_drizzle_orm10.or)(...variants2) : variants2[0] : void 0;
|
|
2610
2622
|
}
|
|
2611
2623
|
const variants = [];
|
|
2612
2624
|
for (const [operatorName, operatorValue] of entries) {
|
|
@@ -2624,7 +2636,7 @@ var extractFiltersColumn2 = (column, columnName, operators) => {
|
|
|
2624
2636
|
column,
|
|
2625
2637
|
columnName
|
|
2626
2638
|
);
|
|
2627
|
-
const opMap = { eq:
|
|
2639
|
+
const opMap = { eq: import_drizzle_orm10.eq, ne: import_drizzle_orm10.ne, gt: import_drizzle_orm10.gt, gte: import_drizzle_orm10.gte, lt: import_drizzle_orm10.lt, lte: import_drizzle_orm10.lte };
|
|
2628
2640
|
variants.push(opMap[operatorName](column, singleValue));
|
|
2629
2641
|
break;
|
|
2630
2642
|
}
|
|
@@ -2632,7 +2644,7 @@ var extractFiltersColumn2 = (column, columnName, operators) => {
|
|
|
2632
2644
|
case "notLike":
|
|
2633
2645
|
case "ilike":
|
|
2634
2646
|
case "notIlike": {
|
|
2635
|
-
const opMap = { like:
|
|
2647
|
+
const opMap = { like: import_drizzle_orm10.like, notLike: import_drizzle_orm10.notLike, ilike: import_drizzle_orm10.ilike, notIlike: import_drizzle_orm10.notIlike };
|
|
2636
2648
|
variants.push(opMap[operatorName](column, operatorValue));
|
|
2637
2649
|
break;
|
|
2638
2650
|
}
|
|
@@ -2646,19 +2658,19 @@ var extractFiltersColumn2 = (column, columnName, operators) => {
|
|
|
2646
2658
|
const arrayValue = operatorValue.map(
|
|
2647
2659
|
(val) => remapFromGraphQLCore(val, column, columnName)
|
|
2648
2660
|
);
|
|
2649
|
-
const opMap = { inArray:
|
|
2661
|
+
const opMap = { inArray: import_drizzle_orm10.inArray, notInArray: import_drizzle_orm10.notInArray };
|
|
2650
2662
|
variants.push(opMap[operatorName](column, arrayValue));
|
|
2651
2663
|
break;
|
|
2652
2664
|
}
|
|
2653
2665
|
case "isNull":
|
|
2654
2666
|
case "isNotNull": {
|
|
2655
|
-
const opMap = { isNull:
|
|
2667
|
+
const opMap = { isNull: import_drizzle_orm10.isNull, isNotNull: import_drizzle_orm10.isNotNull };
|
|
2656
2668
|
variants.push(opMap[operatorName](column));
|
|
2657
2669
|
break;
|
|
2658
2670
|
}
|
|
2659
2671
|
}
|
|
2660
2672
|
}
|
|
2661
|
-
return variants.length ? variants.length > 1 ? (0,
|
|
2673
|
+
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm10.and)(...variants) : variants[0] : void 0;
|
|
2662
2674
|
};
|
|
2663
2675
|
var buildWhereClause = (tableInfo, where) => {
|
|
2664
2676
|
if (!where || Object.keys(where).length === 0) {
|
|
@@ -2676,7 +2688,7 @@ var buildWhereClause = (tableInfo, where) => {
|
|
|
2676
2688
|
if (extracted)
|
|
2677
2689
|
variants.push(extracted);
|
|
2678
2690
|
}
|
|
2679
|
-
return variants.length ? variants.length > 1 ? (0,
|
|
2691
|
+
return variants.length ? variants.length > 1 ? (0, import_drizzle_orm10.or)(...variants) : variants[0] : void 0;
|
|
2680
2692
|
}
|
|
2681
2693
|
const conditions = [];
|
|
2682
2694
|
for (const [columnName, operators] of Object.entries(where)) {
|
|
@@ -2699,8 +2711,10 @@ var buildWhereClause = (tableInfo, where) => {
|
|
|
2699
2711
|
return void 0;
|
|
2700
2712
|
if (conditions.length === 1)
|
|
2701
2713
|
return conditions[0];
|
|
2702
|
-
return (0,
|
|
2714
|
+
return (0, import_drizzle_orm10.and)(...conditions);
|
|
2703
2715
|
};
|
|
2716
|
+
|
|
2717
|
+
// src/buildSchemaSDL/generator/utils/selection.ts
|
|
2704
2718
|
var buildOrderByClause = (tableInfo, orderBy) => {
|
|
2705
2719
|
if (!orderBy || Object.keys(orderBy).length === 0) {
|
|
2706
2720
|
return void 0;
|
|
@@ -2715,7 +2729,7 @@ var buildOrderByClause = (tableInfo, orderBy) => {
|
|
|
2715
2729
|
const column = tableInfo.columns[entry.columnName];
|
|
2716
2730
|
if (column) {
|
|
2717
2731
|
orderClauses.push(
|
|
2718
|
-
entry.direction === "desc" ? (0,
|
|
2732
|
+
entry.direction === "desc" ? (0, import_drizzle_orm11.desc)(column) : (0, import_drizzle_orm11.asc)(column)
|
|
2719
2733
|
);
|
|
2720
2734
|
}
|
|
2721
2735
|
}
|
|
@@ -2787,7 +2801,9 @@ var extractRelationsParams2 = (relationMap, tables, tableName, fields) => {
|
|
|
2787
2801
|
}
|
|
2788
2802
|
return Object.keys(args).length > 0 ? args : void 0;
|
|
2789
2803
|
};
|
|
2790
|
-
|
|
2804
|
+
|
|
2805
|
+
// src/buildSchemaSDL/generator/queries/resolvers.ts
|
|
2806
|
+
var createFindManyResolver = (queryBase, tableInfo, tables, relations) => {
|
|
2791
2807
|
return async (parent, args, context, info) => {
|
|
2792
2808
|
try {
|
|
2793
2809
|
const { where, orderBy, limit, offset } = args;
|
|
@@ -2813,18 +2829,50 @@ var createQueryResolver = (queryBase, tableInfo, tables, relations) => {
|
|
|
2813
2829
|
allFields
|
|
2814
2830
|
)
|
|
2815
2831
|
});
|
|
2816
|
-
return result
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2832
|
+
return result;
|
|
2833
|
+
} catch (e) {
|
|
2834
|
+
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2835
|
+
throw new import_graphql9.GraphQLError(String(e.message));
|
|
2836
|
+
}
|
|
2837
|
+
throw e;
|
|
2838
|
+
}
|
|
2839
|
+
};
|
|
2840
|
+
};
|
|
2841
|
+
var createFindFirstResolver = (queryBase, tableInfo, tables, relations) => {
|
|
2842
|
+
return async (parent, args, context, info) => {
|
|
2843
|
+
try {
|
|
2844
|
+
const { where, orderBy } = args;
|
|
2845
|
+
const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
|
|
2846
|
+
deep: true
|
|
2847
|
+
});
|
|
2848
|
+
const allFields = {};
|
|
2849
|
+
if (parsedInfo.fieldsByTypeName) {
|
|
2850
|
+
for (const fields of Object.values(parsedInfo.fieldsByTypeName)) {
|
|
2851
|
+
Object.assign(allFields, fields);
|
|
2852
|
+
}
|
|
2853
|
+
}
|
|
2854
|
+
const result = await queryBase.findFirst({
|
|
2855
|
+
columns: extractSelectedColumns(allFields, tableInfo),
|
|
2856
|
+
orderBy: buildOrderByClause(tableInfo, orderBy),
|
|
2857
|
+
where: buildWhereClause(tableInfo, where),
|
|
2858
|
+
with: extractRelationsParams2(
|
|
2859
|
+
relations,
|
|
2860
|
+
tables,
|
|
2861
|
+
tableInfo.name,
|
|
2862
|
+
allFields
|
|
2863
|
+
)
|
|
2864
|
+
});
|
|
2865
|
+
return result || null;
|
|
2820
2866
|
} catch (e) {
|
|
2821
2867
|
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2822
|
-
throw new
|
|
2868
|
+
throw new import_graphql9.GraphQLError(String(e.message));
|
|
2823
2869
|
}
|
|
2824
2870
|
throw e;
|
|
2825
2871
|
}
|
|
2826
2872
|
};
|
|
2827
2873
|
};
|
|
2874
|
+
|
|
2875
|
+
// src/buildSchemaSDL/generator/queries/index.ts
|
|
2828
2876
|
var generateQueries = (db, tables, relations) => {
|
|
2829
2877
|
const queries = {};
|
|
2830
2878
|
for (const [tableName, tableInfo] of Object.entries(tables)) {
|
|
@@ -2834,7 +2882,13 @@ var generateQueries = (db, tables, relations) => {
|
|
|
2834
2882
|
`Drizzle-GraphQL Error: Table ${tableName} not found in drizzle instance. Did you forget to pass schema to drizzle constructor?`
|
|
2835
2883
|
);
|
|
2836
2884
|
}
|
|
2837
|
-
queries[`${tableName}FindMany`] =
|
|
2885
|
+
queries[`${tableName}FindMany`] = createFindManyResolver(
|
|
2886
|
+
queryBase,
|
|
2887
|
+
tableInfo,
|
|
2888
|
+
tables,
|
|
2889
|
+
relations
|
|
2890
|
+
);
|
|
2891
|
+
queries[`${tableName}FindFirst`] = createFindFirstResolver(
|
|
2838
2892
|
queryBase,
|
|
2839
2893
|
tableInfo,
|
|
2840
2894
|
tables,
|
|
@@ -2844,49 +2898,123 @@ var generateQueries = (db, tables, relations) => {
|
|
|
2844
2898
|
return queries;
|
|
2845
2899
|
};
|
|
2846
2900
|
|
|
2847
|
-
// src/buildSchemaSDL/generator/mutations.ts
|
|
2848
|
-
var
|
|
2849
|
-
var
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2901
|
+
// src/buildSchemaSDL/generator/mutations/resolvers.ts
|
|
2902
|
+
var import_graphql10 = require("graphql");
|
|
2903
|
+
var createInsertManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn) => {
|
|
2904
|
+
const queryResolver = createFindManyResolver(
|
|
2905
|
+
queryBase,
|
|
2906
|
+
tableInfo,
|
|
2907
|
+
tables,
|
|
2908
|
+
relations
|
|
2909
|
+
);
|
|
2910
|
+
return async (parent, args, context, info) => {
|
|
2911
|
+
try {
|
|
2912
|
+
const { values } = args;
|
|
2913
|
+
if (!values || values.length === 0) {
|
|
2914
|
+
throw new import_graphql10.GraphQLError("No values provided for insert");
|
|
2915
|
+
}
|
|
2916
|
+
const remappedValues = remapFromGraphQLArrayInput(
|
|
2917
|
+
values,
|
|
2918
|
+
tableInfo.table
|
|
2919
|
+
);
|
|
2920
|
+
const insertedRows = await db.insert(tableInfo.table).values(remappedValues).returning();
|
|
2921
|
+
const insertedIds = insertedRows.map(
|
|
2922
|
+
(row) => row[primaryKeyColumn.name]
|
|
2923
|
+
);
|
|
2924
|
+
const result = await queryResolver(
|
|
2925
|
+
parent,
|
|
2926
|
+
{
|
|
2927
|
+
where: {
|
|
2928
|
+
[primaryKeyColumn.name]: { inArray: insertedIds }
|
|
2929
|
+
}
|
|
2930
|
+
},
|
|
2931
|
+
context,
|
|
2932
|
+
info
|
|
2858
2933
|
);
|
|
2934
|
+
return result;
|
|
2935
|
+
} catch (e) {
|
|
2936
|
+
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2937
|
+
throw new import_graphql10.GraphQLError(String(e.message));
|
|
2938
|
+
}
|
|
2939
|
+
throw e;
|
|
2859
2940
|
}
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
2941
|
+
};
|
|
2942
|
+
};
|
|
2943
|
+
var createUpdateManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn) => {
|
|
2944
|
+
const queryResolver = createFindManyResolver(
|
|
2945
|
+
queryBase,
|
|
2946
|
+
tableInfo,
|
|
2947
|
+
tables,
|
|
2948
|
+
relations
|
|
2949
|
+
);
|
|
2950
|
+
return async (parent, args, context, info) => {
|
|
2951
|
+
try {
|
|
2952
|
+
const { where, set } = args;
|
|
2953
|
+
if (!set || Object.keys(set).length === 0) {
|
|
2954
|
+
throw new import_graphql10.GraphQLError("No values provided for update");
|
|
2955
|
+
}
|
|
2956
|
+
const remappedSet = remapFromGraphQLSingleInput(set, tableInfo.table);
|
|
2957
|
+
const whereClause = buildWhereClause(tableInfo, where);
|
|
2958
|
+
let query = db.update(tableInfo.table).set(remappedSet);
|
|
2959
|
+
if (whereClause) {
|
|
2960
|
+
query = query.where(whereClause);
|
|
2961
|
+
}
|
|
2962
|
+
const updatedRows = await query.returning();
|
|
2963
|
+
const updatedIds = updatedRows.map(
|
|
2964
|
+
(row) => row[primaryKeyColumn.name]
|
|
2965
|
+
);
|
|
2966
|
+
const result = await queryResolver(
|
|
2967
|
+
parent,
|
|
2968
|
+
{
|
|
2969
|
+
where: {
|
|
2970
|
+
[primaryKeyColumn.name]: { inArray: updatedIds }
|
|
2971
|
+
}
|
|
2972
|
+
},
|
|
2973
|
+
context,
|
|
2974
|
+
info
|
|
2975
|
+
);
|
|
2976
|
+
return result;
|
|
2977
|
+
} catch (e) {
|
|
2978
|
+
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2979
|
+
throw new import_graphql10.GraphQLError(String(e.message));
|
|
2980
|
+
}
|
|
2981
|
+
throw e;
|
|
2865
2982
|
}
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2983
|
+
};
|
|
2984
|
+
};
|
|
2985
|
+
var createDeleteManyResolver = (db, queryBase, tableInfo, tables, relations, primaryKeyColumn) => {
|
|
2986
|
+
const queryResolver = createFindManyResolver(
|
|
2987
|
+
queryBase,
|
|
2988
|
+
tableInfo,
|
|
2989
|
+
tables,
|
|
2990
|
+
relations
|
|
2991
|
+
);
|
|
2992
|
+
return async (parent, args, context, info) => {
|
|
2993
|
+
try {
|
|
2994
|
+
const { where } = args;
|
|
2995
|
+
const whereClause = buildWhereClause(tableInfo, where);
|
|
2996
|
+
let deleteQuery = db.delete(tableInfo.table);
|
|
2997
|
+
if (whereClause) {
|
|
2998
|
+
deleteQuery = deleteQuery.where(whereClause);
|
|
2999
|
+
}
|
|
3000
|
+
const deletedRows = await deleteQuery.returning();
|
|
3001
|
+
const deletedItems = deletedRows.map((row) => ({
|
|
3002
|
+
id: row[primaryKeyColumn.name]
|
|
3003
|
+
}));
|
|
3004
|
+
return {
|
|
3005
|
+
deletedItems,
|
|
3006
|
+
[tableInfo.name + "FindMany"]: queryResolver
|
|
3007
|
+
};
|
|
3008
|
+
} catch (e) {
|
|
3009
|
+
if (typeof e === "object" && e !== null && "message" in e) {
|
|
3010
|
+
throw new import_graphql10.GraphQLError(String(e.message));
|
|
2881
3011
|
}
|
|
3012
|
+
throw e;
|
|
2882
3013
|
}
|
|
2883
|
-
}
|
|
2884
|
-
if (conditions.length === 0)
|
|
2885
|
-
return void 0;
|
|
2886
|
-
if (conditions.length === 1)
|
|
2887
|
-
return conditions[0];
|
|
2888
|
-
return (0, import_drizzle_orm10.and)(...conditions);
|
|
3014
|
+
};
|
|
2889
3015
|
};
|
|
3016
|
+
|
|
3017
|
+
// src/buildSchemaSDL/generator/mutations/index.ts
|
|
2890
3018
|
var generateMutations = (db, tables, relations) => {
|
|
2891
3019
|
const mutations = {};
|
|
2892
3020
|
const deleteResultResolvers = {};
|
|
@@ -2906,106 +3034,41 @@ var generateMutations = (db, tables, relations) => {
|
|
|
2906
3034
|
`Drizzle-GraphQL Error: Table ${tableName} does not have a primary key column`
|
|
2907
3035
|
);
|
|
2908
3036
|
}
|
|
2909
|
-
|
|
3037
|
+
mutations[`${tableName}InsertMany`] = createInsertManyResolver(
|
|
3038
|
+
db,
|
|
2910
3039
|
queryBase,
|
|
2911
3040
|
tableInfo,
|
|
2912
3041
|
tables,
|
|
2913
|
-
relations
|
|
3042
|
+
relations,
|
|
3043
|
+
primaryKeyColumn
|
|
2914
3044
|
);
|
|
2915
|
-
mutations[`${tableName}
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
context,
|
|
2937
|
-
info
|
|
2938
|
-
);
|
|
2939
|
-
return result.map((row) => ({
|
|
2940
|
-
...row,
|
|
2941
|
-
_operation: "INSERTED"
|
|
2942
|
-
}));
|
|
2943
|
-
} catch (e) {
|
|
2944
|
-
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2945
|
-
throw new import_graphql9.GraphQLError(String(e.message));
|
|
2946
|
-
}
|
|
2947
|
-
throw e;
|
|
2948
|
-
}
|
|
2949
|
-
};
|
|
2950
|
-
mutations[`${tableName}UpdateMany`] = async (parent, args, context, info) => {
|
|
2951
|
-
try {
|
|
2952
|
-
const { where, set } = args;
|
|
2953
|
-
if (!set || Object.keys(set).length === 0) {
|
|
2954
|
-
throw new import_graphql9.GraphQLError("No values provided for update");
|
|
2955
|
-
}
|
|
2956
|
-
const remappedSet = remapFromGraphQLSingleInput(set, tableInfo.table);
|
|
2957
|
-
const whereClause = buildWhereClause2(tableInfo, where);
|
|
2958
|
-
let query = db.update(tableInfo.table).set(remappedSet);
|
|
2959
|
-
if (whereClause) {
|
|
2960
|
-
query = query.where(whereClause);
|
|
2961
|
-
}
|
|
2962
|
-
const updatedRows = await query.returning();
|
|
2963
|
-
const updatedIds = updatedRows.map(
|
|
2964
|
-
(row) => row[primaryKeyColumn.name]
|
|
2965
|
-
);
|
|
2966
|
-
const result = await queryResolver(
|
|
2967
|
-
parent,
|
|
2968
|
-
{
|
|
2969
|
-
where: {
|
|
2970
|
-
[primaryKeyColumn.name]: { inArray: updatedIds }
|
|
2971
|
-
}
|
|
2972
|
-
},
|
|
2973
|
-
context,
|
|
2974
|
-
info
|
|
2975
|
-
);
|
|
2976
|
-
return result.map((row) => ({
|
|
2977
|
-
...row,
|
|
2978
|
-
_operation: "UPDATED"
|
|
2979
|
-
}));
|
|
2980
|
-
} catch (e) {
|
|
2981
|
-
if (typeof e === "object" && e !== null && "message" in e) {
|
|
2982
|
-
throw new import_graphql9.GraphQLError(String(e.message));
|
|
2983
|
-
}
|
|
2984
|
-
throw e;
|
|
2985
|
-
}
|
|
2986
|
-
};
|
|
2987
|
-
mutations[`${tableName}DeleteMany`] = async (parent, args, context, info) => {
|
|
2988
|
-
try {
|
|
2989
|
-
const { where } = args;
|
|
2990
|
-
const whereClause = buildWhereClause2(tableInfo, where);
|
|
2991
|
-
let deleteQuery = db.delete(tableInfo.table);
|
|
2992
|
-
if (whereClause) {
|
|
2993
|
-
deleteQuery = deleteQuery.where(whereClause);
|
|
2994
|
-
}
|
|
2995
|
-
const deletedRows = await deleteQuery.returning();
|
|
2996
|
-
return deletedRows.map((row) => ({
|
|
2997
|
-
...row,
|
|
2998
|
-
_operation: "DELETED"
|
|
2999
|
-
}));
|
|
3000
|
-
} catch (e) {
|
|
3001
|
-
if (typeof e === "object" && e !== null && "message" in e) {
|
|
3002
|
-
throw new import_graphql9.GraphQLError(String(e.message));
|
|
3045
|
+
mutations[`${tableName}UpdateMany`] = createUpdateManyResolver(
|
|
3046
|
+
db,
|
|
3047
|
+
queryBase,
|
|
3048
|
+
tableInfo,
|
|
3049
|
+
tables,
|
|
3050
|
+
relations,
|
|
3051
|
+
primaryKeyColumn
|
|
3052
|
+
);
|
|
3053
|
+
mutations[`${tableName}DeleteMany`] = createDeleteManyResolver(
|
|
3054
|
+
db,
|
|
3055
|
+
queryBase,
|
|
3056
|
+
tableInfo,
|
|
3057
|
+
tables,
|
|
3058
|
+
relations,
|
|
3059
|
+
primaryKeyColumn
|
|
3060
|
+
);
|
|
3061
|
+
deleteResultResolvers[`${capitalizedName}DeleteResult`] = {
|
|
3062
|
+
[`${tableName}FindMany`]: (parent, args, context, info) => {
|
|
3063
|
+
const resolverFn = parent[`${tableName}FindMany`];
|
|
3064
|
+
if (typeof resolverFn === "function") {
|
|
3065
|
+
return resolverFn(parent, args, context, info);
|
|
3003
3066
|
}
|
|
3004
|
-
|
|
3067
|
+
return [];
|
|
3005
3068
|
}
|
|
3006
3069
|
};
|
|
3007
3070
|
}
|
|
3008
|
-
return { mutations, deleteResultResolvers
|
|
3071
|
+
return { mutations, deleteResultResolvers };
|
|
3009
3072
|
};
|
|
3010
3073
|
|
|
3011
3074
|
// src/buildSchemaSDL/index.ts
|
|
@@ -3016,7 +3079,7 @@ var buildSchemaSDL = (db, config) => {
|
|
|
3016
3079
|
"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!"
|
|
3017
3080
|
);
|
|
3018
3081
|
}
|
|
3019
|
-
if (!(0,
|
|
3082
|
+
if (!(0, import_drizzle_orm12.is)(db, import_sqlite_core5.BaseSQLiteDatabase)) {
|
|
3020
3083
|
throw new Error(
|
|
3021
3084
|
"Drizzle-GraphQL Error: buildSchemaSDL currently only supports SQLite databases"
|
|
3022
3085
|
);
|
|
@@ -3044,13 +3107,13 @@ var buildSchemaSDL = (db, config) => {
|
|
|
3044
3107
|
};
|
|
3045
3108
|
|
|
3046
3109
|
// src/helpers.ts
|
|
3047
|
-
var
|
|
3110
|
+
var import_drizzle_orm13 = require("drizzle-orm");
|
|
3048
3111
|
function setCustomGraphQL(table, columnConfig) {
|
|
3049
3112
|
for (const [columnName, config] of Object.entries(columnConfig)) {
|
|
3050
3113
|
const column = table[columnName];
|
|
3051
3114
|
if (!column) {
|
|
3052
3115
|
console.warn(
|
|
3053
|
-
`Warning: Column "${columnName}" not found in table "${(0,
|
|
3116
|
+
`Warning: Column "${columnName}" not found in table "${(0, import_drizzle_orm13.getTableName)(
|
|
3054
3117
|
table
|
|
3055
3118
|
)}"`
|
|
3056
3119
|
);
|