@vllnt/convex-flags 0.1.0-canary.bf53746 → 0.1.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.
Files changed (56) hide show
  1. package/README.md +84 -25
  2. package/dist/client/index.d.ts +62 -16
  3. package/dist/client/index.d.ts.map +1 -1
  4. package/dist/client/index.js +54 -15
  5. package/dist/client/index.js.map +1 -1
  6. package/dist/client/types.d.ts +30 -4
  7. package/dist/client/types.d.ts.map +1 -1
  8. package/dist/component/_generated/api.d.ts +19 -1
  9. package/dist/component/_generated/api.d.ts.map +1 -1
  10. package/dist/component/_generated/api.js +16 -0
  11. package/dist/component/_generated/api.js.map +1 -1
  12. package/dist/component/_generated/component.d.ts +174 -24
  13. package/dist/component/_generated/component.d.ts.map +1 -1
  14. package/dist/component/_generated/server.d.ts +12 -0
  15. package/dist/component/_generated/server.d.ts.map +1 -1
  16. package/dist/component/_generated/server.js +2 -0
  17. package/dist/component/_generated/server.js.map +1 -1
  18. package/dist/component/convex.config.d.ts.map +1 -1
  19. package/dist/component/mutations.d.ts +90 -5
  20. package/dist/component/mutations.d.ts.map +1 -1
  21. package/dist/component/mutations.js +144 -10
  22. package/dist/component/mutations.js.map +1 -1
  23. package/dist/component/queries.d.ts +90 -13
  24. package/dist/component/queries.d.ts.map +1 -1
  25. package/dist/component/queries.js +58 -12
  26. package/dist/component/queries.js.map +1 -1
  27. package/dist/component/schema.d.ts +138 -4
  28. package/dist/component/schema.d.ts.map +1 -1
  29. package/dist/component/schema.js +7 -1
  30. package/dist/component/schema.js.map +1 -1
  31. package/dist/component/validators.d.ts +354 -14
  32. package/dist/component/validators.d.ts.map +1 -1
  33. package/dist/component/validators.js +57 -6
  34. package/dist/component/validators.js.map +1 -1
  35. package/dist/react/index.d.ts +29 -0
  36. package/dist/react/index.d.ts.map +1 -0
  37. package/dist/react/index.js +35 -0
  38. package/dist/react/index.js.map +1 -0
  39. package/dist/shared.d.ts +102 -4
  40. package/dist/shared.d.ts.map +1 -1
  41. package/dist/shared.js +126 -3
  42. package/dist/shared.js.map +1 -1
  43. package/package.json +36 -18
  44. package/src/client/index.ts +145 -20
  45. package/src/client/types.ts +48 -4
  46. package/src/component/_generated/api.ts +19 -1
  47. package/src/component/_generated/component.ts +259 -23
  48. package/src/component/_generated/server.ts +13 -0
  49. package/src/component/mutations.ts +160 -19
  50. package/src/component/queries.ts +74 -12
  51. package/src/component/schema.ts +7 -1
  52. package/src/component/validators.ts +87 -6
  53. package/src/react/index.test.ts +52 -0
  54. package/src/react/index.ts +69 -0
  55. package/src/shared.ts +235 -4
  56. package/src/status-backward-compat.test.ts +51 -0
@@ -1,4 +1,12 @@
1
- import type { EvalReason, FlagEvaluation } from "../shared.js";
1
+ import type {
2
+ EvalContext,
3
+ EvalReason,
4
+ FlagEvaluation,
5
+ Rollout,
6
+ Rule,
7
+ Variant,
8
+ VariantValue,
9
+ } from "../shared.js";
2
10
 
3
11
  /** A flag document as returned by the component queries. */
