@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.js CHANGED
@@ -605,6 +605,203 @@ var resolveQueryExecutor = (db, context, tableName, buildTimeQueryBase) => {
605
605
  queryBase: buildTimeQueryBase ? executor?.query?.[tableName] ?? buildTimeQueryBase : buildTimeQueryBase
606
606
  };
607
607
  };
608
+ var createMutationTxCtx = (options) => options ? { timeoutMs: options.timeoutMs, fieldNames: /* @__PURE__ */ new Set(), states: /* @__PURE__ */ new WeakMap() } : void 0;
609
+ var includedByDirectives = (node, variables) => {
610
+ for (const directive of node.directives ?? []) {
611
+ const name = directive.name.value;
612
+ if (name !== "skip" && name !== "include") {
613
+ continue;
614
+ }
615
+ const ifArg = directive.arguments?.find((arg) => arg.name.value === "if");
616
+ let value;
617
+ if (ifArg?.value.kind === "BooleanValue") {
618
+ value = ifArg.value.value;
619
+ } else if (ifArg?.value.kind === "Variable") {
620
+ value = variables?.[ifArg.value.name.value];
621
+ }
622
+ if (name === "skip" && value === true) {
623
+ return false;
624
+ }
625
+ if (name === "include" && value !== true) {
626
+ return false;
627
+ }
628
+ }
629
+ return true;
630
+ };
631
+ var collectRootMutationKeys = (selectionSet, fragments, variables, known, keys, visitedFragments) => {
632
+ for (const selection of selectionSet.selections) {
633
+ if (!includedByDirectives(selection, variables)) {
634
+ continue;
635
+ }
636
+ if (selection.kind === "Field") {
637
+ const name = selection.name.value;
638
+ if (name === "__typename") {
639
+ continue;
640
+ }
641
+ if (!known.has(name)) {
642
+ return false;
643
+ }
644
+ keys.add(selection.alias?.value ?? name);
645
+ } else if (selection.kind === "InlineFragment") {
646
+ if (!collectRootMutationKeys(selection.selectionSet, fragments, variables, known, keys, visitedFragments)) {
647
+ return false;
648
+ }
649
+ } else if (selection.kind === "FragmentSpread") {
650
+ const fragmentName = selection.name.value;
651
+ if (visitedFragments.has(fragmentName)) {
652
+ continue;
653
+ }
654
+ visitedFragments.add(fragmentName);
655
+ const fragment = fragments?.[fragmentName];
656
+ if (!fragment) {
657
+ return false;
658
+ }
659
+ if (!collectRootMutationKeys(fragment.selectionSet, fragments, variables, known, keys, visitedFragments)) {
660
+ return false;
661
+ }
662
+ }
663
+ }
664
+ return true;
665
+ };
666
+ var abortedBatchError = () => new GraphQLError3(
667
+ "Drizzle-GraphQL Error: Mutation was not executed because an earlier mutation in the same request failed and the shared transaction was rolled back."
668
+ );
669
+ var openSharedTx = (db, context, txCtx, stateKey, expected) => {
670
+ let release;
671
+ let rejectLatch;
672
+ const latch = new Promise((resolve, reject) => {
673
+ release = resolve;
674
+ rejectLatch = reject;
675
+ });
676
+ let readyResolve;
677
+ let readyReject;
678
+ const ready = new Promise((resolve, reject) => {
679
+ readyResolve = resolve;
680
+ readyReject = reject;
681
+ });
682
+ ready.catch(() => {
683
+ });
684
+ const canStash = context !== null && typeof context === "object" && !Object.isFrozen(context);
685
+ const settled = db.transaction(async (tx) => {
686
+ if (canStash) {
687
+ context[drizzleExecutorKey] = tx;
688
+ }
689
+ readyResolve(tx);
690
+ await latch;
691
+ });
692
+ let timer;
693
+ const clearTimer = () => {
694
+ if (timer !== void 0) {
695
+ clearTimeout(timer);
696
+ timer = void 0;
697
+ }
698
+ };
699
+ const state = {
700
+ kind: "shared",
701
+ expected,
702
+ completed: 0,
703
+ failure: void 0,
704
+ aborted: false,
705
+ done: false,
706
+ ready,
707
+ settled,
708
+ release,
709
+ abort: (e) => {
710
+ if (!state.aborted) {
711
+ state.aborted = true;
712
+ state.failure = e;
713
+ }
714
+ rejectLatch(e);
715
+ },
716
+ suspendTimer: clearTimer,
717
+ bumpTimer: () => {
718
+ clearTimer();
719
+ if (state.done) {
720
+ return;
721
+ }
722
+ timer = setTimeout(() => {
723
+ state.abort(
724
+ new GraphQLError3(
725
+ `Drizzle-GraphQL Error: Shared mutation transaction timed out after ${txCtx.timeoutMs}ms waiting for the remaining mutation fields; rolling back.`
726
+ )
727
+ );
728
+ }, txCtx.timeoutMs);
729
+ timer.unref?.();
730
+ }
731
+ };
732
+ const cleanup = () => {
733
+ state.done = true;
734
+ clearTimer();
735
+ if (canStash) {
736
+ delete context[drizzleExecutorKey];
737
+ }
738
+ if (!state.aborted) {
739
+ txCtx.states.delete(stateKey);
740
+ }
741
+ };
742
+ settled.then(cleanup, (e) => {
743
+ cleanup();
744
+ readyReject(e);
745
+ });
746
+ state.bumpTimer();
747
+ return state;
748
+ };
749
+ var runOnSharedTx = async (state, body) => {
750
+ if (state.aborted) {
751
+ await state.settled.catch(() => {
752
+ });
753
+ throw abortedBatchError();
754
+ }
755
+ state.suspendTimer();
756
+ const tx = await state.ready;
757
+ try {
758
+ if (state.aborted) {
759
+ throw abortedBatchError();
760
+ }
761
+ const result = await body(tx);
762
+ state.completed += 1;
763
+ if (state.completed >= state.expected) {
764
+ state.release();
765
+ await state.settled;
766
+ } else {
767
+ state.bumpTimer();
768
+ }
769
+ return result;
770
+ } catch (e) {
771
+ state.abort(e);
772
+ await state.settled.catch(() => {
773
+ });
774
+ throw e;
775
+ }
776
+ };
777
+ var runMutation = async (db, context, info, txCtx, body) => {
778
+ const contextIsObject = context !== null && (typeof context === "object" || typeof context === "function");
779
+ const stateKey = contextIsObject ? context : info.operation;
780
+ let state = txCtx?.states.get(stateKey);
781
+ if (!state) {
782
+ if (contextIsObject && context[drizzleExecutorKey]) {
783
+ return body(context[drizzleExecutorKey]);
784
+ }
785
+ if (!txCtx || info.operation.operation !== "mutation") {
786
+ return body(db);
787
+ }
788
+ const keys = /* @__PURE__ */ new Set();
789
+ const allKnown = collectRootMutationKeys(
790
+ info.operation.selectionSet,
791
+ info.fragments,
792
+ info.variableValues,
793
+ txCtx.fieldNames,
794
+ keys,
795
+ /* @__PURE__ */ new Set()
796
+ );
797
+ state = !allKnown || keys.size <= 1 ? { kind: "passthrough" } : openSharedTx(db, context, txCtx, stateKey, keys.size);
798
+ txCtx.states.set(stateKey, state);
799
+ }
800
+ if (state.kind === "passthrough") {
801
+ return body(resolveExecutor(db, context));
802
+ }
803
+ return runOnSharedTx(state, body);
804
+ };
608
805
  var batchedPaginatedRelationQuery = async (db, targetTable, foreignCol, whereCondition, orderByArg, limit, offset, pkNames, orderCtx) => {
609
806
  const cols = getColumns(targetTable);
610
807
  const orderExprs = [
@@ -3047,7 +3244,7 @@ var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filte
3047
3244
  args: queryArgs
3048
3245
  };
3049
3246
  };
