@kedem/okdb 1.0.4 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -4
- package/bin/okdb.js +1 -1
- package/docs/OKDB-ARCHITECTURE.md +2 -1
- package/docs/manifest.json +1 -0
- package/docs/pipelines.md +2 -0
- package/docs/views.md +513 -0
- package/okdb.js +1 -1
- package/package.json +1 -1
- package/public/sections/queue/parts/queue-buckets.ok.js +1 -1
- package/public/sections/queue/parts/queue-jobs.ok.js +1 -1
- package/public/sections/views/modals/create-view-modal.ok.js +1 -1
- package/public/sections/views/parts/view-detail.ok.js +1 -1
- package/public/sections/views/parts/views-overview.ok.js +1 -1
- package/types/features/views.d.ts +235 -10
|
@@ -1,16 +1,241 @@
|
|
|
1
|
+
/** Sift-style (MongoDB-compatible) filter object */
|
|
2
|
+
export type OKDBFilter = Record<string, unknown>;
|
|
3
|
+
|
|
4
|
+
/** Map operator specs */
|
|
5
|
+
export type OKDBMapRefSpec = { $ref: [type: string, fkField: string, targetField: string] };
|
|
6
|
+
export type OKDBMapConcatSpec = { $concat: string[] };
|
|
7
|
+
export type OKDBMapCoalesceSpec = { $coalesce: string[] };
|
|
8
|
+
export type OKDBMapFieldSpec = string | OKDBMapRefSpec | OKDBMapConcatSpec | OKDBMapCoalesceSpec;
|
|
9
|
+
|
|
10
|
+
/** Declarative per-doc transform merged onto source docs before reducers run */
|
|
11
|
+
export type OKDBViewMap = Record<string, OKDBMapFieldSpec>;
|
|
12
|
+
|
|
13
|
+
/** 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 };
|
|
21
|
+
|
|
22
|
+
/** $ref sub-view aggregation */
|
|
23
|
+
export interface OKDBRefSpec {
|
|
24
|
+
type: string;
|
|
25
|
+
key: string;
|
|
26
|
+
filter?: OKDBFilter;
|
|
27
|
+
reduce: Record<string, OKDBReducerSpec>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type OKDBReducerSpec =
|
|
31
|
+
| OKDBCountSpec
|
|
32
|
+
| OKDBSumSpec
|
|
33
|
+
| OKDBAvgSpec
|
|
34
|
+
| OKDBMinSpec
|
|
35
|
+
| OKDBMaxSpec
|
|
36
|
+
| OKDBCountBySpec
|
|
37
|
+
| { $ref: OKDBRefSpec }
|
|
38
|
+
| OKDBCustomSpec;
|
|
39
|
+
|
|
40
|
+
/** Allowed granularity values for preset:'time' bucket configs */
|
|
41
|
+
export type OKDBBucketTimeGranularity = 'minute' | 'hour' | 'day' | 'week' | 'month' | 'quarter' | 'year';
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 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).
|
|
47
|
+
*/
|
|
48
|
+
export type OKDBBucketConfig =
|
|
49
|
+
| {
|
|
50
|
+
/** Dot path to the timestamp field on each source document (Unix ms or ISO string). */
|
|
51
|
+
field: string;
|
|
52
|
+
/** Time-based bucketing. Currently the only supported preset. */
|
|
53
|
+
preset: 'time';
|
|
54
|
+
/** Bucket size. One of: minute, hour, day, week, month, quarter, year. */
|
|
55
|
+
granularity: OKDBBucketTimeGranularity;
|
|
56
|
+
}
|
|
57
|
+
| {
|
|
58
|
+
/** Dot path to the grouping field on each source document. */
|
|
59
|
+
field: string;
|
|
60
|
+
/** Custom projection: maps a field value to a string bucket key. Not usable in synced environments. */
|
|
61
|
+
project: (value: unknown) => string;
|
|
62
|
+
/** Arbitrary label used in bucket keys and range queries. */
|
|
63
|
+
granularity: string;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** Options for views.range() */
|
|
67
|
+
export interface OKDBBucketRangeOptions {
|
|
68
|
+
/** Lower bound. ISO string, epoch ms, or Date for time-preset views; bucket key string for custom-projection views. */
|
|
69
|
+
from?: string | number | Date | null;
|
|
70
|
+
/** Upper bound (inclusive). Same shape as from. */
|
|
71
|
+
to?: string | number | Date | null;
|
|
72
|
+
/** Must match the view's configured granularity if provided. */
|
|
73
|
+
granularity?: string;
|
|
74
|
+
/** When true, includes the bucket containing Date.now(). Default false. */
|
|
75
|
+
includePartial?: boolean;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A single entry in the array returned by views.range() */
|
|
79
|
+
export interface OKDBBucketRangeEntry {
|
|
80
|
+
/** Canonical string key for this bucket (ISO date string for preset:'time'). */
|
|
81
|
+
bucketKey: string;
|
|
82
|
+
granularity: string;
|
|
83
|
+
/** Per-reducer aggregated values. Scalar → { value: N }; $countBy → { [groupKey]: { value: N } }. */
|
|
84
|
+
reducers: Record<string, OKDBScalarResult | OKDBCountByResult>;
|
|
85
|
+
/** Per-$ref sub-view aggregated values. */
|
|
86
|
+
refs: Record<string, Record<string, OKDBScalarResult | OKDBCountByResult>>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Options for views.listBuckets() */
|
|
90
|
+
export interface OKDBListBucketsOptions {
|
|
91
|
+
/** Required. Must equal the view's configured granularity. */
|
|
92
|
+
granularity: string;
|
|
93
|
+
from?: string | number | Date | null;
|
|
94
|
+
to?: string | number | Date | null;
|
|
95
|
+
/** Cap the number of results returned. */
|
|
96
|
+
limit?: number;
|
|
97
|
+
/** When true, returns latest-first. Default false (oldest-first). */
|
|
98
|
+
reverse?: boolean;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A single entry in the array returned by views.listBuckets() */
|
|
102
|
+
export interface OKDBBucketEntry {
|
|
103
|
+
/** Canonical string key for this bucket. */
|
|
104
|
+
bucketKey: string;
|
|
105
|
+
/** Number of source documents that fall in this bucket. */
|
|
106
|
+
count: number;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** View definition passed to views.create() */
|
|
1
110
|
export interface OKDBViewDefinition {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
111
|
+
/** Source type. Must be registered in this environment. */
|
|
112
|
+
type: string;
|
|
113
|
+
/** Optional sift-style filter. Only matching docs contribute to the view. */
|
|
114
|
+
filter?: OKDBFilter;
|
|
115
|
+
/** Optional declarative transform applied to each doc before reducers run. */
|
|
116
|
+
map?: OKDBViewMap;
|
|
117
|
+
/** Required, non-empty. Maps output field names to reducer specs. */
|
|
118
|
+
reduce: Record<string, OKDBReducerSpec>;
|
|
119
|
+
/**
|
|
120
|
+
* Optional. Enables per-bucket aggregation.
|
|
121
|
+
* Use preset:'time' for time-series bucketing (works over HTTP and in synced envs).
|
|
122
|
+
* Use project for custom string-key bucketing (JS SDK only, not syncable).
|
|
123
|
+
*/
|
|
124
|
+
bucket?: OKDBBucketConfig;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Shape returned for a scalar reducer output ($count, $sum, $avg, $min, $max) */
|
|
128
|
+
export interface OKDBScalarResult {
|
|
129
|
+
value: number | null;
|
|
130
|
+
/** Present when items:true was set — returns the live source docs for this bucket */
|
|
131
|
+
items?: () => unknown[];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Shape returned for a $countBy reducer output */
|
|
135
|
+
export type OKDBCountByResult = Record<string, OKDBScalarResult>;
|
|
136
|
+
|
|
137
|
+
/** Shape returned for a $ref slot */
|
|
138
|
+
export type OKDBRefResult = Record<string, OKDBScalarResult | OKDBCountByResult>;
|
|
139
|
+
|
|
140
|
+
/** The full output object returned by views.get() */
|
|
141
|
+
export type OKDBViewOutput = Record<string, OKDBScalarResult | OKDBCountByResult | OKDBRefResult>;
|
|
142
|
+
|
|
143
|
+
/** View lifecycle meta returned by views.getMeta() */
|
|
144
|
+
export interface OKDBViewMeta {
|
|
145
|
+
state: 'creating' | 'ready' | 'halted' | 'stopped' | 'resetting';
|
|
146
|
+
clock: number;
|
|
147
|
+
error: { clock: number; key: string; error: string } | null;
|
|
148
|
+
refs: Record<string, { state: string; clock: number; error: string | null }>;
|
|
149
|
+
/** Present on bucketed views. Tracks documents whose bucket field could not be parsed. */
|
|
150
|
+
bucketing?: { unbucketedCount: number };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Stored view definition returned by views.getDefinition() (includes name and createdAt) */
|
|
154
|
+
export type OKDBStoredViewDefinition = OKDBViewDefinition & { name: string; createdAt: number };
|
|
155
|
+
|
|
156
|
+
/** Options for views.remove() */
|
|
157
|
+
export interface OKDBViewRemoveOptions {
|
|
158
|
+
/**
|
|
159
|
+
* How to handle indexes auto-created by this view.
|
|
160
|
+
* Required when the view owns indexes that would otherwise become orphaned.
|
|
161
|
+
* - 'drop' — drop the index
|
|
162
|
+
* - 'keep' — retain the index (it becomes unowned)
|
|
163
|
+
*/
|
|
164
|
+
managedIndexes?: 'drop' | 'keep';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Custom reducer registration */
|
|
168
|
+
export interface OKDBCustomReducer {
|
|
169
|
+
apply(state: unknown, before: unknown, after: unknown, opts: unknown): unknown;
|
|
8
170
|
}
|
|
9
171
|
|
|
10
172
|
export declare class OKDBViews {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
173
|
+
/**
|
|
174
|
+
* Create a new materialized view. The source type must already be registered.
|
|
175
|
+
* Synchronously bootstraps existing documents then registers an incremental processor.
|
|
176
|
+
*/
|
|
177
|
+
create(name: string, definition: OKDBViewDefinition): Promise<{ name: string }>;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Read the current view output. Synchronous and O(1).
|
|
181
|
+
* Returns null if the view does not exist.
|
|
182
|
+
*/
|
|
183
|
+
get(name: string): OKDBViewOutput | null;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Get view lifecycle meta (state, clock, error).
|
|
187
|
+
* Returns null if the view does not exist.
|
|
188
|
+
*/
|
|
189
|
+
getMeta(name: string): OKDBViewMeta | null;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Get the stored view definition.
|
|
193
|
+
* Returns null if the view does not exist.
|
|
194
|
+
*/
|
|
195
|
+
getDefinition(name: string): Promise<OKDBStoredViewDefinition | null>;
|
|
196
|
+
|
|
197
|
+
/** List all view names in this environment. */
|
|
198
|
+
list(): Promise<string[]>;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Remove a view. Pass managedIndexes when the view owns auto-created indexes.
|
|
202
|
+
*/
|
|
203
|
+
remove(name: string, options?: OKDBViewRemoveOptions): Promise<void>;
|
|
204
|
+
|
|
205
|
+
/** Clears all accumulated state and re-scans from scratch. */
|
|
206
|
+
rebuild(name: string): Promise<void>;
|
|
207
|
+
|
|
208
|
+
/** Pause the view. Writes that arrive while stopped are replayed on start(). */
|
|
209
|
+
stop(name: string): Promise<void>;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Resume a stopped (or halted) view.
|
|
213
|
+
* Optimistically catches up with new inserts; performs a full rebuild otherwise.
|
|
214
|
+
*/
|
|
215
|
+
start(name: string): Promise<void>;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Register a custom reducer. Must be called before creating any view that uses it.
|
|
219
|
+
* The name must start with '$' and must not conflict with a built-in reducer.
|
|
220
|
+
*/
|
|
221
|
+
registerReducer(name: string, reducer: OKDBCustomReducer): void;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Query per-bucket aggregates for a bucketed view.
|
|
225
|
+
* Returns a sparse ordered array — only buckets with at least one document are included.
|
|
226
|
+
* Returns null if the view does not exist.
|
|
227
|
+
* Throws VIEW_NOT_BUCKETED if the view was not created with a bucket config.
|
|
228
|
+
*/
|
|
229
|
+
range(name: string, options?: OKDBBucketRangeOptions): OKDBBucketRangeEntry[] | null;
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* List populated bucket keys for a bucketed view, with per-bucket document counts.
|
|
233
|
+
* options.granularity is required and must equal the view's configured granularity.
|
|
234
|
+
* Throws VIEW_NOT_FOUND if the view does not exist.
|
|
235
|
+
* Throws VIEW_NOT_BUCKETED if the view was not created with a bucket config.
|
|
236
|
+
*/
|
|
237
|
+
listBuckets(name: string, options: OKDBListBucketsOptions): OKDBBucketEntry[];
|
|
238
|
+
|
|
239
|
+
/** @internal Called by OKDB after sync to activate views that arrived from peers. */
|
|
15
240
|
bootSyncedViews(): Promise<void>;
|
|
16
241
|
}
|