4
12
  export interface FlagDoc {
@@ -6,7 +14,11 @@ export interface FlagDoc {
6
14
  _creationTime: number;
7
15
  key: string;
8
16
  description?: string;
9
- value: boolean;
17
+ value: VariantValue;
18
+ variants?: Variant[];
19
+ rules?: Rule[];
20
+ rollout?: Rollout;
21
+ status: "active" | "archived";
10
22
  createdAt: number;
11
23
  updatedAt: number;
12
24
  }
@@ -14,8 +26,40 @@ export interface FlagDoc {
14
26
  /** A flag definition passed to {@link Flags.define}. */
15
27
  export interface FlagDefinition {
16
28
  key: string;
17
- value: boolean;
29
+ value: VariantValue;
18
30
  description?: string;
31
+ variants?: Variant[];
32
+ rules?: Rule[];
33
+ rollout?: Rollout;
19
34
  }
20
35
 
21
- export type { EvalReason, FlagEvaluation };
36
+ /**
37
+ * A partial patch passed to {@link Flags.update}. Only the supplied fields are
38
+ * changed; omitted fields are left untouched (unlike {@link FlagDefinition},
39
+ * which fully replaces the definition). The key is passed separately.
40
+ */
41
+ export interface FlagUpdate {
42
+ value?: VariantValue;
43
+ description?: string;
44
+ variants?: Variant[];
45
+ rules?: Rule[];
46
+ rollout?: Rollout;
47
+ }
48
+
49
+ /** Options for {@link Flags.evaluate} / {@link Flags.variant}. */
50
+ export interface EvalOptions {
51
+ /** Host-supplied targeting/bucketing context. */
52
+ context?: EvalContext;
53
+ /** Value to serve when the key is unknown (reason `default`). */
54
+ default?: VariantValue;
55
+ }
56
+
57
+ export type {
58
+ EvalContext,
59
+ EvalReason,
60
+ FlagEvaluation,
61
+ Rollout,
62
+ Rule,
63
+ Variant,
64
+ VariantValue,
65
+ };
@@ -10,6 +10,7 @@
10
10
 
11
11
  import type * as mutations from "../mutations.js";
12
12
  import type * as queries from "../queries.js";
13
+ import type * as validators from "../validators.js";
13
14
 
14
15
  import type {
15
16
  ApiFromModules,
@@ -21,16 +22,33 @@ import { anyApi, componentsGeneric } from "convex/server";
21
22
  const fullApi: ApiFromModules<{
22
23
  mutations: typeof mutations;
23
24
  queries: typeof queries;
25
+ validators: typeof validators;
24
26
  }> = anyApi as any;
25
27
 
28
+ /**
29
+ * A utility for referencing Convex functions in your app's public API.
30
+ *
31
+ * Usage:
32
+ * ```js
33
+ * const myFunctionReference = api.myModule.myFunction;
34
+ * ```
35
+ */
26
36
  export const api: FilterApi<
27
37
  typeof fullApi,
28
38
  FunctionReference<any, "public">
29
39
  > = anyApi as any;
30
40
 
41
+ /**
42
+ * A utility for referencing Convex functions in your app's internal API.
43
+ *
44
+ * Usage:
45
+ * ```js
46
+ * const myFunctionReference = internal.myModule.myFunction;
47
+ * ```
48
+ */
31
49
  export const internal: FilterApi<
32
50
  typeof fullApi,
33
51
  FunctionReference<any, "internal">
34
52
  > = anyApi as any;
35
53
 
36
- export const components = componentsGeneric();
54
+ export const components = componentsGeneric() as unknown as {};
@@ -10,34 +10,77 @@
10
10
 
11
11
  import type { FunctionReference } from "convex/server";
12
12
 
13
- type FlagDoc = {
14
- _id: string;
15
- _creationTime: number;
16
- key: string;
17
- description?: string;
18
- value: boolean;
19
- createdAt: number;
20
- updatedAt: number;
21
- };
22
-
23
- type FlagEvaluation = { value: boolean; reason: string };
24
-
25
13
  /**
26
14
  * A utility for referencing a Convex component's exposed API.
27
15
  *
28
- * Useful when expecting a parameter like `components.flags`.
16
+ * Useful when expecting a parameter like `components.myComponent`.
17
+ * Usage:
18
+ * ```ts
19
+ * async function myFunction(ctx: QueryCtx, component: ComponentApi) {
20
+ * return ctx.runQuery(component.someFile.someQuery, { ...args });
21
+ * }
22
+ * ```
29
23
  */
30
24
  export type ComponentApi<Name extends string | undefined = string | undefined> =
31
25
  {
32
26
  mutations: {
27
+ archive: FunctionReference<
28
+ "mutation",
29
+ "internal",
30
+ { key: string },
31
+ null,
32
+ Name
33
+ >;
34
+ clearOverride: FunctionReference<
35
+ "mutation",
36
+ "internal",
37
+ { key: string; subjectRef: string },
38
+ null,
39
+ Name
40
+ >;
33
41
  define: FunctionReference<
34
42
  "mutation",
35
43
  "internal",
36
- { key: string; value: boolean; description?: string },
44
+ {
45
+ description?: string;
46
+ key: string;
47
+ rollout?: {
48
+ by?: string;
49
+ splits: Array<{ value: boolean | string | number; weight: number }>;
50
+ };
51
+ rules?: Array<{
52
+ conditions: Array<{
53
+ attribute: string;
54
+ op:
55
+ | "eq"
56
+ | "neq"
57
+ | "in"
58
+ | "nin"
59
+ | "contains"
60
+ | "gt"
61
+ | "gte"
62
+ | "lt"
63
+ | "lte";
64
+ values: Array<boolean | string | number>;
65
+ }>;
66
+ rollout?: {
67
+ by?: string;
68
+ splits: Array<{
69
+ value: boolean | string | number;
70
+ weight: number;
71
+ }>;
72
+ };
73
+ value?: boolean | string | number;
74
+ }>;
75
+ value: boolean | string | number;
76
+ variants?: Array<{
77
+ label?: string;
78
+ value: boolean | string | number;
79
+ }>;
80
+ },
37
81
  string,
38
82
  Name
39
83
  >;
40
- enable: FunctionReference<"mutation", "internal", { key: string }, null, Name>;
41
84
  disable: FunctionReference<
42
85
  "mutation",
43
86
  "internal",
@@ -45,29 +88,222 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
45
88
  null,
46
89
  Name
47
90
  >;
48
- remove: FunctionReference<"mutation", "internal", { key: string }, null, Name>;
91
+ enable: FunctionReference<
92
+ "mutation",
93
+ "internal",
94
+ { key: string },
95
+ null,
96
+ Name
97
+ >;
98
+ remove: FunctionReference<
99
+ "mutation",
100
+ "internal",
101
+ { key: string },
102
+ null,
103
+ Name
104
+ >;
105
+ restore: FunctionReference<
106
+ "mutation",
107
+ "internal",
108
+ { key: string },
109
+ null,
110
+ Name
111
+ >;
112
+ setOverride: FunctionReference<
113
+ "mutation",
114
+ "internal",
115
+ { key: string; subjectRef: string; value: boolean | string | number },
116
+ null,
117
+ Name
118
+ >;
119
+ update: FunctionReference<
120
+ "mutation",
121
+ "internal",
122
+ {
123
+ description?: string;
124
+ key: string;
125
+ rollout?: {
126
+ by?: string;
127
+ splits: Array<{ value: boolean | string | number; weight: number }>;
128
+ };
129
+ rules?: Array<{
130
+ conditions: Array<{
131
+ attribute: string;
132
+ op:
133
+ | "eq"
134
+ | "neq"
135
+ | "in"
136
+ | "nin"
137
+ | "contains"
138
+ | "gt"
139
+ | "gte"
140
+ | "lt"
141
+ | "lte";
142
+ values: Array<boolean | string | number>;
143
+ }>;
144
+ rollout?: {
145
+ by?: string;
146
+ splits: Array<{
147
+ value: boolean | string | number;
148
+ weight: number;
149
+ }>;
150
+ };
151
+ value?: boolean | string | number;
152
+ }>;
153
+ value?: boolean | string | number;
154
+ variants?: Array<{
155
+ label?: string;
156
+ value: boolean | string | number;
157
+ }>;
158
+ },
159
+ null,
160
+ Name
161
+ >;
49
162
  };
50
163
  queries: {
51
- get: FunctionReference<
164
+ all: FunctionReference<
52
165
  "query",
53
166
  "internal",
54
- { key: string },
55
- FlagDoc | null,
167
+ {
168
+ context?: {
169
+ attributes?: Record<string, boolean | string | number>;
170
+ subjectRef?: string;
171
+ };
172
+ },
173
+ Record<
174
+ string,
175
+ {
176
+ reason:
177
+ | "flag"
178
+ | "rule"
179
+ | "rollout"
180
+ | "override"
181
+ | "disabled"
182
+ | "default"
183
+ | "unknown";
184
+ value: boolean | string | number;
185
+ }
186
+ >,
56
187
  Name
57
188
  >;
58
- list: FunctionReference<"query", "internal", {}, Array<FlagDoc>, Name>;
59
189
  evaluate: FunctionReference<
190
+ "query",
191
+ "internal",
192
+ {
193
+ context?: {
194
+ attributes?: Record<string, boolean | string | number>;
195
+ subjectRef?: string;
196
+ };
197
+ default?: boolean | string | number;
198
+ key: string;
199
+ },
200
+ {
201
+ reason:
202
+ | "flag"
203
+ | "rule"
204
+ | "rollout"
205
+ | "override"
206
+ | "disabled"
207
+ | "default"
208
+ | "unknown";
209
+ value: boolean | string | number;
210
+ },
211
+ Name
212
+ >;
213
+ get: FunctionReference<
60
214
  "query",
61
215
  "internal",
62
216
  { key: string },
63
- FlagEvaluation,
217
+ null | {
218
+ _creationTime: number;
219
+ _id: string;
220
+ createdAt: number;
221
+ description?: string;
222
+ key: string;
223
+ rollout?: {
224
+ by?: string;
225
+ splits: Array<{ value: boolean | string | number; weight: number }>;
226
+ };
227
+ rules?: Array<{
228
+ conditions: Array<{
229
+ attribute: string;
230
+ op:
231
+ | "eq"
232
+ | "neq"
233
+ | "in"
234
+ | "nin"
235
+ | "contains"
236
+ | "gt"
237
+ | "gte"
238
+ | "lt"
239
+ | "lte";
240
+ values: Array<boolean | string | number>;
241
+ }>;
242
+ rollout?: {
243
+ by?: string;
244
+ splits: Array<{
245
+ value: boolean | string | number;
246
+ weight: number;
247
+ }>;
248
+ };
249
+ value?: boolean | string | number;
250
+ }>;
251
+ status: "active" | "archived";
252
+ updatedAt: number;
253
+ value: boolean | string | number;
254
+ variants?: Array<{
255
+ label?: string;
256
+ value: boolean | string | number;
257
+ }>;
258
+ },
64
259
  Name
65
260
  >;
66
- all: FunctionReference<
261
+ list: FunctionReference<
67
262
  "query",
68
263
  "internal",
69
264
  {},
70
- Record<string, FlagEvaluation>,
265
+ Array<{
266
+ _creationTime: number;
267
+ _id: string;
268
+ createdAt: number;
269
+ description?: string;
270
+ key: string;
271
+ rollout?: {
272
+ by?: string;
273
+ splits: Array<{ value: boolean | string | number; weight: number }>;
274
+ };
275
+ rules?: Array<{
276
+ conditions: Array<{
277
+ attribute: string;
278
+ op:
279
+ | "eq"
280
+ | "neq"
281
+ | "in"
282
+ | "nin"
283
+ | "contains"
284
+ | "gt"
285
+ | "gte"
286
+ | "lt"
287
+ | "lte";
288
+ values: Array<boolean | string | number>;
289
+ }>;
290
+ rollout?: {
291
+ by?: string;
292
+ splits: Array<{
293
+ value: boolean | string | number;
294
+ weight: number;
295
+ }>;
296
+ };
297
+ value?: boolean | string | number;
298
+ }>;
299
+ status: "active" | "archived";
300
+ updatedAt: number;
301
+ value: boolean | string | number;
302
+ variants?: Array<{
303
+ label?: string;
304
+ value: boolean | string | number;
305
+ }>;
306
+ }>,
71
307
  Name
72
308
  >;
73
309
  };
@@ -30,6 +30,17 @@ import {
30
30
  } from "convex/server";
31
31
  import type { DataModel } from "./dataModel.js";
32
32
 
33
+ /**
34
+ * Typesafe environment variables.
35
+ *
36
+ * This includes platform-provided env vars and any variables declared in
37
+ * `convex.config.ts`.
38
+ */
39
+ type Env = {
40
+ readonly CONVEX_CLOUD_URL: string;
41
+ readonly CONVEX_SITE_URL: string;
42
+ };
43
+
33
44
  /**
34
45
  * Define a query in this Convex app's public API.
35
46
  *
@@ -106,6 +117,8 @@ export const internalAction: ActionBuilder<DataModel, "internal"> =
106
117
  * @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
107
118
  */
108
119
  export const httpAction: HttpActionBuilder = httpActionGeneric;
120
+ export const env: Env = (globalThis as unknown as { process: { env: Env } })
121
+ .process.env;
109
122
 
110
123
  /**
111
124
  * A set of services for use within Convex query functions.
@@ -2,32 +2,45 @@ import { v } from "convex/values";
2
2
  import { mutation } from "./_generated/server.js";
3
3
  import type { Doc } from "./_generated/dataModel.js";
4
4
  import type { MutationCtx } from "./_generated/server.js";
5
+ import { rollout, rule, variant, variantValue } from "./validators.js";
5
6
 
6
- async function findByKey(
7
- ctx: MutationCtx,
8
- key: string,
9
- ): Promise<Doc<"flags"> | null> {
7
+ async function findByKey(ctx: MutationCtx, key: string): Promise<Doc<"flags"> | null> {
10
8
  return await ctx.db
11
9
  .query("flags")
12
10
  .withIndex("by_key", (q) => q.eq("key", key))
13
11
  .unique();
14
12
  }
15
13
 
16
- /** Create a flag, or update its value/description if the key already exists. */
14
+ /** Throws the component's single code-tagged error. Returns `never` so callers
15
+ * narrow the flag to non-null after the guard. */
16
+ function flagNotFound(): never {
17
+ throw new Error("FLAG_NOT_FOUND");
18
+ }
19
+
20
+ /**
21
+ * Create a flag, or fully replace its definition if the key already exists
22
+ * (value, description, variants, rules, rollout). Status is preserved on update.
23
+ */
17
24
  export const define = mutation({
18
25
  args: {
19
26
  key: v.string(),
20
- value: v.boolean(),
27
+ value: variantValue,
21
28
  description: v.optional(v.string()),
29
+ variants: v.optional(v.array(variant)),
30
+ rules: v.optional(v.array(rule)),
31
+ rollout: v.optional(rollout),
22
32
  },
23
33
  returns: v.id("flags"),
24
34
  handler: async (ctx, args) => {
25
35
  const now = Date.now();
26
36
  const existing = await findByKey(ctx, args.key);
27
37
  if (existing !== null) {
28
- await ctx.db.patch(existing._id, {
38
+ await ctx.db.patch("flags", existing._id, {
29
39
  value: args.value,
30
40
  description: args.description,
41
+ variants: args.variants,
42
+ rules: args.rules,
43
+ rollout: args.rollout,
31
44
  updatedAt: now,
32
45
  });
33
46
  return existing._id;
@@ -36,25 +49,66 @@ export const define = mutation({
36
49
  key: args.key,
37
50
  value: args.value,
38
51
  description: args.description,
52
+ variants: args.variants,
53
+ rules: args.rules,
54
+ rollout: args.rollout,
55
+ status: "active",
39
56
  createdAt: now,
40
57
  updatedAt: now,
41
58
  });
42
59
  },
43
60
  });
44
61
 
45
- async function setValue(
46
- ctx: MutationCtx,
47
- key: string,
48
- value: boolean,
49
- ): Promise<void> {
62
+ /**
63
+ * Partially update a flag: patch ONLY the fields supplied (the opposite of
64
+ * `define`, which fully replaces). Omitted fields are left untouched, so
65
+ * re-sending one field never wipes rules/rollout/variants.
66
+ */
67
+ export const update = mutation({
68
+ args: {
69
+ key: v.string(),
70
+ value: v.optional(variantValue),
71
+ description: v.optional(v.string()),
72
+ variants: v.optional(v.array(variant)),
73
+ rules: v.optional(v.array(rule)),
74
+ rollout: v.optional(rollout),
75
+ },
76
+ returns: v.null(),
77
+ handler: async (ctx, args) => {
78
+ const flag = await findByKey(ctx, args.key);
79
+ if (flag === null) {
80
+ flagNotFound();
81
+ }
82
+ const patch: Partial<Doc<"flags">> = { updatedAt: Date.now() };
83
+ if (args.value !== undefined) {
84
+ patch.value = args.value;
85
+ }
86
+ if (args.description !== undefined) {
87
+ patch.description = args.description;
88
+ }
89
+ if (args.variants !== undefined) {
90
+ patch.variants = args.variants;
91
+ }
92
+ if (args.rules !== undefined) {
93
+ patch.rules = args.rules;
94
+ }
95
+ if (args.rollout !== undefined) {
96
+ patch.rollout = args.rollout;
97
+ }
98
+ await ctx.db.patch("flags", flag._id, patch);
99
+ return null;
100
+ },
101
+ });
102
+
103
+ async function setValue(ctx: MutationCtx, key: string, value: boolean): Promise<void> {
50
104
  const flag = await findByKey(ctx, key);
51
105
  if (flag === null) {
52
- throw new Error("FLAG_NOT_FOUND");
106
+ flagNotFound();
53
107
  }
54
- await ctx.db.patch(flag._id, { value, updatedAt: Date.now() });
108
+ await ctx.db.patch("flags", flag._id, { value, updatedAt: Date.now() });
55
109
  }
56
110
 
57
- /** Turn a flag on (set its value to `true`). */
111
+ /** Turn a boolean flag on (set its value to `true`). */
58
112
  export const enable = mutation({
59
113
  args: { key: v.string() },
60
114
  returns: v.null(),
@@ -64,7 +118,7 @@ export const enable = mutation({
64
118
  },
65
119
  });
66
120
 
67
- /** Turn a flag off (set its value to `false`). */
121
+ /** Turn a boolean flag off (set its value to `false`). */
68
122
  export const disable = mutation({
69
123
  args: { key: v.string() },
70
124
  returns: v.null(),
@@ -74,16 +128,103 @@ export const disable = mutation({
74
128
  },
75
129
  });
76
130
 
77
- /** Permanently delete a flag. */
131
+ async function setStatus(
132
+ ctx: MutationCtx,
133
+ key: string,
134
+ status: "active" | "archived",
135
+ ): Promise<void> {
136
+ const flag = await findByKey(ctx, key);
137
+ if (flag === null) {
138
+ flagNotFound();
139
+ }
140
+ await ctx.db.patch("flags", flag._id, { status, updatedAt: Date.now() });
141
+ }
142
+
143
+ /** Archive a flag: reversible retirement. Evaluation skips targeting and serves
144
+ * the flag's base value with reason `disabled`. */
145
+ export const archive = mutation({
146
+ args: { key: v.string() },
147
+ returns: v.null(),
148
+ handler: async (ctx, args) => {
149
+ await setStatus(ctx, args.key, "archived");
150
+ return null;
151
+ },
152
+ });
153
+
154
+ /** Restore an archived flag to active. */
155
+ export const restore = mutation({
156
+ args: { key: v.string() },
157
+ returns: v.null(),
158
+ handler: async (ctx, args) => {
159
+ await setStatus(ctx, args.key, "active");
160
+ return null;
161
+ },
162
+ });
163
+
164
+ /** Permanently delete a flag and any per-subject overrides it owns. */
78
165
  export const remove = mutation({
79
166
  args: { key: v.string() },
80
167
  returns: v.null(),
81
168
  handler: async (ctx, args) => {
82
169
  const flag = await findByKey(ctx, args.key);
83
170
  if (flag === null) {
84
- throw new Error("FLAG_NOT_FOUND");
171
+ flagNotFound();
172
+ }
173
+ const overrides = await ctx.db
174
+ .query("overrides")
175
+ .withIndex("by_key_subject", (q) => q.eq("key", args.key))
176
+ .collect();
177
+ for (const override of overrides) {
178
+ await ctx.db.delete("overrides", override._id);
179
+ }
180
+ await ctx.db.delete("flags", flag._id);
181
+ return null;
182
+ },
183
+ });
184
+
185
+ /** Force a flag's value for one subject. The flag must exist. */
186
+ export const setOverride = mutation({
187
+ args: { key: v.string(), subjectRef: v.string(), value: variantValue },
188
+ returns: v.null(),
189
+ handler: async (ctx, args) => {
190
+ const flag = await findByKey(ctx, args.key);
191
+ if (flag === null) {
192
+ flagNotFound();
193
+ }
194
+ const existing = await ctx.db
195
+ .query("overrides")
196
+ .withIndex("by_key_subject", (q) =>
197
+ q.eq("key", args.key).eq("subjectRef", args.subjectRef),
198
+ )
199
+ .unique();
200
+ if (existing !== null) {
201
+ await ctx.db.patch("overrides", existing._id, { value: args.value });
202
+ } else {
203
+ await ctx.db.insert("overrides", {
204
+ key: args.key,
205
+ subjectRef: args.subjectRef,
206
+ value: args.value,
207
+ createdAt: Date.now(),
208
+ });
209
+ }
210
+ return null;
211
+ },
212
+ });
213
+
214
+ /** Clear a subject's override for a flag. No-op if none exists. */
215
+ export const clearOverride = mutation({
216
+ args: { key: v.string(), subjectRef: v.string() },
217
+ returns: v.null(),
218
+ handler: async (ctx, args) => {
219
+ const existing = await ctx.db
220
+ .query("overrides")
221
+ .withIndex("by_key_subject", (q) =>
222
+ q.eq("key", args.key).eq("subjectRef", args.subjectRef),
223
+ )
224
+ .unique();
225
+ if (existing !== null) {
226
+ await ctx.db.delete("overrides", existing._id);
85
227
  }
86
- await ctx.db.delete(flag._id);
87
228
  return null;
88
229
  },
89
230
  });