drizzle-graphql-plus 0.8.29 → 0.8.31

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
@@ -2460,8 +2460,17 @@ ${fields.join("\n")}
2460
2460
  false,
2461
2461
  isPrimaryKey
2462
2462
  );
2463
- const nullableType = typeStr.endsWith("!") ? typeStr.slice(0, -1) : typeStr;
2464
- insertFields.push(` ${columnName}: ${nullableType}`);
2463
+ const hasDefault = column.hasDefault || column.default !== void 0;
2464
+ const isAutoIncrement = column.autoIncrement || column.generatedAlwaysAs;
2465
+ const isNotNull3 = column.notNull;
2466
+ const shouldBeOptional = hasDefault || isAutoIncrement || isPrimaryKey && !isNotNull3;
2467
+ let insertFieldType;
2468
+ if (shouldBeOptional) {
2469
+ insertFieldType = typeStr.endsWith("!") ? typeStr.slice(0, -1) : typeStr;
2470
+ } else {
2471
+ insertFieldType = isNotNull3 && !typeStr.endsWith("!") ? `${typeStr}!` : typeStr;
2472
+ }
2473
+ insertFields.push(` ${columnName}: ${insertFieldType}`);
2465
2474
  }
2466
2475
  if (insertFields.length > 0) {
2467
2476
  typeDefs.push(
@@ -2731,6 +2740,126 @@ var buildWhereClause = (tableInfo, where) => {
2731
2740
  return (0, import_drizzle_orm10.and)(...conditions);
2732
2741
  };
2733
2742
 
2743
+ // src/export-tool/utils.ts
2744
+ function isExportVariable(value) {
2745
+ return typeof value === "string" && value.startsWith("$_") && value.length > 2;
2746
+ }
2747
+ function getVariableName(value) {
2748
+ if (!isExportVariable(value)) {
2749
+ return null;
2750
+ }
2751
+ return value.slice(2);
2752
+ }
2753
+ async function resolveExportVariables(args, exportStore, timeout, allowNull = true) {
2754
+ if (isExportVariable(args)) {
2755
+ const varName = getVariableName(args);
2756
+ return await exportStore.waitFor(varName, timeout, allowNull);
2757
+ }
2758
+ if (Array.isArray(args)) {
2759
+ const resolved = await Promise.all(
2760
+ args.map(async (item) => {
2761
+ if (isExportVariable(item)) {
2762
+ const varName = getVariableName(item);
2763
+ return await exportStore.waitFor(varName, timeout, allowNull);
2764
+ } else if (typeof item === "object" && item !== null) {
2765
+ return await resolveExportVariables(item, exportStore, timeout, allowNull);
2766
+ }
2767
+ return item;
2768
+ })
2769
+ );
2770
+ return resolved;
2771
+ }
2772
+ if (typeof args === "object" && args !== null) {
2773
+ const resolved = {};
2774
+ for (const [key, value] of Object.entries(args)) {
2775
+ if (isExportVariable(value)) {
2776
+ const varName = getVariableName(value);
2777
+ resolved[key] = await exportStore.waitFor(varName, timeout, allowNull);
2778
+ } else if (Array.isArray(value)) {
2779
+ resolved[key] = await resolveExportVariables(
2780
+ value,
2781
+ exportStore,
2782
+ timeout,
2783
+ allowNull
2784
+ );
2785
+ } else if (typeof value === "object" && value !== null) {
2786
+ resolved[key] = await resolveExportVariables(
2787
+ value,
2788
+ exportStore,
2789
+ timeout,
2790
+ allowNull
2791
+ );
2792
+ } else {
2793
+ resolved[key] = value;
2794
+ }
2795
+ }
2796
+ return resolved;
2797
+ }
2798
+ return args;
2799
+ }
2800
+ function getExportDirective(fieldNode) {
2801
+ const node = fieldNode;
2802
+ if (!node || !node.directives) {
2803
+ return null;
2804
+ }
2805
+ const exportDirective = node.directives.find(
2806
+ (directive) => directive.name.value === "export"
2807
+ );
2808
+ if (!exportDirective) {
2809
+ return null;
2810
+ }
2811
+ const asArg = exportDirective.arguments?.find(
2812
+ (arg) => arg.name.value === "as"
2813
+ );
2814
+ if (!asArg || asArg.value.kind !== "StringValue") {
2815
+ return null;
2816
+ }
2817
+ return asArg.value.value;
2818
+ }
2819
+ function hasExportVariables(args) {
2820
+ if (isExportVariable(args)) {
2821
+ return true;
2822
+ }
2823
+ if (Array.isArray(args)) {
2824
+ return args.some((item) => hasExportVariables(item));
2825
+ }
2826
+ if (typeof args === "object" && args !== null) {
2827
+ for (const value of Object.values(args)) {
2828
+ if (hasExportVariables(value)) {
2829
+ return true;
2830
+ }
2831
+ }
2832
+ }
2833
+ return false;
2834
+ }
2835
+ function processExports(result, selectionSet, exportStore) {
2836
+ if (!result || !selectionSet)
2837
+ return;
2838
+ for (const selection of selectionSet.selections) {
2839
+ if (selection.kind !== "Field")
2840
+ continue;
2841
+ const resultKey = selection.alias?.value ?? selection.name.value;
2842
+ if (!(resultKey in result))
2843
+ continue;
2844
+ const value = result[resultKey];
2845
+ const exportName = getExportDirective(selection);
2846
+ if (exportName) {
2847
+ exportStore.set(exportName, value);
2848
+ }
2849
+ if (selection.selectionSet && value !== null && value !== void 0) {
2850
+ if (Array.isArray(value)) {
2851
+ value.forEach((item) => {
2852
+ if (item && typeof item === "object") {
2853
+ processExports(item, selection.selectionSet, exportStore);
2854
+ }
2855
+ });
2856
+ } else if (typeof value === "object") {
2857
+ processExports(value, selection.selectionSet, exportStore);
2858
+ }
2859
+ }
2860
+ }
2861
+ }
2862
+
2734
2863
  // src/buildSchemaSDL/generator/utils/selection.ts
2735
2864
  var buildOrderByClause = (tableInfo, orderBy) => {
2736
2865
  if (!orderBy || Object.keys(orderBy).length === 0) {
@@ -2761,7 +2890,7 @@ var extractSelectedColumns = (fields, tableInfo) => {
2761
2890
  }
2762
2891
  return Object.keys(columns).length > 0 ? columns : {};
2763
2892
  };
2764
- var extractRelationsParams2 = (relationMap, tables, tableName, fields) => {
2893
+ var extractRelationsParams2 = async (relationMap, tables, tableName, fields, context) => {
2765
2894
  const relations = relationMap[tableName];
2766
2895
  if (!relations)
2767
2896
  return void 0;
@@ -2787,10 +2916,15 @@ var extractRelationsParams2 = (relationMap, tables, tableName, fields) => {
2787
2916
  const relationArgs = relationField.args;
2788
2917
  if (relationArgs) {
2789
2918
  if (relationArgs["where"]) {
2790
- thisRecord.where = buildWhereClause(
2791
- targetTable,
2792
- relationArgs["where"]
2793
- );
2919
+ let whereClause = relationArgs["where"];
2920
+ if (context?.exportStore && hasExportVariables(whereClause)) {
2921
+ try {
2922
+ whereClause = await resolveExportVariables(whereClause, context.exportStore);
2923
+ } catch (error) {
2924
+ console.warn(`Failed to resolve export variables in nested relation ${relName}:`, error);
2925
+ }
2926
+ }
2927
+ thisRecord.where = buildWhereClause(targetTable, whereClause);
2794
2928
  }
2795
2929
  if (relationArgs["orderBy"]) {
2796
2930
  thisRecord.orderBy = buildOrderByClause(
@@ -2805,11 +2939,12 @@ var extractRelationsParams2 = (relationMap, tables, tableName, fields) => {
2805
2939
  thisRecord.offset = relationArgs["offset"];
2806
2940
  }
2807
2941
  }
2808
- const nestedWith = extractRelationsParams2(
2942
+ const nestedWith = await extractRelationsParams2(
2809
2943
  relationMap,
2810
2944
  tables,
2811
2945
  targetTableName,
2812
- allFields
2946
+ allFields,
2947
+ context
2813
2948
  );
2814
2949
  if (nestedWith) {
2815
2950
  thisRecord.with = nestedWith;
@@ -2839,11 +2974,12 @@ var createFindManyResolver = (queryBase, tableInfo, tables, relations) => {
2839
2974
  limit,
2840
2975
  orderBy: buildOrderByClause(tableInfo, orderBy),
2841
2976
  where: buildWhereClause(tableInfo, where),
2842
- with: extractRelationsParams2(
2977
+ with: await extractRelationsParams2(
2843
2978
  relations,
2844
2979
  tables,
2845
2980
  tableInfo.name,
2846
- allFields
2981
+ allFields,
2982
+ context
2847
2983
  )
2848
2984
  });
2849
2985
  return result;
@@ -2872,11 +3008,12 @@ var createFindFirstResolver = (queryBase, tableInfo, tables, relations) => {
2872
3008
  columns: extractSelectedColumns(allFields, tableInfo),
2873
3009
  orderBy: buildOrderByClause(tableInfo, orderBy),
2874
3010
  where: buildWhereClause(tableInfo, where),
2875
- with: extractRelationsParams2(
3011
+ with: await extractRelationsParams2(
2876
3012
  relations,
2877
3013
  tables,
2878
3014
  tableInfo.name,
2879
- allFields
3015
+ allFields,
3016
+ context
2880
3017
  )
2881
3018
  });
2882
3019
  return result || null;
@@ -2934,7 +3071,9 @@ var createInsertManyResolver = (db, queryBase, tableInfo, tables, relations, pri
2934
3071
  values,
2935
3072
  tableInfo.table
2936
3073
  );
2937
- const insertedRows = await db.insert(tableInfo.table).values(remappedValues).returning();
3074
+ const insertedRows = await db.insert(tableInfo.table).values(remappedValues).returning({
3075
+ [primaryKeyColumn.name]: primaryKeyColumn
3076
+ });
2938
3077
  const insertedIds = insertedRows.map(
2939
3078
  (row) => row[primaryKeyColumn.name]
2940
3079
  );
@@ -2976,7 +3115,9 @@ var createUpdateManyResolver = (db, queryBase, tableInfo, tables, relations, pri
2976
3115
  if (whereClause) {
2977
3116
  query = query.where(whereClause);
2978
3117
  }
2979
- const updatedRows = await query.returning();
3118
+ const updatedRows = await query.returning({
3119
+ [primaryKeyColumn.name]: primaryKeyColumn
3120
+ });
2980
3121
  const updatedIds = updatedRows.map(
2981
3122
  (row) => row[primaryKeyColumn.name]
2982
3123
  );
@@ -3014,7 +3155,9 @@ var createDeleteManyResolver = (db, queryBase, tableInfo, tables, relations, pri
3014
3155
  if (whereClause) {
3015
3156
  deleteQuery = deleteQuery.where(whereClause);
3016
3157
  }
3017
- const deletedRows = await deleteQuery.returning();
3158
+ const deletedRows = await deleteQuery.returning({
3159
+ [primaryKeyColumn.name]: primaryKeyColumn
3160
+ });
3018
3161
  const deletedItems = deletedRows.map((row) => ({
3019
3162
  id: row[primaryKeyColumn.name]
3020
3163
  }));
@@ -3149,8 +3292,9 @@ var ExportStore = class {
3149
3292
  * Wait for a value to be available
3150
3293
  * Returns immediately if value already exists
3151
3294
  * Returns a Promise that resolves when value is set
3295
+ * If timeout occurs and allowNull is true, resolves with null instead of rejecting
3152
3296
  */
3153
- async waitFor(name, timeout = 5e3) {
3297
+ async waitFor(name, timeout = 5e3, allowNull = false) {
3154
3298
  if (this.store.has(name)) {
3155
3299
  return this.store.get(name);
3156
3300
  }
@@ -3165,7 +3309,11 @@ var ExportStore = class {
3165
3309
  const index = callbacks.indexOf(resolve);
3166
3310
  if (index > -1) {
3167
3311
  callbacks.splice(index, 1);
3168
- reject(new Error(`Timeout waiting for export variable "${name}"`));
3312
+ if (allowNull) {
3313
+ resolve(null);
3314
+ } else {
3315
+ reject(new Error(`Timeout waiting for export variable "${name}"`));
3316
+ }
3169
3317
  }
3170
3318
  }
3171
3319
  }, timeout);
@@ -3192,124 +3340,6 @@ var ExportStore = class {
3192
3340
  }
3193
3341
  };
3194
3342
 
3195
- // src/export-tool/utils.ts
3196
- function isExportVariable(value) {
3197
- return typeof value === "string" && value.startsWith("$_") && value.length > 2;
3198
- }
3199
- function getVariableName(value) {
3200
- if (!isExportVariable(value)) {
3201
- return null;
3202
- }
3203
- return value.slice(2);
3204
- }
3205
- async function resolveExportVariables(args, exportStore, timeout) {
3206
- if (isExportVariable(args)) {
3207
- const varName = getVariableName(args);
3208
- return await exportStore.waitFor(varName, timeout);
3209
- }
3210
- if (Array.isArray(args)) {
3211
- const resolved = await Promise.all(
3212
- args.map(async (item) => {
3213
- if (isExportVariable(item)) {
3214
- const varName = getVariableName(item);
3215
- return await exportStore.waitFor(varName, timeout);
3216
- } else if (typeof item === "object" && item !== null) {
3217
- return await resolveExportVariables(item, exportStore, timeout);
3218
- }
3219
- return item;
3220
- })
3221
- );
3222
- return resolved;
3223
- }
3224
- if (typeof args === "object" && args !== null) {
3225
- const resolved = {};
3226
- for (const [key, value] of Object.entries(args)) {
3227
- if (isExportVariable(value)) {
3228
- const varName = getVariableName(value);
3229
- resolved[key] = await exportStore.waitFor(varName, timeout);
3230
- } else if (Array.isArray(value)) {
3231
- resolved[key] = await resolveExportVariables(
3232
- value,
3233
- exportStore,
3234
- timeout
3235
- );
3236
- } else if (typeof value === "object" && value !== null) {
3237
- resolved[key] = await resolveExportVariables(
3238
- value,
3239
- exportStore,
3240
- timeout
3241
- );
3242
- } else {
3243
- resolved[key] = value;
3244
- }
3245
- }
3246
- return resolved;
3247
- }
3248
- return args;
3249
- }
3250
- function getExportDirective(fieldNode) {
3251
- const node = fieldNode;
3252
- if (!node || !node.directives) {
3253
- return null;
3254
- }
3255
- const exportDirective = node.directives.find(
3256
- (directive) => directive.name.value === "export"
3257
- );
3258
- if (!exportDirective) {
3259
- return null;
3260
- }
3261
- const asArg = exportDirective.arguments?.find(
3262
- (arg) => arg.name.value === "as"
3263
- );
3264
- if (!asArg || asArg.value.kind !== "StringValue") {
3265
- return null;
3266
- }
3267
- return asArg.value.value;
3268
- }
3269
- function hasExportVariables(args) {
3270
- if (isExportVariable(args)) {
3271
- return true;
3272
- }
3273
- if (Array.isArray(args)) {
3274
- return args.some((item) => hasExportVariables(item));
3275
- }
3276
- if (typeof args === "object" && args !== null) {
3277
- for (const value of Object.values(args)) {
3278
- if (hasExportVariables(value)) {
3279
- return true;
3280
- }
3281
- }
3282
- }
3283
- return false;
3284
- }
3285
- function processExports(result, selectionSet, exportStore) {
3286
- if (!result || !selectionSet)
3287
- return;
3288
- for (const selection of selectionSet.selections) {
3289
- if (selection.kind !== "Field")
3290
- continue;
3291
- const resultKey = selection.alias?.value ?? selection.name.value;
3292
- if (!(resultKey in result))
3293
- continue;
3294
- const value = result[resultKey];
3295
- const exportName = getExportDirective(selection);
3296
- if (exportName) {
3297
- exportStore.set(exportName, value);
3298
- }
3299
- if (selection.selectionSet && value !== null && value !== void 0) {
3300
- if (Array.isArray(value)) {
3301
- value.forEach((item) => {
3302
- if (item && typeof item === "object") {
3303
- processExports(item, selection.selectionSet, exportStore);
3304
- }
3305
- });
3306
- } else if (typeof value === "object") {
3307
- processExports(value, selection.selectionSet, exportStore);
3308
- }
3309
- }
3310
- }
3311
- }
3312
-
3313
3343
  // src/export-tool/middleware.ts
3314
3344
  function createExportMiddleware() {
3315
3345
  return (next) => {
@@ -3323,6 +3353,11 @@ function createExportMiddleware() {
3323
3353
  try {
3324
3354
  resolvedArgs = await resolveExportVariables(args, exportStore);
3325
3355
  } catch (error) {
3356
+ if (error instanceof Error && error.message.includes("Timeout waiting for export variable")) {
3357
+ throw new Error(
3358
+ `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.`
3359
+ );
3360
+ }
3326
3361
  throw new Error(
3327
3362
  `Failed to resolve export variables in ${info.parentType.name}.${info.fieldName}: ${error instanceof Error ? error.message : String(error)}`
3328
3363
  );
@@ -3333,7 +3368,7 @@ function createExportMiddleware() {
3333
3368
  if (!fieldNode)
3334
3369
  return result;
3335
3370
  const selfExportName = getExportDirective(fieldNode);
3336
- if (selfExportName && result !== void 0 && result !== null) {
3371
+ if (selfExportName && result !== void 0) {
3337
3372
  exportStore.set(selfExportName, result);
3338
3373
  }
3339
3374
  if (fieldNode.selectionSet && result !== void 0 && result !== null) {