@vantreeseba/drizzle-graphql 5.4.0 → 5.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -598,6 +598,203 @@ var resolveQueryExecutor = (db, context, tableName, buildTimeQueryBase) => {
598
598
  queryBase: buildTimeQueryBase ? executor?.query?.[tableName] ?? buildTimeQueryBase : buildTimeQueryBase
599
599
  };
600
600
  };
601
+ var createMutationTxCtx = (options) => options ? { timeoutMs: options.timeoutMs, fieldNames: /* @__PURE__ */ new Set(), states: /* @__PURE__ */ new WeakMap() } : void 0;
602
+ var includedByDirectives = (node, variables) => {
603
+ for (const directive of node.directives ?? []) {
604
+ const name = directive.name.value;
605
+ if (name !== "skip" && name !== "include") {
606
+ continue;
607
+ }
608
+ const ifArg = directive.arguments?.find((arg) => arg.name.value === "if");
609
+ let value;
610
+ if (ifArg?.value.kind === "BooleanValue") {
611
+ value = ifArg.value.value;
612
+ } else if (ifArg?.value.kind === "Variable") {
613
+ value = variables?.[ifArg.value.name.value];
614
+ }
615
+ if (name === "skip" && value === true) {
616
+ return false;
617
+ }
618
+ if (name === "include" && value !== true) {
619
+ return false;
620
+ }
621
+ }
622
+ return true;
623
+ };
624
+ var collectRootMutationKeys = (selectionSet, fragments, variables, known, keys, visitedFragments) => {
625
+ for (const selection of selectionSet.selections) {
626
+ if (!includedByDirectives(selection, variables)) {
627
+ continue;
628
+ }
629
+ if (selection.kind === "Field") {
630
+ const name = selection.name.value;
631
+ if (name === "__typename") {
632
+ continue;
633
+ }
634
+ if (!known.has(name)) {
635
+ return false;
636
+ }
637
+ keys.add(selection.alias?.value ?? name);
638
+ } else if (selection.kind === "InlineFragment") {
639
+ if (!collectRootMutationKeys(selection.selectionSet, fragments, variables, known, keys, visitedFragments)) {
640
+ return false;
641
+ }
642
+ } else if (selection.kind === "FragmentSpread") {
643
+ const fragmentName = selection.name.value;
644
+ if (visitedFragments.has(fragmentName)) {
645
+ continue;
646
+ }
647
+ visitedFragments.add(fragmentName);
648
+ const fragment = fragments?.[fragmentName];
649
+ if (!fragment) {
650
+ return false;
651
+ }
652
+ if (!collectRootMutationKeys(fragment.selectionSet, fragments, variables, known, keys, visitedFragments)) {
653
+ return false;
654
+ }
655
+ }
656
+ }
657
+ return true;
658
+ };
659
+ var abortedBatchError = () => new import_graphql4.GraphQLError(
660
+ "Drizzle-GraphQL Error: Mutation was not executed because an earlier mutation in the same request failed and the shared transaction was rolled back."
661
+ );
662
+ var openSharedTx = (db, context, txCtx, stateKey, expected) => {
663
+ let release;
664
+ let rejectLatch;
665
+ const latch = new Promise((resolve, reject) => {
666
+ release = resolve;
667
+ rejectLatch = reject;
668
+ });
669
+ let readyResolve;
670
+ let readyReject;
671
+ const ready = new Promise((resolve, reject) => {
672
+ readyResolve = resolve;
673
+ readyReject = reject;
674
+ });
675
+ ready.catch(() => {
676
+ });
677
+ const canStash = context !== null && typeof context === "object" && !Object.isFrozen(context);
678
+ const settled = db.transaction(async (tx) => {
679
+ if (canStash) {
680
+ context[drizzleExecutorKey] = tx;
681
+ }
682
+ readyResolve(tx);
683
+ await latch;
684
+ });
685
+ let timer;
686
+ const clearTimer = () => {
687
+ if (timer !== void 0) {
688
+ clearTimeout(timer);
689
+ timer = void 0;
690
+ }
691
+ };
692
+ const state = {
693
+ kind: "shared",
694
+ expected,
695
+ completed: 0,
696
+ failure: void 0,
697
+ aborted: false,
698
+ done: false,
699
+ ready,
700
+ settled,
701
+ release,
702
+ abort: (e) => {
703
+ if (!state.aborted) {
704
+ state.aborted = true;
705
+ state.failure = e;
706
+ }
707
+ rejectLatch(e);
708
+ },
709
+ suspendTimer: clearTimer,
710
+ bumpTimer: () => {
711
+ clearTimer();
712
+ if (state.done) {
713
+ return;
714
+ }
715
+ timer = setTimeout(() => {
716
+ state.abort(
717
+ new import_graphql4.GraphQLError(
718
+ `Drizzle-GraphQL Error: Shared mutation transaction timed out after ${txCtx.timeoutMs}ms waiting for the remaining mutation fields; rolling back.`
719
+ )
720
+ );
721
+ }, txCtx.timeoutMs);
722
+ timer.unref?.();
723
+ }
724
+ };
725
+ const cleanup = () => {
726
+ state.done = true;
727
+ clearTimer();
728
+ if (canStash) {
729
+ delete context[drizzleExecutorKey];
730
+ }
731
+ if (!state.aborted) {
732
+ txCtx.states.delete(stateKey);
733
+ }
734
+ };
735
+ settled.then(cleanup, (e) => {
736
+ cleanup();
737
+ readyReject(e);
738
+ });
739
+ state.bumpTimer();
740
+ return state;
741
+ };
742
+ var runOnSharedTx = async (state, body) => {
743
+ if (state.aborted) {
744
+ await state.settled.catch(() => {
745
+ });
746
+ throw abortedBatchError();
747
+ }
748
+ state.suspendTimer();
749
+ const tx = await state.ready;
750
+ try {
751
+ if (state.aborted) {
752
+ throw abortedBatchError();
753
+ }
754
+ const result = await body(tx);
755
+ state.completed += 1;
756
+ if (state.completed >= state.expected) {
757
+ state.release();
758
+ await state.settled;
759
+ } else {
760
+ state.bumpTimer();
761
+ }
762
+ return result;
763
+ } catch (e) {
764
+ state.abort(e);
765
+ await state.settled.catch(() => {
766
+ });
767
+ throw e;
768
+ }
769
+ };
770
+ var runMutation = async (db, context, info, txCtx, body) => {
771
+ const contextIsObject = context !== null && (typeof context === "object" || typeof context === "function");
772
+ const stateKey = contextIsObject ? context : info.operation;
773
+ let state = txCtx?.states.get(stateKey);
774
+ if (!state) {
775
+ if (contextIsObject && context[drizzleExecutorKey]) {
776
+ return body(context[drizzleExecutorKey]);
777
+ }
778
+ if (!txCtx || info.operation.operation !== "mutation") {
779
+ return body(db);
780
+ }
781
+ const keys = /* @__PURE__ */ new Set();
782
+ const allKnown = collectRootMutationKeys(
783
+ info.operation.selectionSet,
784
+ info.fragments,
785
+ info.variableValues,
786
+ txCtx.fieldNames,
787
+ keys,
788
+ /* @__PURE__ */ new Set()
789
+ );
790
+ state = !allKnown || keys.size <= 1 ? { kind: "passthrough" } : openSharedTx(db, context, txCtx, stateKey, keys.size);
791
+ txCtx.states.set(stateKey, state);
792
+ }
793
+ if (state.kind === "passthrough") {
794
+ return body(resolveExecutor(db, context));
795
+ }
796
+ return runOnSharedTx(state, body);
797
+ };
601
798
  var batchedPaginatedRelationQuery = async (db, targetTable, foreignCol, whereCondition, orderByArg, limit, offset, pkNames, orderCtx) => {
602
799
  const cols = (0, import_drizzle_orm3.getColumns)(targetTable);
603
800
  const orderExprs = [
@@ -3009,7 +3206,7 @@ var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filte
3009
3206
  args: queryArgs
3010
3207
  };
3011
3208
  };
