@kedem/okdb 2.3.4 → 2.4.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 (60) hide show
  1. package/CHANGELOG.md +366 -0
  2. package/algorithm-worker-thread.js +1 -1
  3. package/bin/okdb.js +1 -1
  4. package/docs/auth-and-sync.md +28 -4
  5. package/docs/auth-permissions.md +19 -6
  6. package/docs/change-log.md +79 -59
  7. package/docs/data-model.md +20 -18
  8. package/docs/deployment.md +25 -0
  9. package/docs/diagnostics.md +59 -17
  10. package/docs/embeddings.md +57 -6
  11. package/docs/files.md +1 -1
  12. package/docs/fts.md +50 -5
  13. package/docs/functions.md +21 -8
  14. package/docs/http-api.md +139 -98
  15. package/docs/http-cluster.md +30 -8
  16. package/docs/indexes.md +5 -4
  17. package/docs/logging.md +18 -0
  18. package/docs/migration.md +94 -2
  19. package/docs/pipelines.md +6 -1
  20. package/docs/plugins.md +12 -18
  21. package/docs/process-registry.md +50 -39
  22. package/docs/processors.md +40 -22
  23. package/docs/querying.md +50 -36
  24. package/docs/queue.md +110 -14
  25. package/docs/subscriptions.md +35 -24
  26. package/docs/sync.md +96 -37
  27. package/docs/time-machine.md +23 -17
  28. package/docs/transactions.md +11 -19
  29. package/docs/ttl.md +118 -67
  30. package/docs/upgrade-2.0.md +2 -2
  31. package/docs/views.md +71 -52
  32. package/okdb-functions-sandbox-worker.js +1 -1
  33. package/okdb-http-worker-child.js +1 -1
  34. package/okdb-views-bootstrap-worker.js +1 -1
  35. package/okdb.js +1 -1
  36. package/package.json +1 -1
  37. package/public/sections/db/parts/type-viewer.ok.js +1 -1
  38. package/public/sections/embeddings/parts/engines-overview.ok.js +1 -1
  39. package/public/sections/queue/parts/queue-jobs.ok.js +1 -1
  40. package/public/sections/system/modals/data-ops-modal.ok.js +1 -1
  41. package/types/environment.d.ts +320 -52
  42. package/types/features/admin.d.ts +8 -2
  43. package/types/features/api.d.ts +10 -16
  44. package/types/features/auth.d.ts +135 -16
  45. package/types/features/embeddings.d.ts +639 -31
  46. package/types/features/engines.d.ts +368 -14
  47. package/types/features/files.d.ts +2 -4
  48. package/types/features/fts.d.ts +98 -26
  49. package/types/features/functions.d.ts +175 -15
  50. package/types/features/mcp.d.ts +23 -10
  51. package/types/features/pipelines.d.ts +268 -9
  52. package/types/features/queue.d.ts +230 -34
  53. package/types/features/sync.d.ts +177 -14
  54. package/types/features/time-machine.d.ts +117 -16
  55. package/types/features/views.d.ts +230 -39
  56. package/types/index.d.ts +357 -254
  57. package/types/okdb.d.ts +377 -0
  58. package/types/options.d.ts +309 -48
  59. package/public/test-cron-input.html +0 -60
  60. package/public/test-json-input.html +0 -280
@@ -1,32 +1,133 @@
1
+ /// <reference lib="es2015.generator" />
1
2
  import type { OKDBPrimaryKey } from '../options';
2
3
 
4
+ /**
5
+ * The current snapshot ("head") the time machine keeps for a tracked document —
6
+ * the last value it recorded and the clock it was recorded at.
7
+ */
3
8
  export interface OKDBTimeMachineSnapshot {
9
+ value: unknown;
4
10
  clock: number;
5
- timestamp: number;
6
- [key: string]: unknown;
7
11
  }
8
12
 
