@vllnt/convex-events 0.1.0 → 0.1.1-canary.98c4488
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/CHANGELOG.md +85 -0
- package/README.md +150 -37
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.js +1 -1
- package/dist/component/crons.js +1 -1
- package/dist/component/crons.js.map +1 -1
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +16 -3
- package/dist/component/mutations.js.map +1 -1
- package/dist/component/queries.d.ts.map +1 -1
- package/dist/component/queries.js +9 -2
- package/dist/component/queries.js.map +1 -1
- package/dist/component/schema.js +1 -1
- package/dist/component/schema.js.map +1 -1
- package/docs/API.md +279 -0
- package/llms.txt +46 -0
- package/package.json +44 -24
- package/src/client/index.ts +1 -1
- package/src/component/crons.ts +1 -1
- package/src/component/mutations.ts +17 -3
- package/src/component/queries.ts +11 -3
- package/src/component/schema.ts +1 -1
- package/src/test.ts +2 -1
- package/src/react/index.test.tsx +0 -133
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented here. The format is based on
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to
|
|
5
|
+
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [0.1.1] - Unreleased
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- The published tarball now includes `docs/API.md`, `CHANGELOG.md` and `llms.txt` (with
|
|
12
|
+
`README.md`), so coding agents can read version-matched docs from
|
|
13
|
+
`node_modules/@vllnt/convex-events/`. `llms.txt` links to the shipped files relatively.
|
|
14
|
+
- `docs/API.md` documents the `./react` and `./test` entry points and the remaining exported
|
|
15
|
+
types and constants, and the guard order.
|
|
16
|
+
- `pnpm test:package` (run in CI) fails when a required file is missing from the tarball, a
|
|
17
|
+
repository-only file ships, or `llms.txt` links to a file the tarball lacks.
|
|
18
|
+
- README sections "For coding agents" and "Related packages".
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
|
|
22
|
+
- The published tarball no longer includes the React test file.
|
|
23
|
+
- `record` rejects an empty `subjectRef`, `type` or supplied `actorRef`, and `purge` an empty
|
|
24
|
+
`subjectRef`, with `ConvexError({ code: "INVALID_ARGUMENT" })`.
|
|
25
|
+
- `list` and `paginate` reject a non-finite `since` with `ConvexError({ code: "INVALID_SINCE" })`.
|
|
26
|
+
- `@vllnt/convex-events/test` now type-checks under `moduleResolution: nodenext`
|
|
27
|
+
(explicit `.js` relative imports, Vite client types referenced).
|
|
28
|
+
|
|
29
|
+
### Changed
|
|
30
|
+
|
|
31
|
+
- Upgrade dev tooling (vitest 5, vite 8.3, convex 1.48, convex-test 0.0.65, prettier 3.9.10) and
|
|
32
|
+
override the vulnerable transitive `handlebars` and `deepmerge-ts` releases.
|
|
33
|
+
- Adopt pnpm 10 with a release-age gate; releases are published only from CI with npm
|
|
34
|
+
trusted publishing and provenance.
|
|
35
|
+
- Document the component error codes and how to use `/test` in a consuming app.
|
|
36
|
+
- `package.json` `homepage` is now `https://oss.vllnt.com/packages/convex-events`, with more
|
|
37
|
+
`keywords`.
|
|
38
|
+
- The publish workflow publishes a canary (under `@canary` only, never `@latest`) on every push
|
|
39
|
+
to `main` that changes the package source, `package.json`, the lockfile or a shipped doc; a
|
|
40
|
+
repository variable no longer gates it.
|
|
41
|
+
|
|
42
|
+
## [0.1.0] - 2026-09-01
|
|
43
|
+
|
|
44
|
+
### Added
|
|
45
|
+
|
|
46
|
+
- First release of `@vllnt/convex-events`: append-only per-subject activity
|
|
47
|
+
ledger with `record` / `list` / `count` / `purge` and a typed-generic client.
|
|
48
|
+
- `paginate` — cursor pagination returning Convex's
|
|
49
|
+
`{ page, isDone, continueCursor }`, honoring the `type` / `since` filters; feeds
|
|
50
|
+
page past any single-call limit.
|
|
51
|
+
- Bounded `count` — scans at most `maxCount + 1` rows and returns
|
|
52
|
+
`{ count, isExact }` instead of loading the whole feed.
|
|
53
|
+
- Batched, self-rescheduling `purge` — drains arbitrarily large feeds without a
|
|
54
|
+
single huge transaction.
|
|
55
|
+
- Retention — `configure(retentionMs)` plus an internal idempotent daily cron
|
|
56
|
+
(`prune-expired-events` / `pruneExpired`) that prunes events past the window;
|
|
57
|
+
a no-op until configured.
|
|
58
|
+
- Client value guards — optional `allowedTypes`, `maxTypeLength`,
|
|
59
|
+
`maxMetadataBytes`, and a host `metadataValidator`, validated at the `record`
|
|
60
|
+
boundary and throwing a code-tagged `EventValidationError`.
|
|
61
|
+
|
|
62
|
+
### Changed
|
|
63
|
+
|
|
64
|
+
- `count` now returns `{ count, isExact }` (was a bare `number`).
|
|
65
|
+
- `purge` and `count` take an options object; `purge` returns the count deleted
|
|
66
|
+
in the first batch.
|
|
67
|
+
- `EventDoc.metadata` is now optional (`metadata?: TMeta`) — an event recorded
|
|
68
|
+
without metadata round-trips as absent, so the shape never lies to a host with
|
|
69
|
+
a non-nullable `TMeta`.
|
|
70
|
+
- Treat Convex `_generated` output as CLI-owned, exclude it from formatting, and expose a
|
|
71
|
+
dedicated codegen script.
|
|
72
|
+
- Require `convex@^1.45.0` and update `convex-test` to `^0.0.56`.
|
|
73
|
+
|
|
74
|
+
### Fixed
|
|
75
|
+
|
|
76
|
+
- Reject unsafe purge/prune batches, non-finite cutoffs, and negative or non-finite retention
|
|
77
|
+
windows before they can trigger scheduler loops or destructive retention.
|
|
78
|
+
- Bound and validate list/count controls so malformed values cannot produce invalid results or
|
|
79
|
+
caller-controlled read amplification.
|
|
80
|
+
- Add optional per-subject idempotency keys so ambiguous host retries replay the original event ID
|
|
81
|
+
instead of duplicating ledger entries.
|
|
82
|
+
- `metadataValidator` errors are now wrapped as `EventValidationError` with code
|
|
83
|
+
`EVENTS_METADATA_INVALID` (preserving the original message), making the error
|
|
84
|
+
contract real and catchable by consumers. Previously the host's plain `Error`
|
|
85
|
+
surfaced unwrapped, so `EVENTS_METADATA_INVALID` was never thrown.
|
package/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
<!-- Badges -->
|
|
2
|
+
|
|
2
3
|
[](https://www.convex.dev/components)
|
|
3
4
|
[](https://www.npmjs.com/package/@vllnt/convex-events)
|
|
4
5
|
[](https://github.com/vllnt/convex-events/actions/workflows/ci.yml)
|
|
@@ -6,7 +7,9 @@
|
|
|
6
7
|
|
|
7
8
|
# @vllnt/convex-events
|
|
8
9
|
|
|
9
|
-
Append-only per-subject activity feed and event ledger, as a Convex component —
|
|
10
|
+
Append-only per-subject activity feed and event ledger, as a Convex component —
|
|
11
|
+
record events against an opaque `subjectRef`, read them back newest-first as a
|
|
12
|
+
feed or history.
|
|
10
13
|
|
|
11
14
|
```ts
|
|
12
15
|
const events = new Events(components.events);
|
|
@@ -16,14 +19,23 @@ const feed = await events.list(ctx, subjectRef, { limit: 20 });
|
|
|
16
19
|
|
|
17
20
|
## Features
|
|
18
21
|
|
|
19
|
-
- **Append-only ledger** — `record` stamps `createdAt
|
|
20
|
-
|
|
21
|
-
- **
|
|
22
|
-
-
|
|
23
|
-
- **
|
|
24
|
-
|
|
25
|
-
- **
|
|
26
|
-
|
|
22
|
+
- **Append-only ledger** — `record` stamps `createdAt`; optional
|
|
23
|
+
`idempotencyKey` replays the retained event ID without inserting another row.
|
|
24
|
+
- **Per-subject feeds** — list / count keyed by an opaque `subjectRef`,
|
|
25
|
+
newest-first.
|
|
26
|
+
- **Cursor pagination** — `paginate` returns Convex's
|
|
27
|
+
`{ page, isDone, continueCursor }`.
|
|
28
|
+
- **Type filter & since filter** — narrow a feed (or count) to one `type`; `list`
|
|
29
|
+
and `paginate` can also keep only events with `createdAt >= since`
|
|
30
|
+
(still newest-first).
|
|
31
|
+
- **Bounded count** — scans at most `maxCount + 1` rows and reports
|
|
32
|
+
`{ count, isExact }`.
|
|
33
|
+
- **Batched purge + retention cron** — drains large feeds in self-rescheduling
|
|
34
|
+
batches; an idempotent daily cron prunes past `retentionMs`.
|
|
35
|
+
- **Typed metadata + host validator** — generic over the host's metadata type,
|
|
36
|
+
with optional client-boundary value guards.
|
|
37
|
+
- **Opaque refs** — `subjectRef` / `actorRef` are arbitrary host strings the
|
|
38
|
+
component never inspects.
|
|
27
39
|
|
|
28
40
|
## Installation
|
|
29
41
|
|
|
@@ -31,7 +43,17 @@ const feed = await events.list(ctx, subjectRef, { limit: 20 });
|
|
|
31
43
|
pnpm add @vllnt/convex-events
|
|
32
44
|
```
|
|
33
45
|
|
|
34
|
-
|
|
46
|
+
Node.js >=18; required peer: `convex@^1.45.0`. React >=18 is optional. The
|
|
47
|
+
default npm channel is stable; `@vllnt/convex-events@canary` is built from
|
|
48
|
+
`main`.
|
|
49
|
+
|
|
50
|
+
## For coding agents
|
|
51
|
+
|
|
52
|
+
The docs are bundled in the package at `node_modules/@vllnt/convex-events/docs/`
|
|
53
|
+
and match the installed version: [docs/API.md](docs/API.md) is the API
|
|
54
|
+
reference, and `README.md`, `CHANGELOG.md` and `llms.txt` sit at the package
|
|
55
|
+
root. The TypeScript source is in `node_modules/@vllnt/convex-events/src/`. Read
|
|
56
|
+
them before writing code.
|
|
35
57
|
|
|
36
58
|
## Usage
|
|
37
59
|
|
|
@@ -53,7 +75,9 @@ import { v } from "convex/values";
|
|
|
53
75
|
import { paginationOptsValidator } from "convex/server";
|
|
54
76
|
import { Events } from "@vllnt/convex-events";
|
|
55
77
|
|
|
56
|
-
interface Meta {
|
|
78
|
+
interface Meta {
|
|
79
|
+
note?: string;
|
|
80
|
+
}
|
|
57
81
|
|
|
58
82
|
const events = new Events<Meta>(components.events, {
|
|
59
83
|
allowedTypes: ["created", "updated", "deleted"], // optional value guard
|
|
@@ -61,7 +85,8 @@ const events = new Events<Meta>(components.events, {
|
|
|
61
85
|
|
|
62
86
|
export const log = mutation({
|
|
63
87
|
args: { userId: v.string(), type: v.string() },
|
|
64
|
-
handler: (ctx, { userId, type }) =>
|
|
88
|
+
handler: (ctx, { userId, type }) =>
|
|
89
|
+
events.record(ctx, userId, type, { actorRef: userId }),
|
|
65
90
|
});
|
|
66
91
|
|
|
67
92
|
export const feed = query({
|
|
@@ -71,46 +96,94 @@ export const feed = query({
|
|
|
71
96
|
|
|
72
97
|
export const feedPage = query({
|
|
73
98
|
args: { userId: v.string(), paginationOpts: paginationOptsValidator },
|
|
74
|
-
handler: (ctx, { userId, paginationOpts }) =>
|
|
99
|
+
handler: (ctx, { userId, paginationOpts }) =>
|
|
100
|
+
events.paginate(ctx, userId, paginationOpts),
|
|
75
101
|
});
|
|
76
102
|
```
|
|
77
103
|
|
|
104
|
+
These wrappers illustrate calls, not production auth: authenticate the caller
|
|
105
|
+
and authorize all subject/actor refs before reading or recording. After
|
|
106
|
+
mounting, run `pnpm convex dev` to generate the host's component references.
|
|
107
|
+
|
|
108
|
+
## Configuration and limits
|
|
109
|
+
|
|
110
|
+
Client defaults: `defaultLimit: 50`, `defaultMaxCount: 1000`. List/page sizes
|
|
111
|
+
must be integers in 1–1000; count bounds are integers in 0–1000
|
|
112
|
+
(`INVALID_LIMIT`). `allowedTypes`, `maxTypeLength`, `maxMetadataBytes` and
|
|
113
|
+
`metadataValidator` are opt-in guards; Convex platform limits still apply.
|
|
114
|
+
Retention is disabled until `configure` sets a non-negative finite
|
|
115
|
+
`retentionMs`; `undefined` clears it. Purge/prune batches default to 256 and
|
|
116
|
+
must be integers in 1–500.
|
|
117
|
+
|
|
118
|
+
`record(ctx, subjectRef, type, { actorRef?, metadata?, idempotencyKey? })`
|
|
119
|
+
returns the existing ID on a retry key shared by that subject, even if the new
|
|
120
|
+
payload or type differs. Keys must contain 1–256 UTF-16 code units. Purge or
|
|
121
|
+
retention deletion removes replay protection. Without a key, each call inserts.
|
|
122
|
+
This is an activity ledger, not durable event delivery; the host owns retries
|
|
123
|
+
and any downstream side effects.
|
|
124
|
+
|
|
125
|
+
## Multiple mounts
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
app.use(events, { name: "webEvents" });
|
|
129
|
+
app.use(events, { name: "gameEvents" });
|
|
130
|
+
// new Events(components.webEvents), new Events(components.gameEvents)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Each mount has independent feeds, retry keys, retention configuration and cron
|
|
134
|
+
work.
|
|
135
|
+
|
|
78
136
|
## API Reference
|
|
79
137
|
|
|
80
|
-
| Method
|
|
81
|
-
|
|
82
|
-
| `record(ctx, subjectRef, type, opts?)`
|
|
83
|
-
| `list(ctx, subjectRef, opts?)`
|
|
84
|
-
| `paginate(ctx, subjectRef, paginationOpts, opts?)` | query
|
|
85
|
-
| `count(ctx, subjectRef, opts?)`
|
|
86
|
-
| `purge(ctx, subjectRef, opts?)`
|
|
87
|
-
| `configure(ctx, retentionMs?)`
|
|
88
|
-
| `pruneExpired(ctx, opts?)`
|
|
138
|
+
| Method | Kind | Result |
|
|
139
|
+
| -------------------------------------------------- | -------- | ----------------------------------------- |
|
|
140
|
+
| `record(ctx, subjectRef, type, opts?)` | mutation | new or retained event id (`string`) |
|
|
141
|
+
| `list(ctx, subjectRef, opts?)` | query | `EventDoc<TMeta>[]` (newest-first) |
|
|
142
|
+
| `paginate(ctx, subjectRef, paginationOpts, opts?)` | query | `EventPage<TMeta>` |
|
|
143
|
+
| `count(ctx, subjectRef, opts?)` | query | `{ count, isExact }` |
|
|
144
|
+
| `purge(ctx, subjectRef, opts?)` | mutation | `number` (deleted in the first batch) |
|
|
145
|
+
| `configure(ctx, retentionMs?)` | mutation | `null` (sets/clears the retention window) |
|
|
146
|
+
| `pruneExpired(ctx, opts?)` | mutation | `number` (manual retention sweep) |
|
|
89
147
|
|
|
90
|
-
Full reference: [docs/API.md](docs/API.md) — including client options
|
|
148
|
+
Full reference: [docs/API.md](docs/API.md) — including client options
|
|
149
|
+
(`defaultLimit`, `allowedTypes`, `metadataValidator`, …) and
|
|
150
|
+
`EVENT_ERROR_CODES`.
|
|
91
151
|
|
|
92
152
|
## React
|
|
93
153
|
|
|
94
|
-
Optional, tree-shakeable hooks at `@vllnt/convex-events/react`; `react`
|
|
154
|
+
Optional, tree-shakeable hooks at `@vllnt/convex-events/react`; `react` is
|
|
155
|
+
optional; `convex` is required. Pass the host's own re-exported query refs — the
|
|
156
|
+
component never imports your `api`.
|
|
157
|
+
|
|
158
|
+
The host query refs below must accept `subjectRef` (and pagination options for
|
|
159
|
+
`paginate`); they are not the `userId` wrappers above.
|
|
95
160
|
|
|
96
161
|
```tsx
|
|
97
162
|
import { useActivityFeed, useEventCount } from "@vllnt/convex-events/react";
|
|
98
163
|
import { api } from "../convex/_generated/api";
|
|
99
164
|
|
|
100
|
-
const { results, status, loadMore } = useActivityFeed(
|
|
165
|
+
const { results, status, loadMore } = useActivityFeed(
|
|
166
|
+
api.events.paginate,
|
|
167
|
+
{ subjectRef },
|
|
168
|
+
{ initialNumItems: 20 },
|
|
169
|
+
);
|
|
101
170
|
const count = useEventCount(api.events.count, { subjectRef });
|
|
102
171
|
```
|
|
103
172
|
|
|
104
|
-
| Hook
|
|
105
|
-
|
|
106
|
-
| `useActivityFeed(paginateRef, { subjectRef, type? }, { initialNumItems })` | `usePaginatedQuery` | `{ results, status, loadMore }`
|
|
107
|
-
| `useEventCount(countRef, { subjectRef, type? })`
|
|
173
|
+
| Hook | Wraps | Returns |
|
|
174
|
+
| -------------------------------------------------------------------------- | ------------------- | --------------------------------- |
|
|
175
|
+
| `useActivityFeed(paginateRef, { subjectRef, type? }, { initialNumItems })` | `usePaginatedQuery` | `{ results, status, loadMore }` |
|
|
176
|
+
| `useEventCount(countRef, { subjectRef, type? })` | `useQuery` | `{ count, isExact } \| undefined` |
|
|
108
177
|
|
|
109
178
|
## Security
|
|
110
179
|
|
|
111
|
-
- Auth-agnostic — the host resolves identity, decides who may record or read a
|
|
112
|
-
|
|
113
|
-
-
|
|
180
|
+
- Auth-agnostic — the host resolves identity, decides who may record or read a
|
|
181
|
+
feed, and passes opaque refs.
|
|
182
|
+
- Tables are sandboxed; `subjectRef`, `actorRef`, and `metadata` are opaque and
|
|
183
|
+
never inspected.
|
|
184
|
+
- Optional client-boundary guards (`allowedTypes`, `maxTypeLength`,
|
|
185
|
+
`maxMetadataBytes`, `metadataValidator`) reject malformed input with a
|
|
186
|
+
code-tagged `EventValidationError`.
|
|
114
187
|
|
|
115
188
|
See [docs/API.md](docs/API.md).
|
|
116
189
|
|
|
@@ -121,20 +194,60 @@ pnpm test # single run
|
|
|
121
194
|
pnpm test:coverage # enforced 100% on covered files
|
|
122
195
|
```
|
|
123
196
|
|
|
124
|
-
Tests
|
|
197
|
+
Tests use the simulated `convex-test` backend (`@edge-runtime/vm`), not a real
|
|
198
|
+
Convex deployment; they do not prove production concurrency or scheduler
|
|
199
|
+
delivery.
|
|
200
|
+
|
|
201
|
+
### Testing your app
|
|
202
|
+
|
|
203
|
+
`@vllnt/convex-events/test` exports `register(t, name = "events")` to mount
|
|
204
|
+
the component in a `convex-test` instance. It needs `convex-test` and `vitest`
|
|
205
|
+
(with Vite's `import.meta.glob`) in the consuming app:
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
/// <reference types="vite/client" />
|
|
209
|
+
import { convexTest } from "convex-test";
|
|
210
|
+
import { register } from "@vllnt/convex-events/test";
|
|
211
|
+
import schema from "./schema";
|
|
212
|
+
|
|
213
|
+
const t = convexTest(schema, import.meta.glob("./**/*.ts"));
|
|
214
|
+
register(t as unknown as Parameters<typeof register>[0]);
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
In this version `register` types `t` against the component's own schema, so
|
|
218
|
+
TypeScript rejects a `t` built from a host schema that has tables. Cast it as
|
|
219
|
+
shown; the cast changes only the type, not what is registered.
|
|
220
|
+
|
|
221
|
+
## Related packages
|
|
222
|
+
|
|
223
|
+
- [`@vllnt/convex-notifications`](https://www.npmjs.com/package/@vllnt/convex-notifications)
|
|
224
|
+
— a per-subject inbox with read/unread state. Use it when someone must be told
|
|
225
|
+
and mark items read; use `convex-events` for the history, which has no read
|
|
226
|
+
state.
|
|
227
|
+
- [`@vllnt/convex-analytics`](https://www.npmjs.com/package/@vllnt/convex-analytics)
|
|
228
|
+
— events rolled up into funnels, retention and uniques. Use it for aggregate
|
|
229
|
+
product metrics; `convex-events` keeps individual rows per subject and only a
|
|
230
|
+
bounded count.
|
|
231
|
+
- [`@vllnt/convex-consent`](https://www.npmjs.com/package/@vllnt/convex-consent)
|
|
232
|
+
— an append-only consent ledger per subject and purpose. Use it when the
|
|
233
|
+
records are consent grants and withdrawals; `convex-events` is the generic
|
|
234
|
+
ledger.
|
|
125
235
|
|
|
126
236
|
## Contributing
|
|
127
237
|
|
|
128
|
-
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
238
|
+
See [CONTRIBUTING.md](https://github.com/vllnt/convex-events/blob/main/CONTRIBUTING.md).
|
|
129
239
|
|
|
130
240
|
## Author
|
|
131
241
|
|
|
132
|
-
Built by [bntvllnt](https://github.com/bntvllnt) ·
|
|
133
|
-
|
|
134
|
-
Part of the [@vllnt](https://github.com/vllnt) Convex component fleet — [vllnt.com](https://vllnt.com)
|
|
242
|
+
Built by [bntvllnt](https://github.com/bntvllnt) ·
|
|
243
|
+
[bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
135
244
|
|
|
136
245
|
If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
|
|
137
246
|
|
|
138
247
|
## License
|
|
139
248
|
|
|
140
249
|
MIT — see [LICENSE](LICENSE).
|
|
250
|
+
|
|
251
|
+
---
|
|
252
|
+
|
|
253
|
+
Part of vllnt oss: https://oss.vllnt.com
|
package/dist/client/index.d.ts
CHANGED
|
@@ -74,7 +74,7 @@ export declare class Events<TMeta = unknown> {
|
|
|
74
74
|
/**
|
|
75
75
|
* Append an event of `type` for `subjectRef`. Returns the new event id.
|
|
76
76
|
* Applies the configured boundary guards (`allowedTypes`, `maxTypeLength`,
|
|
77
|
-
* `maxMetadataBytes`,
|
|
77
|
+
* `metadataValidator`, `maxMetadataBytes`, in that order) before writing — throws an
|
|
78
78
|
* {@link EventValidationError} on rejection.
|
|
79
79
|
*/
|
|
80
80
|
record(ctx: RunMutationCtx, subjectRef: string, type: string, opts?: {
|
package/dist/client/index.js
CHANGED
|
@@ -63,7 +63,7 @@ export class Events {
|
|
|
63
63
|
/**
|
|
64
64
|
* Append an event of `type` for `subjectRef`. Returns the new event id.
|
|
65
65
|
* Applies the configured boundary guards (`allowedTypes`, `maxTypeLength`,
|
|
66
|
-
* `maxMetadataBytes`,
|
|
66
|
+
* `metadataValidator`, `maxMetadataBytes`, in that order) before writing — throws an
|
|
67
67
|
* {@link EventValidationError} on rejection.
|
|
68
68
|
*/
|
|
69
69
|
record(ctx, subjectRef, type, opts = {}) {
|
package/dist/component/crons.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { cronJobs } from "convex/server";
|
|
2
|
-
import { api } from "./_generated/api";
|
|
2
|
+
import { api } from "./_generated/api.js";
|
|
3
3
|
/**
|
|
4
4
|
* Component-internal retention sweep. Runs daily and prunes events older than
|
|
5
5
|
* the host-configured `retentionMs` (set via `configure`). Idempotent and a
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crons.js","sourceRoot":"","sources":["../../src/component/crons.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,GAAG,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"crons.js","sourceRoot":"","sources":["../../src/component/crons.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAE1C;;;;GAIG;AACH,MAAM,KAAK,GAAG,QAAQ,EAAE,CAAC;AAEzB,KAAK,CAAC,KAAK,CACT,sBAAsB,EACtB,EAAE,OAAO,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,EAC5B,GAAG,CAAC,SAAS,CAAC,YAAY,EAC1B,EAAE,CACH,CAAC;AAEF,eAAe,KAAK,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AA+BA,eAAO,MAAM,MAAM;;;;;;wDAuCjB,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,KAAK;;;;mBAkChB,CAAC;AAEH,mFAAmF;AACnF,eAAO,MAAM,SAAS;;iBAkBpB,CAAC;AAEH;;;;;GAKG;AACH,eAAO,MAAM,YAAY;;mBAwBvB,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
|
-
import { mutation } from "./_generated/server";
|
|
3
|
-
import { api } from "./_generated/api";
|
|
4
|
-
import { jsonValue } from "./validators";
|
|
2
|
+
import { mutation } from "./_generated/server.js";
|
|
3
|
+
import { api } from "./_generated/api.js";
|
|
4
|
+
import { jsonValue } from "./validators.js";
|
|
5
5
|
/** Default number of rows deleted per `purge` / prune batch before rescheduling. */
|
|
6
6
|
const DEFAULT_PURGE_BATCH = 256;
|
|
7
7
|
const MAX_PURGE_BATCH = 500;
|
|
@@ -14,6 +14,17 @@ function validateBatch(batch) {
|
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
+
/** Reject empty identifiers: an empty ref or type would store a meaningless ledger row. */
|
|
18
|
+
function assertNonEmpty(fields) {
|
|
19
|
+
for (const [name, value] of Object.entries(fields)) {
|
|
20
|
+
if (value === "") {
|
|
21
|
+
throw new ConvexError({
|
|
22
|
+
code: "INVALID_ARGUMENT",
|
|
23
|
+
message: `${name} must be non-empty`,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
17
28
|
export const record = mutation({
|
|
18
29
|
args: {
|
|
19
30
|
subjectRef: v.string(),
|
|
@@ -24,6 +35,7 @@ export const record = mutation({
|
|
|
24
35
|
},
|
|
25
36
|
returns: v.id("events"),
|
|
26
37
|
handler: async (ctx, args) => {
|
|
38
|
+
assertNonEmpty({ subjectRef: args.subjectRef, type: args.type, actorRef: args.actorRef });
|
|
27
39
|
if (args.idempotencyKey !== undefined &&
|
|
28
40
|
(args.idempotencyKey.length === 0 ||
|
|
29
41
|
args.idempotencyKey.length > MAX_IDEMPOTENCY_KEY_LENGTH)) {
|
|
@@ -63,6 +75,7 @@ export const purge = mutation({
|
|
|
63
75
|
},
|
|
64
76
|
returns: v.number(),
|
|
65
77
|
handler: async (ctx, args) => {
|
|
78
|
+
assertNonEmpty({ subjectRef: args.subjectRef });
|
|
66
79
|
const before = args.before;
|
|
67
80
|
const batch = args.batch ?? DEFAULT_PURGE_BATCH;
|
|
68
81
|
validateBatch(batch);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,oFAAoF;AACpF,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAChC,MAAM,eAAe,GAAG,GAAG,CAAC;AAC5B,MAAM,0BAA0B,GAAG,GAAG,CAAC;AAEvC,SAAS,aAA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S,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,SAAS,CAAC,YAAY,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACzE,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AA+BA,eAAO,MAAM,IAAI;;;;;;;;;;;;;;wBAkCf,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCxB,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,KAAK;;;;;;;GA2BhB,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
2
|
import { paginationOptsValidator } from "convex/server";
|
|
3
|
-
import { query } from "./_generated/server";
|
|
4
|
-
import { eventDoc, eventPage, countResult } from "./validators";
|
|
3
|
+
import { query } from "./_generated/server.js";
|
|
4
|
+
import { eventDoc, eventPage, countResult } from "./validators.js";
|
|
5
5
|
/** Default scan bound for a capped `count` before it reports `isExact: false`. */
|
|
6
6
|
const DEFAULT_MAX_COUNT = 1000;
|
|
7
7
|
const MAX_READ_LIMIT = 1000;
|
|
@@ -18,6 +18,11 @@ function validateReadLimit(value, name, allowZero = false) {
|
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
+
function validateSince(since) {
|
|
22
|
+
if (since !== undefined && !Number.isFinite(since)) {
|
|
23
|
+
throw new ConvexError({ code: "INVALID_SINCE", message: "since must be finite" });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
21
26
|
export const list = query({
|
|
22
27
|
args: {
|
|
23
28
|
subjectRef: v.string(),
|
|
@@ -28,6 +33,7 @@ export const list = query({
|
|
|
28
33
|
returns: v.array(eventDoc),
|
|
29
34
|
handler: async (ctx, args) => {
|
|
30
35
|
validateReadLimit(args.limit, "limit");
|
|
36
|
+
validateSince(args.since);
|
|
31
37
|
const since = args.since;
|
|
32
38
|
const type = args.type;
|
|
33
39
|
if (type === undefined) {
|
|
@@ -67,6 +73,7 @@ export const listPaginated = query({
|
|
|
67
73
|
returns: eventPage,
|
|
68
74
|
handler: async (ctx, args) => {
|
|
69
75
|
validateReadLimit(args.paginationOpts.numItems, "paginationOpts.numItems");
|
|
76
|
+
validateSince(args.since);
|
|
70
77
|
const since = args.since;
|
|
71
78
|
const type = args.type;
|
|
72
79
|
if (type === undefined) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAExD,OAAO,EAAE,KAAK,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAExD,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEnE,kFAAkF;AAClF,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAC/B,MAAM,cAAc,GAAG,IAAI,CAAC;AAE5B,SAAS,aAAa,CAAC,KAAoB;IACzC,MAAM,EAAE,cAAc,EAAE,eAAe,EAAE,GAAG,WAAW,EAAE,GAAG,KAAK,CAAC;IAClE,OAAO,WAAW,CAAC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ChB,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;iBACT,KAAK,CAAC,QAAQ,CAAC;iBACf,SAAS,CAAC,YAAY,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;iBACnE,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;YACvB,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE;iBACT,KAAK,CAAC,QAAQ,CAAC;iBACf,SAAS,CAAC,iBAAiB,EAAE,CAAC,CAAC,EAAE,EAAE,CAClC,CAAC,CAAC,EAAE,CAAC,YAAY,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,CAAC,CACrD;iBACA,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC;QAC5B,OAAO,IAAI,CAAC,MAAM,GAAG,QAAQ;YAC3B,CAAC,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE;YACrC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC5C,CAAC;CACF,CAAC,CAAC"}
|
package/dist/component/schema.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineSchema, defineTable } from "convex/server";
|
|
2
2
|
import { v } from "convex/values";
|
|
3
|
-
import { jsonValue } from "./validators";
|
|
3
|
+
import { jsonValue } from "./validators.js";
|
|
4
4
|
/**
|
|
5
5
|
* Sandboxed tables — the activity ledger's own concern. `subjectRef` and
|
|
6
6
|
* `actorRef` are opaque host-owned references (never assume their shape).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;;;GAIG;AACH,eAAe,YAAY,CAAC;IAC1B,MAAM,EAAE,WAAW,CAAC;QAClB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;QACtB,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;QAChB,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QAChC,cAAc,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QACtC,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC;QAC/B,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;KACtB,CAAC;SACC,KAAK,CAAC,YAAY,EAAE,CAAC,YAAY,EAAE,WAAW,CAAC,CAAC;SAChD,KAAK,CAAC,wBAAwB,EAAE,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC;SACjE,KAAK,CAAC,iBAAiB,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;SAC7D,KAAK,CAAC,cAAc,EAAE,CAAC,WAAW,CAAC,CAAC;IACvC;;;OAGG;IACH,MAAM,EAAE,WAAW,CAAC;QAClB,WAAW,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;KACpC,CAAC;CACH,CAAC,CAAC"}
|
package/docs/API.md
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# API Reference — @vllnt/convex-events
|
|
2
|
+
|
|
3
|
+
**Compatibility:** `convex@^1.45.0`; React >=18 is optional. This reference is
|
|
4
|
+
bundled in the package and describes the installed version.
|
|
5
|
+
|
|
6
|
+
Construct the client with the mounted component and optional config. The client
|
|
7
|
+
is generic over the host's metadata type `TMeta`:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Events } from "@vllnt/convex-events";
|
|
11
|
+
|
|
12
|
+
interface Meta {
|
|
13
|
+
note?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const events = new Events<Meta>(components.events, {
|
|
17
|
+
defaultLimit: 50, // page size when a `list` call omits `limit`
|
|
18
|
+
defaultMaxCount: 1000, // scan bound when a `count` call omits `maxCount`
|
|
19
|
+
allowedTypes: ["created", "updated"], // optional `type` allow-list
|
|
20
|
+
maxTypeLength: 64, // optional `type` length cap
|
|
21
|
+
maxMetadataBytes: 4096, // optional serialized-`metadata` byte cap
|
|
22
|
+
metadataValidator: (m) => m, // optional host validator/parser for `metadata`
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
All methods take the host `ctx` (a query or mutation context) as the first
|
|
27
|
+
argument. `subjectRef` and `actorRef` are opaque host strings — the component
|
|
28
|
+
never inspects them.
|
|
29
|
+
|
|
30
|
+
## Entry points
|
|
31
|
+
|
|
32
|
+
| Import | Exports |
|
|
33
|
+
| ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
34
|
+
| `@vllnt/convex-events` | `Events`, `EventValidationError`, `EVENT_ERROR_CODES`; types `EventsComponent`, `EventsOptions`, `EventDoc`, `EventPage`, `CountResult`, `MetadataValidator`, `EventErrorCode` |
|
|
35
|
+
| `@vllnt/convex-events/convex.config` | default export: the component definition, mounted with `app.use(events)` (or `app.use(events, { name })` for several mounts) |
|
|
36
|
+
| `@vllnt/convex-events/react` | `useActivityFeed`, `useEventCount`; types `ActivityFeedRef`, `EventCountRef`, `CountResult` |
|
|
37
|
+
| `@vllnt/convex-events/test` | `register` |
|
|
38
|
+
| `@vllnt/convex-events/_generated/component` | types only: `ComponentApi` |
|
|
39
|
+
|
|
40
|
+
`./convex.config.js` and `./_generated/component.js` are aliases of the rows
|
|
41
|
+
above, and `./package.json` is exported as-is.
|
|
42
|
+
|
|
43
|
+
## Mutations
|
|
44
|
+
|
|
45
|
+
### `record(ctx, subjectRef, type, opts?) → string`
|
|
46
|
+
|
|
47
|
+
Append an event of `type` for `subjectRef` and return the new or replayed event
|
|
48
|
+
id. The component stamps `createdAt` (`Date.now()`) on insert. Before writing,
|
|
49
|
+
the client applies its configured boundary guards (see **Guards** below) — on
|
|
50
|
+
rejection it throws an `EventValidationError`. `opts`:
|
|
51
|
+
|
|
52
|
+
- `actorRef?: string` — opaque ref for who caused the event. If supplied it must
|
|
53
|
+
be non-empty; `subjectRef` and `type` must be non-empty too, otherwise
|
|
54
|
+
`ConvexError` with `INVALID_ARGUMENT`.
|
|
55
|
+
- `idempotencyKey?: string` — opaque per-subject retry key (1–256 UTF-16 code
|
|
56
|
+
units); while the original event remains retained, a replay returns its ID
|
|
57
|
+
without inserting or rewriting another row. Purge and retention delete the
|
|
58
|
+
replay record together with the event. The key is not payload-bound: another
|
|
59
|
+
type or payload with the same subject/key replays the first ID. Invalid keys
|
|
60
|
+
throw `ConvexError` with `INVALID_IDEMPOTENCY_KEY`.
|
|
61
|
+
- `metadata?: TMeta` — host-owned payload. Omitted ⇒ the field is not stored, so
|
|
62
|
+
it reads back absent (`undefined`) rather than a fake `TMeta` value.
|
|
63
|
+
|
|
64
|
+
### `purge(ctx, subjectRef, opts?) → number`
|
|
65
|
+
|
|
66
|
+
Delete events for `subjectRef` and return how many were deleted **in the first
|
|
67
|
+
batch**. Deletion is batched and self-rescheduling: large feeds drain across
|
|
68
|
+
scheduled follow-up mutations rather than one huge transaction. `opts`:
|
|
69
|
+
|
|
70
|
+
- `before?: number` — only delete events with `createdAt < before` (a
|
|
71
|
+
timestamp). Omitted ⇒ delete every event for the subject.
|
|
72
|
+
- `batch?: number` — rows deleted per batch before rescheduling (default `256`,
|
|
73
|
+
integer 1–500; otherwise `INVALID_BATCH`). `before` must be finite
|
|
74
|
+
(`INVALID_BEFORE`). An empty `subjectRef` throws `INVALID_ARGUMENT`.
|
|
75
|
+
|
|
76
|
+
### `configure(ctx, retentionMs?) → null`
|
|
77
|
+
|
|
78
|
+
Set (or clear, by passing `undefined`) the singleton retention window the
|
|
79
|
+
component's internal daily cron prunes against. Without a configured window the
|
|
80
|
+
retention sweep is a no-op — nothing is ever auto-deleted. A supplied window
|
|
81
|
+
must be non-negative and finite (`INVALID_RETENTION`).
|
|
82
|
+
|
|
83
|
+
### `pruneExpired(ctx, opts?) → number`
|
|
84
|
+
|
|
85
|
+
Run the retention sweep manually (the same work the daily cron performs): delete
|
|
86
|
+
one batch of events older than the configured window, rescheduling follow-up
|
|
87
|
+
batches. Returns the count deleted in the first batch. A no-op until `configure`
|
|
88
|
+
sets a window. `opts`: `{ batch?: number }` (default `256`, integer 1–500;
|
|
89
|
+
checked only once a window is set, so an invalid `batch` is ignored before
|
|
90
|
+
then).
|
|
91
|
+
|
|
92
|
+
## Queries
|
|
93
|
+
|
|
94
|
+
### `list(ctx, subjectRef, opts?) → EventDoc<TMeta>[]`
|
|
95
|
+
|
|
96
|
+
The subject's events, newest-first. `opts`:
|
|
97
|
+
|
|
98
|
+
- `type?: string` — restrict to one event type (uses the `by_subject_type`
|
|
99
|
+
index).
|
|
100
|
+
- `since?: number` — only events with `createdAt >= since`. Must be finite
|
|
101
|
+
(`INVALID_SINCE`).
|
|
102
|
+
- `limit?: number` — page size; defaults to the client's `defaultLimit` (50).
|
|
103
|
+
Must be an integer in 1–1000 (`INVALID_LIMIT`).
|
|
104
|
+
|
|
105
|
+
### `paginate(ctx, subjectRef, paginationOpts, opts?) → EventPage<TMeta>`
|
|
106
|
+
|
|
107
|
+
Cursor-paginated feed, newest-first, honoring the same `type` / `since` filters
|
|
108
|
+
as `list`. `paginationOpts` is Convex's `{ cursor, numItems, ... }`
|
|
109
|
+
(`paginationOptsValidator`); `numItems` must be an integer in 1–1000
|
|
110
|
+
(`INVALID_LIMIT`). Returns Convex's pagination shape
|
|
111
|
+
`{ page, isDone, continueCursor }` (plus optional `splitCursor` / `pageStatus`),
|
|
112
|
+
so a host pages past any single-call `limit` by passing `continueCursor` back as
|
|
113
|
+
the next `cursor`.
|
|
114
|
+
|
|
115
|
+
### `count(ctx, subjectRef, opts?) → CountResult`
|
|
116
|
+
|
|
117
|
+
A bounded count for `subjectRef` (optionally one `type`). Scans at most
|
|
118
|
+
`maxCount + 1` rows — it never loads a whole feed. `opts`:
|
|
119
|
+
|
|
120
|
+
- `type?: string` — restrict to one event type.
|
|
121
|
+
- `maxCount?: number` — scan bound; defaults to the client's `defaultMaxCount`
|
|
122
|
+
(1000). Must be an integer in 0–1000 (`INVALID_LIMIT`).
|
|
123
|
+
|
|
124
|
+
Returns `{ count, isExact }`: `isExact` is `false` when the feed has more events
|
|
125
|
+
than the bound, in which case `count` equals the bound rather than the true
|
|
126
|
+
total.
|
|
127
|
+
|
|
128
|
+
## Component errors
|
|
129
|
+
|
|
130
|
+
Coded `ConvexError`s thrown by the component (`error.data.code`):
|
|
131
|
+
|
|
132
|
+
| Code | Thrown by | Meaning |
|
|
133
|
+
| ------------------------- | --------------------------- | ------------------------------------------------------- |
|
|
134
|
+
| `INVALID_ARGUMENT` | `record`, `purge` | `subjectRef`, `type` or a supplied `actorRef` is empty. |
|
|
135
|
+
| `INVALID_BATCH` | `purge`, `pruneExpired` | Batch must be an integer from 1-500. |
|
|
136
|
+
| `INVALID_BEFORE` | `purge` | The cutoff must be finite. |
|
|
137
|
+
| `INVALID_IDEMPOTENCY_KEY` | `record` | Key must be 1-256 UTF-16 code units. |
|
|
138
|
+
| `INVALID_LIMIT` | `list`, `paginate`, `count` | Limit, `numItems` or `maxCount` is out of range. |
|
|
139
|
+
| `INVALID_RETENTION` | `configure` | Retention must be non-negative and finite. |
|
|
140
|
+
| `INVALID_SINCE` | `list`, `paginate` | `since` must be finite. |
|
|
141
|
+
|
|
142
|
+
## Guards
|
|
143
|
+
|
|
144
|
+
When set on the client, these reject malformed input at the `record` boundary by
|
|
145
|
+
throwing an `EventValidationError` (carrying a stable `code`):
|
|
146
|
+
|
|
147
|
+
| Option | Rejects when | `code` |
|
|
148
|
+
| ------------------- | ------------------------------------- | --------------------------- |
|
|
149
|
+
| `allowedTypes` | `type` not in the allow-list | `EVENTS_TYPE_NOT_ALLOWED` |
|
|
150
|
+
| `maxTypeLength` | `type.length` exceeds the cap | `EVENTS_TYPE_TOO_LONG` |
|
|
151
|
+
| `maxMetadataBytes` | serialized `metadata` exceeds the cap | `EVENTS_METADATA_TOO_LARGE` |
|
|
152
|
+
| `metadataValidator` | the host validator throws | `EVENTS_METADATA_INVALID` |
|
|
153
|
+
|
|
154
|
+
`metadataValidator` may also **transform** the payload (e.g. trim fields); its
|
|
155
|
+
return value is what gets stored. When the validator throws an `Error`, its
|
|
156
|
+
message is preserved in the `EventValidationError`; any other thrown value gives
|
|
157
|
+
the message `metadata validation failed`.
|
|
158
|
+
|
|
159
|
+
Guards run in the order `allowedTypes`, `maxTypeLength`, `metadataValidator`,
|
|
160
|
+
`maxMetadataBytes`; the first failure throws. `metadataValidator` and
|
|
161
|
+
`maxMetadataBytes` run only when `metadata` is supplied, and the byte cap is
|
|
162
|
+
measured on the validator's return value as the UTF-8 byte length of
|
|
163
|
+
`JSON.stringify(metadata)` (a value `JSON.stringify` cannot serialize, such as a
|
|
164
|
+
`bigint`, throws its own `TypeError`).
|
|
165
|
+
|
|
166
|
+
## React
|
|
167
|
+
|
|
168
|
+
Optional entry `@vllnt/convex-events/react`; `react` is an optional peer. The
|
|
169
|
+
hooks take the host's own re-exported public query refs; the component never
|
|
170
|
+
imports the host `api`.
|
|
171
|
+
|
|
172
|
+
### `useActivityFeed(paginateRef, args, { initialNumItems })`
|
|
173
|
+
|
|
174
|
+
Wraps `usePaginatedQuery`. `paginateRef` is an `ActivityFeedRef`: a public query
|
|
175
|
+
taking `{ subjectRef, type?, paginationOpts }` and returning a Convex page.
|
|
176
|
+
`args` is `{ subjectRef, type? }`; the hook manages `paginationOpts`. Returns
|
|
177
|
+
the `usePaginatedQuery` result (`results`, `status`, `loadMore`, ...).
|
|
178
|
+
|
|
179
|
+
### `useEventCount(countRef, { subjectRef, type? }) → CountResult | undefined`
|
|
180
|
+
|
|
181
|
+
Wraps `useQuery`. `countRef` is an `EventCountRef`: a public query taking
|
|
182
|
+
`{ subjectRef, type? }` and returning `{ count, isExact }`. Returns `undefined`
|
|
183
|
+
while loading.
|
|
184
|
+
|
|
185
|
+
## Test helper
|
|
186
|
+
|
|
187
|
+
`register(t, name = "events")` from `@vllnt/convex-events/test` mounts the
|
|
188
|
+
component in a `convex-test` instance (`t.registerComponent`). The consuming app
|
|
189
|
+
needs `convex-test` and a Vite-based runner, because the helper uses
|
|
190
|
+
`import.meta.glob`.
|
|
191
|
+
|
|
192
|
+
In this version `register` types `t` against the component's own schema, so
|
|
193
|
+
TypeScript rejects a `t` built from a host schema that has tables. Cast it:
|
|
194
|
+
`register(t as unknown as Parameters<typeof register>[0])`; the cast changes only the type, not what is registered.
|
|
195
|
+
|
|
196
|
+
## Types
|
|
197
|
+
|
|
198
|
+
### `EventDoc<TMeta>`
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
interface EventDoc<TMeta = unknown> {
|
|
202
|
+
_id: string;
|
|
203
|
+
_creationTime: number;
|
|
204
|
+
subjectRef: string;
|
|
205
|
+
type: string;
|
|
206
|
+
actorRef?: string;
|
|
207
|
+
metadata?: TMeta; // omitted when the event was recorded without metadata
|
|
208
|
+
createdAt: number;
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
`metadata` is optional: an event recorded without it round-trips as absent, so
|
|
213
|
+
the shape never lies to a host whose `TMeta` is non-nullable.
|
|
214
|
+
|
|
215
|
+
### `EventPage<TMeta>`
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
interface EventPage<TMeta = unknown> {
|
|
219
|
+
page: EventDoc<TMeta>[];
|
|
220
|
+
isDone: boolean;
|
|
221
|
+
continueCursor: string;
|
|
222
|
+
splitCursor?: string | null;
|
|
223
|
+
pageStatus?: "SplitRecommended" | "SplitRequired" | null;
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### `CountResult`
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
interface CountResult {
|
|
231
|
+
count: number; // capped at the scan bound
|
|
232
|
+
isExact: boolean; // false when more events exist than the bound
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### `EventsOptions<TMeta>`
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
interface EventsOptions<TMeta = unknown> {
|
|
240
|
+
defaultLimit?: number; // default 50
|
|
241
|
+
defaultMaxCount?: number; // default 1000
|
|
242
|
+
allowedTypes?: readonly string[];
|
|
243
|
+
maxTypeLength?: number;
|
|
244
|
+
maxMetadataBytes?: number;
|
|
245
|
+
metadataValidator?: (metadata: TMeta) => TMeta;
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### `MetadataValidator<TMeta>`
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
type MetadataValidator<TMeta> = (metadata: TMeta) => TMeta;
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### `EventValidationError`
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
class EventValidationError extends Error {
|
|
259
|
+
readonly code: EventErrorCode; // one of EVENT_ERROR_CODES
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### `EVENT_ERROR_CODES` and `EventErrorCode`
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
const EVENT_ERROR_CODES = {
|
|
267
|
+
TYPE_NOT_ALLOWED: "EVENTS_TYPE_NOT_ALLOWED",
|
|
268
|
+
TYPE_TOO_LONG: "EVENTS_TYPE_TOO_LONG",
|
|
269
|
+
METADATA_TOO_LARGE: "EVENTS_METADATA_TOO_LARGE",
|
|
270
|
+
METADATA_INVALID: "EVENTS_METADATA_INVALID",
|
|
271
|
+
} as const;
|
|
272
|
+
type EventErrorCode =
|
|
273
|
+
(typeof EVENT_ERROR_CODES)[keyof typeof EVENT_ERROR_CODES];
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### `EventsComponent<TMeta>`
|
|
277
|
+
|
|
278
|
+
The function references the `Events` constructor needs: pass the host's
|
|
279
|
+
`components.events` (or the name given to `app.use`).
|
package/llms.txt
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @vllnt/convex-events
|
|
2
|
+
|
|
3
|
+
> Append-only per-subject activity feed and event ledger, as a Convex component
|
|
4
|
+
|
|
5
|
+
`@vllnt/convex-events` is a publishable Convex component. It targets `convex@^1.45.0`.
|
|
6
|
+
It stores two sandboxed tables - `events` (`subjectRef`, `type`, optional `actorRef`, optional `idempotencyKey`, opaque `metadata`, `createdAt`), indexed `by_subject`, `by_subject_idempotency`, `by_subject_type`, and `by_createdAt`; and a singleton `config` (`retentionMs?`). `record` appends an event (stamping `createdAt`); `list` reads a subject's feed newest-first (with optional `type` / `since` / `limit`), `paginate` returns Convex's cursor page `{ page, isDone, continueCursor }`, and `count` returns a bounded `{ count, isExact }` (scans at most `maxCount + 1` rows). `purge` drops a subject's events in self-rescheduling batches (all, or only those before a cutoff). `configure` sets a `retentionMs` window that an internal idempotent daily cron prunes against (`pruneExpired`). The client is generic over the host's metadata type and applies optional boundary guards (`allowedTypes`, `maxTypeLength`, `maxMetadataBytes`, `metadataValidator`) that throw a code-tagged `EventValidationError`. The host owns the subject, actor, auth, and meaning; the component owns only the ordered event store.
|
|
7
|
+
|
|
8
|
+
`record` accepts the option `idempotencyKey`
|
|
9
|
+
(1–256 UTF-16 code units): retained keys replay the first ID per subject, even
|
|
10
|
+
with a different payload/type. Purge and retention remove that protection.
|
|
11
|
+
Client defaults are list limit 50 and count bound 1000; list/page sizes must be
|
|
12
|
+
integers in 1–1000 and count bounds in 0–1000. Metadata/type guards are
|
|
13
|
+
opt-in. Purge/prune batches default to 256 (integer 1–500). Retention is off until
|
|
14
|
+
configured; its window must be non-negative and finite. Named mounts isolate
|
|
15
|
+
feeds and maintenance. React >=18 is optional; Convex is required.
|
|
16
|
+
|
|
17
|
+
## For coding agents
|
|
18
|
+
|
|
19
|
+
This package bundles its docs, and they match the installed version. Read them before writing
|
|
20
|
+
code: `node_modules/@vllnt/convex-events/docs/API.md` (API reference), with `README.md`,
|
|
21
|
+
`CHANGELOG.md` and this file at the package root. The TypeScript source is in
|
|
22
|
+
`node_modules/@vllnt/convex-events/src/`.
|
|
23
|
+
|
|
24
|
+
## Docs
|
|
25
|
+
|
|
26
|
+
- [README](./README.md): Project overview, installation, usage examples, architecture
|
|
27
|
+
- [API Reference](./docs/API.md): Public class methods, return shapes, input validation
|
|
28
|
+
- [Changelog](./CHANGELOG.md): Version history and release notes
|
|
29
|
+
|
|
30
|
+
## Examples
|
|
31
|
+
|
|
32
|
+
Repository files, not bundled in the package; they track `main`.
|
|
33
|
+
|
|
34
|
+
- [Example component wrapper](https://github.com/vllnt/convex-events/blob/main/example/convex/example.ts): Host-app wrapper functions
|
|
35
|
+
- [Example tests](https://github.com/vllnt/convex-events/blob/main/example/convex/example.test.ts): End-to-end usage coverage with `convex-test`
|
|
36
|
+
|
|
37
|
+
## Optional
|
|
38
|
+
|
|
39
|
+
- [Contributing](https://github.com/vllnt/convex-events/blob/main/CONTRIBUTING.md): Development setup, validation commands, release workflow (repository only)
|
|
40
|
+
- [Security Policy](https://github.com/vllnt/convex-events/blob/main/SECURITY.md): Vulnerability reporting policy (repository only)
|
|
41
|
+
- [Code of Conduct](https://github.com/vllnt/convex-events/blob/main/CODE_OF_CONDUCT.md): Community participation guidelines (repository only)
|
|
42
|
+
- [License](./LICENSE): MIT license
|
|
43
|
+
|
|
44
|
+
## Ecosystem
|
|
45
|
+
|
|
46
|
+
- [vllnt oss](https://oss.vllnt.com/llms.txt): Index of the other vllnt open-source packages
|
package/package.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/convex-events",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1-canary.98c4488",
|
|
4
4
|
"description": "Append-only per-subject activity feed and event ledger, as a Convex component",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"packageManager": "pnpm@
|
|
7
|
+
"packageManager": "pnpm@10.34.8",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist",
|
|
10
|
-
"src"
|
|
10
|
+
"src",
|
|
11
|
+
"docs/API.md",
|
|
12
|
+
"CHANGELOG.md",
|
|
13
|
+
"llms.txt",
|
|
14
|
+
"!src/**/*.test.*",
|
|
15
|
+
"!src/**/*.spec.*"
|
|
11
16
|
],
|
|
12
17
|
"exports": {
|
|
13
18
|
"./package.json": "./package.json",
|
|
@@ -41,17 +46,15 @@
|
|
|
41
46
|
"build": "tsc --project ./tsconfig.build.json",
|
|
42
47
|
"build:codegen": "pnpm codegen && pnpm build",
|
|
43
48
|
"build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
|
|
44
|
-
"typecheck": "tsc --noEmit",
|
|
45
|
-
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
46
|
-
"lint": "pnpm check:agents && eslint .",
|
|
49
|
+
"typecheck": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck.tsbuildinfo",
|
|
50
|
+
"typecheck:ci": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck-ci.tsbuildinfo --project tsconfig.ci.json",
|
|
51
|
+
"lint": "pnpm check:agents && eslint --cache --cache-location node_modules/.cache/eslint/ .",
|
|
47
52
|
"test": "vitest run --passWithNoTests",
|
|
48
53
|
"test:watch": "vitest --typecheck --clearScreen false",
|
|
49
54
|
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
55
|
+
"test:package": "node scripts/generate-llms.mjs && node scripts/test-package.mjs",
|
|
50
56
|
"generate:llms": "node scripts/generate-llms.mjs",
|
|
51
57
|
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
|
|
52
|
-
"prepublishOnly": "npm whoami || npm login",
|
|
53
|
-
"alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
|
|
54
|
-
"release": "npm version patch && npm publish && git push --follow-tags",
|
|
55
58
|
"check:agents": "node -e \"const f=require('node:fs');if(!f.existsSync('AGENTS.md')||f.existsSync('CLAUDE.md')||f.existsSync('.claude'))process.exit(1)\"",
|
|
56
59
|
"codegen": "pnpm convex codegen --component-dir ./src/component"
|
|
57
60
|
},
|
|
@@ -67,24 +70,24 @@
|
|
|
67
70
|
"devDependencies": {
|
|
68
71
|
"@edge-runtime/vm": "^5.0.0",
|
|
69
72
|
"@testing-library/react": "^16.3.3",
|
|
70
|
-
"@types/react": "^19.
|
|
71
|
-
"@types/react-dom": "^19.
|
|
73
|
+
"@types/react": "^19.3.0",
|
|
74
|
+
"@types/react-dom": "^19.3.0",
|
|
72
75
|
"@typescript/native": "npm:typescript@7.0.2",
|
|
73
|
-
"@vitest/coverage-v8": "^
|
|
74
|
-
"@vllnt/eslint-config": "2.
|
|
76
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
77
|
+
"@vllnt/eslint-config": "2.1.0",
|
|
75
78
|
"@vllnt/typescript": "2.0.0",
|
|
76
|
-
"convex": "^1.
|
|
77
|
-
"convex-test": "^0.0.
|
|
79
|
+
"convex": "^1.50.0",
|
|
80
|
+
"convex-test": "^0.0.71",
|
|
78
81
|
"eslint": "^9.39.5",
|
|
79
|
-
"jsdom": "^
|
|
80
|
-
"prettier": "^3.
|
|
81
|
-
"react": "^19.
|
|
82
|
-
"react-dom": "^19.
|
|
82
|
+
"jsdom": "^30.1.3",
|
|
83
|
+
"prettier": "^3.10.1",
|
|
84
|
+
"react": "^19.3.1",
|
|
85
|
+
"react-dom": "^19.3.1",
|
|
83
86
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
84
|
-
"vite": "^8.
|
|
85
|
-
"vitest": "^
|
|
87
|
+
"vite": "^8.3.8",
|
|
88
|
+
"vitest": "^5.1.0"
|
|
86
89
|
},
|
|
87
|
-
"homepage": "https://
|
|
90
|
+
"homepage": "https://oss.vllnt.com/packages/convex-events",
|
|
88
91
|
"bugs": {
|
|
89
92
|
"url": "https://github.com/vllnt/convex-events/issues"
|
|
90
93
|
},
|
|
@@ -110,6 +113,23 @@
|
|
|
110
113
|
"keywords": [
|
|
111
114
|
"convex",
|
|
112
115
|
"convex-component",
|
|
113
|
-
"events"
|
|
114
|
-
|
|
116
|
+
"events",
|
|
117
|
+
"event-ledger",
|
|
118
|
+
"activity-feed",
|
|
119
|
+
"append-only"
|
|
120
|
+
],
|
|
121
|
+
"pnpm": {
|
|
122
|
+
"minimumReleaseAge": 10080,
|
|
123
|
+
"minimumReleaseAgeExclude": [
|
|
124
|
+
"@vllnt/*"
|
|
125
|
+
],
|
|
126
|
+
"ignoredBuiltDependencies": [
|
|
127
|
+
"esbuild"
|
|
128
|
+
],
|
|
129
|
+
"overrides": {
|
|
130
|
+
"minimatch@>=7.0.0 <11.0.1": "11.0.5",
|
|
131
|
+
"deepmerge-ts@<8.0.0": "^8.0.0",
|
|
132
|
+
"handlebars@<4.7.10": "^4.7.10"
|
|
133
|
+
}
|
|
134
|
+
}
|
|
115
135
|
}
|
package/src/client/index.ts
CHANGED
|
@@ -178,7 +178,7 @@ export class Events<TMeta = unknown> {
|
|
|
178
178
|
/**
|
|
179
179
|
* Append an event of `type` for `subjectRef`. Returns the new event id.
|
|
180
180
|
* Applies the configured boundary guards (`allowedTypes`, `maxTypeLength`,
|
|
181
|
-
* `maxMetadataBytes`,
|
|
181
|
+
* `metadataValidator`, `maxMetadataBytes`, in that order) before writing — throws an
|
|
182
182
|
* {@link EventValidationError} on rejection.
|
|
183
183
|
*/
|
|
184
184
|
record(
|
package/src/component/crons.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
|
-
import { mutation } from "./_generated/server";
|
|
3
|
-
import { api } from "./_generated/api";
|
|
4
|
-
import { jsonValue } from "./validators";
|
|
2
|
+
import { mutation } from "./_generated/server.js";
|
|
3
|
+
import { api } from "./_generated/api.js";
|
|
4
|
+
import { jsonValue } from "./validators.js";
|
|
5
5
|
|
|
6
6
|
/** Default number of rows deleted per `purge` / prune batch before rescheduling. */
|
|
7
7
|
const DEFAULT_PURGE_BATCH = 256;
|
|
@@ -17,6 +17,18 @@ function validateBatch(batch: number): void {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/** Reject empty identifiers: an empty ref or type would store a meaningless ledger row. */
|
|
21
|
+
function assertNonEmpty(fields: Record<string, string | undefined>): void {
|
|
22
|
+
for (const [name, value] of Object.entries(fields)) {
|
|
23
|
+
if (value === "") {
|
|
24
|
+
throw new ConvexError({
|
|
25
|
+
code: "INVALID_ARGUMENT",
|
|
26
|
+
message: `${name} must be non-empty`,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
20
32
|
export const record = mutation({
|
|
21
33
|
args: {
|
|
22
34
|
subjectRef: v.string(),
|
|
@@ -27,6 +39,7 @@ export const record = mutation({
|
|
|
27
39
|
},
|
|
28
40
|
returns: v.id("events"),
|
|
29
41
|
handler: async (ctx, args) => {
|
|
42
|
+
assertNonEmpty({ subjectRef: args.subjectRef, type: args.type, actorRef: args.actorRef });
|
|
30
43
|
if (
|
|
31
44
|
args.idempotencyKey !== undefined &&
|
|
32
45
|
(args.idempotencyKey.length === 0 ||
|
|
@@ -70,6 +83,7 @@ export const purge = mutation({
|
|
|
70
83
|
},
|
|
71
84
|
returns: v.number(),
|
|
72
85
|
handler: async (ctx, args) => {
|
|
86
|
+
assertNonEmpty({ subjectRef: args.subjectRef });
|
|
73
87
|
const before = args.before;
|
|
74
88
|
const batch = args.batch ?? DEFAULT_PURGE_BATCH;
|
|
75
89
|
validateBatch(batch);
|
package/src/component/queries.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ConvexError, v } from "convex/values";
|
|
2
2
|
import { paginationOptsValidator } from "convex/server";
|
|
3
|
-
import type { Doc } from "./_generated/dataModel";
|
|
4
|
-
import { query } from "./_generated/server";
|
|
5
|
-
import { eventDoc, eventPage, countResult } from "./validators";
|
|
3
|
+
import type { Doc } from "./_generated/dataModel.js";
|
|
4
|
+
import { query } from "./_generated/server.js";
|
|
5
|
+
import { eventDoc, eventPage, countResult } from "./validators.js";
|
|
6
6
|
|
|
7
7
|
/** Default scan bound for a capped `count` before it reports `isExact: false`. */
|
|
8
8
|
const DEFAULT_MAX_COUNT = 1000;
|
|
@@ -23,6 +23,12 @@ function validateReadLimit(value: number, name: string, allowZero = false): void
|
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
function validateSince(since: number | undefined): void {
|
|
27
|
+
if (since !== undefined && !Number.isFinite(since)) {
|
|
28
|
+
throw new ConvexError({ code: "INVALID_SINCE", message: "since must be finite" });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
26
32
|
export const list = query({
|
|
27
33
|
args: {
|
|
28
34
|
subjectRef: v.string(),
|
|
@@ -33,6 +39,7 @@ export const list = query({
|
|
|
33
39
|
returns: v.array(eventDoc),
|
|
34
40
|
handler: async (ctx, args) => {
|
|
35
41
|
validateReadLimit(args.limit, "limit");
|
|
42
|
+
validateSince(args.since);
|
|
36
43
|
const since = args.since;
|
|
37
44
|
const type = args.type;
|
|
38
45
|
if (type === undefined) {
|
|
@@ -73,6 +80,7 @@ export const listPaginated = query({
|
|
|
73
80
|
returns: eventPage,
|
|
74
81
|
handler: async (ctx, args) => {
|
|
75
82
|
validateReadLimit(args.paginationOpts.numItems, "paginationOpts.numItems");
|
|
83
|
+
validateSince(args.since);
|
|
76
84
|
const since = args.since;
|
|
77
85
|
const type = args.type;
|
|
78
86
|
if (type === undefined) {
|
package/src/component/schema.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineSchema, defineTable } from "convex/server";
|
|
2
2
|
import { v } from "convex/values";
|
|
3
|
-
import { jsonValue } from "./validators";
|
|
3
|
+
import { jsonValue } from "./validators.js";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Sandboxed tables — the activity ledger's own concern. `subjectRef` and
|
package/src/test.ts
CHANGED
package/src/react/index.test.tsx
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
/**
|
|
3
|
-
* Unit tests for the optional React front-tooling. `convex/react` is mocked so
|
|
4
|
-
* the hooks are exercised in isolation — we assert each thin wrapper forwards
|
|
5
|
-
* exactly the right arguments to the underlying Convex hook and returns its
|
|
6
|
-
* result unchanged (loaded and loading branches). No backend runs here.
|
|
7
|
-
*/
|
|
8
|
-
import { renderHook } from "@testing-library/react";
|
|
9
|
-
import { describe, expect, it, vi, beforeEach } from "vitest";
|
|
10
|
-
import { useQuery, usePaginatedQuery } from "convex/react";
|
|
11
|
-
import {
|
|
12
|
-
useActivityFeed,
|
|
13
|
-
useEventCount,
|
|
14
|
-
type ActivityFeedRef,
|
|
15
|
-
type EventCountRef,
|
|
16
|
-
} from "./index";
|
|
17
|
-
|
|
18
|
-
vi.mock("convex/react", () => ({
|
|
19
|
-
useQuery: vi.fn(),
|
|
20
|
-
usePaginatedQuery: vi.fn(),
|
|
21
|
-
}));
|
|
22
|
-
|
|
23
|
-
const mockedUseQuery = vi.mocked(useQuery);
|
|
24
|
-
const mockedUsePaginatedQuery = vi.mocked(usePaginatedQuery);
|
|
25
|
-
|
|
26
|
-
// Opaque function references — the hooks pass these straight through; their
|
|
27
|
-
// runtime shape is irrelevant because the underlying Convex hook is mocked.
|
|
28
|
-
const countRef = { _ref: "count" } as unknown as EventCountRef;
|
|
29
|
-
const paginateRef = { _ref: "paginate" } as unknown as ActivityFeedRef;
|
|
30
|
-
|
|
31
|
-
beforeEach(() => {
|
|
32
|
-
vi.clearAllMocks();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
describe("useEventCount", () => {
|
|
36
|
-
it("forwards (ref, args) to useQuery and returns the loaded value", () => {
|
|
37
|
-
const args = { subjectRef: "user:1", type: "login" };
|
|
38
|
-
mockedUseQuery.mockReturnValue({ count: 3, isExact: true });
|
|
39
|
-
|
|
40
|
-
const { result } = renderHook(() => useEventCount(countRef, args));
|
|
41
|
-
|
|
42
|
-
expect(mockedUseQuery).toHaveBeenCalledTimes(1);
|
|
43
|
-
expect(mockedUseQuery).toHaveBeenCalledWith(countRef, args);
|
|
44
|
-
expect(result.current).toEqual({ count: 3, isExact: true });
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("returns undefined while loading", () => {
|
|
48
|
-
const args = { subjectRef: "user:2" };
|
|
49
|
-
mockedUseQuery.mockReturnValue(undefined);
|
|
50
|
-
|
|
51
|
-
const { result } = renderHook(() => useEventCount(countRef, args));
|
|
52
|
-
|
|
53
|
-
expect(mockedUseQuery).toHaveBeenCalledWith(countRef, args);
|
|
54
|
-
expect(result.current).toBeUndefined();
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe("useActivityFeed", () => {
|
|
59
|
-
it("forwards (ref, args, opts) to usePaginatedQuery and returns its result", () => {
|
|
60
|
-
const args = { subjectRef: "user:1", type: "login" } as never;
|
|
61
|
-
const opts = { initialNumItems: 10 };
|
|
62
|
-
const loadMore = vi.fn();
|
|
63
|
-
const paginated = {
|
|
64
|
-
results: [{ id: "e1" }],
|
|
65
|
-
status: "CanLoadMore" as const,
|
|
66
|
-
isLoading: false as const,
|
|
67
|
-
loadMore,
|
|
68
|
-
};
|
|
69
|
-
mockedUsePaginatedQuery.mockReturnValue(paginated);
|
|
70
|
-
|
|
71
|
-
const { result } = renderHook(() =>
|
|
72
|
-
useActivityFeed(paginateRef, args, opts),
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
expect(mockedUsePaginatedQuery).toHaveBeenCalledTimes(1);
|
|
76
|
-
expect(mockedUsePaginatedQuery).toHaveBeenCalledWith(paginateRef, args, opts);
|
|
77
|
-
expect(result.current).toBe(paginated);
|
|
78
|
-
expect(result.current.results).toEqual([{ id: "e1" }]);
|
|
79
|
-
expect(result.current.status).toBe("CanLoadMore");
|
|
80
|
-
expect(result.current.loadMore).toBe(loadMore);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it("forwards loadMore — calling it invokes the paginated hook's loadMore", () => {
|
|
84
|
-
const args = { subjectRef: "user:3" } as never;
|
|
85
|
-
const opts = { initialNumItems: 5 };
|
|
86
|
-
const loadMore = vi.fn();
|
|
87
|
-
mockedUsePaginatedQuery.mockReturnValue({
|
|
88
|
-
results: [],
|
|
89
|
-
status: "CanLoadMore" as const,
|
|
90
|
-
isLoading: false as const,
|
|
91
|
-
loadMore,
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
const { result } = renderHook(() =>
|
|
95
|
-
useActivityFeed(paginateRef, args, opts),
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
result.current.loadMore(20);
|
|
99
|
-
expect(loadMore).toHaveBeenCalledWith(20);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it("returns Exhausted status when the feed is fully loaded", () => {
|
|
103
|
-
const args = { subjectRef: "user:4" } as never;
|
|
104
|
-
const opts = { initialNumItems: 10 };
|
|
105
|
-
const loadMore = vi.fn();
|
|
106
|
-
const paginated = {
|
|
107
|
-
results: [{ id: "e1" }, { id: "e2" }],
|
|
108
|
-
status: "Exhausted" as const,
|
|
109
|
-
isLoading: false as const,
|
|
110
|
-
loadMore,
|
|
111
|
-
};
|
|
112
|
-
mockedUsePaginatedQuery.mockReturnValue(paginated);
|
|
113
|
-
|
|
114
|
-
const { result } = renderHook(() =>
|
|
115
|
-
useActivityFeed(paginateRef, args, opts),
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
expect(result.current.status).toBe("Exhausted");
|
|
119
|
-
expect(result.current.results).toHaveLength(2);
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
describe("useEventCount — with type", () => {
|
|
124
|
-
it("forwards type in args when present", () => {
|
|
125
|
-
const args = { subjectRef: "user:5", type: "purchase" };
|
|
126
|
-
mockedUseQuery.mockReturnValue({ count: 7, isExact: true });
|
|
127
|
-
|
|
128
|
-
const { result } = renderHook(() => useEventCount(countRef, args));
|
|
129
|
-
|
|
130
|
-
expect(mockedUseQuery).toHaveBeenCalledWith(countRef, args);
|
|
131
|
-
expect(result.current).toEqual({ count: 7, isExact: true });
|
|
132
|
-
});
|
|
133
|
-
});
|