3012
- var generateInsertArray = (db, _tableName, table, baseType, fieldName) => {
3209
+ var generateInsertArray = (db, _tableName, table, baseType, fieldName, txCtx) => {
3013
3210
  const queryArgs = {
3014
3211
  values: {
3015
3212
  type: new import_graphql6.GraphQLNonNull(new import_graphql6.GraphQLList(new import_graphql6.GraphQLNonNull(baseType)))
@@ -3017,14 +3214,16 @@ var generateInsertArray = (db, _tableName, table, baseType, fieldName) => {
3017
3214
  };
3018
3215
  return {
3019
3216
  name: fieldName,
3020
- resolver: async (_source, args, context, _info) => {
3217
+ resolver: async (_source, args, context, info) => {
3021
3218
  try {
3022
- const input = remapFromGraphQLArrayInput(args.values, table);
3023
- if (!input.length) {
3024
- throw new import_graphql6.GraphQLError("No values were provided!");
3025
- }
3026
- await resolveExecutor(db, context).insert(table).values(input);
3027
- return { isSuccess: true };
3219
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3220
+ const input = remapFromGraphQLArrayInput(args.values, table);
3221
+ if (!input.length) {
3222
+ throw new import_graphql6.GraphQLError("No values were provided!");
3223
+ }
3224
+ await executor.insert(table).values(input);
3225
+ return { isSuccess: true };
3226
+ });
3028
3227
  } catch (e) {
3029
3228
  throw toGraphQLError(e);
3030
3229
  }
@@ -3032,7 +3231,7 @@ var generateInsertArray = (db, _tableName, table, baseType, fieldName) => {
3032
3231
  args: queryArgs
3033
3232
  };
3034
3233
  };
3035
- var generateInsertSingle = (db, _tableName, table, baseType, fieldName) => {
3234
+ var generateInsertSingle = (db, _tableName, table, baseType, fieldName, txCtx) => {
3036
3235
  const queryArgs = {
3037
3236
  values: {
3038
3237
  type: new import_graphql6.GraphQLNonNull(baseType)
@@ -3040,11 +3239,13 @@ var generateInsertSingle = (db, _tableName, table, baseType, fieldName) => {
3040
3239
  };
3041
3240
  return {
3042
3241
  name: fieldName,
3043
- resolver: async (_source, args, context, _info) => {
3242
+ resolver: async (_source, args, context, info) => {
3044
3243
  try {
3045
- const input = remapFromGraphQLSingleInput(args.values, table);
3046
- await resolveExecutor(db, context).insert(table).values(input);
3047
- return { isSuccess: true };
3244
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3245
+ const input = remapFromGraphQLSingleInput(args.values, table);
3246
+ await executor.insert(table).values(input);
3247
+ return { isSuccess: true };
3248
+ });
3048
3249
  } catch (e) {
3049
3250
  throw toGraphQLError(e);
3050
3251
  }
@@ -3052,7 +3253,7 @@ var generateInsertSingle = (db, _tableName, table, baseType, fieldName) => {
3052
3253
  args: queryArgs
3053
3254
  };
3054
3255
  };
3055
- var generateUpsert = (db, table, baseType, onConflictType, fieldName, single) => {
3256
+ var generateUpsert = (db, table, baseType, onConflictType, fieldName, single, txCtx) => {
3056
3257
  const queryArgs = {
3057
3258
  values: {
3058
3259
  type: single ? new import_graphql6.GraphQLNonNull(baseType) : new import_graphql6.GraphQLNonNull(new import_graphql6.GraphQLList(new import_graphql6.GraphQLNonNull(baseType)))
@@ -3065,28 +3266,29 @@ var generateUpsert = (db, table, baseType, onConflictType, fieldName, single) =>
3065
3266
  const pkNames = mysqlPrimaryKeyPropNames(table);
3066
3267
  return {
3067
3268
  name: fieldName,
3068
- resolver: async (_source, args, context, _info) => {
3269
+ resolver: async (_source, args, context, info) => {
3069
3270
  try {
3070
- const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
3071
- if (!input.length) {
3072
- throw new import_graphql6.GraphQLError("No values were provided!");
3073
- }
3074
- const plan = resolveConflictPlan({
3075
- table,
3076
- values: input,
3077
- onConflict: args.onConflict,
3078
- pkNames,
3079
- uniqueSets: [],
3080
- excludedRef: mysqlValuesColumnRef,
3081
- withTarget: false
3271
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3272
+ const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
3273
+ if (!input.length) {
3274
+ throw new import_graphql6.GraphQLError("No values were provided!");
3275
+ }
3276
+ const plan = resolveConflictPlan({
3277
+ table,
3278
+ values: input,
3279
+ onConflict: args.onConflict,
3280
+ pkNames,
3281
+ uniqueSets: [],
3282
+ excludedRef: mysqlValuesColumnRef,
3283
+ withTarget: false
3284
+ });
3285
+ if (plan.action === "NOTHING") {
3286
+ await executor.insert(table).ignore().values(input);
3287
+ } else {
3288
+ await executor.insert(table).values(input).onDuplicateKeyUpdate({ set: plan.set });
3289
+ }
3290
+ return { isSuccess: true };
3082
3291
  });
3083
- const executor = resolveExecutor(db, context);
3084
- if (plan.action === "NOTHING") {
3085
- await executor.insert(table).ignore().values(input);
3086
- } else {
3087
- await executor.insert(table).values(input).onDuplicateKeyUpdate({ set: plan.set });
3088
- }
3089
- return { isSuccess: true };
3090
3292
  } catch (e) {
3091
3293
  throw toGraphQLError(e);
3092
3294
  }
@@ -3094,7 +3296,7 @@ var generateUpsert = (db, table, baseType, onConflictType, fieldName, single) =>
3094
3296
  args: queryArgs
3095
3297
  };
3096
3298
  };
3097
- var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, single, requireWhere, filterCtx) => {
3299
+ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, single, requireWhere, filterCtx, txCtx) => {
3098
3300
  const queryArgs = {
3099
3301
  set: {
3100
3302
  type: new import_graphql6.GraphQLNonNull(setArgs)
@@ -3105,25 +3307,26 @@ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, sing
3105
3307
  };
3106
3308
  return {
3107
3309
  name: fieldName,
3108
- resolver: async (_source, args, context, _info) => {
3310
+ resolver: async (_source, args, context, info) => {
3109
3311
  try {
3110
- const { where, set } = args;
3111
- const input = remapFromGraphQLSingleInput(set, table);
3112
- if (!Object.keys(input).length) {
3113
- throw new import_graphql6.GraphQLError("Unable to update with no values specified!");
3114
- }
3115
- const relationCtx = relationFilterCtx(filterCtx, tableName);
3116
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3117
- const executor = resolveExecutor(db, context);
3118
- if (single) {
3119
- await assertSingleMatch(executor, table, filters, fieldName);
3120
- }
3121
- let query = executor.update(table).set(input);
3122
- if (filters) {
3123
- query = query.where(filters);
3124
- }
3125
- await query;
3126
- return { isSuccess: true };
3312
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3313
+ const { where, set } = args;
3314
+ const input = remapFromGraphQLSingleInput(set, table);
3315
+ if (!Object.keys(input).length) {
3316
+ throw new import_graphql6.GraphQLError("Unable to update with no values specified!");
3317
+ }
3318
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
3319
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3320
+ if (single) {
3321
+ await assertSingleMatch(executor, table, filters, fieldName);
3322
+ }
3323
+ let query = executor.update(table).set(input);
3324
+ if (filters) {
3325
+ query = query.where(filters);
3326
+ }
3327
+ await query;
3328
+ return { isSuccess: true };
3329
+ });
3127
3330
  } catch (e) {
3128
3331
  throw toGraphQLError(e);
3129
3332
  }
@@ -3131,7 +3334,7 @@ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, sing
3131
3334
  args: queryArgs
3132
3335
  };
3133
3336
  };
3134
- var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filterCtx) => {
3337
+ var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filterCtx, txCtx) => {
3135
3338
  const queryArgs = {
3136
3339
  updates: {
3137
3340
  type: new import_graphql6.GraphQLNonNull(new import_graphql6.GraphQLList(new import_graphql6.GraphQLNonNull(updateManyInput)))
@@ -3139,33 +3342,34 @@ var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filt
3139
3342
  };
3140
3343
  return {
3141
3344
  name: fieldName,
3142
- resolver: async (_source, args, context, _info) => {
3345
+ resolver: async (_source, args, context, info) => {
3143
3346
  try {
3144
- const { updates } = args;
3145
- if (!updates.length) {
3146
- throw new import_graphql6.GraphQLError("No updates were provided!");
3147
- }
3148
- const entries = updates.map(({ where, set }) => {
3149
- const input = remapFromGraphQLSingleInput(set, table);
3150
- if (!Object.keys(input).length) {
3151
- throw new import_graphql6.GraphQLError("Unable to update with no values specified!");
3347
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3348
+ const { updates } = args;
3349
+ if (!updates.length) {
3350
+ throw new import_graphql6.GraphQLError("No updates were provided!");
3152
3351
  }
3153
- return {
3154
- set: input,
3155
- filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
3156
- };
3157
- });
3158
- const executor = resolveExecutor(db, context);
3159
- await executor.transaction(async (tx) => {
3160
- for (const entry of entries) {
3161
- let query = tx.update(table).set(entry.set);
3162
- if (entry.filters) {
3163
- query = query.where(entry.filters);
3352
+ const entries = updates.map(({ where, set }) => {
3353
+ const input = remapFromGraphQLSingleInput(set, table);
3354
+ if (!Object.keys(input).length) {
3355
+ throw new import_graphql6.GraphQLError("Unable to update with no values specified!");
3164
3356
  }
3165
- await query;
3166
- }
3357
+ return {
3358
+ set: input,
3359
+ filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
3360
+ };
3361
+ });
3362
+ await executor.transaction(async (tx) => {
3363
+ for (const entry of entries) {
3364
+ let query = tx.update(table).set(entry.set);
3365
+ if (entry.filters) {
3366
+ query = query.where(entry.filters);
3367
+ }
3368
+ await query;
3369
+ }
3370
+ });
3371
+ return { isSuccess: true };
3167
3372
  });
3168
- return { isSuccess: true };
3169
3373
  } catch (e) {
3170
3374
  throw toGraphQLError(e);
3171
3375
  }
@@ -3173,7 +3377,7 @@ var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filt
3173
3377
  args: queryArgs
3174
3378
  };
3175
3379
  };
3176
- var generateDelete = (db, tableName, table, filterArgs, fieldName, single, requireWhere, filterCtx) => {
3380
+ var generateDelete = (db, tableName, table, filterArgs, fieldName, single, requireWhere, filterCtx, txCtx) => {
3177
3381
  const queryArgs = {
3178
3382
  where: {
3179
3383
  type: single || requireWhere ? new import_graphql6.GraphQLNonNull(filterArgs) : filterArgs
@@ -3181,21 +3385,22 @@ var generateDelete = (db, tableName, table, filterArgs, fieldName, single, requi
3181
3385
  };
3182
3386
  return {
3183
3387
  name: fieldName,
3184
- resolver: async (_source, args, context, _info) => {
3388
+ resolver: async (_source, args, context, info) => {
3185
3389
  try {
3186
- const { where } = args;
3187
- const relationCtx = relationFilterCtx(filterCtx, tableName);
3188
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3189
- const executor = resolveExecutor(db, context);
3190
- if (single) {
3191
- await assertSingleMatch(executor, table, filters, fieldName);
3192
- }
3193
- let query = executor.delete(table);
3194
- if (filters) {
3195
- query = query.where(filters);
3196
- }
3197
- await query;
3198
- return { isSuccess: true };
3390
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3391
+ const { where } = args;
3392
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
3393
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3394
+ if (single) {
3395
+ await assertSingleMatch(executor, table, filters, fieldName);
3396
+ }
3397
+ let query = executor.delete(table);
3398
+ if (filters) {
3399
+ query = query.where(filters);
3400
+ }
3401
+ await query;
3402
+ return { isSuccess: true };
3403
+ });
3199
3404
  } catch (e) {
3200
3405
  throw toGraphQLError(e);
3201
3406
  }
@@ -3235,6 +3440,7 @@ var generateSchemaData = (db, schema, relations, options) => {
3235
3440
  const relationAggregateFactory = features.relationAggregates ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
3236
3441
  const queries = {};
3237
3442
  const mutations = {};
3443
+ const mutationTxCtx = createMutationTxCtx(options.transactions);
3238
3444
  const gqlSchemaTypes = Object.fromEntries(
3239
3445
  Object.entries(tables).map(([tableName, _table]) => [
3240
3446
  tableName,
@@ -3310,8 +3516,22 @@ var generateSchemaData = (db, schema, relations, options) => {
3310
3516
  typeNameMapper,
3311
3517
  filterCtx
3312
3518
  );
3313
- const insertArrGenerated = features.insert ? generateInsertArray(db, tableName, schema[tableName], insertInput, createArrayFieldName) : void 0;
3314
- const insertSingleGenerated = features.insert ? generateInsertSingle(db, tableName, schema[tableName], insertInput, createSingleFieldName) : void 0;
3519
+ const insertArrGenerated = features.insert ? generateInsertArray(
3520
+ db,
3521
+ tableName,
3522
+ schema[tableName],
3523
+ insertInput,
3524
+ createArrayFieldName,
3525
+ mutationTxCtx
3526
+ ) : void 0;
3527
+ const insertSingleGenerated = features.insert ? generateInsertSingle(
3528
+ db,
3529
+ tableName,
3530
+ schema[tableName],
3531
+ insertInput,
3532
+ createSingleFieldName,
3533
+ mutationTxCtx
3534
+ ) : void 0;
3315
3535
  const onConflictInput = features.upsert ? generateOnConflictInput({
3316
3536
  table: schema[tableName],
3317
3537
  typeName,
@@ -3319,8 +3539,24 @@ var generateSchemaData = (db, schema, relations, options) => {
3319
3539
  tableFilters,
3320
3540
  withTarget: false
3321
3541
  }) : void 0;
3322
- const upsertArrGenerated = onConflictInput ? generateUpsert(db, schema[tableName], insertInput, onConflictInput, upsertArrayFieldName, false) : void 0;
3323
- const upsertSingleGenerated = onConflictInput ? generateUpsert(db, schema[tableName], insertInput, onConflictInput, upsertSingleFieldName, true) : void 0;
3542
+ const upsertArrGenerated = onConflictInput ? generateUpsert(
3543
+ db,
3544
+ schema[tableName],
3545
+ insertInput,
3546
+ onConflictInput,
3547
+ upsertArrayFieldName,
3548
+ false,
3549
+ mutationTxCtx
3550
+ ) : void 0;
3551
+ const upsertSingleGenerated = onConflictInput ? generateUpsert(
3552
+ db,
3553
+ schema[tableName],
3554
+ insertInput,
3555
+ onConflictInput,
3556
+ upsertSingleFieldName,
3557
+ true,
3558
+ mutationTxCtx
3559
+ ) : void 0;
3324
3560
  const updateGenerated = features.update ? generateUpdate(
3325
3561
  db,
3326
3562
  tableName,
@@ -3330,7 +3566,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3330
3566
  updateFieldName,
3331
3567
  false,
3332
3568
  features.requireWhere,
3333
- filterCtx
3569
+ filterCtx,
3570
+ mutationTxCtx
3334
3571
  ) : void 0;
3335
3572
  const updateSingleGenerated = features.update ? generateUpdate(
3336
3573
  db,
@@ -3341,7 +3578,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3341
3578
  updateSingleFieldName,
3342
3579
  true,
3343
3580
  features.requireWhere,
3344
- filterCtx
3581
+ filterCtx,
3582
+ mutationTxCtx
3345
3583
  ) : void 0;
3346
3584
  const updateManyInput = features.update && features.updateMany ? generateUpdateManyInput({ typeName, updatePrefix: prefixes.update, updateInput, tableFilters }) : void 0;
3347
3585
  const updateManyGenerated = updateManyInput ? generateUpdateMany(
@@ -3350,7 +3588,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3350
3588
  schema[tableName],
3351
3589
  updateManyInput,
3352
3590
  updateManyFieldName,
3353
- filterCtx
3591
+ filterCtx,
3592
+ mutationTxCtx
3354
3593
  ) : void 0;
3355
3594
  const deleteGenerated = features.delete ? generateDelete(
3356
3595
  db,
@@ -3360,7 +3599,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3360
3599
  deleteFieldName,
3361
3600
  false,
3362
3601
  features.requireWhere,
3363
- filterCtx
3602
+ filterCtx,
3603
+ mutationTxCtx
3364
3604
  ) : void 0;
3365
3605
  const deleteSingleGenerated = features.delete ? generateDelete(
3366
3606
  db,
@@ -3370,7 +3610,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3370
3610
  deleteSingleFieldName,
3371
3611
  true,
3372
3612
  features.requireWhere,
3373
- filterCtx
3613
+ filterCtx,
3614
+ mutationTxCtx
3374
3615
  ) : void 0;
3375
3616
  const aggregateType = features.aggregates ? generateAggregateTypes(schema[tableName], tableName, typeName, cacheCtx) : void 0;
3376
3617
  const aggregateGenerated = features.aggregates ? generateAggregate(
@@ -3463,6 +3704,11 @@ var generateSchemaData = (db, schema, relations, options) => {
3463
3704
  inputs[havingInput.name] = havingInput;
3464
3705
  }
3465
3706
  }
3707
+ if (mutationTxCtx) {
3708
+ for (const name of Object.keys(mutations)) {
3709
+ mutationTxCtx.fieldNames.add(name);
3710
+ }
3711
+ }
3466
3712
  const fieldResolvers = {};
3467
3713
  for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
3468
3714
  const relResolvers = {};
@@ -3666,7 +3912,7 @@ var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filt
3666
3912
  };
3667
3913
  };
3668
3914
  var pgPrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, import_pg_core2.getTableConfig);
3669
- var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
3915
+ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx) => {
3670
3916
  const queryArgs = {
3671
3917
  values: {
3672
3918
  type: new import_graphql7.GraphQLNonNull(new import_graphql7.GraphQLList(new import_graphql7.GraphQLNonNull(baseType)))
@@ -3677,31 +3923,32 @@ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType,
3677
3923
  name: fieldName,
3678
3924
  resolver: async (_source, args, context, info) => {
3679
3925
  try {
3680
- const input = remapFromGraphQLArrayInput(args.values, table);
3681
- if (!input.length) {
3682
- throw new import_graphql7.GraphQLError("No values were provided!");
3683
- }
3684
- const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
3685
- deep: true
3686
- });
3687
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3688
- relationMap,
3689
- tables,
3690
- tableName,
3691
- typeName,
3692
- typeNameMapper,
3693
- table,
3694
- pkNames,
3695
- parsedInfo
3926
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3927
+ const input = remapFromGraphQLArrayInput(args.values, table);
3928
+ if (!input.length) {
3929
+ throw new import_graphql7.GraphQLError("No values were provided!");
3930
+ }
3931
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
3932
+ deep: true
3933
+ });
3934
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3935
+ relationMap,
3936
+ tables,
3937
+ tableName,
3938
+ typeName,
3939
+ typeNameMapper,
3940
+ table,
3941
+ pkNames,
3942
+ parsedInfo
3943
+ });
3944
+ let query = executor.insert(table).values(input).returning(columns);
3945
+ if (conflictDoNothing) {
3946
+ query = query.onConflictDoNothing();
3947
+ }
3948
+ const result = await query;
3949
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3950
+ return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3696
3951
  });
3697
- const executor = resolveExecutor(db, context);
3698
- let query = executor.insert(table).values(input).returning(columns);
3699
- if (conflictDoNothing) {
3700
- query = query.onConflictDoNothing();
3701
- }
3702
- const result = await query;
3703
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3704
- return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3705
3952
  } catch (e) {
3706
3953
  throw toGraphQLError(e);
3707
3954
  }
@@ -3709,7 +3956,7 @@ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType,
3709
3956
  args: queryArgs
3710
3957
  };
3711
3958
  };
3712
- var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
3959
+ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx) => {
3713
3960
  const queryArgs = {
3714
3961
  values: {
3715
3962
  type: new import_graphql7.GraphQLNonNull(baseType)
@@ -3720,31 +3967,32 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
3720
3967
  name: fieldName,
3721
3968
  resolver: async (_source, args, context, info) => {
3722
3969
  try {
3723
- const input = remapFromGraphQLSingleInput(args.values, table);
3724
- const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
3725
- deep: true
3726
- });
3727
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3728
- relationMap,
3729
- tables,
3730
- tableName,
3731
- typeName,
3732
- typeNameMapper,
3733
- table,
3734
- pkNames,
3735
- parsedInfo
3970
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3971
+ const input = remapFromGraphQLSingleInput(args.values, table);
3972
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
3973
+ deep: true
3974
+ });
3975
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3976
+ relationMap,
3977
+ tables,
3978
+ tableName,
3979
+ typeName,
3980
+ typeNameMapper,
3981
+ table,
3982
+ pkNames,
3983
+ parsedInfo
3984
+ });
3985
+ let query = executor.insert(table).values(input).returning(columns);
3986
+ if (conflictDoNothing) {
3987
+ query = query.onConflictDoNothing();
3988
+ }
3989
+ const result = await query;
3990
+ if (!result[0]) {
3991
+ return void 0;
3992
+ }
3993
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3994
+ return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
3736
3995
  });
3737
- const executor = resolveExecutor(db, context);
3738
- let query = executor.insert(table).values(input).returning(columns);
3739
- if (conflictDoNothing) {
3740
- query = query.onConflictDoNothing();
3741
- }
3742
- const result = await query;
3743
- if (!result[0]) {
3744
- return void 0;
3745
- }
3746
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3747
- return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
3748
3996
  } catch (e) {
3749
3997
  throw toGraphQLError(e);
3750
3998
  }
@@ -3752,7 +4000,7 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
3752
4000
  args: queryArgs
3753
4001
  };
3754
4002
  };
3755
- var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx) => {
4003
+ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx, txCtx) => {
3756
4004
  const queryArgs = {
3757
4005
  values: {
3758
4006
  type: single ? new import_graphql7.GraphQLNonNull(baseType) : new import_graphql7.GraphQLNonNull(new import_graphql7.GraphQLList(new import_graphql7.GraphQLNonNull(baseType)))
@@ -3767,40 +4015,41 @@ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onCo
3767
4015
  name: fieldName,
3768
4016
  resolver: async (_source, args, context, info) => {
3769
4017
  try {
3770
- const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
3771
- if (!input.length) {
3772
- throw new import_graphql7.GraphQLError("No values were provided!");
3773
- }
3774
- const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, { deep: true });
3775
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3776
- relationMap,
3777
- tables,
3778
- tableName,
3779
- typeName,
3780
- typeNameMapper,
3781
- table,
3782
- pkNames,
3783
- parsedInfo
3784
- });
3785
- const plan = resolveConflictPlan({
3786
- table,
3787
- values: input,
3788
- onConflict: args.onConflict,
3789
- pkNames,
3790
- uniqueSets,
3791
- excludedRef: excludedColumnRef,
3792
- withTarget: true,
3793
- buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
4018
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4019
+ const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
4020
+ if (!input.length) {
4021
+ throw new import_graphql7.GraphQLError("No values were provided!");
4022
+ }
4023
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, { deep: true });
4024
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4025
+ relationMap,
4026
+ tables,
4027
+ tableName,
4028
+ typeName,
4029
+ typeNameMapper,
4030
+ table,
4031
+ pkNames,
4032
+ parsedInfo
4033
+ });
4034
+ const plan = resolveConflictPlan({
4035
+ table,
4036
+ values: input,
4037
+ onConflict: args.onConflict,
4038
+ pkNames,
4039
+ uniqueSets,
4040
+ excludedRef: excludedColumnRef,
4041
+ withTarget: true,
4042
+ buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
4043
+ });
4044
+ let query = executor.insert(table).values(input).returning(columns);
4045
+ query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
4046
+ const result = await query;
4047
+ if (single && !result[0]) {
4048
+ return void 0;
4049
+ }
4050
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4051
+ return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3794
4052
  });
3795
- const executor = resolveExecutor(db, context);
3796
- let query = executor.insert(table).values(input).returning(columns);
3797
- query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
3798
- const result = await query;
3799
- if (single && !result[0]) {
3800
- return void 0;
3801
- }
3802
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3803
- return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3804
4053
  } catch (e) {
3805
4054
  throw toGraphQLError(e);
3806
4055
  }
@@ -3808,7 +4057,7 @@ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onCo
3808
4057
  args: queryArgs
3809
4058
  };
3810
4059
  };
3811
- var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx) => {
4060
+ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx, txCtx) => {
3812
4061
  const queryArgs = {
3813
4062
  set: {
3814
4063
  type: new import_graphql7.GraphQLNonNull(setArgs)
@@ -3822,44 +4071,45 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
3822
4071
  name: fieldName,
3823
4072
  resolver: async (_source, args, context, info) => {
3824
4073
  try {
3825
- const { where, set } = args;
3826
- const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
3827
- deep: true
3828
- });
3829
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3830
- relationMap,
3831
- tables,
3832
- tableName,
3833
- typeName,
3834
- typeNameMapper,
3835
- table,
3836
- pkNames,
3837
- parsedInfo
4074
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4075
+ const { where, set } = args;
4076
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
4077
+ deep: true
4078
+ });
4079
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4080
+ relationMap,
4081
+ tables,
4082
+ tableName,
4083
+ typeName,
4084
+ typeNameMapper,
4085
+ table,
4086
+ pkNames,
4087
+ parsedInfo
4088
+ });
4089
+ const input = remapFromGraphQLSingleInput(set, table);
4090
+ if (!Object.keys(input).length) {
4091
+ throw new import_graphql7.GraphQLError("Unable to update with no values specified!");
4092
+ }
4093
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
4094
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4095
+ if (single) {
4096
+ await assertSingleMatch(executor, table, filters, fieldName);
4097
+ }
4098
+ let query = executor.update(table).set(input);
4099
+ if (filters) {
4100
+ query = query.where(filters);
4101
+ }
4102
+ query = query.returning(columns);
4103
+ const result = await query;
4104
+ if (single && result.length > 1) {
4105
+ throw new import_graphql7.GraphQLError(`${fieldName}: 'where' matched more than one row!`);
4106
+ }
4107
+ if (single && !result[0]) {
4108
+ return void 0;
4109
+ }
4110
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4111
+ return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3838
4112
  });
3839
- const input = remapFromGraphQLSingleInput(set, table);
3840
- if (!Object.keys(input).length) {
3841
- throw new import_graphql7.GraphQLError("Unable to update with no values specified!");
3842
- }
3843
- const relationCtx = relationFilterCtx(filterCtx, tableName);
3844
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3845
- const executor = resolveExecutor(db, context);
3846
- if (single) {
3847
- await assertSingleMatch(executor, table, filters, fieldName);
3848
- }
3849
- let query = executor.update(table).set(input);
3850
- if (filters) {
3851
- query = query.where(filters);
3852
- }
3853
- query = query.returning(columns);
3854
- const result = await query;
3855
- if (single && result.length > 1) {
3856
- throw new import_graphql7.GraphQLError(`${fieldName}: 'where' matched more than one row!`);
3857
- }
3858
- if (single && !result[0]) {
3859
- return void 0;
3860
- }
3861
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3862
- return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3863
4113
  } catch (e) {
3864
4114
  throw toGraphQLError(e);
3865
4115
  }