13
+ /**
14
+ * One field-level diff. Paths are dot-notation (nested only where the type's schema
15
+ * declares nested object properties). A create is all `put`; a remove is all `delete`.
16
+ */
9
17
  export interface OKDBTimeMachineDiff {
10
- key: OKDBPrimaryKey;
11
- type: string;
12
- action: 'put' | 'remove';
13
- oldValue?: unknown;
14
- newValue?: unknown;
18
+ /** Env clock the diff was recorded at. */
15
19
  clock: number;
20
+ /** Fields set or updated at this clock (path → new value). */
21
+ put: Record<string, unknown>;
22
+ /** Field paths removed at this clock. */
23
+ delete: string[];
24
+ /** The previous head clock (0 for the initial snapshot / a create). */
25
+ fromClock: number;
26
+ /** Wall-clock ms when the diff was recorded. */
16
27
  timestamp: number;
17
28
  }
18
29
 
19
- export interface OKDBTimeMachineQueryOptions {
20
- from?: number;
21
- to?: number;
30
+ /** A diff yielded by `getChanges()` — carries the type and document key it belongs to. */
31
+ export interface OKDBTimeMachineChange extends OKDBTimeMachineDiff {
32
+ type: string;
33
+ key: OKDBPrimaryKey;
34
+ }
35
+
36
+ export interface OKDBTimeMachineHistoryOptions {
37
+ /** Max diffs returned. */
22
38
  limit?: number;
23
- types?: string[];
39
+ /** Only diffs with clock > after. */
40
+ after?: number;
41
+ /** Only diffs with clock <= maxClock. (The HTTP route's `before` query param maps here.) */
42
+ maxClock?: number;
24
43
  }
25
44
 
45
+ /** Returned by `getHistory()`: ascending diffs plus the current head (null if none / removed). */
46
+ export interface OKDBTimeMachineHistory {
47
+ diffs: OKDBTimeMachineDiff[];
48
+ head: OKDBTimeMachineSnapshot | null;
49
+ }
50
+
51
+ /** One row of `list()` — every non-`~` type in the env, sorted by name. */
52
+ export interface OKDBTimeMachineTypeListEntry {
53
+ type: string;
54
+ enabled: boolean;
55
+ /** Processor state; 'remote' = enabled but indexed by another (processors-role) instance. */
56
+ state: string;
57
+ lastClock: number;
58
+ /** null when this instance does not run the type's processor. */
59
+ lag: number | null;
60
+ }
61
+
62
+ /** Returned by `status(type)`. Also carries the type's processor status fields when it runs locally. */
63
+ export interface OKDBTimeMachineTypeStatus {
64
+ type: string;
65
+ /** True only when this instance runs the type's processor. */
66
+ enabled: boolean;
67
+ startClock: number | null;
68
+ lastProcessedClock: number | null;
69
+ headCount: number;
70
+ [key: string]: unknown;
71
+ }
72
+
73
+ /** Returned by `status()` with no argument. */
74
+ export interface OKDBTimeMachineStatus {
75
+ enabled: boolean;
76
+ autoEnable: boolean;
77
+ startClock: number | null;
78
+ lastProcessedClock: number | null;
79
+ envClock: number | null;
80
+ headCount: number;
81
+ disabledAt: null;
82
+ types: OKDBTimeMachineTypeListEntry[];
83
+ }
84
+
85
+ /**
86
+ * Per-type field-level document history (`env.timeMachine`). Attached only when the
87
+ * `timeMachine` constructor option is truthy and the license allows the feature;
88
+ * otherwise `env.timeMachine` is undefined. Tracking is an async `single` processor per type.
89
+ */
26
90
  export declare class OKDBTimeMachine {
27
- start(): void;
28
- stop(): Promise<void>;
29
- getAt(type: string, key: OKDBPrimaryKey, clock: number): unknown;
30
- getDiff(options?: OKDBTimeMachineQueryOptions): Iterable<OKDBTimeMachineDiff>;
31
- listSnapshots(options?: { limit?: number }): OKDBTimeMachineSnapshot[];
91
+ /**
92
+ * Start tracking `type`. First enable seeds a head + diff for every existing doc; re-enable
93
+ * after `disable()` resumes from the kept cursor; re-enable after `drop()` re-seeds.
94
+ */
95
+ enable(type: string): Promise<void>;
96
+ /** @deprecated use enableAll() */
97
+ enable(): Promise<void>;
98
+ /** Stop tracking `type`; history is kept and queryable. */
99
+ disable(type: string): Promise<void>;
100
+ /** @deprecated use disableAll() — note: the no-arg form does not clear the auto-enable flag. */
101
+ disable(): Promise<void>;
102
+ /** Stop tracking `type` and delete its diffs + heads; resets its cursor. */
103
+ drop(type: string): Promise<void>;
104
+ /** @deprecated use dropAll() */
105
+ drop(): Promise<void>;
106
+ /** Enable every current non-`~` type and auto-enable types registered later. */
107
+ enableAll(): Promise<void>;
108
+ /** Disable every tracked type and clear auto-enable. History is kept. */
109
+ disableAll(): Promise<void>;
110
+ /** Delete all history for every type and reset all cursors. */
111
+ dropAll(): Promise<void>;
112
+
113
+ /** Is `type` tracked? With no argument: is any type tracked? */
114
+ isEnabled(type?: string): boolean;
115
+ /** Every non-`~` type in the env with its tracking state. */
116
+ list(): OKDBTimeMachineTypeListEntry[];
117
+ status(type: string): OKDBTimeMachineTypeStatus;
118
+ status(): OKDBTimeMachineStatus;
119
+ /** The type's durable processor cursor (clock). */
120
+ getCursor(type: string): number;
121
+ /** @deprecated pass a type — returns the max cursor across locally running types. */
122
+ getCursor(): { lastProcessedClock: number } | null;
123
+
124
+ /** Reconstruct the document as of `clock`; undefined if no diff exists at or before it. */
125
+ getStateAt(type: string, key: OKDBPrimaryKey, clock: number): Record<string, unknown> | undefined;
126
+ getHistory(type: string, key: OKDBPrimaryKey, options?: OKDBTimeMachineHistoryOptions): OKDBTimeMachineHistory;
127
+ /** Diffs with fromClock <= clock <= toClock for `type`, or for all types when `type` is null. */
128
+ getChanges(
129
+ type: string | null,
130
+ fromClock: number,
131
+ toClock: number,
132
+ ): Generator<OKDBTimeMachineChange, void, undefined>;
32
133
  }
@@ -2,7 +2,12 @@
2
2
  export type OKDBFilter = Record<string, unknown>;
3
3
 
4
4
  /** Map operator specs */
5
- export type OKDBMapRefSpec = { $ref: [type: string, fkField: string, targetField: string] };
5
+ /**
6
+ * FK lookup: `[type, fkField, targetField]`. `fkField` starting with `$` is a path on the source doc;
7
+ * otherwise it is a literal key. `live: true` maintains an inverse index so changes to the referenced
8
+ * doc re-evaluate every source doc pointing at it (default: resolved once at write time).
9
+ */
10
+ export type OKDBMapRefSpec = { $ref: [type: string, fkField: string, targetField: string]; live?: boolean };
6
11
  export type OKDBMapConcatSpec = { $concat: string[] };
7
12
  export type OKDBMapCoalesceSpec = { $coalesce: string[] };
8
13
  export type OKDBMapFieldSpec = string | OKDBMapRefSpec | OKDBMapConcatSpec | OKDBMapCoalesceSpec;
@@ -10,14 +15,38 @@ export type OKDBMapFieldSpec = string | OKDBMapRefSpec | OKDBMapConcatSpec | OKD
10
15
  /** Declarative per-doc transform merged onto source docs before reducers run */
11
16
  export type OKDBViewMap = Record<string, OKDBMapFieldSpec>;
12
17
 
18
+ /**
19
+ * Options shared by every reducer spec.
20
+ * - `items: true` exposes an `items()` accessor on the reducer's output (live source docs).
21
+ * - `filter` is a per-reducer sift-style filter applied after the view filter + map.
22
+ */
23
+ export interface OKDBReducerCommon {
24
+ items?: boolean;
25
+ filter?: OKDBFilter;
26
+ }
27
+
13
28
  /** Built-in reducer specs */
14
- export type OKDBCountSpec = { $count: true; items?: boolean };
15
- export type OKDBSumSpec = { $sum: string; items?: boolean };
16
- export type OKDBAvgSpec = { $avg: string; items?: boolean };
17
- export type OKDBMinSpec = { $min: string; items?: boolean };
18
- export type OKDBMaxSpec = { $max: string; items?: boolean };
19
- export type OKDBCountBySpec = { $countBy: string; items?: boolean };
20
- export type OKDBCustomSpec = { [reducer: string]: unknown; items?: boolean };
29
+ export type OKDBCountSpec = { $count: true } & OKDBReducerCommon;
30
+ export type OKDBSumSpec = { $sum: string } & OKDBReducerCommon;
31
+ export type OKDBAvgSpec = { $avg: string } & OKDBReducerCommon;
32
+ /** Index-backed: auto-creates (or reuses) a single-field index on the field. Not supported inside `$ref`. */
33
+ export type OKDBMinSpec = { $min: string } & OKDBReducerCommon;
34
+ /** Index-backed: auto-creates (or reuses) a single-field index on the field. Not supported inside `$ref`. */
35
+ export type OKDBMaxSpec = { $max: string } & OKDBReducerCommon;
36
+ export type OKDBCountBySpec = { $countBy: string } & OKDBReducerCommon;
37
+ /**
38
+ * `$group`: partitions docs by `by` (single field or compound key) and runs named sub-reducers per group.
39
+ * `$ref` is not allowed inside `reduce`; `$min`/`$max` are not allowed inside a nested `$group`.
40
+ */
41
+ export type OKDBGroupSpec = {
42
+ $group: {
43
+ by: string | string[];
44
+ filter?: OKDBFilter;
45
+ reduce: Record<string, OKDBReducerSpec>;
46
+ };
47
+ } & OKDBReducerCommon;
48
+ /** A reducer registered via views.registerReducer() / OKDB.registerViewReducer() — `{ $name: opts }`. */
49
+ export type OKDBCustomSpec = { [reducer: string]: unknown; items?: boolean; filter?: OKDBFilter };
21
50
 
22
51
  /** $ref sub-view aggregation */
23
52
  export interface OKDBRefSpec {
@@ -34,6 +63,7 @@ export type OKDBReducerSpec =
34
63
  | OKDBMinSpec
35
64
  | OKDBMaxSpec
36
65
  | OKDBCountBySpec
66
+ | OKDBGroupSpec
37
67
  | { $ref: OKDBRefSpec }
38
68
  | OKDBCustomSpec;
39
69
 
@@ -42,8 +72,9 @@ export type OKDBBucketTimeGranularity = 'minute' | 'hour' | 'day' | 'week' | 'mo
42
72
 
43
73
  /**
44
74
  * Bucket configuration for bucketed views.
45
- * Use preset:'time' for time-series bucketing (JSON-serializable, works in synced envs).
46
- * Use project for custom string-key bucketing (JS only, not usable in synced envs).
75
+ * Use preset:'time' for time-series bucketing.
76
+ * Use project for custom string-key bucketing: the NAME of a projection registered with
77
+ * OKDB.registerBucketProjection() (a function can't be persisted and is refused).
47
78
  */
48
79
  export type OKDBBucketConfig =
49
80
  | {
@@ -57,8 +88,11 @@ export type OKDBBucketConfig =
57
88
  | {
58
89
  /** Dot path to the grouping field on each source document. */
59
90
  field: string;
60
- /** Custom projection: maps a field value to a string bucket key. Not usable in synced environments. */
61
- project: (value: unknown) => string;
91
+ /**
92
+ * Name of a projection registered with OKDB.registerBucketProjection(name, fn), which maps a
93
+ * field value to a string bucket key. Every process that opens the store must register it.
94
+ */
95
+ project: string;
62
96
  /** Arbitrary label used in bucket keys and range queries. */
63
97
  granularity: string;
64
98
  };
@@ -82,10 +116,10 @@ export interface OKDBBucketRangeEntry {
82
116
  /** Canonical string key for this bucket (ISO date string for preset:'time'). */
83
117
  bucketKey: string;
84
118
  granularity: string;
85
- /** Per-reducer aggregated values. Scalar → { value: N }; $countBy → { totalGroups, preview, hasMore, cursor }. */
86
- reducers: Record<string, OKDBScalarResult | OKDBCountByResult>;
119
+ /** Per-reducer aggregated values. Scalar → { value: N }; $countBy/$group → { totalGroups, preview, hasMore, cursor }. */
120
+ reducers: Record<string, OKDBScalarResult | OKDBGroupedResult>;
87
121
  /** Per-$ref sub-view aggregated values. */
88
- refs: Record<string, Record<string, OKDBScalarResult | OKDBCountByResult>>;
122
+ refs: Record<string, Record<string, OKDBScalarResult | OKDBGroupedResult>>;
89
123
  }
90
124
 
91
125
  /** Options for views.listBuckets() */
@@ -120,26 +154,63 @@ export interface OKDBViewDefinition {
120
154
  reduce: Record<string, OKDBReducerSpec>;
121
155
  /**
122
156
  * Optional. Enables per-bucket aggregation.
123
- * Use preset:'time' for time-series bucketing (works over HTTP and in synced envs).
124
- * Use project for custom string-key bucketing (JS SDK only, not syncable).
157
+ * Use preset:'time' for time-series bucketing.
158
+ * Use project (a registered projection name) for custom string-key bucketing.
125
159
  */
126
160
  bucket?: OKDBBucketConfig;
161
+ /** Maintenance timing. Default 'sync' (updated in the writer's commit). */
162
+ mode?: 'sync' | 'async';
163
+ }
164
+
165
+ /** Options for an `items()` accessor on a view output. */
166
+ export interface OKDBViewItemsOptions {
167
+ /** Scope to one bucket of a bucketed view. `granularity` must equal the view's. */
168
+ bucket?: { granularity: string; key: string };
169
+ limit?: number;
170
+ /** Resume after this doc key (a previous page's `lastKey`). */
171
+ startKey?: string;
172
+ reverse?: boolean;
173
+ offset?: number;
174
+ /** Return doc keys instead of `{ key, value }` entries (paginated form only). */
175
+ keysOnly?: boolean;
176
+ }
177
+
178
+ /** One page returned by a paginated `items()` call. */
179
+ export interface OKDBViewItemsPage {
180
+ /** `{ key, value }` entries, or doc keys when `keysOnly: true`. */
181
+ result: Array<{ key: string; value: unknown }> | string[];
182
+ hasMore: boolean;
183
+ lastKey: string | null;
127
184
  }
128
185
 
129
- /** Shape returned for a scalar reducer output ($count, $sum, $avg, $min, $max) */
186
+ /**
187
+ * `items()` accessor attached to view outputs when a reducer sets `items: true`.
188
+ * With no options (or only `bucket`) returns the source docs as a flat array; any pagination
189
+ * option (`limit`/`startKey`/`reverse`/`offset`/`keysOnly`) returns an {@link OKDBViewItemsPage}.
190
+ */
191
+ export interface OKDBViewItemsFn {
192
+ (options?: { bucket?: { granularity: string; key: string } }): unknown[];
193
+ (options: OKDBViewItemsOptions): unknown[] | OKDBViewItemsPage;
194
+ }
195
+
196
+ /** Shape returned for a scalar reducer output ($count, $sum, $avg, $min, $max, custom) */
130
197
  export interface OKDBScalarResult {
131
- value: number | null;
132
- /** Present when items:true was set — returns the live source docs for this bucket */
133
- items?: () => unknown[];
198
+ /** Reducer value; custom reducers may return non-numeric state. */
199
+ value: unknown;
200
+ /** Present when items:true was set — returns the live source docs */
201
+ items?: OKDBViewItemsFn;
134
202
  }
135
203
 
136
204
  /** Pagination controls for a grouped ($countBy) reducer's preview page */
137
205
  export interface OKDBViewPreviewOptions {
138
206
  /** Max groups per page. Default 50. */
139
207
  limit?: number;
140
- /** Sort direction. Default 'desc' for $countBy (by count). */
208
+ /** Sort direction. Default 'desc' ('asc' when axis is 'key'). */
141
209
  order?: 'asc' | 'desc';
142
- /** Sort axis. Default 'count' for $countBy. */
210
+ /**
211
+ * Sort axis for `$group`: a sub-reducer name or 'key' (default: first non-index-backed scalar
212
+ * sub-reducer, else 'key'). Ignored for `$countBy`, which always sorts by count.
213
+ */
143
214
  axis?: string;
144
215
  /** Opaque cursor from a previous page's `cursor` field. */
145
216
  cursor?: string;
@@ -149,10 +220,21 @@ export interface OKDBViewPreviewOptions {
149
220
 
150
221
  /** A single group entry within a $countBy preview page */
151
222
  export interface OKDBCountByPreviewEntry {
223
+ /** Group key (stringified field value). */
152
224
  key: string;
153
225
  value: number;
154
226
  /** Present when items:true was set — returns the live source docs for this group */
155
- items?: () => unknown[];
227
+ items?: OKDBViewItemsFn;
228
+ }
229
+
230
+ /**
231
+ * A single group entry within a $group preview page: `{ key, <subReducer>: { value }, ... }`.
232
+ * Compound `by` keys are joined with '\x00'. Nested grouped sub-reducers are omitted.
233
+ */
234
+ export interface OKDBGroupPreviewEntry {
235
+ key: string;
236
+ items?: OKDBViewItemsFn;
237
+ [subReducer: string]: OKDBScalarResult | OKDBViewItemsFn | string | undefined;
156
238
  }
157
239
 
158
240
  /**
@@ -168,24 +250,119 @@ export interface OKDBCountByResult {
168
250
  cursor: string | null;
169
251
  }
170
252
 
253
+ /** Shape returned for a $group reducer output (same pagination as $countBy). */
254
+ export interface OKDBGroupResult {
255
+ totalGroups: number;
256
+ preview: OKDBGroupPreviewEntry[];
257
+ hasMore: boolean;
258
+ cursor: string | null;
259
+ }
260
+
261
+ /** Output of a grouped reducer ($countBy or $group). */
262
+ export type OKDBGroupedResult = OKDBCountByResult | OKDBGroupResult;
263
+
171
264
  /** Shape returned for a $ref slot */
172
- export type OKDBRefResult = Record<string, OKDBScalarResult | OKDBCountByResult>;
265
+ export interface OKDBRefResult {
266
+ /** Present when the ref has an items-enabled reducer — the live child docs. */
267
+ items?: OKDBViewItemsFn;
268
+ [reducer: string]: OKDBScalarResult | OKDBGroupedResult | OKDBViewItemsFn | undefined;
269
+ }
270
+
271
+ /**
272
+ * The full output object returned by views.get(): one key per `reduce` entry, plus a
273
+ * view-level `items()` accessor when any reducer sets `items: true`.
274
+ */
275
+ export interface OKDBViewOutput {
276
+ items?: OKDBViewItemsFn;
277
+ [reducer: string]: OKDBScalarResult | OKDBGroupedResult | OKDBRefResult | OKDBViewItemsFn | undefined;
278
+ }
279
+
280
+ /** Aggregate lifecycle state of a view. */
281
+ /** 'error' = a stored definition that could not be compiled at open() (held back, never activated). */
282
+ export type OKDBViewState = 'creating' | 'partially-ready' | 'ready' | 'halted' | 'stopped' | 'resetting' | 'error';
283
+
284
+ /** Bootstrap progress of the section currently scanning. */
285
+ export interface OKDBViewBootstrapProgress {
286
+ processed: number;
287
+ total: number | null;
288
+ percent: number | null;
289
+ }
173
290
 
174
- /** The full output object returned by views.get() */
175
- export type OKDBViewOutput = Record<string, OKDBScalarResult | OKDBCountByResult | OKDBRefResult>;
291
+ /** Per-section bootstrap state (primary type + each $ref). */
292
+ export interface OKDBViewSectionState {
293
+ state: string;
294
+ clock: number;
295
+ progress: OKDBViewBootstrapProgress | null;
296
+ }
176
297
 
177
298
  /** View lifecycle meta returned by views.getMeta() */
178
299
  export interface OKDBViewMeta {
179
- state: 'creating' | 'ready' | 'halted' | 'stopped' | 'resetting';
300
+ state: OKDBViewState;
180
301
  clock: number;
181
- error: { clock: number; key: string; error: string } | null;
182
- refs: Record<string, { state: string; clock: number; error: string | null }>;
302
+ /** Halt cause ({clock, key, error}), or {code, error} for a held-back ('error') view. */
303
+ error: { clock?: number; key?: string; code?: string | null; error: string } | null;
304
+ refs: Record<string, { state: string; clock: number; error: unknown }>;
305
+ /** Persisted storage layout version; a mismatch auto-rebuilds on open. */
306
+ storageVersion?: number;
307
+ /** Per-section bootstrap state. Present once a bootstrap has run. */
308
+ sections?: {
309
+ primary?: OKDBViewSectionState;
310
+ refs?: Record<string, OKDBViewSectionState>;
311
+ };
312
+ /** Derived from `sections` — progress of the section still creating, else null. */
313
+ bootstrapProgress?: OKDBViewBootstrapProgress | null;
183
314
  /** Present on bucketed views. Tracks documents whose bucket field could not be parsed. */
184
315
  bucketing?: { unbucketedCount: number };
185
316
  }
186
317
 
187
- /** Stored view definition returned by views.getDefinition() (includes name and createdAt) */
188
- export type OKDBStoredViewDefinition = OKDBViewDefinition & { name: string; createdAt: number };
318
+ /**
319
+ * Payload of the `'view:progress'` event (EVENTS.VIEW_PROGRESS) emitted on `env.events`
320
+ * during view bootstrap.
321
+ */
322
+ export interface OKDBViewProgressEvent {
323
+ name: string;
324
+ phase: 'scan' | 'replay' | 'ref-scan' | 'ref-replay';
325
+ /** 'primary' or `refs.<refName>`. */
326
+ section: string;
327
+ refName?: string;
328
+ processed: number;
329
+ total: number | null;
330
+ percent: number | null;
331
+ }
332
+
333
+ /** Stored view definition returned by views.getDefinition() */
334
+ export type OKDBStoredViewDefinition = OKDBViewDefinition & {
335
+ name: string;
336
+ createdAt: number;
337
+ /** Set once the initial bootstrap completed. */
338
+ readyAt?: number;
339
+ };
340
+
341
+ /** Options for views.itemsGroups() */
342
+ export interface OKDBViewItemsGroupsOptions {
343
+ /** Name of a grouped ($countBy / $group) reducer. Required. */
344
+ reducerName: string;
345
+ /** Look the reducer up inside this $ref slot. */
346
+ refName?: string;
347
+ /** Bucket key (default: the unbucketed '_' aggregate). */
348
+ bucket?: string;
349
+ /** 'count' ($countBy), a $group sub-reducer name, or 'key'. Same defaults as preview. */
350
+ axis?: string;
351
+ order?: 'asc' | 'desc';
352
+ /** Page size. Default 100, clamped to 1..1000. */
353
+ limit?: number;
354
+ /** Opaque cursor from a previous page. */
355
+ cursor?: string;
356
+ }
357
+
358
+ /** One page returned by views.itemsGroups() */
359
+ export interface OKDBViewItemsGroupsPage {
360
+ /** $countBy: `{ key, value }`; $group: `{ key, <subReducer>: { value }, ... }`. */
361
+ items: Array<OKDBCountByPreviewEntry | OKDBGroupPreviewEntry>;
362
+ totalGroups: number;
363
+ hasMore: boolean;
364
+ cursor: string | null;
365
+ }
189
366
 
190
367
  /** Options for views.remove() */
191
368
  export interface OKDBViewRemoveOptions {
@@ -203,10 +380,13 @@ export interface OKDBCustomReducer {
203
380
  apply(state: unknown, before: unknown, after: unknown, opts: unknown): unknown;
204
381
  }
205
382
 
383
+ /** Materialized views for one environment (`env.views`). */
206
384
  export declare class OKDBViews {
207
385
  /**
208
386
  * Create a new materialized view. The source type must already be registered.
209
- * Synchronously bootstraps existing documents then registers an incremental processor.
387
+ * Bootstraps existing documents cooperatively (emits 'view:progress' on `env.events`) then
388
+ * registers the incremental processor. Resolves when ready. An interrupted bootstrap is not
389
+ * resumed: the next open() rebuilds the view from scratch.
210
390
  */
211
391
  create(name: string, definition: OKDBViewDefinition): Promise<{ name: string }>;
212
392
 
@@ -216,6 +396,12 @@ export declare class OKDBViews {
216
396
  */
217
397
  get(name: string, options?: { preview?: OKDBViewPreviewOptions }): OKDBViewOutput | null;
218
398
 
399
+ /**
400
+ * Paginate ALL groups of a grouped reducer ($countBy / $group), ordered by any leaderboard axis.
401
+ * Throws VIEW_NOT_FOUND, VIEW_ITEMS_REDUCER_NOT_FOUND, VIEW_ITEMS_REDUCER_NOT_GROUPED, CURSOR_INVALID.
402
+ */
403
+ itemsGroups(name: string, options: OKDBViewItemsGroupsOptions): OKDBViewItemsGroupsPage;
404
+
219
405
  /**
220
406
  * Get view lifecycle meta (state, clock, error).
221
407
  * Returns null if the view does not exist.
@@ -236,9 +422,15 @@ export declare class OKDBViews {
236
422
  */
237
423
  remove(name: string, options?: OKDBViewRemoveOptions): Promise<void>;
238
424
 
239
- /** Clears all accumulated state and re-scans from scratch. */
425
+ /** Clears all accumulated state and re-scans from scratch. Throws VIEW_NOT_FOUND. */
240
426
  rebuild(name: string): Promise<void>;
241
427
 
428
+ /**
429
+ * Wipe the view's state and re-run the bootstrap scan in place, leaving its live
430
+ * handlers registered. Prefer rebuild(). Throws VIEW_NOT_FOUND.
431
+ */
432
+ resetView(name: string): Promise<void>;
433
+
242
434
  /** Pause the view. Writes that arrive while stopped are replayed on start(). */
243
435
  stop(name: string): Promise<void>;
244
436
 
@@ -249,8 +441,10 @@ export declare class OKDBViews {
249
441
  start(name: string): Promise<void>;
250
442
 
251
443
  /**
252
- * Register a custom reducer. Must be called before creating any view that uses it.
253
- * The name must start with '$' and must not conflict with a built-in reducer.
444
+ * Register a custom reducer for this env. Must be called before creating any view that uses it.
445
+ * The name must start with '$' and must not be a built-in reducer or `$ref`.
446
+ * A stored view held back at open() because this reducer wasn't registered is re-derived now.
447
+ * Prefer OKDB.registerViewReducer() before open() so such views are live from boot.
254
448
  */
255
449
  registerReducer(name: string, reducer: OKDBCustomReducer): void;
256
450
 
@@ -269,7 +463,4 @@ export declare class OKDBViews {
269
463
  * Throws VIEW_NOT_BUCKETED if the view was not created with a bucket config.
270
464
  */
271
465
  listBuckets(name: string, options: OKDBListBucketsOptions): OKDBBucketEntry[];
272
-
273
- /** @internal Called by OKDB after sync to activate views that arrived from peers. */
274
- bootSyncedViews(): Promise<void>;
275
466
  }