drizzle-graphql-plus 0.8.28 → 0.8.30

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 CHANGED
@@ -2259,10 +2259,12 @@ var customScalars = /* @__PURE__ */ new Set();
2259
2259
  var enumDefinitions = /* @__PURE__ */ new Map();
2260
2260
  var requiredFieldFilters = /* @__PURE__ */ new Set();
2261
2261
  var foreignKeyTypes = /* @__PURE__ */ new Map();
2262
- var columnToSDL = (column, columnName, tableName, forceNullable = false) => {
2262
+ var columnToSDL = (column, columnName, tableName, forceNullable = false, isPrimaryKey = false) => {
2263
2263
  let baseType;
2264
2264
  if (column.customGraphqlType) {
2265
2265
  baseType = column.customGraphqlType;
2266
+ } else if (isPrimaryKey) {
2267
+ baseType = "ID";
2266
2268
  } else {
2267
2269
  const foreignKeyType = foreignKeyTypes.get(`${tableName}.${columnName}`);
2268
2270
  if (foreignKeyType) {
@@ -2394,11 +2396,16 @@ var generateTypeDefs = (tables, relations) => {
2394
2396
  continue;
2395
2397
  const referencedCustomType = referencedColumn.customGraphqlType;
2396
2398
  const foreignKeyHasCustomType = !!foreignKeyColumn.customGraphqlType;
2397
- if (referencedCustomType && !foreignKeyHasCustomType) {
2398
- foreignKeyTypes.set(
2399
- `${tableName}.${foreignKeyPropertyName}`,
2400
- referencedCustomType
2401
- );
2399
+ const referencedIsPrimaryKey = referencedColumn.primary || false;
2400
+ if (!foreignKeyHasCustomType) {
2401
+ if (referencedCustomType) {
2402
+ foreignKeyTypes.set(
2403
+ `${tableName}.${foreignKeyPropertyName}`,
2404
+ referencedCustomType
2405
+ );
2406
+ } else if (referencedIsPrimaryKey) {
2407
+ foreignKeyTypes.set(`${tableName}.${foreignKeyPropertyName}`, "ID");
2408
+ }
2402
2409
  }
2403
2410
  }
2404
2411
  }
@@ -2407,11 +2414,13 @@ var generateTypeDefs = (tables, relations) => {
2407
2414
  const typeName = capitalize(tableName);
2408
2415
  const fields = [];
2409
2416
  for (const [columnName, column] of Object.entries(tableInfo.columns)) {
2417
+ const isPrimaryKey = column.primary || false;
2410
2418
  const typeStr = columnToSDL(
2411
2419
  column,
2412
2420
  columnName,
2413
2421
  tableName,
2414
- false
2422
+ false,
2423
+ isPrimaryKey
2415
2424
  );
2416
2425
  const description = column.customGraphqlDescription;
2417
2426
  if (description) {
@@ -2443,11 +2452,13 @@ ${fields.join("\n")}
2443
2452
  }`);
2444
2453
  const insertFields = [];
2445
2454
  for (const [columnName, column] of Object.entries(tableInfo.columns)) {
2455
+ const isPrimaryKey = column.primary || false;
2446
2456
  const typeStr = columnToSDL(
2447
2457
  column,
2448
2458
  columnName,
2449
2459
  tableName,
2450
- false
2460
+ false,
2461
+ isPrimaryKey
2451
2462
  );
2452
2463
  const nullableType = typeStr.endsWith("!") ? typeStr.slice(0, -1) : typeStr;
2453
2464
  insertFields.push(` ${columnName}: ${nullableType}`);
@@ -2461,11 +2472,13 @@ ${insertFields.join("\n")}
2461
2472
  }
2462
2473
  const updateFields = [];
2463
2474
  for (const [columnName, column] of Object.entries(tableInfo.columns)) {
2475
+ const isPrimaryKey = column.primary || false;
2464
2476
  const typeStr = columnToSDL(
2465
2477
  column,
2466
2478
  columnName,
2467
2479
  tableName,
2468
- true
2480
+ true,
2481
+ isPrimaryKey
2469
2482
  );
2470
2483
  updateFields.push(` ${columnName}: ${typeStr}`);
2471
2484
  }
@@ -2476,11 +2489,13 @@ ${updateFields.join("\n")}
2476
2489
  );
2477
2490
  const whereFields = [];
2478
2491
  for (const [columnName, column] of Object.entries(tableInfo.columns)) {
2492
+ const isPrimaryKey = column.primary || false;
2479
2493
  const typeStr = columnToSDL(
2480
2494
  column,
2481
2495
  columnName,
2482
2496
  tableName,
2483
- true
2497
+ true,
2498
+ isPrimaryKey
2484
2499
  );
2485
2500
  const normalizedType = typeStr.replace(/[^a-zA-Z0-9]/g, "");
2486
2501
  const filterTypeName = `${normalizedType}FieldFilter`;
@@ -2716,6 +2731,126 @@ var buildWhereClause = (tableInfo, where) => {
2716
2731
  return (0, import_drizzle_orm10.and)(...conditions);
2717
2732
  };
2718
2733
 
2734
+ // src/export-tool/utils.ts
2735
+ function isExportVariable(value) {
2736
+ return typeof value === "string" && value.startsWith("$_") && value.length > 2;
2737
+ }
2738
+ function getVariableName(value) {
2739
+ if (!isExportVariable(value)) {
2740
+ return null;
2741
+ }
2742
+ return value.slice(2);
2743
+ }
2744
+ async function resolveExportVariables(args, exportStore, timeout, allowNull = true) {
2745
+ if (isExportVariable(args)) {
2746
+ const varName = getVariableName(args);
2747
+ return await exportStore.waitFor(varName, timeout, allowNull);
2748
+ }
2749
+ if (Array.isArray(args)) {
2750
+ const resolved = await Promise.all(
2751
+ args.map(async (item) => {
2752
+ if (isExportVariable(item)) {
2753
+ const varName = getVariableName(item);
2754
+ return await exportStore.waitFor(varName, timeout, allowNull);
2755
+ } else if (typeof item === "object" && item !== null) {
2756
+ return await resolveExportVariables(item, exportStore, timeout, allowNull);
2757
+ }
2758
+ return item;
2759
+ })
2760
+ );
2761
+ return resolved;
2762
+ }
2763
+ if (typeof args === "object" && args !== null) {
2764
+ const resolved = {};
2765
+ for (const [key, value] of Object.entries(args)) {
2766
+ if (isExportVariable(value)) {
2767
+ const varName = getVariableName(value);
2768
+ resolved[key] = await exportStore.waitFor(varName, timeout, allowNull);
2769
+ } else if (Array.isArray(value)) {
2770
+ resolved[key] = await resolveExportVariables(
2771
+ value,
2772
+ exportStore,
2773
+ timeout,
2774
+ allowNull
2775
+ );
2776
+ } else if (typeof value === "object" && value !== null) {
2777
+ resolved[key] = await resolveExportVariables(
2778
+ value,
2779
+ exportStore,
2780
+ timeout,
2781
+ allowNull
2782
+ );
2783
+ } else {
2784
+ resolved[key] = value;
2785
+ }
2786
+ }
2787
+ return resolved;
2788
+ }
2789
+ return args;
2790
+ }
2791
+ function getExportDirective(fieldNode) {
2792
+ const node = fieldNode;
2793
+ if (!node || !node.directives) {
2794
+ return null;
2795
+ }
2796
+ const exportDirective = node.directives.find(
2797
+ (directive) => directive.name.value === "export"
2798
+ );
2799
+ if (!exportDirective) {
2800
+ return null;
2801
+ }
2802
+ const asArg = exportDirective.arguments?.find(
2803
+ (arg) => arg.name.value === "as"
2804
+ );
2805
+ if (!asArg || asArg.value.kind !== "StringValue") {
2806
+ return null;
2807
+ }
2808
+ return asArg.value.value;
2809
+ }
2810
+ function hasExportVariables(args) {
2811
+ if (isExportVariable(args)) {
2812
+ return true;
2813
+ }
2814
+ if (Array.isArray(args)) {
2815
+ return args.some((item) => hasExportVariables(item));
2816
+ }
2817
+ if (typeof args === "object" && args !== null) {
2818
+ for (const value of Object.values(args)) {
2819
+ if (hasExportVariables(value)) {
2820
+ return true;
2821
+ }
2822
+ }
2823
+ }
2824
+ return false;
2825
+ }
2826
+ function processExports(result, selectionSet, exportStore) {
2827
+ if (!result || !selectionSet)
2828
+ return;
2829
+ for (const selection of selectionSet.selections) {
2830
+ if (selection.kind !== "Field")
2831
+ continue;
2832
+ const resultKey = selection.alias?.value ?? selection.name.value;
2833
+ if (!(resultKey in result))
2834
+ continue;
2835
+ const value = result[resultKey];
2836
+ const exportName = getExportDirective(selection);
2837
+ if (exportName) {
2838
+ exportStore.set(exportName, value);
2839
+ }
2840
+ if (selection.selectionSet && value !== null && value !== void 0) {
2841
+ if (Array.isArray(value)) {
2842
+ value.forEach((item) => {
2843
+ if (item && typeof item === "object") {
2844
+ processExports(item, selection.selectionSet, exportStore);
2845
+ }
2846
+ });
2847
+ } else if (typeof value === "object") {
2848
+ processExports(value, selection.selectionSet, exportStore);
2849
+ }
2850
+ }
2851
+ }
2852
+ }
2853
+
2719
2854
  // src/buildSchemaSDL/generator/utils/selection.ts
2720
2855
  var buildOrderByClause = (tableInfo, orderBy) => {
2721
2856
  if (!orderBy || Object.keys(orderBy).length === 0) {
@@ -2746,7 +2881,7 @@ var extractSelectedColumns = (fields, tableInfo) => {
2746
2881
  }
2747
2882
  return Object.keys(columns).length > 0 ? columns : {};
2748
2883
  };
2749
- var extractRelationsParams2 = (relationMap, tables, tableName, fields) => {
2884
+ var extractRelationsParams2 = async (relationMap, tables, tableName, fields, context) => {
2750
2885
  const relations = relationMap[tableName];
2751
2886
  if (!relations)
2752
2887
  return void 0;
@@ -2772,10 +2907,15 @@ var extractRelationsParams2 = (relationMap, tables, tableName, fields) => {
2772
2907
  const relationArgs = relationField.args;
2773
2908
  if (relationArgs) {
2774
2909
  if (relationArgs["where"]) {
2775
- thisRecord.where = buildWhereClause(
2776
- targetTable,
2777
- relationArgs["where"]
2778
- );
2910
+ let whereClause = relationArgs["where"];
2911
+ if (context?.exportStore && hasExportVariables(whereClause)) {
2912
+ try {
2913
+ whereClause = await resolveExportVariables(whereClause, context.exportStore);
2914
+ } catch (error) {
2915
+ console.warn(`Failed to resolve export variables in nested relation ${relName}:`, error);
2916
+ }
2917
+ }
2918
+ thisRecord.where = buildWhereClause(targetTable, whereClause);
2779
2919
  }
2780
2920
  if (relationArgs["orderBy"]) {
2781
2921
  thisRecord.orderBy = buildOrderByClause(
@@ -2790,11 +2930,12 @@ var extractRelationsParams2 = (relationMap, tables, tableName, fields) => {
2790
2930
  thisRecord.offset = relationArgs["offset"];
2791
2931
  }
2792
2932
  }
2793
- const nestedWith = extractRelationsParams2(
2933
+ const nestedWith = await extractRelationsParams2(
2794
2934
  relationMap,
2795
2935
  tables,
2796
2936
  targetTableName,
2797
- allFields
2937
+ allFields,
2938
+ context
2798
2939
  );
2799
2940
  if (nestedWith) {
2800
2941
  thisRecord.with = nestedWith;
@@ -2824,11 +2965,12 @@ var createFindManyResolver = (queryBase, tableInfo, tables, relations) => {
2824
2965
  limit,
2825
2966
  orderBy: buildOrderByClause(tableInfo, orderBy),
2826
2967
  where: buildWhereClause(tableInfo, where),
2827
- with: extractRelationsParams2(
2968
+ with: await extractRelationsParams2(
2828
2969
  relations,
2829
2970
  tables,
2830
2971
  tableInfo.name,
2831
- allFields
2972
+ allFields,
2973
+ context
2832
2974
  )
2833
2975
  });
2834
2976
  return result;
@@ -2857,11 +2999,12 @@ var createFindFirstResolver = (queryBase, tableInfo, tables, relations) => {
2857
2999
  columns: extractSelectedColumns(allFields, tableInfo),
2858
3000
  orderBy: buildOrderByClause(tableInfo, orderBy),
2859
3001
  where: buildWhereClause(tableInfo, where),
2860
- with: extractRelationsParams2(
3002
+ with: await extractRelationsParams2(
2861
3003
  relations,
2862
3004
  tables,
2863
3005
  tableInfo.name,
2864
- allFields
3006
+ allFields,
3007
+ context
2865
3008
  )
2866
3009
  });
2867
3010
  return result || null;
@@ -3134,8 +3277,9 @@ var ExportStore = class {
3134
3277
  * Wait for a value to be available
3135
3278
  * Returns immediately if value already exists
3136
3279
  * Returns a Promise that resolves when value is set
3280
+ * If timeout occurs and allowNull is true, resolves with null instead of rejecting
3137
3281
  */
3138
- async waitFor(name, timeout = 5e3) {
3282
+ async waitFor(name, timeout = 5e3, allowNull = false) {
3139
3283
  if (this.store.has(name)) {
3140
3284
  return this.store.get(name);
3141
3285
  }
@@ -3150,7 +3294,11 @@ var ExportStore = class {
3150
3294
  const index = callbacks.indexOf(resolve);
3151
3295
  if (index > -1) {
3152
3296
  callbacks.splice(index, 1);
3153
- reject(new Error(`Timeout waiting for export variable "${name}"`));
3297
+ if (allowNull) {
3298
+ resolve(null);
3299
+ } else {
3300
+ reject(new Error(`Timeout waiting for export variable "${name}"`));
3301
+ }
3154
3302
  }
3155
3303
  }
3156
3304
  }, timeout);
@@ -3177,124 +3325,6 @@ var ExportStore = class {
3177
3325
  }
3178
3326
  };
3179
3327
 
3180
- // src/export-tool/utils.ts
3181
- function isExportVariable(value) {
3182
- return typeof value === "string" && value.startsWith("$_") && value.length > 2;
3183
- }
3184
- function getVariableName(value) {
3185
- if (!isExportVariable(value)) {
3186
- return null;
3187
- }
3188
- return value.slice(2);
3189
- }
3190
- async function resolveExportVariables(args, exportStore, timeout) {
3191
- if (isExportVariable(args)) {
3192
- const varName = getVariableName(args);
3193
- return await exportStore.waitFor(varName, timeout);
3194
- }
3195
- if (Array.isArray(args)) {
3196
- const resolved = await Promise.all(
3197
- args.map(async (item) => {
3198
- if (isExportVariable(item)) {
3199
- const varName = getVariableName(item);
3200
- return await exportStore.waitFor(varName, timeout);
3201
- } else if (typeof item === "object" && item !== null) {
3202
- return await resolveExportVariables(item, exportStore, timeout);
3203
- }
3204
- return item;
3205
- })
3206
- );
3207
- return resolved;
3208
- }
3209
- if (typeof args === "object" && args !== null) {
3210
- const resolved = {};
3211
- for (const [key, value] of Object.entries(args)) {
3212
- if (isExportVariable(value)) {
3213
- const varName = getVariableName(value);
3214
- resolved[key] = await exportStore.waitFor(varName, timeout);
3215
- } else if (Array.isArray(value)) {
3216
- resolved[key] = await resolveExportVariables(
3217
- value,
3218
- exportStore,
3219
- timeout
3220
- );
3221
- } else if (typeof value === "object" && value !== null) {
3222
- resolved[key] = await resolveExportVariables(
3223
- value,
3224
- exportStore,
3225
- timeout
3226
- );
3227
- } else {
3228
- resolved[key] = value;
3229
- }
3230
- }
3231
- return resolved;
3232
- }
3233
- return args;
3234
- }
3235
- function getExportDirective(fieldNode) {
3236
- const node = fieldNode;
3237
- if (!node || !node.directives) {
3238
- return null;
3239
- }
3240
- const exportDirective = node.directives.find(
3241
- (directive) => directive.name.value === "export"
3242
- );
3243
- if (!exportDirective) {
3244
- return null;
3245
- }
3246
- const asArg = exportDirective.arguments?.find(
3247
- (arg) => arg.name.value === "as"
3248
- );
3249
- if (!asArg || asArg.value.kind !== "StringValue") {
3250
- return null;
3251
- }
3252
- return asArg.value.value;
3253
- }
3254
- function hasExportVariables(args) {
3255
- if (isExportVariable(args)) {
3256
- return true;
3257
- }
3258
- if (Array.isArray(args)) {
3259
- return args.some((item) => hasExportVariables(item));
3260
- }
3261
- if (typeof args === "object" && args !== null) {
3262
- for (const value of Object.values(args)) {
3263
- if (hasExportVariables(value)) {
3264
- return true;
3265
- }
3266
- }
3267
- }
3268
- return false;
3269
- }
3270
- function processExports(result, selectionSet, exportStore) {
3271
- if (!result || !selectionSet)
3272
- return;
3273
- for (const selection of selectionSet.selections) {
3274
- if (selection.kind !== "Field")
3275
- continue;
3276
- const resultKey = selection.alias?.value ?? selection.name.value;
3277
- if (!(resultKey in result))
3278
- continue;
3279
- const value = result[resultKey];
3280
- const exportName = getExportDirective(selection);
3281
- if (exportName) {
3282
- exportStore.set(exportName, value);
3283
- }
3284
- if (selection.selectionSet && value !== null && value !== void 0) {
3285
- if (Array.isArray(value)) {
3286
- value.forEach((item) => {
3287
- if (item && typeof item === "object") {
3288
- processExports(item, selection.selectionSet, exportStore);
3289
- }
3290
- });
3291
- } else if (typeof value === "object") {
3292
- processExports(value, selection.selectionSet, exportStore);
3293
- }
3294
- }
3295
- }
3296
- }
3297
-
3298
3328
  // src/export-tool/middleware.ts
3299
3329
  function createExportMiddleware() {
3300
3330
  return (next) => {
@@ -3308,6 +3338,11 @@ function createExportMiddleware() {
3308
3338
  try {
3309
3339
  resolvedArgs = await resolveExportVariables(args, exportStore);
3310
3340
  } catch (error) {
3341
+ if (error instanceof Error && error.message.includes("Timeout waiting for export variable")) {
3342
+ throw new Error(
3343
+ `Failed to resolve export variables in ${info.parentType.name}.${info.fieldName}: ${error.message}. Consider using a fallback value or checking if the exported field can return null.`
3344
+ );
3345
+ }
3311
3346
  throw new Error(
3312
3347
  `Failed to resolve export variables in ${info.parentType.name}.${info.fieldName}: ${error instanceof Error ? error.message : String(error)}`
3313
3348
  );
@@ -3318,7 +3353,7 @@ function createExportMiddleware() {
3318
3353
  if (!fieldNode)
3319
3354
  return result;
3320
3355
  const selfExportName = getExportDirective(fieldNode);
3321
- if (selfExportName && result !== void 0 && result !== null) {
3356
+ if (selfExportName && result !== void 0) {
3322
3357
  exportStore.set(selfExportName, result);
3323
3358
  }
3324
3359
  if (fieldNode.selectionSet && result !== void 0 && result !== null) {