@@ -3867,7 +4117,7 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
3867
4117
  args: queryArgs
3868
4118
  };
3869
4119
  };
3870
- var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx) => {
4120
+ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx, txCtx) => {
3871
4121
  const queryArgs = {
3872
4122
  updates: {
3873
4123
  type: new import_graphql7.GraphQLNonNull(new import_graphql7.GraphQLList(new import_graphql7.GraphQLNonNull(updateManyInput)))
@@ -3878,60 +4128,61 @@ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateMany
3878
4128
  name: fieldName,
3879
4129
  resolver: async (_source, args, context, info) => {
3880
4130
  try {
3881
- const { updates } = args;
3882
- if (!updates.length) {
3883
- throw new import_graphql7.GraphQLError("No updates were provided!");
3884
- }
3885
- const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
3886
- deep: true
3887
- });
3888
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3889
- relationMap,
3890
- tables,
3891
- tableName,
3892
- typeName,
3893
- typeNameMapper,
3894
- table,
3895
- pkNames,
3896
- parsedInfo
3897
- });
3898
- const entries = updates.map(({ where, set }) => {
3899
- const input = remapFromGraphQLSingleInput(set, table);
3900
- if (!Object.keys(input).length) {
3901
- throw new import_graphql7.GraphQLError("Unable to update with no values specified!");
4131
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4132
+ const { updates } = args;
4133
+ if (!updates.length) {
4134
+ throw new import_graphql7.GraphQLError("No updates were provided!");
3902
4135
  }
3903
- return {
3904
- set: input,
3905
- filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
3906
- };
3907
- });
3908
- const executor = resolveExecutor(db, context);
3909
- const perEntry = await executor.transaction(async (tx) => {
3910
- const results = [];
3911
- for (const entry of entries) {
3912
- let query = tx.update(table).set(entry.set);
3913
- if (entry.filters) {
3914
- query = query.where(entry.filters);
4136
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
4137
+ deep: true
4138
+ });
4139
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4140
+ relationMap,
4141
+ tables,
4142
+ tableName,
4143
+ typeName,
4144
+ typeNameMapper,
4145
+ table,
4146
+ pkNames,
4147
+ parsedInfo
4148
+ });
4149
+ const entries = updates.map(({ where, set }) => {
4150
+ const input = remapFromGraphQLSingleInput(set, table);
4151
+ if (!Object.keys(input).length) {
4152
+ throw new import_graphql7.GraphQLError("Unable to update with no values specified!");
4153
+ }
4154
+ return {
4155
+ set: input,
4156
+ filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
4157
+ };
4158
+ });
4159
+ const perEntry = await executor.transaction(async (tx) => {
4160
+ const results = [];
4161
+ for (const entry of entries) {
4162
+ let query = tx.update(table).set(entry.set);
4163
+ if (entry.filters) {
4164
+ query = query.where(entry.filters);
4165
+ }
4166
+ results.push(await query.returning(columns));
4167
+ }
4168
+ return results;
4169
+ });
4170
+ const flatRows = perEntry.flat();
4171
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, flatRows, pkNames, withParams) : flatRows;
4172
+ const output = [];
4173
+ let offset = 0;
4174
+ for (const rows of perEntry) {
4175
+ if (!rows.length) {
4176
+ output.push(null);
4177
+ continue;
3915
4178
  }
3916
- results.push(await query.returning(columns));
4179
+ for (let i = 0; i < rows.length; i++) {
4180
+ output.push(remapToGraphQLSingleOutput(enriched[offset + i], tableName, table, relationMap));
4181
+ }
4182
+ offset += rows.length;
3917
4183
  }
3918
- return results;
4184
+ return output;
3919
4185
  });
