@vantreeseba/drizzle-graphql 7.5.0 → 7.6.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
@@ -1144,7 +1144,7 @@ var batchedPaginatedRelationQuery = async (db, targetTable, foreignCol, whereCon
1144
1144
  }
1145
1145
  return rows;
1146
1146
  };
1147
- var createRelationResolverFactory = (db, tables, nullOrdering, filterCtx, limits) => ({ tableName, relationName, relEntry, isOne }) => {
1147
+ var createRelationResolverFactory = (db, tables, nullOrdering, filterCtx, limits, scopes) => ({ tableName, relationName, relEntry, isOne }) => {
1148
1148
  const parentTable = tables[tableName];
1149
1149
  const targetTableName = relEntry.targetTableName;
1150
1150
  const targetTable = tables[targetTableName];
@@ -1205,10 +1205,16 @@ var createRelationResolverFactory = (db, tables, nullOrdering, filterCtx, limits
1205
1205
  const loader = getOrCreateLoader(context, loaderKey, async (parentIds) => {
1206
1206
  const executor = resolveExecutor(db, context);
1207
1207
  const uniqueIds = [...new Set(parentIds)];
1208
- let whereCondition = (0, import_drizzle_orm4.and)(
1209
- (0, import_drizzle_orm4.inArray)(foreignCol, uniqueIds),
1210
- whereArg ? extractFilters(targetTable, targetTableName, whereArg, relationFilterCtx(filterCtx, targetTableName)) : void 0,
1211
- cursorValues ? buildCursorCondition(targetTable, cursorEntries, cursorValues, nullOrdering) : void 0
1208
+ const scope = resolveScope(scopes, context, filterCtx);
1209
+ let whereCondition = withScope(
1210
+ scope,
1211
+ targetTableName,
1212
+ targetTable,
1213
+ (0, import_drizzle_orm4.and)(
1214
+ (0, import_drizzle_orm4.inArray)(foreignCol, uniqueIds),
1215
+ whereArg ? extractFilters(targetTable, targetTableName, whereArg, relationFilterCtx(filterCtx, targetTableName)) : void 0,
1216
+ cursorValues ? buildCursorCondition(targetTable, cursorEntries, cursorValues, nullOrdering) : void 0
1217
+ )
1212
1218
  );
1213
1219
  if (distinct) {
1214
1220
  const keys = await selectDistinctKeys({
@@ -2114,9 +2120,11 @@ var generateTableTypes = (tableName, tables, relationMap, withReturning, relatio
2114
2120
  const table = tables[tableName];
2115
2121
  const columns = visibleColumns(table);
2116
2122
  const columnEntries = Object.entries(columns);
2123
+ const contextColumns = cacheCtx.contextValuesOf?.(tableName);
2124
+ const writableEntries = contextColumns ? columnEntries.filter(([columnName]) => !(columnName in contextColumns)) : columnEntries;
2117
2125
  const relaxedColumns = nestedWrites?.relaxedColumns(tableName);
2118
2126
  const insertFields = Object.fromEntries(
2119
- columnEntries.map(([columnName, column]) => {
2127
+ writableEntries.map(([columnName, column]) => {
2120
2128
  const converted = drizzleColumnToGraphQLType(
2121
2129
  column,
2122
2130
  columnName,
@@ -2132,7 +2140,7 @@ var generateTableTypes = (tableName, tables, relationMap, withReturning, relatio
2132
2140
  })
2133
2141
  );
2134
2142
  const updateFields = Object.fromEntries(
2135
- columnEntries.map(([columnName, column]) => {
2143
+ writableEntries.map(([columnName, column]) => {
2136
2144
  const converted = drizzleColumnToGraphQLType(column, columnName, tableName, true, false, true);
2137
2145
  const operations = cacheCtx.featureOf(tableName).fieldUpdateOperations ? fieldUpdateInputType(column, columnName, tableName) : void 0;
2138
2146
  const field = operations ? { ...converted, type: operations } : converted;
@@ -2655,7 +2663,69 @@ var extractFilters = (table, tableName, filters, relationCtx) => {
2655
2663
  }
2656
2664
  return variants.length ? variants.length > 1 ? (0, import_drizzle_orm4.and)(...variants) : variants[0] : void 0;
2657
2665
  };
2658
- var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, originField, typeNameMapper, _isInitial = false, filterCtx, limits) => {
2666
+ var resolveScope = (scopes, context, filterCtx) => {
2667
+ if (!scopes) {
2668
+ return void 0;
2669
+ }
2670
+ return {
2671
+ has: (tableName) => !!scopes(tableName),
2672
+ on: (tableName, table) => {
2673
+ const hook = scopes(tableName);
2674
+ if (!hook) {
2675
+ return void 0;
2676
+ }
2677
+ const predicate = hook(context, table);
2678
+ if (predicate === void 0 || predicate === null) {
2679
+ return void 0;
2680
+ }
2681
+ if ((0, import_drizzle_orm4.is)(predicate, import_drizzle_orm4.SQL)) {
2682
+ return predicate;
2683
+ }
2684
+ if (typeof predicate !== "object") {
2685
+ throw new import_graphql5.GraphQLError(
2686
+ `Drizzle-GraphQL Error: the scope for '${tableName}' returned a ${typeof predicate}. A scope returns a filter object, a Drizzle SQL expression, or undefined.`
2687
+ );
2688
+ }
2689
+ return extractFilters(table, tableName, predicate, relationFilterCtx(filterCtx, tableName));
2690
+ }
2691
+ };
2692
+ };
2693
+ var withScope = (scope, tableName, table, condition) => {
2694
+ const predicate = scope?.on(tableName, table);
2695
+ return predicate ? (0, import_drizzle_orm4.and)(condition, predicate) : condition;
2696
+ };
2697
+ var bindPolicies = (policies, filterCtx) => policies?.scope || policies?.contextValues ? {
2698
+ scope: policies.scope ? (context) => resolveScope(policies.scope, context, filterCtx) : void 0,
2699
+ contextValues: policies.contextValues
2700
+ } : void 0;
2701
+ var applyContextValues = (values, hooks, context) => {
2702
+ if (!hooks) {
2703
+ return values;
2704
+ }
2705
+ for (const [columnName, hook] of Object.entries(hooks)) {
2706
+ values[columnName] = hook(context);
2707
+ }
2708
+ return values;
2709
+ };
2710
+ var applyContextValuesAll = (rows, hooks, context) => {
2711
+ if (!hooks) {
2712
+ return rows;
2713
+ }
2714
+ for (const row of rows) {
2715
+ applyContextValues(row, hooks, context);
2716
+ }
2717
+ return rows;
2718
+ };
2719
+ var stripContextValues = (values, hooks) => {
2720
+ if (!hooks) {
2721
+ return values;
2722
+ }
2723
+ for (const columnName of Object.keys(hooks)) {
2724
+ delete values[columnName];
2725
+ }
2726
+ return values;
2727
+ };
2728
+ var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, originField, typeNameMapper, _isInitial = false, filterCtx, limits, scope) => {
2659
2729
  const relationsForTable = relationMap[tableName];
2660
2730
  if (!relationsForTable) {
2661
2731
  return void 0;
@@ -2701,8 +2771,13 @@ var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, ori
2701
2771
  const offset = relationArgs?.offset ?? void 0;
2702
2772
  const limit = (0, import_drizzle_orm4.is)(relEntry.relation, import_drizzle_orm4.One) ? relationArgs?.limit ?? void 0 : applyLimitPolicy(relationArgs?.limit, limits?.(targetTableName), `${tableName}.${relName}`);
2703
2773
  const relWhere = relationArgs?.where;
2704
- thisRecord.where = relWhere ? {
2705
- RAW: (aliasedTable2) => extractFilters(aliasedTable2, relName, relWhere, relationFilterCtx(filterCtx, targetTableName))
2774
+ thisRecord.where = relWhere || scope?.has(targetTableName) ? {
2775
+ RAW: (aliasedTable2) => withScope(
2776
+ scope,
2777
+ targetTableName,
2778
+ aliasedTable2,
2779
+ relWhere ? extractFilters(aliasedTable2, relName, relWhere, relationFilterCtx(filterCtx, targetTableName)) : void 0
2780
+ )
2706
2781
  } : void 0;
2707
2782
  const hasPagination = offset != null || limit != null;
2708
2783
  const pkNames = targetPkNames ?? [];
@@ -2718,14 +2793,15 @@ var extractRelationsParamsInner = (relationMap, tables, tableName, typeName, ori
2718
2793
  typeNameMapper,
2719
2794
  false,
2720
2795
  filterCtx,
2721
- limits
2796
+ limits,
2797
+ scope
2722
2798
  ) : void 0;
2723
2799
  thisRecord.with = relWith;
2724
2800
  args[relName] = thisRecord;
2725
2801
  }
2726
2802
  return args;
2727
2803
  };
2728
- var extractRelationsParams = (relationMap, tables, tableName, info, typeName, typeNameMapper, filterCtx, limits) => {
2804
+ var extractRelationsParams = (relationMap, tables, tableName, info, typeName, typeNameMapper, filterCtx, limits, scope) => {
2729
2805
  if (!info) {
2730
2806
  return void 0;
2731
2807
  }
@@ -2738,7 +2814,8 @@ var extractRelationsParams = (relationMap, tables, tableName, info, typeName, ty
2738
2814
  typeNameMapper,
2739
2815
  true,
2740
2816
  filterCtx,
2741
- limits
2817
+ limits,
2818
+ scope
2742
2819
  );
2743
2820
  };
2744
2821
  var pruneNonEagerRelations = (relationMap, shouldEagerLoad) => {
@@ -3148,7 +3225,8 @@ var prepareMutationRelationColumns = (params) => {
3148
3225
  typeName,
3149
3226
  typeNameMapper,
3150
3227
  void 0,
3151
- params.limits
3228
+ params.limits,
3229
+ params.scope
3152
3230
  ) : void 0;
3153
3231
  const hasRelations = !!(withParams && Object.keys(withParams).length);
3154
3232
  const baseColumns = extractSelectedColumnsFromTreeSQLFormat(parsedInfo.fieldsByTypeName[typeName], table, {
@@ -3382,7 +3460,8 @@ var runRelationalSelect = async (opts) => {
3382
3460
  single,
3383
3461
  filterCtx,
3384
3462
  pkNames,
3385
- after
3463
+ after,
3464
+ scope
3386
3465
  } = opts;
3387
3466
  const distinct = opts.distinct?.length ? opts.distinct : void 0;
3388
3467
  const cursorSelected = !single && isCursorFieldSelected(parsedInfo.fieldsByTypeName[typeName], table);
@@ -3414,7 +3493,12 @@ var runRelationalSelect = async (opts) => {
3414
3493
  tableName,
3415
3494
  distinct,
3416
3495
  pkNames: pkNames ?? [],
3417
- where: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0,
3496
+ where: withScope(
3497
+ scope,
3498
+ tableName,
3499
+ table,
3500
+ where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
3501
+ ),
3418
3502
  orderBy,
3419
3503
  limit: single ? 1 : opts.limit,
3420
3504
  offset
@@ -3441,15 +3525,23 @@ var runRelationalSelect = async (opts) => {
3441
3525
  // exactly the ordering the cursor encodes and the keyset predicate compares against.
3442
3526
  (aliasedTable2) => cursorOrderExprs(aliasedTable2, cursorEntries)
3443
3527
  ) : orderBy ? (aliasedTable2) => extractOrderBy(aliasedTable2, orderBy, relationFilterCtx(filterCtx, tableName), where) : needsDefaultOrder && pkNames?.length ? (aliasedTable2) => primaryKeyOrderExprs(aliasedTable2, pkNames) : void 0,
3444
- where: distinctKeys ? { RAW: (aliasedTable2) => primaryKeyRestriction(aliasedTable2, pkNames, distinctKeys) } : where || cursorValues ? {
3445
- RAW: (aliasedTable2) => (0, import_drizzle_orm4.and)(
3446
- where ? extractFilters(aliasedTable2, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0,
3447
- cursorValues ? buildCursorCondition(
3448
- aliasedTable2,
3449
- cursorEntries,
3450
- cursorValues,
3451
- opts.nullOrdering ?? "nulls-smallest"
3452
- ) : void 0
3528
+ where: distinctKeys ? (
3529
+ // The distinct pass already ran inside the scope, so the keys it picked are in it.
3530
+ { RAW: (aliasedTable2) => primaryKeyRestriction(aliasedTable2, pkNames, distinctKeys) }
3531
+ ) : where || cursorValues || scope?.has(tableName) ? {
3532
+ RAW: (aliasedTable2) => withScope(
3533
+ scope,
3534
+ tableName,
3535
+ aliasedTable2,
3536
+ (0, import_drizzle_orm4.and)(
3537
+ where ? extractFilters(aliasedTable2, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0,
3538
+ cursorValues ? buildCursorCondition(
3539
+ aliasedTable2,
3540
+ cursorEntries,
3541
+ cursorValues,
3542
+ opts.nullOrdering ?? "nulls-smallest"
3543
+ ) : void 0
3544
+ )
3453
3545
  )
3454
3546
  } : void 0,
3455
3547
  with: relationMap[tableName] ? extractRelationsParams(
@@ -3460,7 +3552,8 @@ var runRelationalSelect = async (opts) => {
3460
3552
  typeName,
3461
3553
  typeNameMapper,
3462
3554
  filterCtx,
3463
- opts.limits
3555
+ opts.limits,
3556
+ scope
3464
3557
  ) : void 0
3465
3558
  };
3466
3559
  if (single) {
@@ -3765,7 +3858,7 @@ var assembleAggregateRow = (row, request, target) => {
3765
3858
  }
3766
3859
  return result;
3767
3860
  };
3768
- var generateAggregate = (db, tableName, table, typeName, fieldName, filterArgs, filterCtx) => {
3861
+ var generateAggregate = (db, tableName, table, typeName, fieldName, filterArgs, filterCtx, scopes) => {
3769
3862
  const target = aggregateTarget(table, tableName, typeName);
3770
3863
  const queryArgs = {
3771
3864
  where: { type: filterArgs }
@@ -3779,8 +3872,14 @@ var generateAggregate = (db, tableName, table, typeName, fieldName, filterArgs,
3779
3872
  return {};
3780
3873
  }
3781
3874
  let query = resolveExecutor(db, context).select(request.selection).from(table);
3782
- if (args.where) {
3783
- query = query.where(extractFilters(table, tableName, args.where, relationFilterCtx(filterCtx, tableName)));
3875
+ const where = withScope(
3876
+ resolveScope(scopes, context, filterCtx),
3877
+ tableName,
3878
+ table,
3879
+ args.where ? extractFilters(table, tableName, args.where, relationFilterCtx(filterCtx, tableName)) : void 0
3880
+ );
3881
+ if (where) {
3882
+ query = query.where(where);
3784
3883
  }
3785
3884
  const rows = await query;
3786
3885
  return assembleAggregateRow(rows[0] ?? {}, request, target);
@@ -3792,7 +3891,7 @@ var generateAggregate = (db, tableName, table, typeName, fieldName, filterArgs,
3792
3891
  };
3793
3892
  };
3794
3893
  var GROUP_KEY = "__dgql_group_key";
3795
- var createRelationAggregateFactory = (db, tables, cacheCtx, typeNameMapper, filterCtx) => {
3894
+ var createRelationAggregateFactory = (db, tables, cacheCtx, typeNameMapper, filterCtx, scopes) => {
3796
3895
  return ({ tableName, relationName, relEntry }) => {
3797
3896
  const parentTable = tables[tableName];
3798
3897
  const targetTableName = relEntry.targetTableName;
@@ -3827,9 +3926,14 @@ var createRelationAggregateFactory = (db, tables, cacheCtx, typeNameMapper, filt
3827
3926
  const loader = getOrCreateLoader(context, loaderKey, async (parentIds) => {
3828
3927
  const executor = resolveExecutor(db, context);
3829
3928
  const uniqueIds = [...new Set(parentIds)];
3830
- const whereCondition = (0, import_drizzle_orm5.and)(
3831
- (0, import_drizzle_orm5.inArray)(foreignCol, uniqueIds),
3832
- whereArg ? extractFilters(targetTable, targetTableName, whereArg, relationFilterCtx(filterCtx, targetTableName)) : void 0
3929
+ const whereCondition = withScope(
3930
+ resolveScope(scopes, context, filterCtx),
3931
+ targetTableName,
3932
+ targetTable,
3933
+ (0, import_drizzle_orm5.and)(
3934
+ (0, import_drizzle_orm5.inArray)(foreignCol, uniqueIds),
3935
+ whereArg ? extractFilters(targetTable, targetTableName, whereArg, relationFilterCtx(filterCtx, targetTableName)) : void 0
3936
+ )
3833
3937
  );
3834
3938
  const rows = await executor.select({ [GROUP_KEY]: foreignCol, ...request.selection }).from(targetTable).where(whereCondition).groupBy(foreignCol);
3835
3939
  const byKey = new Map(rows.map((row) => [row[GROUP_KEY], row]));
@@ -3962,7 +4066,7 @@ var buildHavingCondition = (having, columns) => {
3962
4066
  }
3963
4067
  return conditions.length ? (0, import_drizzle_orm5.and)(...conditions) : void 0;
3964
4068
  };
3965
- var generateGroupBy = (db, tableName, table, typeName, fieldName, filterArgs, groupByEnum, havingInput, filterCtx) => {
4069
+ var generateGroupBy = (db, tableName, table, typeName, fieldName, filterArgs, groupByEnum, havingInput, filterCtx, scopes) => {
3966
4070
  const target = aggregateTarget(table, tableName, typeName);
3967
4071
  const groupable = groupableColumns(table, tableName);
3968
4072
  const columns = (0, import_drizzle_orm5.getColumns)(table);
@@ -3996,8 +4100,14 @@ var generateGroupBy = (db, tableName, table, typeName, fieldName, filterArgs, gr
3996
4100
  ...request.selection
3997
4101
  };
3998
4102
  let query = resolveExecutor(db, context).select(selection).from(table);
3999
- if (args.where) {
4000
- query = query.where(extractFilters(table, tableName, args.where, relationFilterCtx(filterCtx, tableName)));
4103
+ const where = withScope(
4104
+ resolveScope(scopes, context, filterCtx),
4105
+ tableName,
4106
+ table,
4107
+ args.where ? extractFilters(table, tableName, args.where, relationFilterCtx(filterCtx, tableName)) : void 0
4108
+ );
4109
+ if (where) {
4110
+ query = query.where(where);
4001
4111
  }
4002
4112
  query = query.groupBy(...keyColumns.map(([, column]) => column));
4003
4113
  const havingCondition = buildHavingCondition(args.having, columns);
@@ -4023,7 +4133,7 @@ var generateGroupBy = (db, tableName, table, typeName, fieldName, filterArgs, gr
4023
4133
  };
4024
4134
 
4025
4135
  // src/util/builders/mysql.ts
4026
- var generateSelectArray = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, distinctEnabled = true, limits) => {
4136
+ var generateSelectArray = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, distinctEnabled = true, limits, policies) => {
4027
4137
  const queryBase = db.query[tableName];
4028
4138
  if (!queryBase) {
4029
4139
  throw new Error(
@@ -4058,6 +4168,7 @@ var generateSelectArray = (db, tableName, tables, relationMap, orderArgs, filter
4058
4168
  single: false,
4059
4169
  filterCtx,
4060
4170
  limits,
4171
+ scope: policies?.scope?.(context),
4061
4172
  pkNames,
4062
4173
  db: executor,
4063
4174
  // MySQL sorts NULLs as the smallest values (first in ASC).
@@ -4070,7 +4181,7 @@ var generateSelectArray = (db, tableName, tables, relationMap, orderArgs, filter
4070
4181
  args: queryArgs
4071
4182
  };
4072
4183
  };
4073
- var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, limits) => {
4184
+ var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, limits, policies) => {
4074
4185
  const queryBase = db.query[tableName];
4075
4186
  if (!queryBase) {
4076
4187
  throw new Error(
@@ -4099,6 +4210,7 @@ var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filte
4099
4210
  single: true,
4100
4211
  filterCtx,
4101
4212
  limits,
4213
+ scope: policies?.scope?.(context),
4102
4214
  pkNames,
4103
4215
  db: executor
4104
4216
  });
@@ -4109,7 +4221,7 @@ var generateSelectSingle = (db, tableName, tables, relationMap, orderArgs, filte
4109
4221
  args: queryArgs
4110
4222
  };
4111
4223
  };
4112
- var generateInsertArray = (db, _tableName, table, baseType, fieldName, txCtx) => {
4224
+ var generateInsertArray = (db, tableName, table, baseType, fieldName, txCtx, policies) => {
4113
4225
  const queryArgs = {
4114
4226
  values: {
4115
4227
  type: new import_graphql7.GraphQLNonNull(new import_graphql7.GraphQLList(new import_graphql7.GraphQLNonNull(baseType)))
@@ -4120,7 +4232,11 @@ var generateInsertArray = (db, _tableName, table, baseType, fieldName, txCtx) =>
4120
4232
  resolver: async (_source, args, context, info) => {
4121
4233
  try {
4122
4234
  return await runMutation(db, context, info, txCtx, async (executor) => {
4123
- const input = remapFromGraphQLArrayInput(args.values, table);
4235
+ const input = applyContextValuesAll(
4236
+ remapFromGraphQLArrayInput(args.values, table),
4237
+ policies?.contextValues?.(tableName),
4238
+ context
4239
+ );
4124
4240
  if (!input.length) {
4125
4241
  throw new import_graphql7.GraphQLError("No values were provided!");
4126
4242
  }
@@ -4134,7 +4250,7 @@ var generateInsertArray = (db, _tableName, table, baseType, fieldName, txCtx) =>
4134
4250
  args: queryArgs
4135
4251
  };
4136
4252
  };
4137
- var generateInsertSingle = (db, _tableName, table, baseType, fieldName, txCtx) => {
4253
+ var generateInsertSingle = (db, tableName, table, baseType, fieldName, txCtx, policies) => {
4138
4254
  const queryArgs = {
4139
4255
  values: {
4140
4256
  type: new import_graphql7.GraphQLNonNull(baseType)
@@ -4145,7 +4261,11 @@ var generateInsertSingle = (db, _tableName, table, baseType, fieldName, txCtx) =
4145
4261
  resolver: async (_source, args, context, info) => {
4146
4262
  try {
4147
4263
  return await runMutation(db, context, info, txCtx, async (executor) => {
4148
- const input = remapFromGraphQLSingleInput(args.values, table);
4264
+ const input = applyContextValues(
4265
+ remapFromGraphQLSingleInput(args.values, table),
4266
+ policies?.contextValues?.(tableName),
4267
+ context
4268
+ );
4149
4269
  await executor.insert(table).values(input);
4150
4270
  return { isSuccess: true };
4151
4271
  });
@@ -4156,7 +4276,7 @@ var generateInsertSingle = (db, _tableName, table, baseType, fieldName, txCtx) =
4156
4276
  args: queryArgs
4157
4277
  };
4158
4278
  };
4159
- var generateUpsert = (db, table, baseType, onConflictType, fieldName, single, txCtx) => {
4279
+ var generateUpsert = (db, tableName, table, baseType, onConflictType, fieldName, single, txCtx, policies) => {
4160
4280
  const queryArgs = {
4161
4281
  values: {
4162
4282
  type: single ? new import_graphql7.GraphQLNonNull(baseType) : new import_graphql7.GraphQLNonNull(new import_graphql7.GraphQLList(new import_graphql7.GraphQLNonNull(baseType)))
@@ -4172,7 +4292,11 @@ var generateUpsert = (db, table, baseType, onConflictType, fieldName, single, tx
4172
4292
  resolver: async (_source, args, context, info) => {
4173
4293
  try {
4174
4294
  return await runMutation(db, context, info, txCtx, async (executor) => {
4175
- const input = single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table);
4295
+ const input = applyContextValuesAll(
4296
+ single ? [remapFromGraphQLSingleInput(args.values, table)] : remapFromGraphQLArrayInput(args.values, table),
4297
+ policies?.contextValues?.(tableName),
4298
+ context
4299
+ );
4176
4300
  if (!input.length) {
4177
4301
  throw new import_graphql7.GraphQLError("No values were provided!");
4178
4302
  }
@@ -4199,7 +4323,7 @@ var generateUpsert = (db, table, baseType, onConflictType, fieldName, single, tx
4199
4323
  args: queryArgs
4200
4324
  };
4201
4325
  };
4202
- var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, single, requireWhere, filterCtx, txCtx) => {
4326
+ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, single, requireWhere, filterCtx, txCtx, policies) => {
4203
4327
  const queryArgs = {
4204
4328
  set: {
4205
4329
  type: new import_graphql7.GraphQLNonNull(setArgs)
@@ -4214,12 +4338,21 @@ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, sing
4214
4338
  try {
4215
4339
  return await runMutation(db, context, info, txCtx, async (executor) => {
4216
4340
  const { where, set } = args;
4217
- const input = remapUpdateInput(set, table, tableName);
4341
+ const scope = policies?.scope?.(context);
4342
+ const input = stripContextValues(
4343
+ remapUpdateInput(set, table, tableName),
4344
+ policies?.contextValues?.(tableName)
4345
+ );
4218
4346
  if (!Object.keys(input).length) {
4219
4347
  throw new import_graphql7.GraphQLError("Unable to update with no values specified!");
4220
4348
  }
4221
4349
  const relationCtx = relationFilterCtx(filterCtx, tableName);
4222
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4350
+ const filters = withScope(
4351
+ scope,
4352
+ tableName,
4353
+ table,
4354
+ single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0
4355
+ );
4223
4356
  if (single) {
4224
4357
  await assertSingleMatch(executor, table, filters, fieldName);
4225
4358
  }
@@ -4237,7 +4370,7 @@ var generateUpdate = (db, tableName, table, setArgs, filterArgs, fieldName, sing
4237
4370
  args: queryArgs
4238
4371
  };
4239
4372
  };
4240
- var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filterCtx, txCtx) => {
4373
+ var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filterCtx, txCtx, policies) => {
4241
4374
  const queryArgs = {
4242
4375
  updates: {
4243
4376
  type: new import_graphql7.GraphQLNonNull(new import_graphql7.GraphQLList(new import_graphql7.GraphQLNonNull(updateManyInput)))
@@ -4252,14 +4385,21 @@ var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filt
4252
4385
  if (!updates.length) {
4253
4386
  throw new import_graphql7.GraphQLError("No updates were provided!");
4254
4387
  }
4388
+ const scope = policies?.scope?.(context);
4389
+ const contextColumns = policies?.contextValues?.(tableName);
4255
4390
  const entries = updates.map(({ where, set }) => {
4256
- const input = remapUpdateInput(set, table, tableName);
4391
+ const input = stripContextValues(remapUpdateInput(set, table, tableName), contextColumns);
4257
4392
  if (!Object.keys(input).length) {
4258
4393
  throw new import_graphql7.GraphQLError("Unable to update with no values specified!");
4259
4394
  }
4260
4395
  return {
4261
4396
  set: input,
4262
- filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
4397
+ filters: withScope(
4398
+ scope,
4399
+ tableName,
4400
+ table,
4401
+ where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
4402
+ )
4263
4403
  };
4264
4404
  });
4265
4405
  await executor.transaction(async (tx) => {
@@ -4280,7 +4420,7 @@ var generateUpdateMany = (db, tableName, table, updateManyInput, fieldName, filt
4280
4420
  args: queryArgs
4281
4421
  };
4282
4422
  };
4283
- var generateDelete = (db, tableName, table, filterArgs, fieldName, single, requireWhere, filterCtx, txCtx) => {
4423
+ var generateDelete = (db, tableName, table, filterArgs, fieldName, single, requireWhere, filterCtx, txCtx, policies) => {
4284
4424
  const queryArgs = {
4285
4425
  where: {
4286
4426
  type: single || requireWhere ? new import_graphql7.GraphQLNonNull(filterArgs) : filterArgs
@@ -4292,8 +4432,14 @@ var generateDelete = (db, tableName, table, filterArgs, fieldName, single, requi
4292
4432
  try {
4293
4433
  return await runMutation(db, context, info, txCtx, async (executor) => {
4294
4434
  const { where } = args;
4435
+ const scope = policies?.scope?.(context);
4295
4436
  const relationCtx = relationFilterCtx(filterCtx, tableName);
4296
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
4437
+ const filters = withScope(
4438
+ scope,
4439
+ tableName,
4440
+ table,
4441
+ single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0
4442
+ );
4297
4443
  if (single) {
4298
4444
  await assertSingleMatch(executor, table, filters, fieldName);
4299
4445
  }
@@ -4341,12 +4487,16 @@ var generateSchemaData = (db, schema, relations, options) => {
4341
4487
  attachTargetPrimaryKeys(namedRelations, tables, mysqlPrimaryKeyPropNames);
4342
4488
  const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
4343
4489
  const filterCtx = { tables, relationMap: namedRelations };
4490
+ const scopes = options.policies?.scope;
4491
+ const contextValuesOf = options.policies?.contextValues;
4492
+ const policies = bindPolicies(options.policies, filterCtx);
4344
4493
  const resolverFactory = createRelationResolverFactory(
4345
4494
  db,
4346
4495
  tables,
4347
4496
  "nulls-smallest",
4348
4497
  filterCtx,
4349
- limits
4498
+ limits,
4499
+ scopes
4350
4500
  );
4351
4501
  const cacheCtx = {
4352
4502
  genericFilterCache: /* @__PURE__ */ new Map(),
@@ -4364,9 +4514,10 @@ var generateSchemaData = (db, schema, relations, options) => {
4364
4514
  limits,
4365
4515
  docs: options.docs ?? {},
4366
4516
  primaryKeyOf: (name) => tables[name] ? mysqlPrimaryKeyPropNames(tables[name]) : [],
4517
+ contextValuesOf,
4367
4518
  featureOf
4368
4519
  };
4369
- const relationAggregateFactory = anyTable("relationAggregates") ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
4520
+ const relationAggregateFactory = anyTable("relationAggregates") ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx, scopes) : void 0;
4370
4521
  const queries = {};
4371
4522
  const mutations = {};
4372
4523
  const mutationTxCtx = createMutationTxCtx(options.transactions);
@@ -4436,7 +4587,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4436
4587
  typeNameMapper,
4437
4588
  filterCtx,
4438
4589
  tableFeatures.distinct,
4439
- limits
4590
+ limits,
4591
+ policies
4440
4592
  );
4441
4593
  const selectSingleGenerated = generateSelectSingle(
4442
4594
  db,
@@ -4449,7 +4601,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4449
4601
  typeName,
4450
4602
  typeNameMapper,
4451
4603
  filterCtx,
4452
- limits
4604
+ limits,
4605
+ policies
4453
4606
  );
4454
4607
  const insertArrGenerated = tableFeatures.insert ? generateInsertArray(
4455
4608
  db,
@@ -4457,7 +4610,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4457
4610
  schema[tableName],
4458
4611
  insertInput,
4459
4612
  createArrayFieldName,
4460
- mutationTxCtx
4613
+ mutationTxCtx,
4614
+ policies
4461
4615
  ) : void 0;
4462
4616
  const insertSingleGenerated = tableFeatures.insert ? generateInsertSingle(
4463
4617
  db,
@@ -4465,7 +4619,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4465
4619
  schema[tableName],
4466
4620
  insertInput,
4467
4621
  createSingleFieldName,
4468
- mutationTxCtx
4622
+ mutationTxCtx,
4623
+ policies
4469
4624
  ) : void 0;
4470
4625
  const onConflictInput = tableFeatures.upsert ? generateOnConflictInput({
4471
4626
  table: schema[tableName],
@@ -4476,21 +4631,25 @@ var generateSchemaData = (db, schema, relations, options) => {
4476
4631
  }) : void 0;
4477
4632
  const upsertArrGenerated = onConflictInput ? generateUpsert(
4478
4633
  db,
4634
+ tableName,
4479
4635
  schema[tableName],
4480
4636
  insertInput,
4481
4637
  onConflictInput,
4482
4638
  upsertArrayFieldName,
4483
4639
  false,
4484
- mutationTxCtx
4640
+ mutationTxCtx,
4641
+ policies
4485
4642
  ) : void 0;
4486
4643
  const upsertSingleGenerated = onConflictInput ? generateUpsert(
4487
4644
  db,
4645
+ tableName,
4488
4646
  schema[tableName],
4489
4647
  insertInput,
4490
4648
  onConflictInput,
4491
4649
  upsertSingleFieldName,
4492
4650
  true,
4493
- mutationTxCtx
4651
+ mutationTxCtx,
4652
+ policies
4494
4653
  ) : void 0;
4495
4654
  const updateGenerated = tableFeatures.update ? generateUpdate(
4496
4655
  db,
@@ -4502,7 +4661,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4502
4661
  false,
4503
4662
  tableFeatures.requireWhere,
4504
4663
  filterCtx,
4505
- mutationTxCtx
4664
+ mutationTxCtx,
4665
+ policies
4506
4666
  ) : void 0;
4507
4667
  const updateSingleGenerated = tableFeatures.update ? generateUpdate(
4508
4668
  db,
@@ -4514,7 +4674,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4514
4674
  true,
4515
4675
  tableFeatures.requireWhere,
4516
4676
  filterCtx,
4517
- mutationTxCtx
4677
+ mutationTxCtx,
4678
+ policies
4518
4679
  ) : void 0;
4519
4680
  const updateManyInput = tableFeatures.update && tableFeatures.updateMany ? generateUpdateManyInput({ typeName, updatePrefix: prefixes.update, updateInput, tableFilters }) : void 0;
4520
4681
  const updateManyGenerated = updateManyInput ? generateUpdateMany(
@@ -4524,7 +4685,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4524
4685
  updateManyInput,
4525
4686
  updateManyFieldName,
4526
4687
  filterCtx,
4527
- mutationTxCtx
4688
+ mutationTxCtx,
4689
+ policies
4528
4690
  ) : void 0;
4529
4691
  const deleteGenerated = tableFeatures.delete ? generateDelete(
4530
4692
  db,
@@ -4535,7 +4697,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4535
4697
  false,
4536
4698
  tableFeatures.requireWhere,
4537
4699
  filterCtx,
4538
- mutationTxCtx
4700
+ mutationTxCtx,
4701
+ policies
4539
4702
  ) : void 0;
4540
4703
  const deleteSingleGenerated = tableFeatures.delete ? generateDelete(
4541
4704
  db,
@@ -4546,7 +4709,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4546
4709
  true,
4547
4710
  tableFeatures.requireWhere,
4548
4711
  filterCtx,
4549
- mutationTxCtx
4712
+ mutationTxCtx,
4713
+ policies
4550
4714
  ) : void 0;
4551
4715
  const updateCountGenerated = tableFeatures.update && tableFeatures.countMutations ? generateWriteCount({
4552
4716
  db,
@@ -4579,7 +4743,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4579
4743
  typeName,
4580
4744
  aggregateFieldName,
4581
4745
  tableFilters,
4582
- filterCtx
4746
+ filterCtx,
4747
+ scopes
4583
4748
  ) : void 0;
4584
4749
  const groupByType = tableFeatures.aggregates && tableFeatures.groupBy ? generateGroupByType(schema[tableName], tableName, typeName, cacheCtx) : void 0;
4585
4750
  const groupByEnum = groupByType ? generateGroupByEnum(schema[tableName], tableName, typeName) : void 0;
@@ -4593,7 +4758,8 @@ var generateSchemaData = (db, schema, relations, options) => {
4593
4758
  tableFilters,
4594
4759
  groupByEnum,
4595
4760
  havingInput,
4596
- filterCtx
4761
+ filterCtx,
4762
+ scopes
4597
4763
  ) : void 0;
4598
4764
  queries[selectArrGenerated.name] = {
4599
4765
  type: selectArrOutput,
@@ -4821,8 +4987,9 @@ var createNestedWriteTypes = (params) => {
4821
4987
  return cached;
4822
4988
  }
4823
4989
  const omitted = plan.fkSide === "child" ? plan.foreignColPropName : void 0;
4990
+ const contextColumns = cacheCtx.contextValuesOf?.(plan.targetTableName);
4824
4991
  const fields = Object.fromEntries(
4825
- Object.entries(visibleColumns(plan.targetTable)).filter(([columnName]) => columnName !== omitted).map(([columnName, column]) => [
4992
+ Object.entries(visibleColumns(plan.targetTable)).filter(([columnName]) => columnName !== omitted && !contextColumns?.[columnName]).map(([columnName, column]) => [
4826
4993
  columnName,
4827
4994
  drizzleColumnToGraphQLType(column, columnName, plan.targetTableName, false, true, true)
4828
4995
  ])
@@ -4921,8 +5088,8 @@ var createNestedWriteTypes = (params) => {
4921
5088
  var mergedOps = (entries) => Object.assign({}, ...entries.map((entry) => entry.ops));
4922
5089
  var asArray = (value) => Array.isArray(value) ? value : value === void 0 ? [] : [value];
4923
5090
  var createNestedWriteRuntime = (params) => {
4924
- const { plans, filterCtx } = params;
4925
- const conditionOf = (plan, filter, operation) => {
5091
+ const { plans, filterCtx, scopes, contextValues } = params;
5092
+ const conditionOf = (plan, filter, operation, scope) => {
4926
5093
  const condition = extractFilters(
4927
5094
  plan.targetTable,
4928
5095
  plan.targetTableName,
@@ -4934,7 +5101,7 @@ var createNestedWriteRuntime = (params) => {
4934
5101
  `Drizzle-GraphQL Error: '${operation}' on '${plan.relationName}' needs a filter that selects rows \u2014 it was given one that matches everything.`
4935
5102
  );
4936
5103
  }
4937
- return condition;
5104
+ return withScope(scope, plan.targetTableName, plan.targetTable, condition);
4938
5105
  };
4939
5106
  const assertSingleOperation = (plan, op) => {
4940
5107
  const supplied = ["create", "connect", "disconnect"].filter((key) => op[key] !== void 0 && op[key] !== null);
@@ -4944,7 +5111,7 @@ var createNestedWriteRuntime = (params) => {
4944
5111
  );
4945
5112
  }
4946
5113
  };
4947
- const detachAll = async (executor, plan, key) => executor.update(plan.targetTable).set({ [plan.foreignColPropName]: null }).where((0, import_drizzle_orm7.eq)(plan.foreignCol, key));
5114
+ const detachAll = async (executor, plan, key, scope) => executor.update(plan.targetTable).set({ [plan.foreignColPropName]: null }).where(withScope(scope, plan.targetTableName, plan.targetTable, (0, import_drizzle_orm7.eq)(plan.foreignCol, key)));
4948
5115
  return {
4949
5116
  enabled: (tableName) => !!plans[tableName],
4950
5117
  split: (tableName, values) => {
@@ -4978,12 +5145,13 @@ var createNestedWriteRuntime = (params) => {
4978
5145
  }
4979
5146
  return columns;
4980
5147
  },
4981
- applyParentSide: async (executor, tableName, ops) => {
5148
+ applyParentSide: async (executor, tableName, ops, context) => {
4982
5149
  const tablePlans = plans[tableName];
4983
5150
  const patch = {};
4984
5151
  if (!tablePlans) {
4985
5152
  return patch;
4986
5153
  }
5154
+ const scope = resolveScope(scopes, context, filterCtx);
4987
5155
  for (const [relationName, op] of Object.entries(ops)) {
4988
5156
  const plan = tablePlans[relationName];
4989
5157
  if (!plan || plan.fkSide !== "parent") {
@@ -4991,7 +5159,11 @@ var createNestedWriteRuntime = (params) => {
4991
5159
  }
4992
5160
  assertSingleOperation(plan, op);
4993
5161
  if (op["create"] !== void 0) {
4994
- const values = remapFromGraphQLSingleInput({ ...op["create"] }, plan.targetTable);
5162
+ const values = applyContextValues(
5163
+ remapFromGraphQLSingleInput({ ...op["create"] }, plan.targetTable),
5164
+ contextValues?.(plan.targetTableName),
5165
+ context
5166
+ );
4995
5167
  const inserted = await executor.insert(plan.targetTable).values(values).returning();
4996
5168
  const created = inserted[0];
4997
5169
  if (!created) {
@@ -5001,7 +5173,7 @@ var createNestedWriteRuntime = (params) => {
5001
5173
  }
5002
5174
  patch[plan.localColPropName] = created[plan.foreignColPropName];
5003
5175
  } else if (op["connect"] !== void 0) {
5004
- const rows = await executor.select().from(plan.targetTable).where(conditionOf(plan, op["connect"], "connect"));
5176
+ const rows = await executor.select().from(plan.targetTable).where(conditionOf(plan, op["connect"], "connect", scope));
5005
5177
  if (rows.length !== 1) {
5006
5178
  throw new import_graphql8.GraphQLError(
5007
5179
  `Drizzle-GraphQL Error: 'connect' on '${relationName}' matched ${rows.length} rows \u2014 it attaches a single row, so its filter must match exactly one.`
@@ -5014,11 +5186,12 @@ var createNestedWriteRuntime = (params) => {
5014
5186
  }
5015
5187
  return patch;
5016
5188
  },
5017
- applyChildSide: async (executor, tableName, ops, parentRows) => {
5189
+ applyChildSide: async (executor, tableName, ops, parentRows, context) => {
5018
5190
  const tablePlans = plans[tableName];
5019
5191
  if (!tablePlans) {
5020
5192
  return;
5021
5193
  }
5194
+ const scope = resolveScope(scopes, context, filterCtx);
5022
5195
  for (const [relationName, op] of Object.entries(ops)) {
5023
5196
  const plan = tablePlans[relationName];
5024
5197
  if (!plan || plan.fkSide !== "child") {
@@ -5035,34 +5208,38 @@ var createNestedWriteRuntime = (params) => {
5035
5208
  );
5036
5209
  }
5037
5210
  if (op["set"] !== void 0) {
5038
- await detachAll(executor, plan, key);
5211
+ await detachAll(executor, plan, key, scope);
5039
5212
  for (const filter of asArray(op["set"])) {
5040
- await executor.update(plan.targetTable).set({ [plan.foreignColPropName]: key }).where(conditionOf(plan, filter, "set"));
5213
+ await executor.update(plan.targetTable).set({ [plan.foreignColPropName]: key }).where(conditionOf(plan, filter, "set", scope));
5041
5214
  }
5042
5215
  }
5043
5216
  if (op["disconnect"] !== void 0) {
5044
5217
  if (plan.isOne) {
5045
5218
  if (op["disconnect"] === true) {
5046
- await detachAll(executor, plan, key);
5219
+ await detachAll(executor, plan, key, scope);
5047
5220
  }
5048
5221
  } else {
5049
5222
  for (const filter of asArray(op["disconnect"])) {
5050
- await executor.update(plan.targetTable).set({ [plan.foreignColPropName]: null }).where((0, import_drizzle_orm7.and)((0, import_drizzle_orm7.eq)(plan.foreignCol, key), conditionOf(plan, filter, "disconnect")));
5223
+ await executor.update(plan.targetTable).set({ [plan.foreignColPropName]: null }).where((0, import_drizzle_orm7.and)((0, import_drizzle_orm7.eq)(plan.foreignCol, key), conditionOf(plan, filter, "disconnect", scope)));
5051
5224
  }
5052
5225
  }
5053
5226
  }
5054
5227
  const attaches = op["connect"] !== void 0 || op["create"] !== void 0;
5055
5228
  if (plan.isOne && attaches && plan.canDetach && op["set"] === void 0) {
5056
- await detachAll(executor, plan, key);
5229
+ await detachAll(executor, plan, key, scope);
5057
5230
  }
5058
5231
  if (op["connect"] !== void 0) {
5059
5232
  for (const filter of asArray(op["connect"])) {
5060
- await executor.update(plan.targetTable).set({ [plan.foreignColPropName]: key }).where(conditionOf(plan, filter, "connect"));
5233
+ await executor.update(plan.targetTable).set({ [plan.foreignColPropName]: key }).where(conditionOf(plan, filter, "connect", scope));
5061
5234
  }
5062
5235
  }
5063
5236
  if (op["create"] !== void 0) {
5064
5237
  const rows = asArray(op["create"]).map((row) => ({
5065
- ...remapFromGraphQLSingleInput({ ...row }, plan.targetTable),
5238
+ ...applyContextValues(
5239
+ remapFromGraphQLSingleInput({ ...row }, plan.targetTable),
5240
+ contextValues?.(plan.targetTableName),
5241
+ context
5242
+ ),
5066
5243
  [plan.foreignColPropName]: key
5067
5244
  }));
5068
5245
  if (rows.length) {
@@ -5075,30 +5252,30 @@ var createNestedWriteRuntime = (params) => {
5075
5252
  };
5076
5253
  };
5077
5254
  var writeWithNestedOps = async (params) => {
5078
- const { executor, runtime, tableName, entries, remapValues, write } = params;
5255
+ const { executor, runtime, tableName, entries, remapValues, write, context } = params;
5079
5256
  return executor.transaction(async (tx) => {
5080
5257
  const rows = [];
5081
5258
  for (const entry of entries) {
5082
- const patch = await runtime.applyParentSide(tx, tableName, entry.ops);
5259
+ const patch = await runtime.applyParentSide(tx, tableName, entry.ops, context);
5083
5260
  const written = await write(tx, { ...remapValues(entry.columns), ...patch });
5084
- await runtime.applyChildSide(tx, tableName, entry.ops, written);
5261
+ await runtime.applyChildSide(tx, tableName, entry.ops, written, context);
5085
5262
  rows.push(...written);
5086
5263
  }
5087
5264
  return rows;
5088
5265
  });
5089
5266
  };
5090
5267
  var updateWithNestedOps = async (params) => {
5091
- const { executor, runtime, tableName, columns, ops, remapValues, write } = params;
5268
+ const { executor, runtime, tableName, columns, ops, remapValues, write, context } = params;
5092
5269
  return executor.transaction(async (tx) => {
5093
- const patch = await runtime.applyParentSide(tx, tableName, ops);
5270
+ const patch = await runtime.applyParentSide(tx, tableName, ops, context);
5094
5271
  const rows = await write(tx, { ...remapValues(columns), ...patch });
5095
- await runtime.applyChildSide(tx, tableName, ops, rows);
5272
+ await runtime.applyChildSide(tx, tableName, ops, rows, context);
5096
5273
  return rows;
5097
5274
  });
5098
5275
  };
5099
5276
 
5100
5277
  // src/util/builders/pg.ts
5101
- var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, distinctEnabled = true, limits) => {
5278
+ var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, distinctEnabled = true, limits, policies) => {
5102
5279
  const queryBase = db.query[tableName];
5103
5280
  const table = tables[tableName];
5104
5281
  const limitPolicy = limits?.(tableName);
@@ -5114,6 +5291,7 @@ var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filte
5114
5291
  try {
5115
5292
  const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, { deep: true });
5116
5293
  const { executor, queryBase: requestQueryBase } = resolveQueryExecutor(db, context, tableName, queryBase);
5294
+ const scope = policies?.scope?.(context);
5117
5295
  const limit = applyLimitPolicy(args.limit, limitPolicy, fieldName);
5118
5296
  if (requestQueryBase) {
5119
5297
  return await runRelationalSelect({
@@ -5132,6 +5310,7 @@ var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filte
5132
5310
  limits,
5133
5311
  pkNames,
5134
5312
  db: executor,
5313
+ scope,
5135
5314
  // PostgreSQL sorts NULLs as the largest values (last in ASC).
5136
5315
  nullOrdering: "nulls-largest"
5137
5316
  });
@@ -5170,7 +5349,12 @@ var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filte
5170
5349
  }
5171
5350
  }
5172
5351
  const cursorCondition = after != null && cursorEntries ? buildCursorCondition(table, cursorEntries, decodeCursor(after, cursorEntries), "nulls-largest") : void 0;
5173
- const baseWhereSql = where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0;
5352
+ const baseWhereSql = withScope(
5353
+ scope,
5354
+ tableName,
5355
+ table,
5356
+ where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
5357
+ );
5174
5358
  const whereSql = cursorCondition ? (0, import_drizzle_orm8.and)(baseWhereSql, cursorCondition) : baseWhereSql;
5175
5359
  let distinctKeys;
5176
5360
  if (distinct?.length) {
@@ -5225,7 +5409,7 @@ var generateSelectArray2 = (db, tableName, tables, relationMap, orderArgs, filte
5225
5409
  args: queryArgs
5226
5410
  };
5227
5411
  };
5228
- var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, limits) => {
5412
+ var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, limits, policies) => {
5229
5413
  const queryBase = db.query[tableName];
5230
5414
  const queryArgs = selectSingleArgs(orderArgs, filterArgs);
5231
5415
  const table = tables[tableName];
@@ -5236,6 +5420,7 @@ var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filt
5236
5420
  try {
5237
5421
  const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, { deep: true });
5238
5422
  const { executor, queryBase: requestQueryBase } = resolveQueryExecutor(db, context, tableName, queryBase);
5423
+ const scope = policies?.scope?.(context);
5239
5424
  if (requestQueryBase) {
5240
5425
  return await runRelationalSelect({
5241
5426
  queryBase: requestQueryBase,
@@ -5251,7 +5436,8 @@ var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filt
5251
5436
  filterCtx,
5252
5437
  limits,
5253
5438
  pkNames,
5254
- db: executor
5439
+ db: executor,
5440
+ scope
5255
5441
  });
5256
5442
  }
5257
5443
  const { offset, orderBy, where } = args;
@@ -5261,8 +5447,14 @@ var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filt
5261
5447
  { tableName, relationMap, tables }
5262
5448
  );
5263
5449
  let q = executor.select(selectedColumnsSql).from(table);
5264
- if (where) {
5265
- q = q.where(extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)));
5450
+ const whereSql = withScope(
5451
+ scope,
5452
+ tableName,
5453
+ table,
5454
+ where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
5455
+ );
5456
+ if (whereSql) {
5457
+ q = q.where(whereSql);
5266
5458
  }
5267
5459
  if (orderBy) {
5268
5460
  q = q.orderBy(...extractOrderBy(table, orderBy, relationFilterCtx(filterCtx, tableName), where));
@@ -5283,7 +5475,7 @@ var generateSelectSingle2 = (db, tableName, tables, relationMap, orderArgs, filt
5283
5475
  };
5284
5476
  };
5285
5477
  var pgPrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, import_pg_core2.getTableConfig);
5286
- var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx, nested, limits) => {
5478
+ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx, nested, limits, policies) => {
5287
5479
  const queryArgs = {
5288
5480
  values: {
5289
5481
  type: new import_graphql9.GraphQLNonNull(new import_graphql9.GraphQLList(new import_graphql9.GraphQLNonNull(baseType)))
@@ -5300,7 +5492,13 @@ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType,
5300
5492
  }
5301
5493
  const entries = nested?.enabled(tableName) ? args.values.map((values) => nested.split(tableName, values)) : void 0;
5302
5494
  const nestedEntries = entries?.some((entry) => nested.hasOps(entry.ops)) ? entries : void 0;
5303
- const input = nestedEntries ? [] : remapFromGraphQLArrayInput(entries ? entries.map((entry) => entry.columns) : args.values, table);
5495
+ const contextColumns = policies?.contextValues?.(tableName);
5496
+ const scope = policies?.scope?.(context);
5497
+ const input = nestedEntries ? [] : applyContextValuesAll(
5498
+ remapFromGraphQLArrayInput(entries ? entries.map((entry) => entry.columns) : args.values, table),
5499
+ contextColumns,
5500
+ context
5501
+ );
5304
5502
  const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
5305
5503
  deep: true
5306
5504
  });
@@ -5313,7 +5511,8 @@ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType,
5313
5511
  table,
5314
5512
  pkNames,
5315
5513
  parsedInfo,
5316
- limits
5514
+ limits,
5515
+ scope
5317
5516
  });
5318
5517
  const returning = nestedEntries ? nested.withJoinColumns(tableName, mergedOps(nestedEntries), { ...columns }, table) : columns;
5319
5518
  const runInsert = async (target, values) => {
@@ -5328,8 +5527,9 @@ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType,
5328
5527
  runtime: nested,
5329
5528
  tableName,
5330
5529
  entries: nestedEntries,
5331
- remapValues: (values) => remapFromGraphQLSingleInput(values, table),
5332
- write: (tx, values) => runInsert(tx, [values])
5530
+ remapValues: (values) => applyContextValues(remapFromGraphQLSingleInput(values, table), contextColumns, context),
5531
+ write: (tx, values) => runInsert(tx, [values]),
5532
+ context
5333
5533
  }) : await runInsert(executor, input);
5334
5534
  const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
5335
5535
  return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
@@ -5341,7 +5541,7 @@ var generateInsertArray2 = (db, tableName, table, tables, relationMap, baseType,
5341
5541
  args: queryArgs
5342
5542
  };
5343
5543
  };
5344
- var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx, nested, limits) => {
5544
+ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx, nested, limits, policies) => {
5345
5545
  const queryArgs = {
5346
5546
  values: {
5347
5547
  type: new import_graphql9.GraphQLNonNull(baseType)
@@ -5355,7 +5555,13 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
5355
5555
  return await runMutation(db, context, info, txCtx, async (executor) => {
5356
5556
  const entry = nested?.enabled(tableName) ? nested.split(tableName, args.values) : void 0;
5357
5557
  const nestedEntry = entry && nested.hasOps(entry.ops) ? entry : void 0;
5358
- const input = nestedEntry ? {} : remapFromGraphQLSingleInput(entry ? entry.columns : args.values, table);
5558
+ const contextColumns = policies?.contextValues?.(tableName);
5559
+ const scope = policies?.scope?.(context);
5560
+ const input = nestedEntry ? {} : applyContextValues(
5561
+ remapFromGraphQLSingleInput(entry ? entry.columns : args.values, table),
5562
+ contextColumns,
5563
+ context
5564
+ );
5359
5565
  const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
5360
5566
  deep: true
5361
5567
  });
@@ -5368,7 +5574,8 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
5368
5574
  table,
5369
5575
  pkNames,
5370
5576
  parsedInfo,
5371
- limits
5577
+ limits,
5578
+ scope
5372
5579
  });
5373
5580
  const returning = nestedEntry ? nested.withJoinColumns(tableName, nestedEntry.ops, { ...columns }, table) : columns;
5374
5581
  const runInsert = async (target, values) => {
@@ -5383,8 +5590,9 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
5383
5590
  runtime: nested,
5384
5591
  tableName,
5385
5592
  entries: [nestedEntry],
5386
- remapValues: (values) => remapFromGraphQLSingleInput(values, table),
5387
- write: runInsert
5593
+ remapValues: (values) => applyContextValues(remapFromGraphQLSingleInput(values, table), contextColumns, context),
5594
+ write: runInsert,
5595
+ context
5388
5596
  }) : await runInsert(executor, input);
5389
5597
  if (!result[0]) {
5390
5598
  if (!conflictDoNothing) {
@@ -5402,7 +5610,7 @@ var generateInsertSingle2 = (db, tableName, table, tables, relationMap, baseType
5402
5610
  args: queryArgs
5403
5611
  };
5404
5612
  };
5405
- var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx, txCtx, nested, limits) => {
5613
+ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx, txCtx, nested, limits, policies) => {
5406
5614
  const queryArgs = {
5407
5615
  values: {
5408
5616
  type: single ? new import_graphql9.GraphQLNonNull(baseType) : new import_graphql9.GraphQLNonNull(new import_graphql9.GraphQLList(new import_graphql9.GraphQLNonNull(baseType)))
@@ -5424,7 +5632,13 @@ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onCo
5424
5632
  }
5425
5633
  const entries = nested?.enabled(tableName) ? supplied.map((values) => nested.split(tableName, values)) : void 0;
5426
5634
  const nestedEntries = entries?.some((entry) => nested.hasOps(entry.ops)) ? entries : void 0;
5427
- const input = nestedEntries ? [] : remapFromGraphQLArrayInput(entries ? entries.map((entry) => entry.columns) : supplied, table);
5635
+ const contextColumns = policies?.contextValues?.(tableName);
5636
+ const scope = policies?.scope?.(context);
5637
+ const input = nestedEntries ? [] : applyContextValuesAll(
5638
+ remapFromGraphQLArrayInput(entries ? entries.map((entry) => entry.columns) : supplied, table),
5639
+ contextColumns,
5640
+ context
5641
+ );
5428
5642
  const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, { deep: true });
5429
5643
  const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
5430
5644
  relationMap,
@@ -5435,7 +5649,8 @@ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onCo
5435
5649
  table,
5436
5650
  pkNames,
5437
5651
  parsedInfo,
5438
- limits
5652
+ limits,
5653
+ scope
5439
5654
  });
5440
5655
  const returning = nestedEntries ? nested.withJoinColumns(tableName, mergedOps(nestedEntries), { ...columns }, table) : columns;
5441
5656
  const runUpsert = async (target, values) => {
@@ -5450,7 +5665,8 @@ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onCo
5450
5665
  buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
5451
5666
  });
5452
5667
  let query = target.insert(table).values(values).returning(returning);
5453
- query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
5668
+ const setWhere = withScope(scope, tableName, table, plan.setWhere);
5669
+ query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere });
5454
5670
  return await query;
5455
5671
  };
5456
5672
  const result = nestedEntries ? await writeWithNestedOps({
@@ -5458,8 +5674,9 @@ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onCo
5458
5674
  runtime: nested,
5459
5675
  tableName,
5460
5676
  entries: nestedEntries,
5461
- remapValues: (values) => remapFromGraphQLSingleInput(values, table),
5462
- write: (tx, values) => runUpsert(tx, [values])
5677
+ remapValues: (values) => applyContextValues(remapFromGraphQLSingleInput(values, table), contextColumns, context),
5678
+ write: (tx, values) => runUpsert(tx, [values]),
5679
+ context
5463
5680
  }) : await runUpsert(executor, input);
5464
5681
  if (single && !result[0]) {
5465
5682
  return void 0;
@@ -5474,7 +5691,7 @@ var generateUpsert2 = (db, tableName, table, tables, relationMap, baseType, onCo
5474
5691
  args: queryArgs
5475
5692
  };
5476
5693
  };
5477
- var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx, txCtx, nested, limits) => {
5694
+ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx, txCtx, nested, limits, policies) => {
5478
5695
  const queryArgs = {
5479
5696
  set: {
5480
5697
  type: new import_graphql9.GraphQLNonNull(setArgs)
@@ -5490,6 +5707,7 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
5490
5707
  try {
5491
5708
  return await runMutation(db, context, info, txCtx, async (executor) => {
5492
5709
  const { where, set } = args;
5710
+ const scope = policies?.scope?.(context);
5493
5711
  const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
5494
5712
  deep: true
5495
5713
  });
@@ -5502,16 +5720,25 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
5502
5720
  table,
5503
5721
  pkNames,
5504
5722
  parsedInfo,
5505
- limits
5723
+ limits,
5724
+ scope
5506
5725
  });
5507
5726
  const entry = nested?.enabled(tableName) ? nested.split(tableName, set) : void 0;
5508
5727
  const nestedOps = entry && nested.hasOps(entry.ops) ? entry.ops : void 0;
5509
- const input = remapUpdateInput(entry ? entry.columns : set, table, tableName);
5728
+ const input = stripContextValues(
5729
+ remapUpdateInput(entry ? entry.columns : set, table, tableName),
5730
+ policies?.contextValues?.(tableName)
5731
+ );
5510
5732
  if (!Object.keys(input).length && !nestedOps) {
5511
5733
  throw new import_graphql9.GraphQLError("Unable to update with no values specified!");
5512
5734
  }
5513
5735
  const relationCtx = relationFilterCtx(filterCtx, tableName);
5514
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
5736
+ const filters = withScope(
5737
+ scope,
5738
+ tableName,
5739
+ table,
5740
+ single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0
5741
+ );
5515
5742
  if (single) {
5516
5743
  await assertSingleMatch(executor, table, filters, fieldName);
5517
5744
  }
@@ -5531,7 +5758,8 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
5531
5758
  columns: input,
5532
5759
  ops: nestedOps,
5533
5760
  remapValues: (values) => values,
5534
- write: runUpdate
5761
+ write: runUpdate,
5762
+ context
5535
5763
  }) : await runUpdate(executor, input);
5536
5764
  if (single && result.length > 1) {
5537
5765
  throw new import_graphql9.GraphQLError(`${fieldName}: 'where' matched more than one row!`);
@@ -5549,7 +5777,7 @@ var generateUpdate2 = (db, tableName, table, tables, relationMap, setArgs, filte
5549
5777
  args: queryArgs
5550
5778
  };
5551
5779
  };
5552
- var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx, txCtx, nested, limits) => {
5780
+ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx, txCtx, nested, limits, policies) => {
5553
5781
  const queryArgs = {
5554
5782
  updates: {
5555
5783
  type: new import_graphql9.GraphQLNonNull(new import_graphql9.GraphQLList(new import_graphql9.GraphQLNonNull(updateManyInput)))
@@ -5565,6 +5793,8 @@ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateMany
5565
5793
  if (!updates.length) {
5566
5794
  throw new import_graphql9.GraphQLError("No updates were provided!");
5567
5795
  }
5796
+ const scope = policies?.scope?.(context);
5797
+ const contextColumns = policies?.contextValues?.(tableName);
5568
5798
  const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
5569
5799
  deep: true
5570
5800
  });
@@ -5577,19 +5807,28 @@ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateMany
5577
5807
  table,
5578
5808
  pkNames,
5579
5809
  parsedInfo,
5580
- limits
5810
+ limits,
5811
+ scope
5581
5812
  });
5582
5813
  const entries = updates.map(({ where, set }) => {
5583
5814
  const split = nested?.enabled(tableName) ? nested.split(tableName, set) : void 0;
5584
5815
  const ops = split && nested.hasOps(split.ops) ? split.ops : void 0;
5585
- const input = remapUpdateInput(split ? split.columns : set, table, tableName);
5816
+ const input = stripContextValues(
5817
+ remapUpdateInput(split ? split.columns : set, table, tableName),
5818
+ contextColumns
5819
+ );
5586
5820
  if (!Object.keys(input).length && !ops) {
5587
5821
  throw new import_graphql9.GraphQLError("Unable to update with no values specified!");
5588
5822
  }
5589
5823
  return {
5590
5824
  set: input,
5591
5825
  ops,
5592
- filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
5826
+ filters: withScope(
5827
+ scope,
5828
+ tableName,
5829
+ table,
5830
+ where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
5831
+ )
5593
5832
  };
5594
5833
  });
5595
5834
  const returning = entries.some((entry) => entry.ops) ? nested.withJoinColumns(
@@ -5603,7 +5842,7 @@ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateMany
5603
5842
  const perEntry = await executor.transaction(async (tx) => {
5604
5843
  const results = [];
5605
5844
  for (const entry of entries) {
5606
- const values = entry.ops ? { ...entry.set, ...await nested.applyParentSide(tx, tableName, entry.ops) } : entry.set;
5845
+ const values = entry.ops ? { ...entry.set, ...await nested.applyParentSide(tx, tableName, entry.ops, context) } : entry.set;
5607
5846
  const writes = Object.keys(values).length > 0;
5608
5847
  let query = writes ? tx.update(table).set(values) : tx.select(returning).from(table);
5609
5848
  if (entry.filters) {
@@ -5611,7 +5850,7 @@ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateMany
5611
5850
  }
5612
5851
  const rows = await (writes ? query.returning(returning) : query);
5613
5852
  if (entry.ops) {
5614
- await nested.applyChildSide(tx, tableName, entry.ops, rows);
5853
+ await nested.applyChildSide(tx, tableName, entry.ops, rows, context);
5615
5854
  }
5616
5855
  results.push(rows);
5617
5856
  }
@@ -5640,7 +5879,7 @@ var generateUpdateMany2 = (db, tableName, table, tables, relationMap, updateMany
5640
5879
  args: queryArgs
5641
5880
  };
5642
5881
  };
5643
- var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx, txCtx) => {
5882
+ var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx, txCtx, policies) => {
5644
5883
  const queryArgs = {
5645
5884
  where: {
5646
5885
  type: single || requireWhere ? new import_graphql9.GraphQLNonNull(filterArgs) : filterArgs
@@ -5652,6 +5891,7 @@ var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, si
5652
5891
  try {
5653
5892
  return await runMutation(db, context, info, txCtx, async (executor) => {
5654
5893
  const { where } = args;
5894
+ const scope = policies?.scope?.(context);
5655
5895
  const parsedInfo = (0, import_graphql_parse_resolve_info3.parseResolveInfo)(info, {
5656
5896
  deep: true
5657
5897
  });
@@ -5661,7 +5901,12 @@ var generateDelete2 = (db, tableName, table, filterArgs, fieldName, typeName, si
5661
5901
  selectionCtx
5662
5902
  );
5663
5903
  const relationCtx = relationFilterCtx(filterCtx, tableName);
5664
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
5904
+ const filters = withScope(
5905
+ scope,
5906
+ tableName,
5907
+ table,
5908
+ single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0
5909
+ );
5665
5910
  if (single) {
5666
5911
  await assertSingleMatch(executor, table, filters, fieldName);
5667
5912
  }
@@ -5719,12 +5964,16 @@ function generateSchemaData2(db, schema, relations, options) {
5719
5964
  attachTargetPrimaryKeys(namedRelations, tables, pgPrimaryKeyPropNames);
5720
5965
  const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
5721
5966
  const filterCtx = { tables, relationMap: namedRelations };
5967
+ const scopes = options.policies?.scope;
5968
+ const contextValuesOf = options.policies?.contextValues;
5969
+ const policies = bindPolicies(options.policies, filterCtx);
5722
5970
  const resolverFactory = createRelationResolverFactory(
5723
5971
  db,
5724
5972
  tables,
5725
5973
  "nulls-largest",
5726
5974
  filterCtx,
5727
- limits
5975
+ limits,
5976
+ scopes
5728
5977
  );
5729
5978
  const cacheCtx = {
5730
5979
  genericFilterCache: /* @__PURE__ */ new Map(),
@@ -5742,6 +5991,7 @@ function generateSchemaData2(db, schema, relations, options) {
5742
5991
  limits,
5743
5992
  docs: options.docs ?? {},
5744
5993
  primaryKeyOf: (name) => tables[name] ? pgPrimaryKeyPropNames(tables[name]) : [],
5994
+ contextValuesOf,
5745
5995
  featureOf
5746
5996
  };
5747
5997
  const nestedPlans = features.nestedWrites ? buildNestedWritePlans(
@@ -5752,8 +6002,8 @@ function generateSchemaData2(db, schema, relations, options) {
5752
6002
  extractRelationJoinColumns
5753
6003
  ) : void 0;
5754
6004
  const nestedTypes = nestedPlans ? createNestedWriteTypes({ plans: nestedPlans, cacheCtx, typeNameMapper, insertPrefix: prefixes.insert }) : void 0;
5755
- const nestedRuntime = nestedPlans ? createNestedWriteRuntime({ plans: nestedPlans, filterCtx }) : void 0;
5756
- const relationAggregateFactory = anyTable("relationAggregates") ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
6005
+ const nestedRuntime = nestedPlans ? createNestedWriteRuntime({ plans: nestedPlans, filterCtx, scopes, contextValues: contextValuesOf }) : void 0;
6006
+ const relationAggregateFactory = anyTable("relationAggregates") ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx, scopes) : void 0;
5757
6007
  const queries = {};
5758
6008
  const mutations = {};
5759
6009
  const mutationTxCtx = createMutationTxCtx(options.transactions);
@@ -5813,7 +6063,8 @@ function generateSchemaData2(db, schema, relations, options) {
5813
6063
  typeNameMapper,
5814
6064
  filterCtx,
5815
6065
  tableFeatures.distinct,
5816
- limits
6066
+ limits,
6067
+ policies
5817
6068
  );
5818
6069
  const selectSingleGenerated = generateSelectSingle2(
5819
6070
  db,
@@ -5826,7 +6077,8 @@ function generateSchemaData2(db, schema, relations, options) {
5826
6077
  typeName,
5827
6078
  typeNameMapper,
5828
6079
  filterCtx,
5829
- limits
6080
+ limits,
6081
+ policies
5830
6082
  );
5831
6083
  const insertArrGenerated = tableFeatures.insert ? generateInsertArray2(
5832
6084
  db,
@@ -5841,7 +6093,8 @@ function generateSchemaData2(db, schema, relations, options) {
5841
6093
  conflictDoNothing,
5842
6094
  mutationTxCtx,
5843
6095
  nestedRuntime,
5844
- limits
6096
+ limits,
6097
+ policies
5845
6098
  ) : void 0;
5846
6099
  const insertSingleGenerated = tableFeatures.insert ? generateInsertSingle2(
5847
6100
  db,
@@ -5856,7 +6109,8 @@ function generateSchemaData2(db, schema, relations, options) {
5856
6109
  conflictDoNothing,
5857
6110
  mutationTxCtx,
5858
6111
  nestedRuntime,
5859
- limits
6112
+ limits,
6113
+ policies
5860
6114
  ) : void 0;
5861
6115
  const uniqueSets = tableFeatures.upsert ? getUniqueColumnSets(schema[tableName], import_pg_core2.getTableConfig) : [];
5862
6116
  const onConflictInput = tableFeatures.upsert ? generateOnConflictInput({
@@ -5882,7 +6136,8 @@ function generateSchemaData2(db, schema, relations, options) {
5882
6136
  filterCtx,
5883
6137
  mutationTxCtx,
5884
6138
  nestedRuntime,
5885
- limits
6139
+ limits,
6140
+ policies
5886
6141
  ) : void 0;
5887
6142
  const upsertSingleGenerated = onConflictInput ? generateUpsert2(
5888
6143
  db,
@@ -5900,7 +6155,8 @@ function generateSchemaData2(db, schema, relations, options) {
5900
6155
  filterCtx,
5901
6156
  mutationTxCtx,
5902
6157
  nestedRuntime,
5903
- limits
6158
+ limits,
6159
+ policies
5904
6160
  ) : void 0;
5905
6161
  const updateGenerated = tableFeatures.update ? generateUpdate2(
5906
6162
  db,
@@ -5918,7 +6174,8 @@ function generateSchemaData2(db, schema, relations, options) {
5918
6174
  filterCtx,
5919
6175
  mutationTxCtx,
5920
6176
  nestedRuntime,
5921
- limits
6177
+ limits,
6178
+ policies
5922
6179
  ) : void 0;
5923
6180
  const updateSingleGenerated = tableFeatures.update ? generateUpdate2(
5924
6181
  db,
@@ -5936,7 +6193,8 @@ function generateSchemaData2(db, schema, relations, options) {
5936
6193
  filterCtx,
5937
6194
  mutationTxCtx,
5938
6195
  nestedRuntime,
5939
- limits
6196
+ limits,
6197
+ policies
5940
6198
  ) : void 0;
5941
6199
  const updateManyInput = tableFeatures.update && tableFeatures.updateMany ? generateUpdateManyInput({ typeName, updatePrefix: prefixes.update, updateInput, tableFilters }) : void 0;
5942
6200
  const updateManyGenerated = updateManyInput ? generateUpdateMany2(
@@ -5952,7 +6210,8 @@ function generateSchemaData2(db, schema, relations, options) {
5952
6210
  filterCtx,
5953
6211
  mutationTxCtx,
5954
6212
  nestedRuntime,
5955
- limits
6213
+ limits,
6214
+ policies
5956
6215
  ) : void 0;
5957
6216
  const deleteGenerated = tableFeatures.delete ? generateDelete2(
5958
6217
  db,
@@ -5965,7 +6224,8 @@ function generateSchemaData2(db, schema, relations, options) {
5965
6224
  tableFeatures.requireWhere,
5966
6225
  filterCtx,
5967
6226
  { tableName, relationMap: namedRelations, tables },
5968
- mutationTxCtx
6227
+ mutationTxCtx,
6228
+ policies
5969
6229
  ) : void 0;
5970
6230
  const deleteSingleGenerated = tableFeatures.delete ? generateDelete2(
5971
6231
  db,
@@ -5978,7 +6238,8 @@ function generateSchemaData2(db, schema, relations, options) {
5978
6238
  tableFeatures.requireWhere,
5979
6239
  filterCtx,
5980
6240
  { tableName, relationMap: namedRelations, tables },
5981
- mutationTxCtx
6241
+ mutationTxCtx,
6242
+ policies
5982
6243
  ) : void 0;
5983
6244
  const updateCountGenerated = tableFeatures.update && tableFeatures.countMutations ? generateWriteCount({
5984
6245
  db,
@@ -6012,7 +6273,8 @@ function generateSchemaData2(db, schema, relations, options) {
6012
6273
  typeName,
6013
6274
  aggregateFieldName,
6014
6275
  tableFilters,
6015
- filterCtx
6276
+ filterCtx,
6277
+ scopes
6016
6278
  ) : void 0;
6017
6279
  const groupByType = tableFeatures.aggregates && tableFeatures.groupBy ? generateGroupByType(schema[tableName], tableName, typeName, cacheCtx) : void 0;
6018
6280
  const groupByEnum = groupByType ? generateGroupByEnum(schema[tableName], tableName, typeName) : void 0;
@@ -6026,7 +6288,8 @@ function generateSchemaData2(db, schema, relations, options) {
6026
6288
  tableFilters,
6027
6289
  groupByEnum,
6028
6290
  havingInput,
6029
- filterCtx
6291
+ filterCtx,
6292
+ scopes
6030
6293
  ) : void 0;
6031
6294
  queries[selectArrGenerated.name] = {
6032
6295
  type: selectArrOutput,
@@ -6228,7 +6491,7 @@ var import_drizzle_orm9 = require("drizzle-orm");
6228
6491
  var import_sqlite_core2 = require("drizzle-orm/sqlite-core");
6229
6492
  var import_graphql10 = require("graphql");
6230
6493
  var import_graphql_parse_resolve_info4 = require("graphql-parse-resolve-info");
6231
- var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, distinctEnabled = true, limits) => {
6494
+ var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, distinctEnabled = true, limits, policies) => {
6232
6495
  const queryBase = db.query[tableName];
6233
6496
  if (!queryBase) {
6234
6497
  throw new Error(
@@ -6265,6 +6528,7 @@ var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filte
6265
6528
  limits,
6266
6529
  pkNames,
6267
6530
  db: executor,
6531
+ scope: policies?.scope?.(context),
6268
6532
  // SQLite sorts NULLs as the smallest values (first in ASC).
6269
6533
  nullOrdering: "nulls-smallest"
6270
6534
  });
@@ -6275,7 +6539,7 @@ var generateSelectArray3 = (db, tableName, tables, relationMap, orderArgs, filte
6275
6539
  args: queryArgs
6276
6540
  };
6277
6541
  };
6278
- var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, limits) => {
6542
+ var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filterArgs, fieldName, typeName, typeNameMapper, filterCtx, limits, policies) => {
6279
6543
  const queryBase = db.query[tableName];
6280
6544
  if (!queryBase) {
6281
6545
  throw new Error(
@@ -6305,7 +6569,8 @@ var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filt
6305
6569
  filterCtx,
6306
6570
  limits,
6307
6571
  pkNames,
6308
- db: executor
6572
+ db: executor,
6573
+ scope: policies?.scope?.(context)
6309
6574
  });
6310
6575
  } catch (e) {
6311
6576
  throw toGraphQLError(e);
@@ -6315,7 +6580,7 @@ var generateSelectSingle3 = (db, tableName, tables, relationMap, orderArgs, filt
6315
6580
  };
6316
6581
  };
6317
6582
  var sqlitePrimaryKeyPropNames = (table) => getPrimaryKeyPropNamesFromConfig(table, import_sqlite_core2.getTableConfig);
6318
- var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx, nested, limits) => {
6583
+ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx, nested, limits, policies) => {
6319
6584
  const queryArgs = {
6320
6585
  values: {
6321
6586
  type: new import_graphql10.GraphQLNonNull(new import_graphql10.GraphQLList(new import_graphql10.GraphQLNonNull(baseType)))
@@ -6332,7 +6597,13 @@ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType,
6332
6597
  }
6333
6598
  const entries = nested?.enabled(tableName) ? args.values.map((values) => nested.split(tableName, values)) : void 0;
6334
6599
  const nestedEntries = entries?.some((entry) => nested.hasOps(entry.ops)) ? entries : void 0;
6335
- const input = nestedEntries ? [] : remapFromGraphQLArrayInput(entries ? entries.map((entry) => entry.columns) : args.values, table);
6600
+ const contextColumns = policies?.contextValues?.(tableName);
6601
+ const scope = policies?.scope?.(context);
6602
+ const input = nestedEntries ? [] : applyContextValuesAll(
6603
+ remapFromGraphQLArrayInput(entries ? entries.map((entry) => entry.columns) : args.values, table),
6604
+ contextColumns,
6605
+ context
6606
+ );
6336
6607
  const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
6337
6608
  deep: true
6338
6609
  });
@@ -6345,7 +6616,8 @@ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType,
6345
6616
  table,
6346
6617
  pkNames,
6347
6618
  parsedInfo,
6348
- limits
6619
+ limits,
6620
+ scope
6349
6621
  });
6350
6622
  const returning = nestedEntries ? nested.withJoinColumns(tableName, mergedOps(nestedEntries), { ...columns }, table) : columns;
6351
6623
  const runInsert = async (target, values) => {
@@ -6360,8 +6632,9 @@ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType,
6360
6632
  runtime: nested,
6361
6633
  tableName,
6362
6634
  entries: nestedEntries,
6363
- remapValues: (values) => remapFromGraphQLSingleInput(values, table),
6364
- write: (tx, values) => runInsert(tx, [values])
6635
+ remapValues: (values) => applyContextValues(remapFromGraphQLSingleInput(values, table), contextColumns, context),
6636
+ write: (tx, values) => runInsert(tx, [values]),
6637
+ context
6365
6638
  }) : await runInsert(executor, input);
6366
6639
  const enriched = hasRelations ? await eagerLoadMutationRelations(executor, tableName, result, pkNames, withParams) : result;
6367
6640
  return remapToGraphQLArrayOutput(enriched, tableName, table, relationMap);
@@ -6373,7 +6646,7 @@ var generateInsertArray3 = (db, tableName, table, tables, relationMap, baseType,
6373
6646
  args: queryArgs
6374
6647
  };
6375
6648
  };
6376
- var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx, nested, limits) => {
6649
+ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType, fieldName, typeName, typeNameMapper, conflictDoNothing = false, txCtx, nested, limits, policies) => {
6377
6650
  const queryArgs = {
6378
6651
  values: {
6379
6652
  type: new import_graphql10.GraphQLNonNull(baseType)
@@ -6387,7 +6660,13 @@ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType
6387
6660
  return await runMutation(db, context, info, txCtx, async (executor) => {
6388
6661
  const entry = nested?.enabled(tableName) ? nested.split(tableName, args.values) : void 0;
6389
6662
  const nestedEntry = entry && nested.hasOps(entry.ops) ? entry : void 0;
6390
- const input = nestedEntry ? {} : remapFromGraphQLSingleInput(entry ? entry.columns : args.values, table);
6663
+ const contextColumns = policies?.contextValues?.(tableName);
6664
+ const scope = policies?.scope?.(context);
6665
+ const input = nestedEntry ? {} : applyContextValues(
6666
+ remapFromGraphQLSingleInput(entry ? entry.columns : args.values, table),
6667
+ contextColumns,
6668
+ context
6669
+ );
6391
6670
  const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
6392
6671
  deep: true
6393
6672
  });
@@ -6400,7 +6679,8 @@ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType
6400
6679
  table,
6401
6680
  pkNames,
6402
6681
  parsedInfo,
6403
- limits
6682
+ limits,
6683
+ scope
6404
6684
  });
6405
6685
  const returning = nestedEntry ? nested.withJoinColumns(tableName, nestedEntry.ops, { ...columns }, table) : columns;
6406
6686
  const runInsert = async (target, values) => {
@@ -6415,8 +6695,9 @@ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType
6415
6695
  runtime: nested,
6416
6696
  tableName,
6417
6697
  entries: [nestedEntry],
6418
- remapValues: (values) => remapFromGraphQLSingleInput(values, table),
6419
- write: runInsert
6698
+ remapValues: (values) => applyContextValues(remapFromGraphQLSingleInput(values, table), contextColumns, context),
6699
+ write: runInsert,
6700
+ context
6420
6701
  }) : await runInsert(executor, input);
6421
6702
  if (!result[0]) {
6422
6703
  if (!conflictDoNothing) {
@@ -6434,7 +6715,7 @@ var generateInsertSingle3 = (db, tableName, table, tables, relationMap, baseType
6434
6715
  args: queryArgs
6435
6716
  };
6436
6717
  };
6437
- var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx, txCtx, nested, limits) => {
6718
+ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onConflictType, uniqueSets, fieldName, typeName, single, typeNameMapper, filterCtx, txCtx, nested, limits, policies) => {
6438
6719
  const queryArgs = {
6439
6720
  values: {
6440
6721
  type: single ? new import_graphql10.GraphQLNonNull(baseType) : new import_graphql10.GraphQLNonNull(new import_graphql10.GraphQLList(new import_graphql10.GraphQLNonNull(baseType)))
@@ -6456,7 +6737,13 @@ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onCo
6456
6737
  }
6457
6738
  const entries = nested?.enabled(tableName) ? supplied.map((values) => nested.split(tableName, values)) : void 0;
6458
6739
  const nestedEntries = entries?.some((entry) => nested.hasOps(entry.ops)) ? entries : void 0;
6459
- const input = nestedEntries ? [] : remapFromGraphQLArrayInput(entries ? entries.map((entry) => entry.columns) : supplied, table);
6740
+ const contextColumns = policies?.contextValues?.(tableName);
6741
+ const scope = policies?.scope?.(context);
6742
+ const input = nestedEntries ? [] : applyContextValuesAll(
6743
+ remapFromGraphQLArrayInput(entries ? entries.map((entry) => entry.columns) : supplied, table),
6744
+ contextColumns,
6745
+ context
6746
+ );
6460
6747
  const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, { deep: true });
6461
6748
  const { columns, hasRelations, withParams } = prepareMutationRelationColumns({
6462
6749
  relationMap,
@@ -6467,7 +6754,8 @@ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onCo
6467
6754
  table,
6468
6755
  pkNames,
6469
6756
  parsedInfo,
6470
- limits
6757
+ limits,
6758
+ scope
6471
6759
  });
6472
6760
  const returning = nestedEntries ? nested.withJoinColumns(tableName, mergedOps(nestedEntries), { ...columns }, table) : columns;
6473
6761
  const runUpsert = async (target, values) => {
@@ -6482,7 +6770,8 @@ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onCo
6482
6770
  buildWhere: (where) => extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName))
6483
6771
  });
6484
6772
  let query = target.insert(table).values(values).returning(returning);
6485
- query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere: plan.setWhere });
6773
+ const setWhere = withScope(scope, tableName, table, plan.setWhere);
6774
+ query = plan.action === "NOTHING" ? query.onConflictDoNothing(plan.target ? { target: plan.target } : void 0) : query.onConflictDoUpdate({ target: plan.target, set: plan.set, setWhere });
6486
6775
  return await query;
6487
6776
  };
6488
6777
  const result = nestedEntries ? await writeWithNestedOps({
@@ -6490,8 +6779,9 @@ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onCo
6490
6779
  runtime: nested,
6491
6780
  tableName,
6492
6781
  entries: nestedEntries,
6493
- remapValues: (values) => remapFromGraphQLSingleInput(values, table),
6494
- write: (tx, values) => runUpsert(tx, [values])
6782
+ remapValues: (values) => applyContextValues(remapFromGraphQLSingleInput(values, table), contextColumns, context),
6783
+ write: (tx, values) => runUpsert(tx, [values]),
6784
+ context
6495
6785
  }) : await runUpsert(executor, input);
6496
6786
  if (single && !result[0]) {
6497
6787
  return void 0;
@@ -6506,7 +6796,7 @@ var generateUpsert3 = (db, tableName, table, tables, relationMap, baseType, onCo
6506
6796
  args: queryArgs
6507
6797
  };
6508
6798
  };
6509
- var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx, txCtx, nested, limits) => {
6799
+ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filterArgs, fieldName, typeName, single, requireWhere, typeNameMapper, filterCtx, txCtx, nested, limits, policies) => {
6510
6800
  const queryArgs = {
6511
6801
  set: {
6512
6802
  type: new import_graphql10.GraphQLNonNull(setArgs)
@@ -6522,6 +6812,7 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
6522
6812
  try {
6523
6813
  return await runMutation(db, context, info, txCtx, async (executor) => {
6524
6814
  const { where, set } = args;
6815
+ const scope = policies?.scope?.(context);
6525
6816
  const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
6526
6817
  deep: true
6527
6818
  });
@@ -6534,16 +6825,25 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
6534
6825
  table,
6535
6826
  pkNames,
6536
6827
  parsedInfo,
6537
- limits
6828
+ limits,
6829
+ scope
6538
6830
  });
6539
6831
  const entry = nested?.enabled(tableName) ? nested.split(tableName, set) : void 0;
6540
6832
  const nestedOps = entry && nested.hasOps(entry.ops) ? entry.ops : void 0;
6541
- const input = remapUpdateInput(entry ? entry.columns : set, table, tableName);
6833
+ const input = stripContextValues(
6834
+ remapUpdateInput(entry ? entry.columns : set, table, tableName),
6835
+ policies?.contextValues?.(tableName)
6836
+ );
6542
6837
  if (!Object.keys(input).length && !nestedOps) {
6543
6838
  throw new import_graphql10.GraphQLError("Unable to update with no values specified!");
6544
6839
  }
6545
6840
  const relationCtx = relationFilterCtx(filterCtx, tableName);
6546
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
6841
+ const filters = withScope(
6842
+ scope,
6843
+ tableName,
6844
+ table,
6845
+ single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0
6846
+ );
6547
6847
  if (single) {
6548
6848
  await assertSingleMatch(executor, table, filters, fieldName);
6549
6849
  }
@@ -6563,7 +6863,8 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
6563
6863
  columns: input,
6564
6864
  ops: nestedOps,
6565
6865
  remapValues: (values) => values,
6566
- write: runUpdate
6866
+ write: runUpdate,
6867
+ context
6567
6868
  }) : await runUpdate(executor, input);
6568
6869
  if (single && result.length > 1) {
6569
6870
  throw new import_graphql10.GraphQLError(`${fieldName}: 'where' matched more than one row!`);
@@ -6581,7 +6882,7 @@ var generateUpdate3 = (db, tableName, table, tables, relationMap, setArgs, filte
6581
6882
  args: queryArgs
6582
6883
  };
6583
6884
  };
6584
- var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx, txCtx, nested, limits) => {
6885
+ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateManyInput, fieldName, typeName, typeNameMapper, filterCtx, txCtx, nested, limits, policies) => {
6585
6886
  const queryArgs = {
6586
6887
  updates: {
6587
6888
  type: new import_graphql10.GraphQLNonNull(new import_graphql10.GraphQLList(new import_graphql10.GraphQLNonNull(updateManyInput)))
@@ -6597,6 +6898,8 @@ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateMany
6597
6898
  if (!updates.length) {
6598
6899
  throw new import_graphql10.GraphQLError("No updates were provided!");
6599
6900
  }
6901
+ const scope = policies?.scope?.(context);
6902
+ const contextColumns = policies?.contextValues?.(tableName);
6600
6903
  const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
6601
6904
  deep: true
6602
6905
  });
@@ -6609,19 +6912,28 @@ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateMany
6609
6912
  table,
6610
6913
  pkNames,
6611
6914
  parsedInfo,
6612
- limits
6915
+ limits,
6916
+ scope
6613
6917
  });
6614
6918
  const entries = updates.map(({ where, set }) => {
6615
6919
  const split = nested?.enabled(tableName) ? nested.split(tableName, set) : void 0;
6616
6920
  const ops = split && nested.hasOps(split.ops) ? split.ops : void 0;
6617
- const input = remapUpdateInput(split ? split.columns : set, table, tableName);
6921
+ const input = stripContextValues(
6922
+ remapUpdateInput(split ? split.columns : set, table, tableName),
6923
+ contextColumns
6924
+ );
6618
6925
  if (!Object.keys(input).length && !ops) {
6619
6926
  throw new import_graphql10.GraphQLError("Unable to update with no values specified!");
6620
6927
  }
6621
6928
  return {
6622
6929
  set: input,
6623
6930
  ops,
6624
- filters: where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
6931
+ filters: withScope(
6932
+ scope,
6933
+ tableName,
6934
+ table,
6935
+ where ? extractFilters(table, tableName, where, relationFilterCtx(filterCtx, tableName)) : void 0
6936
+ )
6625
6937
  };
6626
6938
  });
6627
6939
  const anyNested = entries.some((entry) => entry.ops);
@@ -6639,7 +6951,7 @@ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateMany
6639
6951
  return query.returning(returning).all();
6640
6952
  };
6641
6953
  const runNestedEntry = async (tx, entry) => {
6642
- const values = entry.ops ? { ...entry.set, ...await nested.applyParentSide(tx, tableName, entry.ops) } : entry.set;
6954
+ const values = entry.ops ? { ...entry.set, ...await nested.applyParentSide(tx, tableName, entry.ops, context) } : entry.set;
6643
6955
  const writes = Object.keys(values).length > 0;
6644
6956
  let query = writes ? tx.update(table).set(values) : tx.select(returning).from(table);
6645
6957
  if (entry.filters) {
@@ -6647,7 +6959,7 @@ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateMany
6647
6959
  }
6648
6960
  const rows = await (writes ? query.returning(returning) : query);
6649
6961
  if (entry.ops) {
6650
- await nested.applyChildSide(tx, tableName, entry.ops, rows);
6962
+ await nested.applyChildSide(tx, tableName, entry.ops, rows, context);
6651
6963
  }
6652
6964
  return rows;
6653
6965
  };
@@ -6700,7 +7012,7 @@ var generateUpdateMany3 = (db, tableName, table, tables, relationMap, updateMany
6700
7012
  args: queryArgs
6701
7013
  };
6702
7014
  };
6703
- var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx, txCtx) => {
7015
+ var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, single, requireWhere, filterCtx, selectionCtx, txCtx, policies) => {
6704
7016
  const queryArgs = {
6705
7017
  where: {
6706
7018
  type: single || requireWhere ? new import_graphql10.GraphQLNonNull(filterArgs) : filterArgs
@@ -6712,6 +7024,7 @@ var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, si
6712
7024
  try {
6713
7025
  return await runMutation(db, context, info, txCtx, async (executor) => {
6714
7026
  const { where } = args;
7027
+ const scope = policies?.scope?.(context);
6715
7028
  const parsedInfo = (0, import_graphql_parse_resolve_info4.parseResolveInfo)(info, {
6716
7029
  deep: true
6717
7030
  });
@@ -6721,7 +7034,12 @@ var generateDelete3 = (db, tableName, table, filterArgs, fieldName, typeName, si
6721
7034
  selectionCtx
6722
7035
  );
6723
7036
  const relationCtx = relationFilterCtx(filterCtx, tableName);
6724
- const filters = single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0;
7037
+ const filters = withScope(
7038
+ scope,
7039
+ tableName,
7040
+ table,
7041
+ single || requireWhere ? extractRequiredFilters(table, tableName, where, fieldName, relationCtx) : where ? extractFilters(table, tableName, where, relationCtx) : void 0
7042
+ );
6725
7043
  if (single) {
6726
7044
  await assertSingleMatch(executor, table, filters, fieldName);
6727
7045
  }
@@ -6780,12 +7098,16 @@ var generateSchemaData3 = (db, schema, relations, options) => {
6780
7098
  attachTargetPrimaryKeys(namedRelations, tables, sqlitePrimaryKeyPropNames);
6781
7099
  const eagerRelations = pruneNonEagerRelations(namedRelations, shouldEagerLoad);
6782
7100
  const filterCtx = { tables, relationMap: namedRelations };
7101
+ const scopes = options.policies?.scope;
7102
+ const contextValuesOf = options.policies?.contextValues;
7103
+ const policies = bindPolicies(options.policies, filterCtx);
6783
7104
  const resolverFactory = createRelationResolverFactory(
6784
7105
  db,
6785
7106
  tables,
6786
7107
  "nulls-smallest",
6787
7108
  filterCtx,
6788
- limits
7109
+ limits,
7110
+ scopes
6789
7111
  );
6790
7112
  const cacheCtx = {
6791
7113
  genericFilterCache: /* @__PURE__ */ new Map(),
@@ -6803,6 +7125,7 @@ var generateSchemaData3 = (db, schema, relations, options) => {
6803
7125
  limits,
6804
7126
  docs: options.docs ?? {},
6805
7127
  primaryKeyOf: (name) => tables[name] ? sqlitePrimaryKeyPropNames(tables[name]) : [],
7128
+ contextValuesOf,
6806
7129
  featureOf
6807
7130
  };
6808
7131
  if (features.nestedWrites && db.resultKind === "sync") {
@@ -6818,8 +7141,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
6818
7141
  extractRelationJoinColumns
6819
7142
  ) : void 0;
6820
7143
  const nestedTypes = nestedPlans ? createNestedWriteTypes({ plans: nestedPlans, cacheCtx, typeNameMapper, insertPrefix: prefixes.insert }) : void 0;
6821
- const nestedRuntime = nestedPlans ? createNestedWriteRuntime({ plans: nestedPlans, filterCtx }) : void 0;
6822
- const relationAggregateFactory = anyTable("relationAggregates") ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx) : void 0;
7144
+ const nestedRuntime = nestedPlans ? createNestedWriteRuntime({ plans: nestedPlans, filterCtx, scopes, contextValues: contextValuesOf }) : void 0;
7145
+ const relationAggregateFactory = anyTable("relationAggregates") ? createRelationAggregateFactory(db, tables, cacheCtx, typeNameMapper, filterCtx, scopes) : void 0;
6823
7146
  const queries = {};
6824
7147
  const mutations = {};
6825
7148
  if (options.transactions && db.resultKind === "sync") {
@@ -6884,7 +7207,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
6884
7207
  typeNameMapper,
6885
7208
  filterCtx,
6886
7209
  tableFeatures.distinct,
6887
- limits
7210
+ limits,
7211
+ policies
6888
7212
  );
6889
7213
  const selectSingleGenerated = generateSelectSingle3(
6890
7214
  db,
@@ -6897,7 +7221,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
6897
7221
  typeName,
6898
7222
  typeNameMapper,
6899
7223
  filterCtx,
6900
- limits
7224
+ limits,
7225
+ policies
6901
7226
  );
6902
7227
  const insertArrGenerated = tableFeatures.insert ? generateInsertArray3(
6903
7228
  db,
@@ -6912,7 +7237,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
6912
7237
  conflictDoNothing,
6913
7238
  mutationTxCtx,
6914
7239
  nestedRuntime,
6915
- limits
7240
+ limits,
7241
+ policies
6916
7242
  ) : void 0;
6917
7243
  const insertSingleGenerated = tableFeatures.insert ? generateInsertSingle3(
6918
7244
  db,
@@ -6927,7 +7253,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
6927
7253
  conflictDoNothing,
6928
7254
  mutationTxCtx,
6929
7255
  nestedRuntime,
6930
- limits
7256
+ limits,
7257
+ policies
6931
7258
  ) : void 0;
6932
7259
  const uniqueSets = tableFeatures.upsert ? getUniqueColumnSets(schema[tableName], import_sqlite_core2.getTableConfig) : [];
6933
7260
  const onConflictInput = tableFeatures.upsert ? generateOnConflictInput({
@@ -6953,7 +7280,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
6953
7280
  filterCtx,
6954
7281
  mutationTxCtx,
6955
7282
  nestedRuntime,
6956
- limits
7283
+ limits,
7284
+ policies
6957
7285
  ) : void 0;
6958
7286
  const upsertSingleGenerated = onConflictInput ? generateUpsert3(
6959
7287
  db,
@@ -6971,7 +7299,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
6971
7299
  filterCtx,
6972
7300
  mutationTxCtx,
6973
7301
  nestedRuntime,
6974
- limits
7302
+ limits,
7303
+ policies
6975
7304
  ) : void 0;
6976
7305
  const updateGenerated = tableFeatures.update ? generateUpdate3(
6977
7306
  db,
@@ -6989,7 +7318,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
6989
7318
  filterCtx,
6990
7319
  mutationTxCtx,
6991
7320
  nestedRuntime,
6992
- limits
7321
+ limits,
7322
+ policies
6993
7323
  ) : void 0;
6994
7324
  const updateSingleGenerated = tableFeatures.update ? generateUpdate3(
6995
7325
  db,
@@ -7007,7 +7337,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
7007
7337
  filterCtx,
7008
7338
  mutationTxCtx,
7009
7339
  nestedRuntime,
7010
- limits
7340
+ limits,
7341
+ policies
7011
7342
  ) : void 0;
7012
7343
  const updateManyInput = tableFeatures.update && tableFeatures.updateMany ? generateUpdateManyInput({ typeName, updatePrefix: prefixes.update, updateInput, tableFilters }) : void 0;
7013
7344
  const updateManyGenerated = updateManyInput ? generateUpdateMany3(
@@ -7023,7 +7354,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
7023
7354
  filterCtx,
7024
7355
  mutationTxCtx,
7025
7356
  nestedRuntime,
7026
- limits
7357
+ limits,
7358
+ policies
7027
7359
  ) : void 0;
7028
7360
  const deleteGenerated = tableFeatures.delete ? generateDelete3(
7029
7361
  db,
@@ -7036,7 +7368,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
7036
7368
  tableFeatures.requireWhere,
7037
7369
  filterCtx,
7038
7370
  { tableName, relationMap: namedRelations, tables },
7039
- mutationTxCtx
7371
+ mutationTxCtx,
7372
+ policies
7040
7373
  ) : void 0;
7041
7374
  const deleteSingleGenerated = tableFeatures.delete ? generateDelete3(
7042
7375
  db,
@@ -7049,7 +7382,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
7049
7382
  tableFeatures.requireWhere,
7050
7383
  filterCtx,
7051
7384
  { tableName, relationMap: namedRelations, tables },
7052
- mutationTxCtx
7385
+ mutationTxCtx,
7386
+ policies
7053
7387
  ) : void 0;
7054
7388
  const updateCountGenerated = tableFeatures.update && tableFeatures.countMutations ? generateWriteCount({
7055
7389
  db,
@@ -7083,7 +7417,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
7083
7417
  typeName,
7084
7418
  aggregateFieldName,
7085
7419
  tableFilters,
7086
- filterCtx
7420
+ filterCtx,
7421
+ scopes
7087
7422
  ) : void 0;
7088
7423
  const groupByType = tableFeatures.aggregates && tableFeatures.groupBy ? generateGroupByType(schema[tableName], tableName, typeName, cacheCtx) : void 0;
7089
7424
  const groupByEnum = groupByType ? generateGroupByEnum(schema[tableName], tableName, typeName) : void 0;
@@ -7097,7 +7432,8 @@ var generateSchemaData3 = (db, schema, relations, options) => {
7097
7432
  tableFilters,
7098
7433
  groupByEnum,
7099
7434
  havingInput,
7100
- filterCtx
7435
+ filterCtx,
7436
+ scopes
7101
7437
  ) : void 0;
7102
7438
  queries[selectArrGenerated.name] = {
7103
7439
  type: selectArrOutput,
@@ -7489,6 +7825,43 @@ var buildSchema = (db, config) => {
7489
7825
  limitPolicyCache.set(tableName, resolved);
7490
7826
  return resolved;
7491
7827
  } : void 0;
7828
+ const scopeConfig = config?.scope;
7829
+ const contextValuesConfig = config?.contextValues;
7830
+ if (scopeConfig || contextValuesConfig) {
7831
+ const tableNames = new Set(tableKeys);
7832
+ for (const [tableName, hook] of Object.entries(scopeConfig ?? {})) {
7833
+ if (!tableNames.has(tableName)) {
7834
+ throw new Error(
7835
+ `Drizzle-GraphQL Error: config.scope names '${tableName}', which is not a table in the Drizzle schema.`
7836
+ );
7837
+ }
7838
+ if (typeof hook !== "function") {
7839
+ throw new Error(`Drizzle-GraphQL Error: config.scope.${tableName} must be a function.`);
7840
+ }
7841
+ }
7842
+ for (const [tableName, columnHooks] of Object.entries(contextValuesConfig ?? {})) {
7843
+ if (!tableNames.has(tableName)) {
7844
+ throw new Error(
7845
+ `Drizzle-GraphQL Error: config.contextValues names '${tableName}', which is not a table in the Drizzle schema.`
7846
+ );
7847
+ }
7848
+ const columns = (0, import_drizzle_orm11.getColumns)(schema[tableName]);
7849
+ for (const [columnName, hook] of Object.entries(columnHooks)) {
7850
+ if (!columns[columnName]) {
7851
+ throw new Error(
7852
+ `Drizzle-GraphQL Error: config.contextValues names '${tableName}.${columnName}', which is not a column of that table.`
7853
+ );
7854
+ }
7855
+ if (typeof hook !== "function") {
7856
+ throw new Error(`Drizzle-GraphQL Error: config.contextValues.${tableName}.${columnName} must be a function.`);
7857
+ }
7858
+ }
7859
+ }
7860
+ }
7861
+ const policies = scopeConfig || contextValuesConfig ? {
7862
+ scope: scopeConfig ? (tableName) => scopeConfig[tableName] : void 0,
7863
+ contextValues: contextValuesConfig ? (tableName) => contextValuesConfig[tableName] : void 0
7864
+ } : void 0;
7492
7865
  const exclude = config?.exclude;
7493
7866
  if (exclude) {
7494
7867
  const tableNames = new Set(tableKeys);
@@ -7606,6 +7979,7 @@ var buildSchema = (db, config) => {
7606
7979
  enumNameMapper: config?.enumNameMapper,
7607
7980
  transactions,
7608
7981
  limits,
7982
+ policies,
7609
7983
  exclude,
7610
7984
  docs: {
7611
7985
  describeColumn: config?.describeColumn,