@kedem/okdb 3.1.0 → 3.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/CHANGELOG.md CHANGED
@@ -4,6 +4,27 @@ All notable changes to `@kedem/okdb` are documented here. Format loosely follows
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.0.0/); versions follow [SemVer](https://semver.org/).
5
5
  Dates are release dates.
6
6
 
7
+ ## [3.1.2] - 2026-10-26
8
+
9
+ ### Fixed
10
+
11
+ - **`env.compact()` / `repair()` no longer refuse a store that has file-backed bodies.** The logical rebuild did not know the
12
+ `~file:*` sub-DBs (`~file:blob_status`, the blob reference counts) and refused with "unclassified sub-DB(s)"; they are now
13
+ copied as durable state. Unknown sub-DBs still refuse before a byte is written.
14
+ - **The compaction swap never leaves `data.mdb` absent.** The old file is hard-linked to `data.mdb.pre-compact`, then the compacted
15
+ file replaces `data.mdb` in one atomic rename (falls back to the two-rename form if the link fails).
16
+
17
+ ## [3.1.1] - 2026-10-22
18
+
19
+ ### Added
20
+
21
+ - **Sparse indexes.** `registerIndex(type, fields, { sparse: true })` (also in `ensureType` index specs, the HTTP `POST .../index/:idx`
22
+ body, the MCP `okdb_index` tool and the admin _Create Index_ dialog) indexes only records that have a non-null value for every
23
+ indexed field. By default a record missing a field still gets an entry with `null` in that position; on a type with many
24
+ indexes that is most of the cost of a put (110 indexes: ~0.6 ms -> ~0.2 ms CPU per put). A sparse index cannot be queried for
25
+ missing values, is fixed at registration, and is refused on geo indexes. Existing indexes are unchanged. See
26
+ [docs/indexes.md](docs/indexes.md#sparse-indexes).
27
+
7
28
  ## [3.1.0] - 2026-10-19
8
29
 
9
30
  Planned as 3.1.0. **The on-disk layout changes** (see the upgrade notes below).
package/docs/http-api.md CHANGED
@@ -503,7 +503,7 @@ POST /api[/env/:env]/type/:type → register a type
503
503
  DELETE /api[/env/:env]/type/:type → drop a type
504
504
 
505
505
  GET /api[/env/:env]/type/:type/indexes → list indexes
506
- POST /api[/env/:env]/type/:type/index/:idx → register an index (body: { type?, precision?, unique? })
506
+ POST /api[/env/:env]/type/:type/index/:idx → register an index (body: { type?, precision?, unique?, sparse? })
507
507
  DELETE /api[/env/:env]/type/:type/index/:idx → drop an index
508
508
  POST /api[/env/:env]/type/:type/index/:idx/reset → rebuild an index
509
509
  GET /api[/env/:env]/type/:type/index/:idx/count?start=<json>&end=<json> → count index entries in range
package/docs/indexes.md CHANGED
@@ -20,6 +20,10 @@ await okdb.registerIndex('articles', ['author', 'publishedAt']);
20
20
  // Unique constraint
21
21
  await okdb.registerIndex('users', ['email'], { unique: true });
22
22
 
23
+ // Sparse — only records that have every indexed field
24
+ await okdb.registerType('things');
25
+ await okdb.registerIndex('things', ['facet', 'weight'], { sparse: true });
26
+
23
27
  // Geospatial
24
28
  await okdb.registerIndex('places', ['location'], { type: 'geo', precision: 7 });
25
29
  ```
@@ -213,6 +217,46 @@ Geo indexes cannot be unique. Geohash collisions make uniqueness semantics undef
213
217
 
214
218
  ---
215
219
 
220
+ ## Sparse indexes
221
+
222
+ By default an index holds **every** record of its type. A record that lacks an indexed field is still entered, with `null` in
223
+ that position, so `{ facet: 'x' }` appears in an index on `['facet', 'weight']` under the key `['x', null]`. That keeps the
224
+ index complete, but when a type has many indexes and most records only carry a few of the fields, every write pays for index
225
+ entries nobody will ever query.
226
+
227
+ A **sparse** index leaves those records out: a record gets an entry only if it has a non-null value for **every** field of the
228
+ index.
229
+
230
+ ```javascript
231
+ await okdb.registerType('things');
232
+ await okdb.registerIndex('things', ['facet', 'weight'], { sparse: true });
233
+
234
+ await okdb.put('things', 'a', { facet: 'x', weight: 3 }); // indexed
235
+ await okdb.put('things', 'b', { facet: 'x' }); // no `weight`: not in this index
236
+ await okdb.put('things', 'c', { facet: 'x', weight: null }); // null counts as missing: not in this index
237
+
238
+ [...okdb.byIndex('things', ['facet', 'weight'])].length; // 1
239
+ ```
240
+
241
+ **When to use it.** Many indexes on one type, each covering fields that only some records have (a "facet"/attribute style
242
+ model). Writes get cheaper — a type with 110 such indexes went from about 0.6 ms to 0.2 ms of CPU per put — and the index
243
+ files shrink.
244
+
245
+ **What you give up.** A record missing a field never shows up in `byIndex`, `getIndex`, counts or prefix scans on that
246
+ index — you cannot ask a sparse index for "records where this field is missing". Use a regular index if you need that.
247
+ Everything else is unchanged: a record that gains the field enters the index on its next write, one that loses it leaves,
248
+ and removing a record removes its entry.
249
+
250
+ **Rules.**
251
+
252
+ - Chosen when the index is registered and fixed afterwards. To change it, drop the index and register it again.
253
+ - Combines with `unique` (a unique index already ignores records with a null field, so `unique + sparse` only saves space).
254
+ - Not available on geo indexes — they already skip records without a location (`INVALID_INDEX_OPTIONS`).
255
+ - Shown as `sparse: true` in the index metadata (`getIndexMeta`, `GET .../indexes`), replicated to peers with the index, and kept
256
+ by export/import. The admin UI shows a _sparse_ badge and a checkbox in _Create Index_.
257
+
258
+ ---
259
+
216
260
  ## Geo indexes
217
261
 
218
262
  Geospatial indexes use geohash encoding to map `{ lat, lon }` coordinates into a lexicographically ordered string space.
@@ -259,6 +303,7 @@ await okdb.ensureType('products', {
259
303
  ['category', 'price'],
260
304
  { fields: ['location'], type: 'geo', precision: 6 },
261
305
  { fields: ['sku'], unique: true },
306
+ { fields: ['category', 'discount'], sparse: true },
262
307
  ],
263
308
  });
264
309
  ```
package/docs/querying.md CHANGED
@@ -192,6 +192,34 @@ There is no query planner. If you pass `index`, OKDB uses it without cost analys
192
192
 
193
193
  ---
194
194
 
195
+ ### Paging with `query()`
196
+
197
+ With `index`, page by primary key: pass the last row's `key` of the previous page as `startKey`. That row is skipped, and rows that
198
+ share its index key are paged correctly (several records with the same value never get lost or repeated at a page boundary). This
199
+ works forward and with `reverse: true`, and `endKey` stops before a key the same way.
200
+
201
+ ```javascript
202
+ await okdb.ensureType('orders', { indexes: [['status', 'createdAt']] });
203
+ let last;
204
+ for (;;) {
205
+ const page = Array.from(
206
+ okdb.query(
207
+ 'orders',
208
+ { status: 'open' },
209
+ { index: ['status', 'createdAt'], limit: 50, ...(last ? { startKey: last } : {}) },
210
+ ),
211
+ );
212
+ if (!page.length) break;
213
+ // ... handle page ...
214
+ last = page[page.length - 1].key;
215
+ }
216
+ ```
217
+
218
+ `startIndex` / `endIndex` take index values instead: `startIndex` is inclusive, `endIndex` exclusive (append
219
+ `OKDBEnv.HIGH_SENTINEL` to include the last value), and they are always low / high, also with `reverse`. If the record named by
220
+ `startKey` has been deleted since the previous page, the scan finds no boundary and returns nothing; fall back to `startIndex`
221
+ from the last row's index values in that case.
222
+
195
223
  ## Index scans
196
224
 
197
225
  `byIndex` iterates entries in index order, returning `{ key, value, version, indexKey }`. `indexKey` is the stored index key: the indexed field values followed by a trailing `null` sentinel (`['x', null]` for a one-field index, `['x', 3, null]` for two fields), as in 2.x.
@@ -236,6 +264,8 @@ for (const { key, value } of okdb.byIndex('articles', ['author', 'publishedAt'],
236
264
  | `offset` | number | Skip this many entries |
237
265
  | `includeViolations` | boolean | For unique indexes: include all violating entries |
238
266
 
267
+ Bounds are written from field values alone, and `start` is inclusive while `end` is exclusive: `start: ['b', 1], end: ['b', 3]` returns the entries for `['b', 1]` and `['b', 2]`. To include the last value, append `OKDBEnv.HIGH_SENTINEL`: `end: ['b', 3, OKDBEnv.HIGH_SENTINEL]`. This holds identically for `reverse` (every entry ends in a `null` so a bound never equals a stored key). To page, resume after the last row with `start: [...lastValues, OKDBEnv.HIGH_SENTINEL]` (forward) or `end: lastValues` (backward), where `lastValues` is the row's `indexKey` without its trailing `null`.
268
+
239
269
  `start`/`end` are always the lower/upper bound regardless of `reverse` — `byIndex`/`getIndex`/`countByIndex` swap them internally for a descending scan, so you never need to. Unrecognized option keys throw immediately rather than being silently ignored.
240
270
 
241
271
  `getIndex` returns primary keys from the index sub-database (not full records) as a **lazy iterable**. Spread to an array if you need `.length`: `[...env.getIndex(...)]`.