3920
- const flatRows = perEntry.flat();
3921
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, flatRows, pkNames, withParams) : flatRows;
3922
- const output = [];
3923
- let offset = 0;
3924
- for (const rows of perEntry) {
3925
- if (!rows.length) {
3926
- output.push(null);
3927
- continue;
3928
- }
3929
- for (let i = 0; i < rows.length; i++) {
3930
- output.push(remapToGraphQLSingleOutput(enriched[offset + i], tableName, table, relationMap));
3931
- }
3932
- offset += rows.length;
3933
- }
3934
- return output;
3935
4186
  } catch (e) {
3936
4187
  throw toGraphQLError(e);
3937
4188
  }
@@ -3939,7 +4190,7 @@ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateMany
3939
4190
  args: queryArgs
3940
4191
  };
3941
4192
  };
3942
- var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx) => {
4193
+ var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx, txCtx) => {
3943
4194
  const queryArgs = {
3944
4195
  where: {
3945
4196
  type: single || requireWhere ? new import_graphql7.GraphQLNonNull(filterArgs) : filterArgs
@@ -3949,34 +4200,35 @@ var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, si
3949
4200
  name: fieldName,
3950
4201
  resolver: async (_source, args, context, info) => {
3951
4202
  try {
3952
- const { where } = args;
3953
- const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
3954
- deep: true
4203
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4204
+ const { where } = args;
4205
+ const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
4206
+ deep: true
4207
+ });
4208
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
4209
+ parsedInfo.fieldsByTypeName[typeName],
4210
+ table,
4211
+ selectionCtx
4212
+ );
4213
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
4214
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4215
+ if (single) {
4216
+ await assertSingleMatch(executor, table, filters, fieldName);
4217
+ }
4218
+ let query = executor.delete(table);
4219
+ if (filters) {
4220
+ query = query.where(filters);
4221
+ }
4222
+ query = query.returning(columns);
4223
+ const result = await query;
4224
+ if (single && result.length > 1) {
4225
+ throw new import_graphql7.GraphQLError(`${fieldName}: 'where' matched more than one row!`);
4226
+ }
4227
+ if (single) {
4228
+ return result[0] ? remapToGraphQLSingleOutput(result[0], tableName, table) : void 0;
4229
+ }
4230
+ return remapToGraphQLArrayOutput(result, tableName, table);
3955
4231
  });