3050
- var generateInsertArray = (db, _tableName, table, baseType, fieldName) => {
3247
+ var generateInsertArray = (db, _tableName, table, baseType, fieldName, txCtx) => {
3051
3248
  const queryArgs = {
3052
3249
  values: {
3053
3250
  type: new GraphQLNonNull4(new GraphQLList4(new GraphQLNonNull4(baseType)))
@@ -3055,14 +3252,16 @@ var generateInsertArray = (db, _tableName, table, baseType, fieldName) => {
3055
3252
  };
3056
3253
  return {
3057
3254
  name: fieldName,
3058
- resolver: async (_source, args, context, _info) => {
3255
+ resolver: async (_source, args, context, info) => {
3059
3256
  try {
3060
- const input = remapFromGraphQLArrayInput(args.values, table);
3061
- if (!input.length) {
3062
- throw new GraphQLError5("No values were provided!");
3063
- }
3064
- await resolveExecutor(db, context).insert(table).values(input);
3065
- return { isSuccess: true };
3257
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3258
+ const input = remapFromGraphQLArrayInput(args.values, table);
3259
+ if (!input.length) {
3260
+ throw new GraphQLError5("No values were provided!");
3261
+ }
3262
+ await executor.insert(table).values(input);
3263
+ return { isSuccess: true };
3264
+ });
3066
3265
  } catch (e) {
3067
3266
  throw toGraphQLError(e);
3068
3267
  }
@@ -3070,7 +3269,7 @@ var generateInsertArray = (db, _tableName, table, baseType, fieldName) => {
3070
3269
  args: queryArgs
3071
3270
  };
3072
3271
  };
3073
- var generateInsertSingle = (db, _tableName, table, baseType, fieldName) => {
3272
+ var generateInsertSingle = (db, _tableName, table, baseType, fieldName, txCtx) => {
3074
3273
  const queryArgs = {
3075
3274
  values: {
3076
3275
  type: new GraphQLNonNull4(baseType)
@@ -3078,11 +3277,13 @@ var generateInsertSingle = (db, _tableName, table, baseType, fieldName) => {
3078
3277
  };
3079
3278
  return {
3080
3279
  name: fieldName,
3081
- resolver: async (_source, args, context, _info) => {
3280
+ resolver: async (_source, args, context, info) => {
3082
3281
  try {
3083
- const input = remapFromGraphQLSingleInput(args.values, table);
3084
- await resolveExecutor(db, context).insert(table).values(input);
3085
- return { isSuccess: true };
3282
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3283
+ const input = remapFromGraphQLSingleInput(args.values, table);
3284
+ await executor.insert(table).values(input);
3285
+ return { isSuccess: true };
3286
+ });
3086
3287
  } catch (e) {
3087
3288
  throw toGraphQLError(e);
3088
3289
  }
@@ -3090,7 +3291,7 @@ var generateInsertSingle = (db, _tableName, table, baseType, fieldName) => {
3090
3291
  args: queryArgs
3091
3292
  };
3092
3293
  };
3093
- var generateUpsert = (db, table, baseType, onConflictType, fieldName, single) => {
3294
+ var generateUpsert = (db, table, baseType, onConflictType, fieldName, single, txCtx) => {
3094
3295
  const queryArgs = {
3095
3296
  values: {
3096
3297
  type: single ? new GraphQLNonNull4(baseType) : new GraphQLNonNull4(new GraphQLList4(new GraphQLNonNull4(baseType)))
@@ -3103,28 +3304,29 @@ var generateUpsert = (db, table, baseType, onConflictType, fieldName, single) =>
3103
3304
  const pkNames = mysqlPrimaryKeyPropNames(table);
3104
3305
  return {
3105
3306
  name: fieldName,
3106
- resolver: async (_source, args, context, _info) => {
3307
+ resolver: async (_source, args, context, info) => {
3107
3308
  try {
3108
- const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
3109
- if (!input.length) {
3110
- throw new GraphQLError5("No values were provided!");
3111
- }
3112
- const plan = resolveConflictPlan({
3113
- table,
3114
- values: input,
3115
- onConflict: args.onConflict,
3116
- pkNames,
3117
- uniqueSets: [],
3118
- excludedRef: mysqlValuesColumnRef,
3119
- withTarget: false
3309
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3310
+ const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
3311
+ if (!input.length) {
3312
+ throw new GraphQLError5("No values were provided!");
3313
+ }
3314
+ const plan = resolveConflictPlan({
3315
+ table,
3316
+ values: input,
3317
+ onConflict: args.onConflict,
3318
+ pkNames,
3319
+ uniqueSets: [],
3320
+ excludedRef: mysqlValuesColumnRef,
3321
+ withTarget: false
3322
+ });
3323
+ if (plan.action === "NOTHING") {
3324
+ await executor.insert(table).ignore().values(input);
3325
+ } else {
3326
+ await executor.insert(table).values(input).onDuplicateKeyUpdate({ set: plan.set });
3327
+ }
3328
+ return { isSuccess: true };
3120
3329
  });
3121
- const executor = resolveExecutor(db, context);
3122
- if (plan.action === "NOTHING") {
3123
- await executor.insert(table).ignore().values(input);
3124
- } else {
3125
- await executor.insert(table).values(input).onDuplicateKeyUpdate({ set: plan.set });
3126
- }
3127
- return { isSuccess: true };
3128
3330
  } catch (e) {
3129
3331
  throw toGraphQLError(e);
3130
3332
  }
@@ -3132,7 +3334,7 @@ var generateUpsert = (db, table, baseType, onConflictType, fieldName, single) =>
3132
3334
  args: queryArgs
3133
3335
  };
3134
3336
  };
3135
- var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, single, requireWhere, filterCtx) => {
3337
+ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, single, requireWhere, filterCtx, txCtx) => {
3136
3338
  const queryArgs = {
3137
3339
  set: {
3138
3340
  type: new GraphQLNonNull4(setArgs)
@@ -3143,25 +3345,26 @@ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, sing
3143
3345
  };
3144
3346
  return {
3145
3347
  name: fieldName,
3146
- resolver: async (_source, args, context, _info) => {
3348
+ resolver: async (_source, args, context, info) => {
3147
3349
  try {
3148
- const { where, set } = args;
3149
- const input = remapFromGraphQLSingleInput(set, table);
3150
- if (!Object.keys(input).length) {
3151
- throw new GraphQLError5("Unable to update with no values specified!");
3152
- }
3153
- const relationCtx = relationFilterCtx(filterCtx, tableName);
3154
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3155
- const executor = resolveExecutor(db, context);
3156
- if (single) {
3157
- await assertSingleMatch(executor, table, filters, fieldName);
3158
- }
3159
- let query = executor.update(table).set(input);
3160
- if (filters) {
3161
- query = query.where(filters);
3162
- }
3163
- await query;
3164
- return { isSuccess: true };
3350
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3351
+ const { where, set } = args;
3352
+ const input = remapFromGraphQLSingleInput(set, table);
3353
+ if (!Object.keys(input).length) {
3354
+ throw new GraphQLError5("Unable to update with no values specified!");
3355
+ }
3356
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
3357
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3358
+ if (single) {
3359
+ await assertSingleMatch(executor, table, filters, fieldName);
3360
+ }
3361
+ let query = executor.update(table).set(input);
3362
+ if (filters) {
3363
+ query = query.where(filters);
3364
+ }
3365
+ await query;
3366
+ return { isSuccess: true };
3367
+ });
3165
3368
  } catch (e) {
3166
3369
  throw toGraphQLError(e);
3167
3370
  }
@@ -3169,7 +3372,7 @@ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, sing
3169
3372
  args: queryArgs
3170
3373
  };
3171
3374
  };
3172
- var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filterCtx) => {
3375
+ var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filterCtx, txCtx) => {
3173
3376
  const queryArgs = {
3174
3377
  updates: {
3175
3378
  type: new GraphQLNonNull4(new GraphQLList4(new GraphQLNonNull4(updateManyInput)))
@@ -3177,33 +3380,34 @@ var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filt
3177
3380
  };
3178
3381
  return {
3179
3382
  name: fieldName,
3180
- resolver: async (_source, args, context, _info) => {
3383
+ resolver: async (_source, args, context, info) => {
3181
3384
  try {
3182
- const { updates } = args;
3183
- if (!updates.length) {
3184
- throw new GraphQLError5("No updates were provided!");
3185
- }
3186
- const entries = updates.map(({ where, set }) => {
3187
- const input = remapFromGraphQLSingleInput(set, table);
3188
- if (!Object.keys(input).length) {
3189
- throw new GraphQLError5("Unable to update with no values specified!");
3385
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3386
+ const { updates } = args;
3387
+ if (!updates.length) {
3388
+ throw new GraphQLError5("No updates were provided!");
3190
3389
  }
3191
- return {
3192
- set: input,
3193
- filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
3194
- };
3195
- });
3196
- const executor = resolveExecutor(db, context);
3197
- await executor.transaction(async (tx) => {
3198
- for (const entry of entries) {
3199
- let query = tx.update(table).set(entry.set);
3200
- if (entry.filters) {
3201
- query = query.where(entry.filters);
3390
+ const entries = updates.map(({ where, set }) => {
3391
+ const input = remapFromGraphQLSingleInput(set, table);
3392
+ if (!Object.keys(input).length) {
3393
+ throw new GraphQLError5("Unable to update with no values specified!");
3202
3394
  }
3203
- await query;
3204
- }
3395
+ return {
3396
+ set: input,
3397
+ filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
3398
+ };
3399
+ });
3400
+ await executor.transaction(async (tx) => {
3401
+ for (const entry of entries) {
3402
+ let query = tx.update(table).set(entry.set);
3403
+ if (entry.filters) {
3404
+ query = query.where(entry.filters);
3405
+ }
3406
+ await query;
3407
+ }
3408
+ });
3409
+ return { isSuccess: true };
3205
3410
  });
3206
- return { isSuccess: true };
3207
3411
  } catch (e) {
3208
3412
  throw toGraphQLError(e);
3209
3413
  }
@@ -3211,7 +3415,7 @@ var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filt
3211
3415
  args: queryArgs
3212
3416
  };
3213
3417
  };
3214
- var generateDelete = (db, tableName, table, filterArgs, fieldName, single, requireWhere, filterCtx) => {
3418
+ var generateDelete = (db, tableName, table, filterArgs, fieldName, single, requireWhere, filterCtx, txCtx) => {
3215
3419
  const queryArgs = {
3216
3420
  where: {
3217
3421
  type: single || requireWhere ? new GraphQLNonNull4(filterArgs) : filterArgs
@@ -3219,21 +3423,22 @@ var generateDelete = (db, tableName, table, filterArgs, fieldName, single, requi
3219
3423
  };
3220
3424
  return {
3221
3425
  name: fieldName,
3222
- resolver: async (_source, args, context, _info) => {
3426
+ resolver: async (_source, args, context, info) => {
3223
3427
  try {
3224
- const { where } = args;
3225
- const relationCtx = relationFilterCtx(filterCtx, tableName);
3226
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3227
- const executor = resolveExecutor(db, context);
3228
- if (single) {
3229
- await assertSingleMatch(executor, table, filters, fieldName);
3230
- }
3231
- let query = executor.delete(table);
3232
- if (filters) {
3233
- query = query.where(filters);
3234
- }
3235
- await query;
3236
- return { isSuccess: true };
3428
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3429
+ const { where } = args;
3430
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
3431
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3432
+ if (single) {
3433
+ await assertSingleMatch(executor, table, filters, fieldName);
3434
+ }
3435
+ let query = executor.delete(table);
3436
+ if (filters) {
3437
+ query = query.where(filters);
3438
+ }
3439
+ await query;
3440
+ return { isSuccess: true };
3441
+ });
3237
3442
  } catch (e) {
3238
3443
  throw toGraphQLError(e);
3239
3444
  }
@@ -3273,6 +3478,7 @@ var generateSchemaData = (db, schema, relations, options) => {
3273
3478
  const relationAggregateFactory = features.relationAggregates ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
3274
3479
  const queries = {};
3275
3480
  const mutations = {};
3481
+ const mutationTxCtx = createMutationTxCtx(options.transactions);
3276
3482
  const gqlSchemaTypes = Object.fromEntries(
3277
3483
  Object.entries(tables).map(([tableName, _table]) => [
3278
3484
  tableName,
@@ -3348,8 +3554,22 @@ var generateSchemaData = (db, schema, relations, options) => {
3348
3554
  typeNameMapper,
3349
3555
  filterCtx
3350
3556
  );
3351
- const insertArrGenerated = features.insert ? generateInsertArray(db, tableName, schema[tableName], insertInput, createArrayFieldName) : void 0;
3352
- const insertSingleGenerated = features.insert ? generateInsertSingle(db, tableName, schema[tableName], insertInput, createSingleFieldName) : void 0;
3557
+ const insertArrGenerated = features.insert ? generateInsertArray(
3558
+ db,
3559
+ tableName,
3560
+ schema[tableName],
3561
+ insertInput,
3562
+ createArrayFieldName,
3563
+ mutationTxCtx
3564
+ ) : void 0;
3565
+ const insertSingleGenerated = features.insert ? generateInsertSingle(
3566
+ db,
3567
+ tableName,
3568
+ schema[tableName],
3569
+ insertInput,
3570
+ createSingleFieldName,
3571
+ mutationTxCtx
3572
+ ) : void 0;
3353
3573
  const onConflictInput = features.upsert ? generateOnConflictInput({
3354
3574
  table: schema[tableName],
3355
3575
  typeName,
@@ -3357,8 +3577,24 @@ var generateSchemaData = (db, schema, relations, options) => {
3357
3577
  tableFilters,
3358
3578
  withTarget: false
3359
3579
  }) : void 0;
3360
- const upsertArrGenerated = onConflictInput ? generateUpsert(db, schema[tableName], insertInput, onConflictInput, upsertArrayFieldName, false) : void 0;
3361
- const upsertSingleGenerated = onConflictInput ? generateUpsert(db, schema[tableName], insertInput, onConflictInput, upsertSingleFieldName, true) : void 0;
3580
+ const upsertArrGenerated = onConflictInput ? generateUpsert(
3581
+ db,
3582
+ schema[tableName],
3583
+ insertInput,
3584
+ onConflictInput,
3585
+ upsertArrayFieldName,
3586
+ false,
3587
+ mutationTxCtx
3588
+ ) : void 0;
3589
+ const upsertSingleGenerated = onConflictInput ? generateUpsert(
3590
+ db,
3591
+ schema[tableName],
3592
+ insertInput,
3593
+ onConflictInput,
3594
+ upsertSingleFieldName,
3595
+ true,
3596
+ mutationTxCtx
3597
+ ) : void 0;
3362
3598
  const updateGenerated = features.update ? generateUpdate(
3363
3599
  db,
3364
3600
  tableName,
@@ -3368,7 +3604,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3368
3604
  updateFieldName,
3369
3605
  false,
3370
3606
  features.requireWhere,
3371
- filterCtx
3607
+ filterCtx,
3608
+ mutationTxCtx
3372
3609
  ) : void 0;
3373
3610
  const updateSingleGenerated = features.update ? generateUpdate(
3374
3611
  db,
@@ -3379,7 +3616,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3379
3616
  updateSingleFieldName,
3380
3617
  true,
3381
3618
  features.requireWhere,
3382
- filterCtx
3619
+ filterCtx,
3620
+ mutationTxCtx
3383
3621
  ) : void 0;
3384
3622
  const updateManyInput = features.update && features.updateMany ? generateUpdateManyInput({ typeName, updatePrefix: prefixes.update, updateInput, tableFilters }) : void 0;
3385
3623
  const updateManyGenerated = updateManyInput ? generateUpdateMany(
@@ -3388,7 +3626,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3388
3626
  schema[tableName],
3389
3627
  updateManyInput,
3390
3628
  updateManyFieldName,
3391
- filterCtx
3629
+ filterCtx,
3630
+ mutationTxCtx
3392
3631
  ) : void 0;
3393
3632
  const deleteGenerated = features.delete ? generateDelete(
3394
3633
  db,
@@ -3398,7 +3637,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3398
3637
  deleteFieldName,
3399
3638
  false,
3400
3639
  features.requireWhere,
3401
- filterCtx
3640
+ filterCtx,
3641
+ mutationTxCtx
3402
3642
  ) : void 0;
3403
3643
  const deleteSingleGenerated = features.delete ? generateDelete(
3404
3644
  db,
@@ -3408,7 +3648,8 @@ var generateSchemaData = (db, schema, relations, options) => {
3408
3648
  deleteSingleFieldName,
3409
3649
  true,
3410
3650
  features.requireWhere,
3411
- filterCtx
3651
+ filterCtx,
3652
+ mutationTxCtx
3412
3653
  ) : void 0;
3413
3654
  const aggregateType = features.aggregates ? generateAggregateTypes(schema[tableName], tableName, typeName, cacheCtx) : void 0;
3414
3655
  const aggregateGenerated = features.aggregates ? generateAggregate(
@@ -3501,6 +3742,11 @@ var generateSchemaData = (db, schema, relations, options) => {
3501
3742
  inputs[havingInput.name] = havingInput;
3502
3743
  }
3503
3744
  }
3745
+ if (mutationTxCtx) {
3746
+ for (const name of Object.keys(mutations)) {
3747
+ mutationTxCtx.fieldNames.add(name);
3748
+ }
3749
+ }
3504
3750
  const fieldResolvers = {};
3505
3751
  for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
3506
3752
  const relResolvers = {};
@@ -3708,7 +3954,7 @@ var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filt
3708
3954
  };
3709
3955
  };
3710
3956
  var pgPrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, getTableConfig2);
3711
- var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
3957
+ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx) => {
3712
3958
  const queryArgs = {
3713
3959
  values: {
3714
3960
  type: new GraphQLNonNull5(new GraphQLList5(new GraphQLNonNull5(baseType)))
@@ -3719,31 +3965,32 @@ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType,
3719
3965
  name: fieldName,
3720
3966
  resolver: async (_source, args, context, info) => {
3721
3967
  try {
3722
- const input = remapFromGraphQLArrayInput(args.values, table);
3723
- if (!input.length) {
3724
- throw new GraphQLError6("No values were provided!");
3725
- }
3726
- const parsedInfo = parseResolveInfo3(info, {
3727
- deep: true
3728
- });
3729
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3730
- relationMap,
3731
- tables,
3732
- tableName,
3733
- typeName,
3734
- typeNameMapper,
3735
- table,
3736
- pkNames,
3737
- parsedInfo
3968
+ return await runMutation(db, context, info, txCtx, async (executor) => {
3969
+ const input = remapFromGraphQLArrayInput(args.values, table);
3970
+ if (!input.length) {
3971
+ throw new GraphQLError6("No values were provided!");
3972
+ }
3973
+ const parsedInfo = parseResolveInfo3(info, {
3974
+ deep: true
3975
+ });
3976
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3977
+ relationMap,
3978
+ tables,
3979
+ tableName,
3980
+ typeName,
3981
+ typeNameMapper,
3982
+ table,
3983
+ pkNames,
3984
+ parsedInfo
3985
+ });
3986
+ let query = executor.insert(table).values(input).returning(columns);
3987
+ if (conflictDoNothing) {
3988
+ query = query.onConflictDoNothing();
3989
+ }
3990
+ const result = await query;
3991
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3992
+ return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3738
3993
  });
3739
- const executor = resolveExecutor(db, context);
3740
- let query = executor.insert(table).values(input).returning(columns);
3741
- if (conflictDoNothing) {
3742
- query = query.onConflictDoNothing();
3743
- }
3744
- const result = await query;
3745
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3746
- return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3747
3994
  } catch (e) {
3748
3995
  throw toGraphQLError(e);
3749
3996
  }
@@ -3751,7 +3998,7 @@ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType,
3751
3998
  args: queryArgs
3752
3999
  };
3753
4000
  };
3754
- var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
4001
+ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx) => {
3755
4002
  const queryArgs = {
3756
4003
  values: {
3757
4004
  type: new GraphQLNonNull5(baseType)
@@ -3762,31 +4009,32 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
3762
4009
  name: fieldName,
3763
4010
  resolver: async (_source, args, context, info) => {
3764
4011
  try {
3765
- const input = remapFromGraphQLSingleInput(args.values, table);
3766
- const parsedInfo = parseResolveInfo3(info, {
3767
- deep: true
3768
- });
3769
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3770
- relationMap,
3771
- tables,
3772
- tableName,
3773
- typeName,
3774
- typeNameMapper,
3775
- table,
3776
- pkNames,
3777
- parsedInfo
4012
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4013
+ const input = remapFromGraphQLSingleInput(args.values, table);
4014
+ const parsedInfo = parseResolveInfo3(info, {
4015
+ deep: true
4016
+ });
4017
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4018
+ relationMap,
4019
+ tables,
4020
+ tableName,
4021
+ typeName,
4022
+ typeNameMapper,
4023
+ table,
4024
+ pkNames,
4025
+ parsedInfo
4026
+ });
4027
+ let query = executor.insert(table).values(input).returning(columns);
4028
+ if (conflictDoNothing) {
4029
+ query = query.onConflictDoNothing();
4030
+ }
4031
+ const result = await query;
4032
+ if (!result[0]) {
4033
+ return void 0;
4034
+ }
4035
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4036
+ return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
3778
4037
  });
3779
- const executor = resolveExecutor(db, context);
3780
- let query = executor.insert(table).values(input).returning(columns);
3781
- if (conflictDoNothing) {
3782
- query = query.onConflictDoNothing();
3783
- }
3784
- const result = await query;
3785
- if (!result[0]) {
3786
- return void 0;
3787
- }
3788
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3789
- return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
3790
4038
  } catch (e) {
3791
4039
  throw toGraphQLError(e);
3792
4040
  }
@@ -3794,7 +4042,7 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
3794
4042
  args: queryArgs
3795
4043
  };
3796
4044
  };
3797
- var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx) => {
4045
+ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx, txCtx) => {
3798
4046
  const queryArgs = {
3799
4047
  values: {
3800
4048
  type: single ? new GraphQLNonNull5(baseType) : new GraphQLNonNull5(new GraphQLList5(new GraphQLNonNull5(baseType)))
@@ -3809,40 +4057,41 @@ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onCo
3809
4057
  name: fieldName,
3810
4058
  resolver: async (_source, args, context, info) => {
3811
4059
  try {
3812
- const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
3813
- if (!input.length) {
3814
- throw new GraphQLError6("No values were provided!");
3815
- }
3816
- const parsedInfo = parseResolveInfo3(info, { deep: true });
3817
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3818
- relationMap,
3819
- tables,
3820
- tableName,
3821
- typeName,
3822
- typeNameMapper,
3823
- table,
3824
- pkNames,
3825
- parsedInfo
3826
- });
3827
- const plan = resolveConflictPlan({
3828
- table,
3829
- values: input,
3830
- onConflict: args.onConflict,
3831
- pkNames,
3832
- uniqueSets,
3833
- excludedRef: excludedColumnRef,
3834
- withTarget: true,
3835
- buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
4060
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4061
+ const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
4062
+ if (!input.length) {
4063
+ throw new GraphQLError6("No values were provided!");
4064
+ }
4065
+ const parsedInfo = parseResolveInfo3(info, { deep: true });
4066
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4067
+ relationMap,
4068
+ tables,
4069
+ tableName,
4070
+ typeName,
4071
+ typeNameMapper,
4072
+ table,
4073
+ pkNames,
4074
+ parsedInfo
4075
+ });
4076
+ const plan = resolveConflictPlan({
4077
+ table,
4078
+ values: input,
4079
+ onConflict: args.onConflict,
4080
+ pkNames,
4081
+ uniqueSets,
4082
+ excludedRef: excludedColumnRef,
4083
+ withTarget: true,
4084
+ buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
4085
+ });
4086
+ let query = executor.insert(table).values(input).returning(columns);
4087
+ query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
4088
+ const result = await query;
4089
+ if (single && !result[0]) {
4090
+ return void 0;
4091
+ }
4092
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4093
+ return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3836
4094
  });
3837
- const executor = resolveExecutor(db, context);
3838
- let query = executor.insert(table).values(input).returning(columns);
3839
- query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
3840
- const result = await query;
3841
- if (single && !result[0]) {
3842
- return void 0;
3843
- }
3844
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3845
- return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3846
4095
  } catch (e) {
3847
4096
  throw toGraphQLError(e);
3848
4097
  }
@@ -3850,7 +4099,7 @@ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onCo
3850
4099
  args: queryArgs
3851
4100
  };
3852
4101
  };
3853
- var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx) => {
4102
+ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx, txCtx) => {
3854
4103
  const queryArgs = {
3855
4104
  set: {
3856
4105
  type: new GraphQLNonNull5(setArgs)
@@ -3864,44 +4113,45 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
3864
4113
  name: fieldName,
3865
4114
  resolver: async (_source, args, context, info) => {
3866
4115
  try {
3867
- const { where, set } = args;
3868
- const parsedInfo = parseResolveInfo3(info, {
3869
- deep: true
3870
- });
3871
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3872
- relationMap,
3873
- tables,
3874
- tableName,
3875
- typeName,
3876
- typeNameMapper,
3877
- table,
3878
- pkNames,
3879
- parsedInfo
4116
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4117
+ const { where, set } = args;
4118
+ const parsedInfo = parseResolveInfo3(info, {
4119
+ deep: true
4120
+ });
4121
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4122
+ relationMap,
4123
+ tables,
4124
+ tableName,
4125
+ typeName,
4126
+ typeNameMapper,
4127
+ table,
4128
+ pkNames,
4129
+ parsedInfo
4130
+ });
4131
+ const input = remapFromGraphQLSingleInput(set, table);
4132
+ if (!Object.keys(input).length) {
4133
+ throw new GraphQLError6("Unable to update with no values specified!");
4134
+ }
4135
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
4136
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4137
+ if (single) {
4138
+ await assertSingleMatch(executor, table, filters, fieldName);
4139
+ }
4140
+ let query = executor.update(table).set(input);
4141
+ if (filters) {
4142
+ query = query.where(filters);
4143
+ }
4144
+ query = query.returning(columns);
4145
+ const result = await query;
4146
+ if (single && result.length > 1) {
4147
+ throw new GraphQLError6(`${fieldName}: 'where' matched more than one row!`);
4148
+ }
4149
+ if (single && !result[0]) {
4150
+ return void 0;
4151
+ }
4152
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4153
+ return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3880
4154
  });
3881
- const input = remapFromGraphQLSingleInput(set, table);
3882
- if (!Object.keys(input).length) {
3883
- throw new GraphQLError6("Unable to update with no values specified!");
3884
- }
3885
- const relationCtx = relationFilterCtx(filterCtx, tableName);
3886
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
3887
- const executor = resolveExecutor(db, context);
3888
- if (single) {
3889
- await assertSingleMatch(executor, table, filters, fieldName);
3890
- }
3891
- let query = executor.update(table).set(input);
3892
- if (filters) {
3893
- query = query.where(filters);
3894
- }
3895
- query = query.returning(columns);
3896
- const result = await query;
3897
- if (single && result.length > 1) {
3898
- throw new GraphQLError6(`${fieldName}: 'where' matched more than one row!`);
3899
- }
3900
- if (single && !result[0]) {
3901
- return void 0;
3902
- }
3903
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
3904
- return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
3905
4155
  } catch (e) {
3906
4156
  throw toGraphQLError(e);
3907
4157
  }
@@ -3909,7 +4159,7 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
3909
4159
  args: queryArgs
3910
4160
  };
3911
4161
  };
3912
- var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx) => {
4162
+ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx, txCtx) => {
3913
4163
  const queryArgs = {
3914
4164
  updates: {
3915
4165
  type: new GraphQLNonNull5(new GraphQLList5(new GraphQLNonNull5(updateManyInput)))
@@ -3920,60 +4170,61 @@ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateMany
3920
4170
  name: fieldName,
3921
4171
  resolver: async (_source, args, context, info) => {
3922
4172
  try {
3923
- const { updates } = args;
3924
- if (!updates.length) {
3925
- throw new GraphQLError6("No updates were provided!");
3926
- }
3927
- const parsedInfo = parseResolveInfo3(info, {
3928
- deep: true
3929
- });
3930
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
3931
- relationMap,
3932
- tables,
3933
- tableName,
3934
- typeName,
3935
- typeNameMapper,
3936
- table,
3937
- pkNames,
3938
- parsedInfo
3939
- });
3940
- const entries = updates.map(({ where, set }) => {
3941
- const input = remapFromGraphQLSingleInput(set, table);
3942
- if (!Object.keys(input).length) {
3943
- throw new GraphQLError6("Unable to update with no values specified!");
4173
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4174
+ const { updates } = args;
4175
+ if (!updates.length) {
4176
+ throw new GraphQLError6("No updates were provided!");
3944
4177
  }
3945
- return {
3946
- set: input,
3947
- filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
3948
- };
3949
- });
3950
- const executor = resolveExecutor(db, context);
3951
- const perEntry = await executor.transaction(async (tx) => {
3952
- const results = [];
3953
- for (const entry of entries) {
3954
- let query = tx.update(table).set(entry.set);
3955
- if (entry.filters) {
3956
- query = query.where(entry.filters);
4178
+ const parsedInfo = parseResolveInfo3(info, {
4179
+ deep: true
4180
+ });
4181
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4182
+ relationMap,
4183
+ tables,
4184
+ tableName,
4185
+ typeName,
4186
+ typeNameMapper,
4187
+ table,
4188
+ pkNames,
4189
+ parsedInfo
4190
+ });
4191
+ const entries = updates.map(({ where, set }) => {
4192
+ const input = remapFromGraphQLSingleInput(set, table);
4193
+ if (!Object.keys(input).length) {
4194
+ throw new GraphQLError6("Unable to update with no values specified!");
4195
+ }
4196
+ return {
4197
+ set: input,
4198
+ filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
4199
+ };
4200
+ });
4201
+ const perEntry = await executor.transaction(async (tx) => {
4202
+ const results = [];
4203
+ for (const entry of entries) {
4204
+ let query = tx.update(table).set(entry.set);
4205
+ if (entry.filters) {
4206
+ query = query.where(entry.filters);
4207
+ }
4208
+ results.push(await query.returning(columns));
4209
+ }
4210
+ return results;
4211
+ });
4212
+ const flatRows = perEntry.flat();
4213
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, flatRows, pkNames, withParams) : flatRows;
4214
+ const output = [];
4215
+ let offset = 0;
4216
+ for (const rows of perEntry) {
4217
+ if (!rows.length) {
4218
+ output.push(null);
4219
+ continue;
3957
4220
  }
3958
- results.push(await query.returning(columns));
4221
+ for (let i = 0; i < rows.length; i++) {
4222
+ output.push(remapToGraphQLSingleOutput(enriched[offset + i], tableName, table, relationMap));
4223
+ }
4224
+ offset += rows.length;
3959
4225
  }
3960
- return results;
4226
+ return output;
3961
4227
  });
3962
- const flatRows = perEntry.flat();
3963
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, flatRows, pkNames, withParams) : flatRows;
3964
- const output = [];
3965
- let offset = 0;
3966
- for (const rows of perEntry) {
3967
- if (!rows.length) {
3968
- output.push(null);
3969
- continue;
3970
- }
3971
- for (let i = 0; i < rows.length; i++) {
3972
- output.push(remapToGraphQLSingleOutput(enriched[offset + i], tableName, table, relationMap));
3973
- }
3974
- offset += rows.length;
3975
- }
3976
- return output;
3977
4228
  } catch (e) {
3978
4229
  throw toGraphQLError(e);
3979
4230
  }
@@ -3981,7 +4232,7 @@ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateMany
3981
4232
  args: queryArgs
3982
4233
  };
3983
4234
  };
3984
- var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx) => {
4235
+ var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx, txCtx) => {
3985
4236
  const queryArgs = {
3986
4237
  where: {
3987
4238
  type: single || requireWhere ? new GraphQLNonNull5(filterArgs) : filterArgs
@@ -3991,34 +4242,35 @@ var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, si
3991
4242
  name: fieldName,
3992
4243
  resolver: async (_source, args, context, info) => {
3993
4244
  try {
3994
- const { where } = args;
3995
- const parsedInfo = parseResolveInfo3(info, {
3996
- deep: true
4245
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4246
+ const { where } = args;
4247
+ const parsedInfo = parseResolveInfo3(info, {
4248
+ deep: true
4249
+ });
4250
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
4251
+ parsedInfo.fieldsByTypeName[typeName],
4252
+ table,
4253
+ selectionCtx
4254
+ );
4255
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
4256
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4257
+ if (single) {
4258
+ await assertSingleMatch(executor, table, filters, fieldName);
4259
+ }
4260
+ let query = executor.delete(table);
4261
+ if (filters) {
4262
+ query = query.where(filters);
4263
+ }
4264
+ query = query.returning(columns);
4265
+ const result = await query;
4266
+ if (single && result.length > 1) {
4267
+ throw new GraphQLError6(`${fieldName}: 'where' matched more than one row!`);
4268
+ }
4269
+ if (single) {
4270
+ return result[0] ? remapToGraphQLSingleOutput(result[0], tableName, table) : void 0;
4271
+ }
4272
+ return remapToGraphQLArrayOutput(result, tableName, table);
3997
4273
  });
3998
- const columns = extractSelectedColumnsFromTreeSQLFormat(
3999
- parsedInfo.fieldsByTypeName[typeName],
4000
- table,
4001
- selectionCtx
4002
- );
4003
- const relationCtx = relationFilterCtx(filterCtx, tableName);
4004
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4005
- const executor = resolveExecutor(db, context);
4006
- if (single) {
4007
- await assertSingleMatch(executor, table, filters, fieldName);
4008
- }
4009
- let query = executor.delete(table);
4010
- if (filters) {
4011
- query = query.where(filters);
4012
- }
4013
- query = query.returning(columns);
4014
- const result = await query;
4015
- if (single && result.length > 1) {
4016
- throw new GraphQLError6(`${fieldName}: 'where' matched more than one row!`);
4017
- }
4018
- if (single) {
4019
- return result[0] ? remapToGraphQLSingleOutput(result[0], tableName, table) : void 0;
4020
- }
4021
- return remapToGraphQLArrayOutput(result, tableName, table);
4022
4274
  } catch (e) {
4023
4275
  throw toGraphQLError(e);
4024
4276
  }
@@ -4065,6 +4317,7 @@ function generateSchemaData2(db, schema, relations, options) {
4065
4317
  const relationAggregateFactory = features.relationAggregates ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
4066
4318
  const queries = {};
4067
4319
  const mutations = {};
4320
+ const mutationTxCtx = createMutationTxCtx(options.transactions);
4068
4321
  const gqlSchemaTypes = Object.fromEntries(
4069
4322
  Object.entries(tables).map(([tableName, _table]) => [
4070
4323
  tableName,
@@ -4139,7 +4392,8 @@ function generateSchemaData2(db, schema, relations, options) {
4139
4392
  createArrayFieldName,
4140
4393
  typeName,
4141
4394
  typeNameMapper,
4142
- conflictDoNothing
4395
+ conflictDoNothing,
4396
+ mutationTxCtx
4143
4397
  ) : void 0;
4144
4398
  const insertSingleGenerated = features.insert ? generateInsertSingle2(
4145
4399
  db,
@@ -4151,7 +4405,8 @@ function generateSchemaData2(db, schema, relations, options) {
4151
4405
  createSingleFieldName,
4152
4406
  typeName,
4153
4407
  typeNameMapper,
4154
- conflictDoNothing
4408
+ conflictDoNothing,
4409
+ mutationTxCtx
4155
4410
  ) : void 0;
4156
4411
  const uniqueSets = features.upsert ? getUniqueColumnSets(schema[tableName], getTableConfig2) : [];
4157
4412
  const onConflictInput = features.upsert ? generateOnConflictInput({
@@ -4174,7 +4429,8 @@ function generateSchemaData2(db, schema, relations, options) {
4174
4429
  typeName,
4175
4430
  false,
4176
4431
  typeNameMapper,
4177
- filterCtx
4432
+ filterCtx,
4433
+ mutationTxCtx
4178
4434
  ) : void 0;
4179
4435
  const upsertSingleGenerated = onConflictInput ? generateUpsert2(
4180
4436
  db,
@@ -4189,7 +4445,8 @@ function generateSchemaData2(db, schema, relations, options) {
4189
4445
  typeName,
4190
4446
  true,
4191
4447
  typeNameMapper,
4192
- filterCtx
4448
+ filterCtx,
4449
+ mutationTxCtx
4193
4450
  ) : void 0;
4194
4451
  const updateGenerated = features.update ? generateUpdate2(
4195
4452
  db,
@@ -4204,7 +4461,8 @@ function generateSchemaData2(db, schema, relations, options) {
4204
4461
  false,
4205
4462
  features.requireWhere,
4206
4463
  typeNameMapper,
4207
- filterCtx
4464
+ filterCtx,
4465
+ mutationTxCtx
4208
4466
  ) : void 0;
4209
4467
  const updateSingleGenerated = features.update ? generateUpdate2(
4210
4468
  db,
@@ -4219,7 +4477,8 @@ function generateSchemaData2(db, schema, relations, options) {
4219
4477
  true,
4220
4478
  features.requireWhere,
4221
4479
  typeNameMapper,
4222
- filterCtx
4480
+ filterCtx,
4481
+ mutationTxCtx
4223
4482
  ) : void 0;
4224
4483
  const updateManyInput = features.update && features.updateMany ? generateUpdateManyInput({ typeName, updatePrefix: prefixes.update, updateInput, tableFilters }) : void 0;
4225
4484
  const updateManyGenerated = updateManyInput ? generateUpdateMany2(
@@ -4232,7 +4491,8 @@ function generateSchemaData2(db, schema, relations, options) {
4232
4491
  updateManyFieldName,
4233
4492
  typeName,
4234
4493
  typeNameMapper,
4235
- filterCtx
4494
+ filterCtx,
4495
+ mutationTxCtx
4236
4496
  ) : void 0;
4237
4497
  const deleteGenerated = features.delete ? generateDelete2(
4238
4498
  db,
@@ -4244,7 +4504,8 @@ function generateSchemaData2(db, schema, relations, options) {
4244
4504
  false,
4245
4505
  features.requireWhere,
4246
4506
  filterCtx,
4247
- { tableName, relationMap: namedRelations, tables }
4507
+ { tableName, relationMap: namedRelations, tables },
4508
+ mutationTxCtx
4248
4509
  ) : void 0;
4249
4510
  const deleteSingleGenerated = features.delete ? generateDelete2(
4250
4511
  db,
@@ -4256,7 +4517,8 @@ function generateSchemaData2(db, schema, relations, options) {
4256
4517
  true,
4257
4518
  features.requireWhere,
4258
4519
  filterCtx,
4259
- { tableName, relationMap: namedRelations, tables }
4520
+ { tableName, relationMap: namedRelations, tables },
4521
+ mutationTxCtx
4260
4522
  ) : void 0;
4261
4523
  const aggregateType = features.aggregates ? generateAggregateTypes(schema[tableName], tableName, typeName, cacheCtx) : void 0;
4262
4524
  const aggregateGenerated = features.aggregates ? generateAggregate(
@@ -4395,6 +4657,11 @@ function generateSchemaData2(db, schema, relations, options) {
4395
4657
  inputs[havingInput.name] = havingInput;
4396
4658
  }
4397
4659
  }
4660
+ if (mutationTxCtx) {
4661
+ for (const name of Object.keys(mutations)) {
4662
+ mutationTxCtx.fieldNames.add(name);
4663
+ }
4664
+ }
4398
4665
  const fieldResolvers = {};
4399
4666
  for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
4400
4667
  const relResolvers = {};
@@ -4504,7 +4771,7 @@ var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filt
4504
4771
  };
4505
4772
  };
4506
4773
  var sqlitePrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, getTableConfig3);
4507
- var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
4774
+ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx) => {
4508
4775
  const queryArgs = {
4509
4776
  values: {
4510
4777
  type: new GraphQLNonNull6(new GraphQLList6(new GraphQLNonNull6(baseType)))
@@ -4515,31 +4782,32 @@ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType,
4515
4782
  name: fieldName,
4516
4783
  resolver: async (_source, args, context, info) => {
4517
4784
  try {
4518
- const input = remapFromGraphQLArrayInput(args.values, table);
4519
- if (!input.length) {
4520
- throw new GraphQLError7("No values were provided!");
4521
- }
4522
- const parsedInfo = parseResolveInfo4(info, {
4523
- deep: true
4524
- });
4525
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4526
- relationMap,
4527
- tables,
4528
- tableName,
4529
- typeName,
4530
- typeNameMapper,
4531
- table,
4532
- pkNames,
4533
- parsedInfo
4785
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4786
+ const input = remapFromGraphQLArrayInput(args.values, table);
4787
+ if (!input.length) {
4788
+ throw new GraphQLError7("No values were provided!");
4789
+ }
4790
+ const parsedInfo = parseResolveInfo4(info, {
4791
+ deep: true
4792
+ });
4793
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4794
+ relationMap,
4795
+ tables,
4796
+ tableName,
4797
+ typeName,
4798
+ typeNameMapper,
4799
+ table,
4800
+ pkNames,
4801
+ parsedInfo
4802
+ });
4803
+ let query = executor.insert(table).values(input).returning(columns);
4804
+ if (conflictDoNothing) {
4805
+ query = query.onConflictDoNothing();
4806
+ }
4807
+ const result = await query;
4808
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4809
+ return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4534
4810
  });
4535
- const executor = resolveExecutor(db, context);
4536
- let query = executor.insert(table).values(input).returning(columns);
4537
- if (conflictDoNothing) {
4538
- query = query.onConflictDoNothing();
4539
- }
4540
- const result = await query;
4541
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4542
- return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4543
4811
  } catch (e) {
4544
4812
  throw toGraphQLError(e);
4545
4813
  }
@@ -4547,7 +4815,7 @@ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType,
4547
4815
  args: queryArgs
4548
4816
  };
4549
4817
  };
4550
- var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false) => {
4818
+ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx) => {
4551
4819
  const queryArgs = {
4552
4820
  values: {
4553
4821
  type: new GraphQLNonNull6(baseType)
@@ -4558,31 +4826,32 @@ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType
4558
4826
  name: fieldName,
4559
4827
  resolver: async (_source, args, context, info) => {
4560
4828
  try {
4561
- const input = remapFromGraphQLSingleInput(args.values, table);
4562
- const parsedInfo = parseResolveInfo4(info, {
4563
- deep: true
4564
- });
4565
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4566
- relationMap,
4567
- tables,
4568
- tableName,
4569
- typeName,
4570
- typeNameMapper,
4571
- table,
4572
- pkNames,
4573
- parsedInfo
4829
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4830
+ const input = remapFromGraphQLSingleInput(args.values, table);
4831
+ const parsedInfo = parseResolveInfo4(info, {
4832
+ deep: true
4833
+ });
4834
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4835
+ relationMap,
4836
+ tables,
4837
+ tableName,
4838
+ typeName,
4839
+ typeNameMapper,
4840
+ table,
4841
+ pkNames,
4842
+ parsedInfo
4843
+ });
4844
+ let query = executor.insert(table).values(input).returning(columns);
4845
+ if (conflictDoNothing) {
4846
+ query = query.onConflictDoNothing();
4847
+ }
4848
+ const result = await query;
4849
+ if (!result[0]) {
4850
+ return void 0;
4851
+ }
4852
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4853
+ return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
4574
4854
  });
4575
- const executor = resolveExecutor(db, context);
4576
- let query = executor.insert(table).values(input).returning(columns);
4577
- if (conflictDoNothing) {
4578
- query = query.onConflictDoNothing();
4579
- }
4580
- const result = await query;
4581
- if (!result[0]) {
4582
- return void 0;
4583
- }
4584
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4585
- return remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap);
4586
4855
  } catch (e) {
4587
4856
  throw toGraphQLError(e);
4588
4857
  }
@@ -4590,7 +4859,7 @@ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType
4590
4859
  args: queryArgs
4591
4860
  };
4592
4861
  };
4593
- var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx) => {
4862
+ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx, txCtx) => {
4594
4863
  const queryArgs = {
4595
4864
  values: {
4596
4865
  type: single ? new GraphQLNonNull6(baseType) : new GraphQLNonNull6(new GraphQLList6(new GraphQLNonNull6(baseType)))
@@ -4605,40 +4874,41 @@ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onCo
4605
4874
  name: fieldName,
4606
4875
  resolver: async (_source, args, context, info) => {
4607
4876
  try {
4608
- const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
4609
- if (!input.length) {
4610
- throw new GraphQLError7("No values were provided!");
4611
- }
4612
- const parsedInfo = parseResolveInfo4(info, { deep: true });
4613
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4614
- relationMap,
4615
- tables,
4616
- tableName,
4617
- typeName,
4618
- typeNameMapper,
4619
- table,
4620
- pkNames,
4621
- parsedInfo
4622
- });
4623
- const plan = resolveConflictPlan({
4624
- table,
4625
- values: input,
4626
- onConflict: args.onConflict,
4627
- pkNames,
4628
- uniqueSets,
4629
- excludedRef: excludedColumnRef,
4630
- withTarget: true,
4631
- buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
4877
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4878
+ const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
4879
+ if (!input.length) {
4880
+ throw new GraphQLError7("No values were provided!");
4881
+ }
4882
+ const parsedInfo = parseResolveInfo4(info, { deep: true });
4883
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4884
+ relationMap,
4885
+ tables,
4886
+ tableName,
4887
+ typeName,
4888
+ typeNameMapper,
4889
+ table,
4890
+ pkNames,
4891
+ parsedInfo
4892
+ });
4893
+ const plan = resolveConflictPlan({
4894
+ table,
4895
+ values: input,
4896
+ onConflict: args.onConflict,
4897
+ pkNames,
4898
+ uniqueSets,
4899
+ excludedRef: excludedColumnRef,
4900
+ withTarget: true,
4901
+ buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
4902
+ });
4903
+ let query = executor.insert(table).values(input).returning(columns);
4904
+ query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
4905
+ const result = await query;
4906
+ if (single && !result[0]) {
4907
+ return void 0;
4908
+ }
4909
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4910
+ return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4632
4911
  });
4633
- const executor = resolveExecutor(db, context);
4634
- let query = executor.insert(table).values(input).returning(columns);
4635
- query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
4636
- const result = await query;
4637
- if (single && !result[0]) {
4638
- return void 0;
4639
- }
4640
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4641
- return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4642
4912
  } catch (e) {
4643
4913
  throw toGraphQLError(e);
4644
4914
  }
@@ -4646,7 +4916,7 @@ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onCo
4646
4916
  args: queryArgs
4647
4917
  };
4648
4918
  };
4649
- var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx) => {
4919
+ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx, txCtx) => {
4650
4920
  const queryArgs = {
4651
4921
  set: {
4652
4922
  type: new GraphQLNonNull6(setArgs)
@@ -4660,44 +4930,45 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
4660
4930
  name: fieldName,
4661
4931
  resolver: async (_source, args, context, info) => {
4662
4932
  try {
4663
- const { where, set } = args;
4664
- const parsedInfo = parseResolveInfo4(info, {
4665
- deep: true
4666
- });
4667
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4668
- relationMap,
4669
- tables,
4670
- tableName,
4671
- typeName,
4672
- typeNameMapper,
4673
- table,
4674
- pkNames,
4675
- parsedInfo
4933
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4934
+ const { where, set } = args;
4935
+ const parsedInfo = parseResolveInfo4(info, {
4936
+ deep: true
4937
+ });
4938
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4939
+ relationMap,
4940
+ tables,
4941
+ tableName,
4942
+ typeName,
4943
+ typeNameMapper,
4944
+ table,
4945
+ pkNames,
4946
+ parsedInfo
4947
+ });
4948
+ const input = remapFromGraphQLSingleInput(set, table);
4949
+ if (!Object.keys(input).length) {
4950
+ throw new GraphQLError7("Unable to update with no values specified!");
4951
+ }
4952
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
4953
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4954
+ if (single) {
4955
+ await assertSingleMatch(executor, table, filters, fieldName);
4956
+ }
4957
+ let query = executor.update(table).set(input);
4958
+ if (filters) {
4959
+ query = query.where(filters);
4960
+ }
4961
+ query = query.returning(columns);
4962
+ const result = await query;
4963
+ if (single && result.length > 1) {
4964
+ throw new GraphQLError7(`${fieldName}: 'where' matched more than one row!`);
4965
+ }
4966
+ if (single && !result[0]) {
4967
+ return void 0;
4968
+ }
4969
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4970
+ return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4676
4971
  });
4677
- const input = remapFromGraphQLSingleInput(set, table);
4678
- if (!Object.keys(input).length) {
4679
- throw new GraphQLError7("Unable to update with no values specified!");
4680
- }
4681
- const relationCtx = relationFilterCtx(filterCtx, tableName);
4682
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4683
- const executor = resolveExecutor(db, context);
4684
- if (single) {
4685
- await assertSingleMatch(executor, table, filters, fieldName);
4686
- }
4687
- let query = executor.update(table).set(input);
4688
- if (filters) {
4689
- query = query.where(filters);
4690
- }
4691
- query = query.returning(columns);
4692
- const result = await query;
4693
- if (single && result.length > 1) {
4694
- throw new GraphQLError7(`${fieldName}: 'where' matched more than one row!`);
4695
- }
4696
- if (single && !result[0]) {
4697
- return void 0;
4698
- }
4699
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
4700
- return single ? remapToGraphQLSingleOutput(enriched[0], tableName, table, relationMap) : remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
4701
4972
  } catch (e) {
4702
4973
  throw toGraphQLError(e);
4703
4974
  }
@@ -4705,7 +4976,7 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
4705
4976
  args: queryArgs
4706
4977
  };
4707
4978
  };
4708
- var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx) => {
4979
+ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx, txCtx) => {
4709
4980
  const queryArgs = {
4710
4981
  updates: {
4711
4982
  type: new GraphQLNonNull6(new GraphQLList6(new GraphQLNonNull6(updateManyInput)))
@@ -4716,73 +4987,74 @@ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateMany
4716
4987
  name: fieldName,
4717
4988
  resolver: async (_source, args, context, info) => {
4718
4989
  try {
4719
- const { updates } = args;
4720
- if (!updates.length) {
4721
- throw new GraphQLError7("No updates were provided!");
4722
- }
4723
- const parsedInfo = parseResolveInfo4(info, {
4724
- deep: true
4725
- });
4726
- const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4727
- relationMap,
4728
- tables,
4729
- tableName,
4730
- typeName,
4731
- typeNameMapper,
4732
- table,
4733
- pkNames,
4734
- parsedInfo
4735
- });
4736
- const entries = updates.map(({ where, set }) => {
4737
- const input = remapFromGraphQLSingleInput(set, table);
4738
- if (!Object.keys(input).length) {
4739
- throw new GraphQLError7("Unable to update with no values specified!");
4990
+ return await runMutation(db, context, info, txCtx, async (executor) => {
4991
+ const { updates } = args;
4992
+ if (!updates.length) {
4993
+ throw new GraphQLError7("No updates were provided!");
4740
4994
  }
4741
- return {
4742
- set: input,
4743
- filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
4995
+ const parsedInfo = parseResolveInfo4(info, {
4996
+ deep: true
4997
+ });
4998
+ const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
4999
+ relationMap,
5000
+ tables,
5001
+ tableName,
5002
+ typeName,
5003
+ typeNameMapper,
5004
+ table,
5005
+ pkNames,
5006
+ parsedInfo
5007
+ });
5008
+ const entries = updates.map(({ where, set }) => {
5009
+ const input = remapFromGraphQLSingleInput(set, table);
5010
+ if (!Object.keys(input).length) {
5011
+ throw new GraphQLError7("Unable to update with no values specified!");
5012
+ }
5013
+ return {
5014
+ set: input,
5015
+ filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
5016
+ };
5017
+ });
5018
+ const runEntry = (tx, entry) => {
5019
+ let query = tx.update(table).set(entry.set);
5020
+ if (entry.filters) {
5021
+ query = query.where(entry.filters);
5022
+ }
5023
+ return query.returning(columns).all();
4744
5024
  };
4745
- });
4746
- const runEntry = (tx, entry) => {
4747
- let query = tx.update(table).set(entry.set);
4748
- if (entry.filters) {
4749
- query = query.where(entry.filters);
4750
- }
4751
- return query.returning(columns).all();
4752
- };
4753
- const executor = resolveExecutor(db, context);
4754
- const perEntry = await executor.transaction((tx) => {
4755
- const first = runEntry(tx, entries[0]);
4756
- if (typeof first?.then === "function") {
4757
- return (async () => {
4758
- const results2 = [await first];
4759
- for (let i = 1; i < entries.length; i++) {
4760
- results2.push(await runEntry(tx, entries[i]));
4761
- }
4762
- return results2;
4763
- })();
4764
- }
4765
- const results = [first];
4766
- for (let i = 1; i < entries.length; i++) {
4767
- results.push(runEntry(tx, entries[i]));
5025
+ const perEntry = await executor.transaction((tx) => {
5026
+ const first = runEntry(tx, entries[0]);
5027
+ if (typeof first?.then === "function") {
5028
+ return (async () => {
5029
+ const results2 = [await first];
5030
+ for (let i = 1; i < entries.length; i++) {
5031
+ results2.push(await runEntry(tx, entries[i]));
5032
+ }
5033
+ return results2;
5034
+ })();
5035
+ }
5036
+ const results = [first];
5037
+ for (let i = 1; i < entries.length; i++) {
5038
+ results.push(runEntry(tx, entries[i]));
5039
+ }
5040
+ return results;
5041
+ });
5042
+ const flatRows = perEntry.flat();
5043
+ const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, flatRows, pkNames, withParams) : flatRows;
5044
+ const output = [];
5045
+ let offset = 0;
5046
+ for (const rows of perEntry) {
5047
+ if (!rows.length) {
5048
+ output.push(null);
5049
+ continue;
5050
+ }
5051
+ for (let i = 0; i < rows.length; i++) {
5052
+ output.push(remapToGraphQLSingleOutput(enriched[offset + i], tableName, table, relationMap));
5053
+ }
5054
+ offset += rows.length;
4768
5055
  }
4769
- return results;
5056
+ return output;
4770
5057
  });
4771
- const flatRows = perEntry.flat();
4772
- const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, flatRows, pkNames, withParams) : flatRows;
4773
- const output = [];
4774
- let offset = 0;
4775
- for (const rows of perEntry) {
4776
- if (!rows.length) {
4777
- output.push(null);
4778
- continue;
4779
- }
4780
- for (let i = 0; i < rows.length; i++) {
4781
- output.push(remapToGraphQLSingleOutput(enriched[offset + i], tableName, table, relationMap));
4782
- }
4783
- offset += rows.length;
4784
- }
4785
- return output;
4786
5058
  } catch (e) {
4787
5059
  throw toGraphQLError(e);
4788
5060
  }
@@ -4790,7 +5062,7 @@ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateMany
4790
5062
  args: queryArgs
4791
5063
  };
4792
5064
  };
4793
- var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx) => {
5065
+ var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx, txCtx) => {
4794
5066
  const queryArgs = {
4795
5067
  where: {
4796
5068
  type: single || requireWhere ? new GraphQLNonNull6(filterArgs) : filterArgs
@@ -4800,34 +5072,35 @@ var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, si
4800
5072
  name: fieldName,
4801
5073
  resolver: async (_source, args, context, info) => {
4802
5074
  try {
4803
- const { where } = args;
4804
- const parsedInfo = parseResolveInfo4(info, {
4805
- deep: true
5075
+ return await runMutation(db, context, info, txCtx, async (executor) => {
5076
+ const { where } = args;
5077
+ const parsedInfo = parseResolveInfo4(info, {
5078
+ deep: true
5079
+ });
5080
+ const columns = extractSelectedColumnsFromTreeSQLFormat(
5081
+ parsedInfo.fieldsByTypeName[typeName],
5082
+ table,
5083
+ selectionCtx
5084
+ );
5085
+ const relationCtx = relationFilterCtx(filterCtx, tableName);
5086
+ const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
5087
+ if (single) {
5088
+ await assertSingleMatch(executor, table, filters, fieldName);
5089
+ }
5090
+ let query = executor.delete(table);
5091
+ if (filters) {
5092
+ query = query.where(filters);
5093
+ }
5094
+ query = query.returning(columns);
5095
+ const result = await query;
5096
+ if (single && result.length > 1) {
5097
+ throw new GraphQLError7(`${fieldName}: 'where' matched more than one row!`);
5098
+ }
5099
+ if (single) {
5100
+ return result[0] ? remapToGraphQLSingleOutput(result[0], tableName, table) : void 0;
5101
+ }
5102
+ return remapToGraphQLArrayOutput(result, tableName, table);
4806
5103
  });
4807
- const columns = extractSelectedColumnsFromTreeSQLFormat(
4808
- parsedInfo.fieldsByTypeName[typeName],
4809
- table,
4810
- selectionCtx
4811
- );
4812
- const relationCtx = relationFilterCtx(filterCtx, tableName);
4813
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4814
- const executor = resolveExecutor(db, context);
4815
- if (single) {
4816
- await assertSingleMatch(executor, table, filters, fieldName);
4817
- }
4818
- let query = executor.delete(table);
4819
- if (filters) {
4820
- query = query.where(filters);
4821
- }
4822
- query = query.returning(columns);
4823
- const result = await query;
4824
- if (single && result.length > 1) {
4825
- throw new GraphQLError7(`${fieldName}: 'where' matched more than one row!`);
4826
- }
4827
- if (single) {
4828
- return result[0] ? remapToGraphQLSingleOutput(result[0], tableName, table) : void 0;
4829
- }
4830
- return remapToGraphQLArrayOutput(result, tableName, table);
4831
5104
  } catch (e) {
4832
5105
  throw toGraphQLError(e);
4833
5106
  }
@@ -4875,6 +5148,12 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4875
5148
  const relationAggregateFactory = features.relationAggregates ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
4876
5149
  const queries = {};
4877
5150
  const mutations = {};
5151
+ if (options.transactions && db.resultKind === "sync") {
5152
+ throw new Error(
5153
+ "Drizzle-GraphQL Error: transactions: 'auto' requires an asynchronous SQLite driver (e.g. libsql). Synchronous drivers cannot hold a transaction open across resolvers."
5154
+ );
5155
+ }
5156
+ const mutationTxCtx = createMutationTxCtx(options.transactions);
4878
5157
  const gqlSchemaTypes = Object.fromEntries(
4879
5158
  Object.entries(tables).map(([tableName, _table]) => [
4880
5159
  tableName,
@@ -4949,7 +5228,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4949
5228
  createArrayFieldName,
4950
5229
  typeName,
4951
5230
  typeNameMapper,
4952
- conflictDoNothing
5231
+ conflictDoNothing,
5232
+ mutationTxCtx
4953
5233
  ) : void 0;
4954
5234
  const insertSingleGenerated = features.insert ? generateInsertSingle3(
4955
5235
  db,
@@ -4961,7 +5241,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4961
5241
  createSingleFieldName,
4962
5242
  typeName,
4963
5243
  typeNameMapper,
4964
- conflictDoNothing
5244
+ conflictDoNothing,
5245
+ mutationTxCtx
4965
5246
  ) : void 0;
4966
5247
  const uniqueSets = features.upsert ? getUniqueColumnSets(schema[tableName], getTableConfig3) : [];
4967
5248
  const onConflictInput = features.upsert ? generateOnConflictInput({
@@ -4984,7 +5265,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4984
5265
  typeName,
4985
5266
  false,
4986
5267
  typeNameMapper,
4987
- filterCtx
5268
+ filterCtx,
5269
+ mutationTxCtx
4988
5270
  ) : void 0;
4989
5271
  const upsertSingleGenerated = onConflictInput ? generateUpsert3(
4990
5272
  db,
@@ -4999,7 +5281,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
4999
5281
  typeName,
5000
5282
  true,
5001
5283
  typeNameMapper,
5002
- filterCtx
5284
+ filterCtx,
5285
+ mutationTxCtx
5003
5286
  ) : void 0;
5004
5287
  const updateGenerated = features.update ? generateUpdate3(
5005
5288
  db,
@@ -5014,7 +5297,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5014
5297
  false,
5015
5298
  features.requireWhere,
5016
5299
  typeNameMapper,
5017
- filterCtx
5300
+ filterCtx,
5301
+ mutationTxCtx
5018
5302
  ) : void 0;
5019
5303
  const updateSingleGenerated = features.update ? generateUpdate3(
5020
5304
  db,
@@ -5029,7 +5313,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5029
5313
  true,
5030
5314
  features.requireWhere,
5031
5315
  typeNameMapper,
5032
- filterCtx
5316
+ filterCtx,
5317
+ mutationTxCtx
5033
5318
  ) : void 0;
5034
5319
  const updateManyInput = features.update && features.updateMany ? generateUpdateManyInput({ typeName, updatePrefix: prefixes.update, updateInput, tableFilters }) : void 0;
5035
5320
  const updateManyGenerated = updateManyInput ? generateUpdateMany3(
@@ -5042,7 +5327,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5042
5327
  updateManyFieldName,
5043
5328
  typeName,
5044
5329
  typeNameMapper,
5045
- filterCtx
5330
+ filterCtx,
5331
+ mutationTxCtx
5046
5332
  ) : void 0;
5047
5333
  const deleteGenerated = features.delete ? generateDelete3(
5048
5334
  db,
@@ -5054,7 +5340,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5054
5340
  false,
5055
5341
  features.requireWhere,
5056
5342
  filterCtx,
5057
- { tableName, relationMap: namedRelations, tables }
5343
+ { tableName, relationMap: namedRelations, tables },
5344
+ mutationTxCtx
5058
5345
  ) : void 0;
5059
5346
  const deleteSingleGenerated = features.delete ? generateDelete3(
5060
5347
  db,
@@ -5066,7 +5353,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5066
5353
  true,
5067
5354
  features.requireWhere,
5068
5355
  filterCtx,
5069
- { tableName, relationMap: namedRelations, tables }
5356
+ { tableName, relationMap: namedRelations, tables },
5357
+ mutationTxCtx
5070
5358
  ) : void 0;
5071
5359
  const aggregateType = features.aggregates ? generateAggregateTypes(schema[tableName], tableName, typeName, cacheCtx) : void 0;
5072
5360
  const aggregateGenerated = features.aggregates ? generateAggregate(
@@ -5205,6 +5493,11 @@ var generateSchemaData3 = (db, schema, relations, options) => {
5205
5493
  inputs[havingInput.name] = havingInput;
5206
5494
  }
5207
5495
  }
5496
+ if (mutationTxCtx) {
5497
+ for (const name of Object.keys(mutations)) {
5498
+ mutationTxCtx.fieldNames.add(name);
5499
+ }
5500
+ }
5208
5501
  const fieldResolvers = {};
5209
5502
  for (const [tableName, tableRelations] of Object.entries(namedRelations)) {
5210
5503
  const relResolvers = {};
@@ -5261,6 +5554,8 @@ var buildSchema = (db, config) => {
5261
5554
  defaultListSize: (complexityConfig === true ? void 0 : complexityConfig.defaultListSize) ?? 10,
5262
5555
  aggregateCost: (complexityConfig === true ? void 0 : complexityConfig.aggregateCost) ?? 10
5263
5556
  };
5557
+ const transactionsOpt = config?.transactions;
5558
+ const transactions = transactionsOpt === void 0 || transactionsOpt === "none" ? void 0 : { timeoutMs: (transactionsOpt === "auto" ? void 0 : transactionsOpt.timeoutMs) ?? 3e4 };
5264
5559
  const eagerOpt = config?.eagerLoadRelations;
5265
5560
  const shouldEagerLoad = eagerOpt === void 0 || eagerOpt === true ? () => true : eagerOpt === false ? () => false : eagerOpt;
5266
5561
  if (!typeNameMapper && suffixes.list === suffixes.single) {
@@ -5288,7 +5583,8 @@ var buildSchema = (db, config) => {
5288
5583
  typeNameMapper,
5289
5584
  shouldEagerLoad,
5290
5585
  features,
5291
- complexity
5586
+ complexity,
5587
+ transactions
5292
5588
  };
5293
5589
  let generatorOutput;
5294
5590
  if (is7(db, MySqlDatabase)) {