@kedem/okdb 1.1.0 → 1.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kedem/okdb",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "A fast, type-oriented database — strong consistency and rich indexing at the core, with sync, vector embeddings, full-text search, and AI tooling built in. Designed for the AI era.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "engines": {
@@ -37,6 +37,75 @@ export type OKDBReducerSpec =
37
37
  | { $ref: OKDBRefSpec }
38
38
  | OKDBCustomSpec;
39
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
+
40
109
  /** View definition passed to views.create() */
41
110
  export interface OKDBViewDefinition {
42
111
  /** Source type. Must be registered in this environment. */
@@ -47,6 +116,12 @@ export interface OKDBViewDefinition {
47
116
  map?: OKDBViewMap;
48
117
  /** Required, non-empty. Maps output field names to reducer specs. */
49
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;
50
125
  }
51
126
 
52
127
  /** Shape returned for a scalar reducer output ($count, $sum, $avg, $min, $max) */
@@ -71,6 +146,8 @@ export interface OKDBViewMeta {
71
146
  clock: number;
72
147
  error: { clock: number; key: string; error: string } | null;
73
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 };
74
151
  }
75
152
 
76
153
  /** Stored view definition returned by views.getDefinition() (includes name and createdAt) */
@@ -143,6 +220,22 @@ export declare class OKDBViews {
143
220
  */
144
221
  registerReducer(name: string, reducer: OKDBCustomReducer): void;
145
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
+
146
239
  /** @internal Called by OKDB after sync to activate views that arrived from peers. */
147
240
  bootSyncedViews(): Promise<void>;
148
241
  }