3956
- const columns = extractSelectedColumnsFromTreeSQLFormat(
3957
- parsedInfo.fieldsByTypeName[typeName],
3958
- table,
3959
- selectionCtx
3960
- );
3961
- const relationCtx = relationFilterCtx(filterCtx, tableName);
3962
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3963
- const executor = resolveExecutor(db, context);
3964
- if (single) {
3965
- await assertSingleMatch(executor, table, filters, fieldName);
3966
- }
3967
- let query = executor.delete(table);
3968
- if (filters) {
3969
- query = query.where(filters);
3970
- }
3971
- query = query.returning(columns);
3972
- const result = await query;
3973
- if (single && result.length > 1) {
3974
- throw new import_graphql7.GraphQLError(`${fieldName}: 'where' matched more than one row!`);
3975
- }
3976
- if (single) {
3977
- return result[0] ? remapToGraphQLSingleOutput(result[0], tableName, table) : void 0;
3978
- }
3979
- return remapToGraphQLArrayOutput(result, tableName, table);
3980
4232
  } catch (e) {
3981
4233
  throw toGraphQLError(e);
3982
4234
  }
@@ -4023,6 +4275,7 @@ function generateSchemaData2(db, schema, relations, options) {
4023
4275
  const relationAggregateFactory = features.relationAggregates ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
4024
4276
  const queries = {};
4025
4277
  const mutations = {};
4278
+ const mutationTxCtx = createMutationTxCtx(options.transactions);
4026
4279
  const gqlSchemaTypes = Object.fromEntries(
4027
4280
  Object.entries(tables).map(([tableName, _table]) => [
4028
4281
  tableName,
@@ -4097,7 +4350,8 @@ function generateSchemaData2(db, schema, relations, options) {
4097
4350
  createArrayFieldName,
4098
4351
  typeName,
4099
4352
  typeNameMapper,
4100
- conflictDoNothing
4353
+ conflictDoNothing,
4354
+ mutationTxCtx
4101
4355
  ) : void 0;
4102
4356
  const insertSingleGenerated = features.insert ? generateInsertSingle2(
4103
4357
  db,
@@ -4109,7 +4363,8 @@ function generateSchemaData2(db, schema, relations, options) {
4109
4363
  createSingleFieldName,
4110
4364
  typeName,
4111
4365
  typeNameMapper,
4112
- conflictDoNothing
4366
+ conflictDoNothing,
4367
+ mutationTxCtx
4113
4368
  ) : void 0;
4114
4369
  const uniqueSets = features.upsert ? getUniqueColumnSets(schema[tableName], import_pg_core2.getTableConfig) : [];
4115
4370
  const onConflictInput = features.upsert ? generateOnConflictInput({
@@ -4132,7 +4387,8 @@ function generateSchemaData2(db, schema, relations, options) {
4132
4387
  typeName,
4133
4388
  false,
4134
4389
  typeNameMapper,
4135
- filterCtx
4390
+ filterCtx,
4391
+ mutationTxCtx
4136
4392
  ) : void 0;
4137
4393
  const upsertSingleGenerated = onConflictInput ? generateUpsert2(
4138
4394
  db,
@@ -4147,7 +4403,8 @@ function generateSchemaData2(db, schema, relations, options) {
4147
4403
  typeName,
4148
4404
  true,
4149
4405
  typeNameMapper,
4150
- filterCtx
4406
+ filterCtx,
4407
+ mutationTxCtx
4151
4408
  ) : void 0;
4152
4409
  const updateGenerated = features.update ? generateUpdate2(
4153
4410
  db,
@@ -4162,7 +4419,8 @@ function generateSchemaData2(db, schema, relations, options) {
4162
4419
  false,
4163
4420
  features.requireWhere,
4164
4421
  typeNameMapper,
4165
- filterCtx
4422
+ filterCtx,
4423
+ mutationTxCtx
4166
4424
  ) : void 0;
4167
4425
  const updateSingleGenerated = features.update ? generateUpdate2(
4168
4426
  db,
@@ -4177,7 +4435,8 @@ function generateSchemaData2(db, schema, relations, options) {
4177
4435
  true,
4178
4436
  features.requireWhere,
4179
4437
  typeNameMapper,
4180
- filterCtx
4438
+ filterCtx,
4439
+ mutationTxCtx
4181
4440
  ) : void 0;
4182
4441
  const updateManyInput = features.update && features.updateMany ? generateUpdateManyInput({ typeName, updatePrefix: prefixes.update, updateInput, tableFilters }) : void 0;
4183
4442
  const updateManyGenerated = updateManyInput ? generateUpdateMany2(
@@ -4190,7 +4449,8 @@ function generateSchemaData2(db, schema, relations, options) {
4190
4449
  updateManyFieldName,
4191
4450
  typeName,
4192
4451
  typeNameMapper,
4193
- filterCtx
4452
+ filterCtx,
4453
+ mutationTxCtx
4194
4454
  ) : void 0;
4195
4455
  const deleteGenerated = features.delete ? generateDelete2(
4196
4456
  db,
@@ -4202,7 +4462,8 @@ function generateSchemaData2(db, schema, relations, options) {
4202
4462
  false,
4203
4463
  features.requireWhere,
4204
4464
  filterCtx,
4205
- { tableName, relationMap: namedRelations, tables }
4465
+ { tableName, relationMap: namedRelations, tables },
4466
+ mutationTxCtx
4206
4467
  ) : void 0;
4207
4468
  const deleteSingleGenerated = features.delete ? generateDelete2(
4208
4469
  db,
@@ -4214,7 +4475,8 @@ function generateSchemaData2(db, schema, relations, options) {
4214
4475
  true,
4215
4476
  features.requireWhere,
4216
4477
  filterCtx,
4217
- { tableName, relationMap: namedRelations, tables }
4478
+ { tableName, relationMap: namedRelations, tables },
4479
+ mutationTxCtx
4218
4480
  ) : void 0;
4219
4481
  const aggregateType = features.aggregates ? generateAggregateTypes(schema[tableName], tableName, typeName, cacheCtx) : void 0;
4220
4482
  const aggregateGenerated = features.aggregates ? generateAggregate(
@@ -4353,6 +4615,11 @@ function generateSchemaData2(db, schema, relations, options) {
4353
4615
  inputs[havingInput.name] = havingInput;
4354
4616
  }
4355
4617
  }
4618
+ if (mutationTxCtx) {
4619
+ for (const name of Object.keys(mutations)) {
4620
+ mutationTxCtx.fieldNames.add(name);
4621
+ }
4622
+ }
4356
4623
  const fieldResolvers = {};
4357
4624
  for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
4358
4625
  const relResolvers = {};
@@ -4458,7 +4725,7 @@ var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filt
4458
4725
  };
4459
4726
  };
4460
4727
  var sqlitePrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, import_sqlite_core2.getTableConfig);
4461
- var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
4728
+ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx) => {
4462
4729
  const queryArgs = {
4463
4730
  values: {
4464
4731
  type: new import_graphql8.GraphQLNonNull(new import_graphql8.GraphQLList(new import_graphql8.GraphQLNonNull(baseType)))
@@ -4469,31 +4736,32 @@ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType,
4469
4736
  name: fieldName,
4470
4737
  resolver: async (_source, args, context, info) => {
4471
4738
  try {
4472
- const input = remapFromGraphQLArrayInput(args.values, table);
4473
- if (!input.length) {
4474
- throw new import_graphql8.GraphQLError("No values were provided!");
4475
- }
4476
- const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
4477
- deep: true
4478
- });
4479
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4480
- relationMap,
4481
- tables,
4482
- tableName,
4483
- typeName,
4484
- typeNameMapper,
4485
- table,
4486
- pkNames,
4487
- parsedInfo
4739
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4740
+ const input = remapFromGraphQLArrayInput(args.values, table);
4741
+ if (!input.length) {
4742
+ throw new import_graphql8.GraphQLError("No values were provided!");
4743
+ }
4744
+ const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
4745
+ deep: true
4746
+ });
4747
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4748
+ relationMap,
4749
+ tables,
4750
+ tableName,
4751
+ typeName,
4752
+ typeNameMapper,
4753
+ table,
4754
+ pkNames,
4755
+ parsedInfo
4756
+ });
4757
+ let query = executor.insert(table).values(input).returning(columns);
4758
+ if (conflictDoNothing) {
4759
+ query = query.onConflictDoNothing();
4760
+ }
4761
+ const result = await query;
4762
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4763
+ return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4488
4764
  });
4489
- const executor = resolveExecutor(db, context);
4490
- let query = executor.insert(table).values(input).returning(columns);
4491
- if (conflictDoNothing) {
4492
- query = query.onConflictDoNothing();
4493
- }
4494
- const result = await query;
4495
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4496
- return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4497
4765
  } catch (e) {
4498
4766
  throw toGraphQLError(e);
4499
4767
  }
@@ -4501,7 +4769,7 @@ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType,
4501
4769
  args: queryArgs
4502
4770
  };
4503
4771
  };
4504
- var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
4772
+ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx) => {
4505
4773
  const queryArgs = {
4506
4774
  values: {
4507
4775
  type: new import_graphql8.GraphQLNonNull(baseType)
@@ -4512,31 +4780,32 @@ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType
4512
4780
  name: fieldName,
4513
4781
  resolver: async (_source, args, context, info) => {
4514
4782
  try {
4515
- const input = remapFromGraphQLSingleInput(args.values, table);
4516
- const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
4517
- deep: true
4518
- });
4519
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4520
- relationMap,
4521
- tables,
4522
- tableName,
4523
- typeName,
4524
- typeNameMapper,
4525
- table,
4526
- pkNames,
4527
- parsedInfo
4783
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4784
+ const input = remapFromGraphQLSingleInput(args.values, table);
4785
+ const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
4786
+ deep: true
4787
+ });
4788
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4789
+ relationMap,
4790
+ tables,
4791
+ tableName,
4792
+ typeName,
4793
+ typeNameMapper,
4794
+ table,
4795
+ pkNames,
4796
+ parsedInfo
4797
+ });
4798
+ let query = executor.insert(table).values(input).returning(columns);
4799
+ if (conflictDoNothing) {
4800
+ query = query.onConflictDoNothing();
4801
+ }
4802
+ const result = await query;
4803
+ if (!result[0]) {
4804
+ return void 0;
4805
+ }
4806
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4807
+ return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
4528
4808
  });
