@vllnt/convex-flags 0.1.0-canary.a2bb74e → 0.1.0-canary.b9ab77f
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/README.md +173 -46
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,24 +6,54 @@
|
|
|
6
6
|
|
|
7
7
|
# @vllnt/convex-flags
|
|
8
8
|
|
|
9
|
-
Feature flags as a Convex component
|
|
9
|
+
Feature flags as a **Convex component**. Boolean kill-switches evaluated on the
|
|
10
|
+
backend and streamed to every connected client in real time over Convex's reactive
|
|
11
|
+
queries. Flip a flag → clients update instantly. No redeploy, no polling, no
|
|
12
|
+
third-party flag SaaS.
|
|
13
|
+
|
|
14
|
+
> **Status (v0.1.0):** the shipped surface is the boolean core below. Sections
|
|
15
|
+
> tagged **[planned]** are the roadmap, not yet implemented.
|
|
10
16
|
|
|
11
17
|
```ts
|
|
12
|
-
|
|
13
|
-
await flags.
|
|
14
|
-
const enabled = await flags.isEnabled(ctx, "new-checkout"); // reactive in a query
|
|
18
|
+
// backend
|
|
19
|
+
const enabled = await flags.isEnabled(ctx, "new-checkout");
|
|
15
20
|
```
|
|
16
21
|
|
|
22
|
+
---
|
|
23
|
+
|
|
17
24
|
## Features
|
|
18
25
|
|
|
19
|
-
- **Boolean kill-switches** — backend-evaluated, reactive, streamed to clients
|
|
20
|
-
- **Data isolation** — definitions live in the component's own sandboxed
|
|
21
|
-
|
|
22
|
-
- **
|
|
23
|
-
- **
|
|
24
|
-
|
|
26
|
+
- **Boolean kill-switches** — backend-evaluated, reactive, streamed to clients
|
|
27
|
+
- **Data isolation** — flag definitions live in the component's own sandboxed
|
|
28
|
+
`flags` table; no name collisions, no blast radius into your schema
|
|
29
|
+
- **Reuse** — mount into any Convex backend with one `app.use()`
|
|
30
|
+
- **Real-time** — evaluation runs in a Convex query; flag changes propagate
|
|
31
|
+
through the same subscription mechanism as your app data
|
|
32
|
+
- **Dual-target** — runs unchanged on Convex Cloud and self-hosted
|
|
33
|
+
`convex-backend`; code never branches on the host
|
|
34
|
+
- **Auth-agnostic** — the component is domain- and auth-neutral; the host gates
|
|
35
|
+
every management call
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Architecture
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
src/
|
|
43
|
+
├── shared.ts # EVAL_REASON codes + FlagEvaluation type
|
|
44
|
+
├── test.ts # convex-test registration helper (./test)
|
|
45
|
+
├── client/
|
|
46
|
+
│ ├── types.ts # FlagDoc, FlagDefinition interfaces
|
|
47
|
+
│ └── index.ts # Flags client class — the consumer-facing API
|
|
48
|
+
└── component/
|
|
49
|
+
├── schema.ts # flags table (by_key index)
|
|
50
|
+
├── convex.config.ts # defineComponent("flags")
|
|
51
|
+
├── mutations.ts # define, enable, disable, remove
|
|
52
|
+
├── queries.ts # get, list, evaluate, all
|
|
53
|
+
└── validators.ts # flagFields, flagDoc, evaluation validators
|
|
54
|
+
```
|
|
25
55
|
|
|
26
|
-
|
|
56
|
+
---
|
|
27
57
|
|
|
28
58
|
## Installation
|
|
29
59
|
|
|
@@ -42,74 +72,169 @@ app.use(flags);
|
|
|
42
72
|
export default app;
|
|
43
73
|
```
|
|
44
74
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
## Usage
|
|
75
|
+
Instantiate the client once and re-export it:
|
|
48
76
|
|
|
49
77
|
```ts
|
|
50
|
-
// convex/flags.ts
|
|
78
|
+
// convex/flags.ts
|
|
51
79
|
import { Flags } from "@vllnt/convex-flags";
|
|
52
80
|
import { components } from "./_generated/api";
|
|
53
81
|
|
|
54
82
|
export const flags = new Flags(components.flags);
|
|
55
83
|
```
|
|
56
84
|
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## Usage
|
|
88
|
+
|
|
89
|
+
### Defining flags
|
|
90
|
+
|
|
91
|
+
Flags are data. Create them from your own authorized mutations or a seed script.
|
|
92
|
+
The host owns auth — gate the management methods behind your own access checks.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
await flags.define(ctx, {
|
|
96
|
+
key: "new-checkout",
|
|
97
|
+
value: false,
|
|
98
|
+
description: "Routes traffic to the rebuilt checkout flow",
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Enabling / disabling
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
await flags.enable(ctx, "new-checkout"); // set true
|
|
106
|
+
await flags.disable(ctx, "new-checkout"); // set false
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Evaluating flags
|
|
110
|
+
|
|
111
|
+
Call the client inside any query, mutation, or action:
|
|
112
|
+
|
|
57
113
|
```ts
|
|
58
|
-
// Flags are data — create them from your own authorized mutations (host owns auth),
|
|
59
|
-
// then evaluate inside any query, mutation, or action.
|
|
60
114
|
import { query } from "./_generated/server";
|
|
61
115
|
import { flags } from "./flags";
|
|
62
116
|
|
|
63
|
-
export const setup = async (ctx) => {
|
|
64
|
-
await flags.define(ctx, { key: "new-checkout", value: false, description: "Rebuilt checkout" });
|
|
65
|
-
await flags.enable(ctx, "new-checkout"); // or flags.disable(ctx, key)
|
|
66
|
-
};
|
|
67
|
-
|
|
68
117
|
export const checkout = query({
|
|
69
118
|
args: {},
|
|
70
|
-
handler: async (ctx) =>
|
|
71
|
-
(await flags.isEnabled(ctx, "new-checkout"))
|
|
72
|
-
|
|
73
|
-
|
|
119
|
+
handler: async (ctx) => {
|
|
120
|
+
if (await flags.isEnabled(ctx, "new-checkout")) {
|
|
121
|
+
return loadNewCheckout(ctx);
|
|
122
|
+
}
|
|
123
|
+
return loadLegacyCheckout(ctx);
|
|
124
|
+
},
|
|
74
125
|
});
|
|
75
126
|
```
|
|
76
127
|
|
|
128
|
+
### How evaluation works
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
evaluate(key)
|
|
132
|
+
│
|
|
133
|
+
▼
|
|
134
|
+
┌──────────────────────────────┐
|
|
135
|
+
│ flag exists & value is true? │── no ──▶ false (reason: "unknown" or "flag")
|
|
136
|
+
└──────────────┬───────────────┘
|
|
137
|
+
│ yes
|
|
138
|
+
▼
|
|
139
|
+
true (reason: "flag")
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Environment targeting [planned]
|
|
143
|
+
|
|
144
|
+
Per-environment rules (`development` / `staging` / `production`) keyed via
|
|
145
|
+
`CONVEX_FLAGS_ENV` are planned. The targeting environment is a logical concept
|
|
146
|
+
independent of where the backend runs — same code on Convex Cloud and self-hosted
|
|
147
|
+
`convex-backend`.
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
77
151
|
## API Reference
|
|
78
152
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
|
84
|
-
|
|
85
|
-
| `
|
|
86
|
-
| `
|
|
87
|
-
| `
|
|
88
|
-
| `
|
|
153
|
+
See [`docs/API.md`](docs/API.md) for the full reference.
|
|
154
|
+
|
|
155
|
+
### `Flags` — evaluation
|
|
156
|
+
|
|
157
|
+
| Method | Returns | Purpose |
|
|
158
|
+
|--------|---------|---------|
|
|
159
|
+
| `isEnabled(ctx, key)` | `boolean` | Boolean flag value; `false` for undefined keys |
|
|
160
|
+
| `evaluate(ctx, key)` | `FlagEvaluation` | Full result with `value` and `reason` |
|
|
161
|
+
| `get(ctx, key)` | `FlagDoc \| null` | Fetch a single flag definition |
|
|
162
|
+
| `list(ctx)` | `FlagDoc[]` | Every flag definition |
|
|
163
|
+
| `all(ctx)` | `Record<string, FlagEvaluation>` | Bootstrap payload for a reactive client |
|
|
164
|
+
|
|
165
|
+
### `Flags` — management (host gates access)
|
|
166
|
+
|
|
167
|
+
| Method | Purpose |
|
|
168
|
+
|--------|---------|
|
|
169
|
+
| `define(ctx, definition)` | Create or update a flag |
|
|
170
|
+
| `enable(ctx, key)` | Set flag value to `true` |
|
|
171
|
+
| `disable(ctx, key)` | Set flag value to `false` |
|
|
172
|
+
| `remove(ctx, key)` | Hard-delete a flag |
|
|
173
|
+
|
|
174
|
+
### [planned] Multivariate variants
|
|
175
|
+
|
|
176
|
+
`variant(ctx, key, context?)`, `get(ctx, key, context?)`,
|
|
177
|
+
`evaluate(ctx, key, context?)` with typed `EvalContext` — planned.
|
|
89
178
|
|
|
90
|
-
|
|
179
|
+
### [planned] Percentage rollouts and attribute targeting
|
|
91
180
|
|
|
92
|
-
|
|
181
|
+
`setRules(ctx, key, env, rules)` with `rollout`, `userIds`, `attributes` conditions — planned.
|
|
93
182
|
|
|
94
|
-
|
|
95
|
-
- Flag keys are opaque host-chosen strings; the component never interprets or validates their structure.
|
|
96
|
-
- Tables are sandboxed — the component never reads host or sibling tables.
|
|
183
|
+
### [planned] React hooks
|
|
97
184
|
|
|
98
|
-
|
|
185
|
+
`FlagsProvider`, `useFlag`, `useVariant`, `useFlagValue`, `useFlags` via
|
|
186
|
+
`@vllnt/convex-flags/react` — planned.
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
|
|
190
|
+
## Security Model
|
|
191
|
+
|
|
192
|
+
`@vllnt/convex-flags` is auth-agnostic. The component owns only its sandboxed
|
|
193
|
+
`flags` table and exposes all management mutations as internal Convex functions.
|
|
194
|
+
The host controls access.
|
|
195
|
+
|
|
196
|
+
- **Who may call `define` / `enable` / `disable` / `remove`:** only the host,
|
|
197
|
+
via `ctx.runMutation(components.flags.mutations.define, ...)`. Wrap these in
|
|
198
|
+
your own authorized mutations and apply your auth checks there.
|
|
199
|
+
- **Read (`get`, `list`, `evaluate`, `all`):** similarly internal; the host
|
|
200
|
+
exposes a public query only if it chooses to.
|
|
201
|
+
- **Opaque keys:** flag keys are host-chosen strings. The component stores and
|
|
202
|
+
returns them as-is; it never interprets or validates key structure.
|
|
203
|
+
- **No cross-table access:** the component never reads host or sibling tables.
|
|
204
|
+
|
|
205
|
+
---
|
|
99
206
|
|
|
100
207
|
## Testing
|
|
101
208
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
209
|
+
Backend logic is covered with `convex-test` (in-process, no running backend):
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
import { convexTest } from "convex-test";
|
|
213
|
+
import { test, expect } from "vitest";
|
|
214
|
+
import { api } from "./_generated/api";
|
|
215
|
+
import schema from "./schema";
|
|
216
|
+
import { registerFlags } from "@vllnt/convex-flags/test";
|
|
217
|
+
|
|
218
|
+
test("enabled flag returns true", async () => {
|
|
219
|
+
const t = convexTest(schema);
|
|
220
|
+
registerFlags(t, "flags");
|
|
221
|
+
|
|
222
|
+
await t.mutation(api.example.defineAndEnable, { key: "my-flag" });
|
|
223
|
+
expect(await t.query(api.example.isEnabled, { key: "my-flag" })).toBe(true);
|
|
224
|
+
});
|
|
105
225
|
```
|
|
106
226
|
|
|
107
|
-
|
|
227
|
+
Coverage is enforced at 100% for all included source files — see
|
|
228
|
+
`vitest.config.mts`.
|
|
229
|
+
|
|
230
|
+
---
|
|
108
231
|
|
|
109
232
|
## Contributing
|
|
110
233
|
|
|
111
234
|
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
112
235
|
|
|
236
|
+
---
|
|
237
|
+
|
|
113
238
|
## Author
|
|
114
239
|
|
|
115
240
|
Built by [bntvllnt](https://github.com/bntvllnt) · [bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
@@ -118,6 +243,8 @@ Part of the [@vllnt](https://github.com/vllnt) Convex component fleet — [vllnt
|
|
|
118
243
|
|
|
119
244
|
If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
|
|
120
245
|
|
|
246
|
+
---
|
|
247
|
+
|
|
121
248
|
## License
|
|
122
249
|
|
|
123
250
|
MIT
|
package/package.json
CHANGED