4529
- const executor = resolveExecutor(db, context);
4530
- let query = executor.insert(table).values(input).returning(columns);
4531
- if (conflictDoNothing) {
4532
- query = query.onConflictDoNothing();
4533
- }
4534
- const result = await query;
4535
- if (!result[0]) {
4536
- return void 0;
4537
- }
4538
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4539
- return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
4540
4809
  } catch (e) {
4541
4810
  throw toGraphQLError(e);
4542
4811
  }
@@ -4544,7 +4813,7 @@ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType
4544
4813
  args: queryArgs
4545
4814
  };
4546
4815
  };
4547
- var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx) => {
4816
+ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx, txCtx) => {
4548
4817
  const queryArgs = {
4549
4818
  values: {
4550
4819
  type: single ? new import_graphql8.GraphQLNonNull(baseType) : new import_graphql8.GraphQLNonNull(new import_graphql8.GraphQLList(new import_graphql8.GraphQLNonNull(baseType)))
@@ -4559,40 +4828,41 @@ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onCo
4559
4828
  name: fieldName,
4560
4829
  resolver: async (_source, args, context, info) => {
4561
4830
  try {
4562
- const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
4563
- if (!input.length) {
4564
- throw new import_graphql8.GraphQLError("No values were provided!");
4565
- }
4566
- const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, { deep: true });
4567
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4568
- relationMap,
4569
- tables,
4570
- tableName,
4571
- typeName,
4572
- typeNameMapper,
4573
- table,
4574
- pkNames,
4575
- parsedInfo
4576
- });
4577
- const plan = resolveConflictPlan({
4578
- table,
4579
- values: input,
4580
- onConflict: args.onConflict,
4581
- pkNames,
4582
- uniqueSets,
4583
- excludedRef: excludedColumnRef,
4584
- withTarget: true,
4585
- buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
4831
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4832
+ const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
4833
+ if (!input.length) {
4834
+ throw new import_graphql8.GraphQLError("No values were provided!");
4835
+ }
4836
+ const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, { deep: true });
4837
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4838
+ relationMap,
4839
+ tables,
4840
+ tableName,
4841
+ typeName,
4842
+ typeNameMapper,
4843
+ table,
4844
+ pkNames,
4845
+ parsedInfo
4846
+ });
4847
+ const plan = resolveConflictPlan({
4848
+ table,
4849
+ values: input,
4850
+ onConflict: args.onConflict,
4851
+ pkNames,
4852
+ uniqueSets,
4853
+ excludedRef: excludedColumnRef,
4854
+ withTarget: true,
4855
+ buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
4856
+ });
4857
+ let query = executor.insert(table).values(input).returning(columns);
4858
+ query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
4859
+ const result = await query;
4860
+ if (single && !result[0]) {
4861
+ return void 0;
4862
+ }
4863
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4864
+ return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4586
4865
  });
4587
- const executor = resolveExecutor(db, context);
4588
- let query = executor.insert(table).values(input).returning(columns);
4589
- query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
4590
- const result = await query;
4591
- if (single && !result[0]) {
4592
- return void 0;
4593
- }
4594
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4595
- return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4596
4866
  } catch (e) {
4597
4867
  throw toGraphQLError(e);
4598
4868
  }
@@ -4600,7 +4870,7 @@ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onCo
4600
4870
  args: queryArgs
4601
4871
  };
4602
4872
  };
4603
- var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx) => {
4873
+ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx, txCtx) => {
4604
4874
  const queryArgs = {
4605
4875
  set: {
4606
4876
  type: new import_graphql8.GraphQLNonNull(setArgs)
@@ -4614,44 +4884,45 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
4614
4884
  name: fieldName,
4615
4885
  resolver: async (_source, args, context, info) => {
4616
4886
  try {
4617
- const { where, set } = args;
4618
- const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
4619
- deep: true
4620
- });
4621
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4622
- relationMap,
4623
- tables,
4624
- tableName,
4625
- typeName,
4626
- typeNameMapper,
4627
- table,
4628
- pkNames,
4629
- parsedInfo
4887
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4888
+ const { where, set } = args;
4889
+ const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
4890
+ deep: true
4891
+ });
4892
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4893
+ relationMap,
4894
+ tables,
4895
+ tableName,
4896
+ typeName,
4897
+ typeNameMapper,
4898
+ table,
4899
+ pkNames,
4900
+ parsedInfo
4901
+ });
4902
+ const input = remapFromGraphQLSingleInput(set, table);
4903
+ if (!Object.keys(input).length) {
4904
+ throw new import_graphql8.GraphQLError("Unable to update with no values specified!");
4905
+ }
4906
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
4907
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4908
+ if (single) {
4909
+ await assertSingleMatch(executor, table, filters, fieldName);
4910
+ }
4911
+ let query = executor.update(table).set(input);
4912
+ if (filters) {
4913
+ query = query.where(filters);
4914
+ }
4915
+ query = query.returning(columns);
4916
+ const result = await query;
4917
+ if (single && result.length > 1) {
4918
+ throw new import_graphql8.GraphQLError(`${fieldName}: 'where' matched more than one row!`);
4919
+ }
4920
+ if (single && !result[0]) {
4921
+ return void 0;
4922
+ }
4923
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4924
+ return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4630
4925
  });
4631
- const input = remapFromGraphQLSingleInput(set, table);
4632
- if (!Object.keys(input).length) {
4633
- throw new import_graphql8.GraphQLError("Unable to update with no values specified!");
4634
- }
4635
- const relationCtx = relationFilterCtx(filterCtx, tableName);
4636
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4637
- const executor = resolveExecutor(db, context);
4638
- if (single) {
4639
- await assertSingleMatch(executor, table, filters, fieldName);
4640
- }
4641
- let query = executor.update(table).set(input);
4642
- if (filters) {
4643
- query = query.where(filters);
4644
- }
4645
- query = query.returning(columns);
4646
- const result = await query;
4647
- if (single && result.length > 1) {
4648
- throw new import_graphql8.GraphQLError(`${fieldName}: 'where' matched more than one row!`);
4649
- }
4650
- if (single && !result[0]) {
4651
- return void 0;
4652
- }
4653
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4654
- return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4655
4926
  } catch (e) {
4656
4927
  throw toGraphQLError(e);
4657
4928
  }
@@ -4659,7 +4930,7 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
4659
4930
  args: queryArgs
4660
4931
  };
4661
4932
  };
4662
- var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx) => {
4933
+ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx, txCtx) => {
4663
4934
  const queryArgs = {
4664
4935
  updates: {
4665
4936
  type: new import_graphql8.GraphQLNonNull(new import_graphql8.GraphQLList(new import_graphql8.GraphQLNonNull(updateManyInput)))
@@ -4670,73 +4941,74 @@ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateMany
4670
4941
  name: fieldName,
4671
4942
  resolver: async (_source, args, context, info) => {
4672
4943
  try {
4673
- const { updates } = args;
4674
- if (!updates.length) {
4675
- throw new import_graphql8.GraphQLError("No updates were provided!");
4676
- }
4677
- const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
4678
- deep: true
4679
- });
4680
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4681
- relationMap,
4682
- tables,
4683
- tableName,
4684
- typeName,
4685
- typeNameMapper,
4686
- table,
4687
- pkNames,
4688
- parsedInfo
4689
- });
4690
- const entries = updates.map(({ where, set }) => {
4691
- const input = remapFromGraphQLSingleInput(set, table);
4692
- if (!Object.keys(input).length) {
4693
- throw new import_graphql8.GraphQLError("Unable to update with no values specified!");
4944
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4945
+ const { updates } = args;
4946
+ if (!updates.length) {
4947
+ throw new import_graphql8.GraphQLError("No updates were provided!");
4694
4948
  }
4695
- return {
4696
- set: input,
4697
- filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
4949
+ const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
4950
+ deep: true
4951
+ });
4952
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4953
+ relationMap,
4954
+ tables,
4955
+ tableName,
4956
+ typeName,
4957
+ typeNameMapper,
4958
+ table,
4959
+ pkNames,
4960
+ parsedInfo
4961
+ });
4962
+ const entries = updates.map(({ where, set }) => {
4963
+ const input = remapFromGraphQLSingleInput(set, table);
4964
+ if (!Object.keys(input).length) {
4965
+ throw new import_graphql8.GraphQLError("Unable to update with no values specified!");
4966
+ }
4967
+ return {
4968
+ set: input,
4969
+ filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
4970
+ };
4971
+ });
4972
+ const runEntry = (tx, entry) => {
4973
+ let query = tx.update(table).set(entry.set);
4974
+ if (entry.filters) {
4975
+ query = query.where(entry.filters);
4976
+ }
4977
+ return query.returning(columns).all();
4698
4978
  };
4699
- });
4700
- const runEntry = (tx, entry) => {
4701
- let query = tx.update(table).set(entry.set);
4702
- if (entry.filters) {
4703
- query = query.where(entry.filters);
4704
- }
4705
- return query.returning(columns).all();
4706
- };
4707
- const executor = resolveExecutor(db, context);
4708
- const perEntry = await executor.transaction((tx) => {
4709
- const first = runEntry(tx, entries[0]);
4710
- if (typeof first?.then === "function") {
4711
- return (async () => {
4712
- const results2 = [await first];
4713
- for (let i = 1; i < entries.length; i++) {
4714
- results2.push(await runEntry(tx, entries[i]));
4715
- }
4716
- return results2;
4717
- })();
4718
- }
4719
- const results = [first];
4720
- for (let i = 1; i < entries.length; i++) {
4721
- results.push(runEntry(tx, entries[i]));
4979
+ const perEntry = await executor.transaction((tx) => {
4980
+ const first = runEntry(tx, entries[0]);
4981
+ if (typeof first?.then === "function") {
4982
+ return (async () => {
4983
+ const results2 = [await first];
4984
+ for (let i = 1; i < entries.length; i++) {
4985
+ results2.push(await runEntry(tx, entries[i]));
4986
+ }
4987
+ return results2;
4988
+ })();
4989
+ }
4990
+ const results = [first];
4991
+ for (let i = 1; i < entries.length; i++) {
4992
+ results.push(runEntry(tx, entries[i]));
4993
+ }
4994
+ return results;
4995
+ });
4996
+ const flatRows = perEntry.flat();
4997
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, flatRows, pkNames, withParams) : flatRows;
4998
+ const output = [];
4999
+ let offset = 0;
5000
+ for (const rows of perEntry) {
5001
+ if (!rows.length) {
5002
+ output.push(null);
5003
+ continue;
5004
+ }
5005
+ for (let i = 0; i < rows.length; i++) {
5006
+ output.push(remapToGraphQLSingleOutput(enriched[offset + i], tableName, table, relationMap));
5007
+ }
5008
+ offset += rows.length;
4722
5009
  }
4723
- return results;
5010
+ return output;
4724
5011
  });
4725
- const flatRows = perEntry.flat();
4726
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, flatRows, pkNames, withParams) : flatRows;
4727
- const output = [];
4728
- let offset = 0;
4729
- for (const rows of perEntry) {
4730
- if (!rows.length) {
4731
- output.push(null);
4732
- continue;
4733
- }
4734
- for (let i = 0; i < rows.length; i++) {
4735
- output.push(remapToGraphQLSingleOutput(enriched[offset + i], tableName, table, relationMap));
4736
- }
4737
- offset += rows.length;
4738
- }
4739
- return output;
4740
5012
  } catch (e) {
4741
5013
  throw toGraphQLError(e);
4742
5014
  }
@@ -4744,7 +5016,7 @@ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateMany
4744
5016
  args: queryArgs
4745
5017
  };
4746
5018
  };
4747
- var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx) => {
5019
+ var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx, txCtx) => {
4748
5020
  const queryArgs = {
4749
5021
  where: {
4750
5022
  type: single || requireWhere ? new import_graphql8.GraphQLNonNull(filterArgs) : filterArgs
@@ -4754,34 +5026,35 @@ var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, si
4754
5026
  name: fieldName,
4755
5027
  resolver: async (_source, args, context, info) => {
4756
5028
  try {
4757
- const { where } = args;
4758
- const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
4759
- deep: true
5029
+ return await runMutation(db, context, info, txCtx, async (executor) => {
5030
+ const { where } = args;
5031
+ const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
5032
+ deep: true
5033
+ });
5034
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
5035
+ parsedInfo.fieldsByTypeName[typeName],
5036
+ table,
5037
+ selectionCtx
5038
+ );
5039
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
5040
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
5041
+ if (single) {
5042
+ await assertSingleMatch(executor, table, filters, fieldName);
5043
+ }
5044
+ let query = executor.delete(table);
5045
+ if (filters) {
5046
+ query = query.where(filters);
5047
+ }
5048
+ query = query.returning(columns);
5049
+ const result = await query;
5050
+ if (single && result.length > 1) {
5051
+ throw new import_graphql8.GraphQLError(`${fieldName}: 'where' matched more than one row!`);
5052
+ }
5053
+ if (single) {
5054
+ return result[0] ? remapToGraphQLSingleOutput(result[0], tableName, table) : void 0;
5055
+ }
5056
+ return remapToGraphQLArrayOutput(result, tableName, table);
4760
5057
  });
4761
- const columns = extractSelectedColumnsFromTreeSQLFormat(
4762
- parsedInfo.fieldsByTypeName[typeName],
4763
- table,
4764
- selectionCtx
4765
- );
4766
- const relationCtx = relationFilterCtx(filterCtx, tableName);
4767
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4768
- const executor = resolveExecutor(db, context);
4769
- if (single) {
4770
- await assertSingleMatch(executor, table, filters, fieldName);
4771
- }
4772
- let query = executor.delete(table);
4773
- if (filters) {
4774
- query = query.where(filters);
4775
- }
4776
- query = query.returning(columns);
4777
- const result = await query;
4778
- if (single && result.length > 1) {
4779
- throw new import_graphql8.GraphQLError(`${fieldName}: 'where' matched more than one row!`);
4780
- }
4781
- if (single) {
4782
- return result[0] ? remapToGraphQLSingleOutput(result[0], tableName, table) : void 0;
4783
- }
4784
- return remapToGraphQLArrayOutput(result, tableName, table);
4785
5058
  } catch (e) {
4786
5059
  throw toGraphQLError(e);
4787
5060
  }
@@ -4829,6 +5102,12 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4829
5102
  const relationAggregateFactory = features.relationAggregates ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
4830
5103
  const queries = {};
4831
5104
  const mutations = {};
5105
+ if (options.transactions && db.resultKind === "sync") {
5106
+ throw new Error(
5107
+ "Drizzle-GraphQL Error: transactions: 'auto' requires an asynchronous SQLite driver (e.g. libsql). Synchronous drivers cannot hold a transaction open across resolvers."
5108
+ );
5109
+ }
5110
+ const mutationTxCtx = createMutationTxCtx(options.transactions);
4832
5111
  const gqlSchemaTypes = Object.fromEntries(
4833
5112
  Object.entries(tables).map(([tableName, _table]) => [
4834
5113
  tableName,
@@ -4903,7 +5182,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4903
5182
  createArrayFieldName,
4904
5183
  typeName,
4905
5184
  typeNameMapper,
4906
- conflictDoNothing
5185
+ conflictDoNothing,
5186
+ mutationTxCtx
4907
5187
  ) : void 0;
4908
5188
  const insertSingleGenerated = features.insert ? generateInsertSingle3(
4909
5189
  db,
@@ -4915,7 +5195,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4915
5195
  createSingleFieldName,
4916
5196
  typeName,
4917
5197
  typeNameMapper,
4918
- conflictDoNothing
5198
+ conflictDoNothing,
5199
+ mutationTxCtx
4919
5200
  ) : void 0;
4920
5201
  const uniqueSets = features.upsert ? getUniqueColumnSets(schema[tableName], import_sqlite_core2.getTableConfig) : [];
4921
5202
  const onConflictInput = features.upsert ? generateOnConflictInput({
@@ -4938,7 +5219,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4938
5219
  typeName,
4939
5220
  false,
4940
5221
  typeNameMapper,
4941
- filterCtx
5222
+ filterCtx,
5223
+ mutationTxCtx
4942
5224
  ) : void 0;
4943
5225
  const upsertSingleGenerated = onConflictInput ? generateUpsert3(
4944
5226
  db,
@@ -4953,7 +5235,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4953
5235
  typeName,
4954
5236
  true,
4955
5237
  typeNameMapper,
4956
- filterCtx
5238
+ filterCtx,
5239
+ mutationTxCtx
4957
5240
  ) : void 0;
4958
5241
  const updateGenerated = features.update ? generateUpdate3(
4959
5242
  db,
@@ -4968,7 +5251,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4968
5251
  false,
4969
5252
  features.requireWhere,
4970
5253
  typeNameMapper,
4971
- filterCtx
5254
+ filterCtx,
5255
+ mutationTxCtx
4972
5256
  ) : void 0;
4973
5257
  const updateSingleGenerated = features.update ? generateUpdate3(
4974
5258
  db,
@@ -4983,7 +5267,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4983
5267
  true,
4984
5268
  features.requireWhere,
4985
5269
  typeNameMapper,
4986
- filterCtx
5270
+ filterCtx,
5271
+ mutationTxCtx
4987
5272
  ) : void 0;
4988
5273
  const updateManyInput = features.update && features.updateMany ? generateUpdateManyInput({ typeName, updatePrefix: prefixes.update, updateInput, tableFilters }) : void 0;
4989
5274
  const updateManyGenerated = updateManyInput ? generateUpdateMany3(
@@ -4996,7 +5281,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4996
5281
  updateManyFieldName,
4997
5282
  typeName,
4998
5283
  typeNameMapper,
4999
- filterCtx
5284
+ filterCtx,
5285
+ mutationTxCtx
5000
5286
  ) : void 0;
5001
5287
  const deleteGenerated = features.delete ? generateDelete3(
5002
5288
  db,
@@ -5008,7 +5294,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5008
5294
  false,
5009
5295
  features.requireWhere,
5010
5296
  filterCtx,
5011
- { tableName, relationMap: namedRelations, tables }
5297
+ { tableName, relationMap: namedRelations, tables },
5298
+ mutationTxCtx
5012
5299
  ) : void 0;
5013
5300
  const deleteSingleGenerated = features.delete ? generateDelete3(
5014
5301
  db,
@@ -5020,7 +5307,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5020
5307
  true,
5021
5308
  features.requireWhere,
5022
5309
  filterCtx,
5023
- { tableName, relationMap: namedRelations, tables }
5310
+ { tableName, relationMap: namedRelations, tables },
5311
+ mutationTxCtx
5024
5312
  ) : void 0;
5025
5313
  const aggregateType = features.aggregates ? generateAggregateTypes(schema[tableName], tableName, typeName, cacheCtx) : void 0;
5026
5314
  const aggregateGenerated = features.aggregates ? generateAggregate(
@@ -5159,6 +5447,11 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5159
5447
  inputs[havingInput.name] = havingInput;
5160
5448
  }
5161
5449
  }
5450
+ if (mutationTxCtx) {
5451
+ for (const name of Object.keys(mutations)) {
5452
+ mutationTxCtx.fieldNames.add(name);
5453
+ }
5454
+ }
5162
5455
  const fieldResolvers = {};
5163
5456
  for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
5164
5457
  const relResolvers = {};
@@ -5215,6 +5508,8 @@ var buildSchema = (db, config) => {
5215
5508
  defaultListSize: (complexityConfig === true ? void 0 : complexityConfig.defaultListSize) ?? 10,
5216
5509
  aggregateCost: (complexityConfig === true ? void 0 : complexityConfig.aggregateCost) ?? 10
5217
5510
  };
5511
+ const transactionsOpt = config?.transactions;
5512
+ const transactions = transactionsOpt === void 0 || transactionsOpt === "none" ? void 0 : { timeoutMs: (transactionsOpt === "auto" ? void 0 : transactionsOpt.timeoutMs) ?? 3e4 };
5218
5513
  const eagerOpt = config?.eagerLoadRelations;
5219
5514
  const shouldEagerLoad = eagerOpt === void 0 || eagerOpt === true ? () => true : eagerOpt === false ? () => false : eagerOpt;
5220
5515
  if (!typeNameMapper && suffixes.list === suffixes.single) {
@@ -5242,7 +5537,8 @@ var buildSchema = (db, config) => {
5242
5537
  typeNameMapper,
5243
5538
  shouldEagerLoad,
5244
5539
  features,
5245
- complexity
5540
+ complexity,
5541
+ transactions
5246
5542
  };
5247
5543
  let generatorOutput;
5248
5544
  if ((0, import_drizzle_orm8.is)(db, import_mysql_core3.MySqlDatabase)) {