@kedem/okdb 2.3.5 → 2.5.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.
- package/CHANGELOG.md +427 -0
- package/algorithm-worker-thread.js +1 -1
- package/bin/okdb.js +1 -1
- package/docs/auth-and-sync.md +30 -5
- package/docs/auth-permissions.md +19 -6
- package/docs/change-log.md +79 -59
- package/docs/data-model.md +20 -18
- package/docs/deployment.md +25 -0
- package/docs/diagnostics.md +59 -17
- package/docs/embeddings.md +57 -6
- package/docs/fts.md +50 -5
- package/docs/functions.md +21 -8
- package/docs/http-api.md +139 -98
- package/docs/http-cluster.md +30 -8
- package/docs/indexes.md +5 -4
- package/docs/licensing.md +6 -1
- package/docs/logging.md +18 -0
- package/docs/migration.md +153 -2
- package/docs/pipelines.md +6 -1
- package/docs/plugins.md +12 -18
- package/docs/process-registry.md +50 -39
- package/docs/processors.md +40 -22
- package/docs/querying.md +50 -36
- package/docs/queue.md +110 -14
- package/docs/subscriptions.md +37 -24
- package/docs/sync.md +96 -37
- package/docs/time-machine.md +23 -17
- package/docs/transactions.md +11 -19
- package/docs/ttl.md +118 -67
- package/docs/upgrade-2.0.md +2 -2
- package/docs/views.md +75 -52
- package/okdb-functions-sandbox-worker.js +1 -1
- package/okdb-http-worker-child.js +1 -1
- package/okdb-views-bootstrap-worker.js +1 -1
- package/okdb.js +1 -1
- package/package.json +1 -1
- package/public/sections/db/parts/type-viewer.ok.js +1 -1
- package/public/sections/embeddings/parts/engines-overview.ok.js +1 -1
- package/public/sections/queue/parts/queue-jobs.ok.js +1 -1
- package/public/sections/system/modals/data-ops-modal.ok.js +1 -1
- package/types/environment.d.ts +320 -52
- package/types/features/admin.d.ts +8 -2
- package/types/features/api.d.ts +10 -16
- package/types/features/auth.d.ts +135 -16
- package/types/features/embeddings.d.ts +639 -31
- package/types/features/engines.d.ts +368 -14
- package/types/features/files.d.ts +0 -3
- package/types/features/fts.d.ts +98 -26
- package/types/features/functions.d.ts +175 -15
- package/types/features/mcp.d.ts +23 -10
- package/types/features/pipelines.d.ts +268 -9
- package/types/features/queue.d.ts +230 -34
- package/types/features/sync.d.ts +177 -14
- package/types/features/time-machine.d.ts +117 -16
- package/types/features/views.d.ts +230 -39
- package/types/index.d.ts +357 -254
- package/types/okdb.d.ts +397 -0
- package/types/options.d.ts +309 -48
- package/public/test-cron-input.html +0 -60
- package/public/test-json-input.html +0 -280
package/docs/sync.md
CHANGED
|
@@ -7,8 +7,8 @@ OKDB includes a peer-to-peer, last-write-wins (LWW) replication system. Nodes sy
|
|
|
7
7
|
## How it works
|
|
8
8
|
|
|
9
9
|
1. Every write increments the local clock and logs a change entry
|
|
10
|
-
2. When the clock advances,
|
|
11
|
-
3.
|
|
10
|
+
2. When the clock advances, the node pulls from each known peer and then pokes it over HTTP (`POST /api/sync/poke`)
|
|
11
|
+
3. A poked peer pulls a **delta** (`POST /api/sync/delta`) from the changed node
|
|
12
12
|
4. Each incoming change is compared to the local version using HLC timestamp
|
|
13
13
|
5. The newer timestamp wins; ties are broken by origin node ID (lexicographic)
|
|
14
14
|
6. Applied changes are written through the normal write path (with `origin` set to prevent echo-loops)
|
|
@@ -23,11 +23,7 @@ This is eventual consistency. It's designed for loosely-coupled multi-node topol
|
|
|
23
23
|
|
|
24
24
|
```javascript
|
|
25
25
|
const okdb = new OKDB('./node1', {
|
|
26
|
-
auth: {
|
|
27
|
-
tokens: { 'cluster-secret': ['admin'] },
|
|
28
|
-
},
|
|
29
26
|
sync: {
|
|
30
|
-
token: 'cluster-secret',
|
|
31
27
|
address: 'http://192.168.1.10:8080', // this node's public address
|
|
32
28
|
},
|
|
33
29
|
});
|
|
@@ -35,32 +31,42 @@ await okdb.open();
|
|
|
35
31
|
okdb.http.listen(8080);
|
|
36
32
|
```
|
|
37
33
|
|
|
38
|
-
Every node needs
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
34
|
+
Every node needs `sync.address` — the URL other nodes will use to reach this one. There is no
|
|
35
|
+
shared cluster secret: each node has its own Ed25519 keypair, and peers authenticate every sync
|
|
36
|
+
request by signature against the public keys stored in `~sync_nodes`. (`sync.token` is an
|
|
37
|
+
optional Bearer also sent on peer requests, as a fallback while a peer has not yet received this
|
|
38
|
+
node's public key.) See [Auth and Sync](./auth-and-sync.md#part-2--sync) for the details and the
|
|
39
|
+
full `sync` option table.
|
|
43
40
|
|
|
44
41
|
### Joining a cluster
|
|
45
42
|
|
|
43
|
+
Joining needs a credential on the remote **once** — a token that exists in node 1's `~tokens`
|
|
44
|
+
with `sync:read` + `sync:peer`, or a token name + password:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# on node 1
|
|
48
|
+
okdb token create --label 'cluster join' --permissions 'sync:read,sync:peer'
|
|
49
|
+
```
|
|
50
|
+
|
|
46
51
|
```javascript
|
|
47
52
|
// Node 2 joins node 1
|
|
48
|
-
await okdb.sync.join('http://192.168.1.10:8080');
|
|
53
|
+
await okdb.sync.join('http://192.168.1.10:8080', { token: process.env.OKDB_JOIN_TOKEN });
|
|
54
|
+
// or: { username, password }
|
|
49
55
|
```
|
|
50
56
|
|
|
51
|
-
`join` does
|
|
57
|
+
`join` does four things:
|
|
52
58
|
|
|
53
|
-
1. Registers node 1 as a known peer in `~sync_nodes` type (which itself syncs)
|
|
54
|
-
2.
|
|
55
|
-
3.
|
|
59
|
+
1. Registers node 1 as a known peer in `~sync_nodes` type (which itself syncs) and creates a data link
|
|
60
|
+
2. Sends this node's record (with its public key) to node 1's `/api/sync/join`
|
|
61
|
+
3. Pulls an initial delta from node 1
|
|
62
|
+
4. From that point, auto-reconcile keeps them in sync on clock change — with signed requests
|
|
56
63
|
|
|
57
64
|
### Multi-node example
|
|
58
65
|
|
|
59
66
|
```javascript
|
|
60
67
|
const nodes = [8080, 8081, 8082].map((port) => {
|
|
61
68
|
const okdb = new OKDB(`./node-${port}`, {
|
|
62
|
-
|
|
63
|
-
sync: { token: 'secret', address: `http://localhost:${port}` },
|
|
69
|
+
sync: { address: `http://localhost:${port}` },
|
|
64
70
|
});
|
|
65
71
|
okdb._port = port;
|
|
66
72
|
return okdb;
|
|
@@ -69,9 +75,9 @@ const nodes = [8080, 8081, 8082].map((port) => {
|
|
|
69
75
|
await Promise.all(nodes.map((k) => k.open()));
|
|
70
76
|
for (const node of nodes) node.http.listen(node._port);
|
|
71
77
|
|
|
72
|
-
// Join node 1 and 2 to node 0
|
|
78
|
+
// Join node 1 and 2 to node 0, using a join token created on node 0
|
|
73
79
|
for (const node of nodes.slice(1)) {
|
|
74
|
-
await node.sync.join('http://localhost:8080');
|
|
80
|
+
await node.sync.join('http://localhost:8080', { token: joinToken });
|
|
75
81
|
}
|
|
76
82
|
```
|
|
77
83
|
|
|
@@ -83,20 +89,49 @@ for (const node of nodes.slice(1)) {
|
|
|
83
89
|
|
|
84
90
|
Peers are stored in the `~sync_nodes` OKDB type — which is itself synced. So adding a peer on one node propagates to all other nodes automatically, forming a mesh.
|
|
85
91
|
|
|
86
|
-
Local peer progress (which clock was last seen from each peer) is stored outside of sync in `~system` — it's local state that doesn't replicate.
|
|
92
|
+
Local peer progress (which clock was last seen from each peer) is stored outside of sync in `~system` (the raw `__sync:peer_state` sub-db) — it's local state that doesn't replicate.
|
|
87
93
|
|
|
88
94
|
### Delta endpoint
|
|
89
95
|
|
|
90
96
|
```
|
|
91
|
-
|
|
92
|
-
|
|
97
|
+
POST /api/sync/delta
|
|
98
|
+
Content-Type: application/json
|
|
99
|
+
|
|
100
|
+
{ "from_clocks": { "default": 120, "~system": 40 }, "limit": 500, "envs": null }
|
|
93
101
|
```
|
|
94
102
|
|
|
95
|
-
Returns
|
|
103
|
+
Returns `{ clock, clocks, changes }` (+ `denied` when requested envs were outside the link): up
|
|
104
|
+
to `limit` change records after each env's clock (the puller asks for its `delta_limit`, default 500) across the changelog-enabled environments the requester may pull (see Data links above).
|
|
105
|
+
Each change includes `_env` so the receiver routes it to the right environment. The request is
|
|
106
|
+
Ed25519-signed by the pulling node.
|
|
107
|
+
|
|
108
|
+
### Data links (scope and direction)
|
|
109
|
+
|
|
110
|
+
Once any link exists (`join()` creates a bidirectional one unless `dataLink: false`), data flows
|
|
111
|
+
only over links — `okdb.sync.upsertLink(peerId, { direction, envs, enabled })`, or
|
|
112
|
+
`PUT /api/sync/links/:nodeA/:nodeB`:
|
|
113
|
+
|
|
114
|
+
- `direction` — `both`, `lo_to_hi` or `hi_to_lo`, canonical over the alphabetically sorted node
|
|
115
|
+
pair (`lo_to_hi`: the lower id sends, the higher id receives).
|
|
116
|
+
- `envs` — the data envs the link carries; `null` = all.
|
|
117
|
+
|
|
118
|
+
The link is **enforced on both ends, not trusted from the request**. The node serving
|
|
119
|
+
`/api/sync/delta` serves a signed peer only `~system` plus the data envs the link lets it send to
|
|
120
|
+
that peer — asking for another env (or pulling against the link's direction) gets those envs
|
|
121
|
+
omitted and listed in the response's `denied`. The pulling node applies only envs it asked for and
|
|
122
|
+
the link lets it receive, whatever a peer sends. A signed `/api/sync/poke` only triggers a pull
|
|
123
|
+
from the signer, and a pull with no explicit env list (join, poke, `reconcilePeer`) asks for the
|
|
124
|
+
link's envs only.
|
|
125
|
+
|
|
126
|
+
`~system` is the control plane and always flows with every registered peer, link or not: it
|
|
127
|
+
carries `~sync_nodes`, `~sync_links`, `~envs`, `~tokens`, `~licenses`, engine and pipeline
|
|
128
|
+
declarations. So a link scopes _data_, not trust — see
|
|
129
|
+
[Auth and Sync → What a peer can do](./auth-and-sync.md#what-a-peer-can-do). With no link anywhere
|
|
130
|
+
the cluster runs in legacy full-mesh mode (every env with every peer).
|
|
96
131
|
|
|
97
132
|
### Multi-environment sync
|
|
98
133
|
|
|
99
|
-
The delta spans **
|
|
134
|
+
The delta spans **every open changelog-enabled environment** simultaneously — every user env, plus `~system` (the control plane: `~sync_nodes`, `~tokens`, …) and the embeddings vector envs (`~<env>:emb:<type>`). The response carries a `clocks` map (`{ default: N, '~system': N, ... }`), and each environment's cursor advances independently. Custom environments created via `createEnvironment` are included automatically — every user env enables its changelog and participates in sync (the per-env `sync` option was removed in 2.0 for user envs).
|
|
100
135
|
|
|
101
136
|
---
|
|
102
137
|
|
|
@@ -126,9 +161,9 @@ const info = okdb.sync.info();
|
|
|
126
161
|
// reconciling: [],
|
|
127
162
|
// }
|
|
128
163
|
|
|
129
|
-
//
|
|
164
|
+
// This node's own peer record (known peers: GET /api/sync/peers)
|
|
130
165
|
const self = okdb.sync.getSelfNode();
|
|
131
|
-
// { id, address, meta, updated }
|
|
166
|
+
// { id, address, meta, tags, updated, publicKey, ... }
|
|
132
167
|
```
|
|
133
168
|
|
|
134
169
|
---
|
|
@@ -140,20 +175,44 @@ LWW depends on timestamps. If two nodes have significantly different system cloc
|
|
|
140
175
|
:::
|
|
141
176
|
|
|
142
177
|
:::note Non-syncable data
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
178
|
+
Not replicated — rebuilt or kept per node:
|
|
179
|
+
|
|
180
|
+
- **Derived indexes and state**: FTS posting lists (separate `~fts/` LMDB files), secondary
|
|
181
|
+
indexes, view state, time-machine history and the in-memory HNSW search graphs. Each node
|
|
182
|
+
rebuilds them locally from the synced data.
|
|
183
|
+
- **Changelog-less internal envs**: `~sub` (subscriptions), `~log`, `~processes` — opened
|
|
184
|
+
`sync: false`, so they have nothing to ship.
|
|
185
|
+
- **Node-local `~system` state**: identity + keypair, `__tokenSecret`, locks, processor cursors,
|
|
186
|
+
peer progress (raw keys / sub-dbs, not in the changelog), and — as of 2.4.0 — the
|
|
187
|
+
`~migrations` log, which records which layout migrations ran on _this_ store's files and is
|
|
188
|
+
never sent in or applied from a delta.
|
|
189
|
+
|
|
190
|
+
Vector data **does** replicate: the raw vectors live in the changelog-enabled
|
|
191
|
+
`~<env>:emb:<type>` envs, so an expensive embedding is computed once and shipped; each node
|
|
192
|
+
reconciles its search graph against the synced vectors (see [Embeddings](./embeddings.md)).
|
|
148
193
|
:::
|
|
149
194
|
|
|
150
195
|
---
|
|
151
196
|
|
|
152
197
|
## UDP discovery bus
|
|
153
198
|
|
|
154
|
-
`okdb-bus` sends
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
199
|
+
`okdb-bus` sends UDP multicast datagrams on `239.1.2.3:30303` (POKE on commit, DRAIN on
|
|
200
|
+
compaction, …) so that the **processes sharing one store path** hear each other's writes — a
|
|
201
|
+
write in a passive HTTP worker wakes the process that drives sync, which then reconciles and
|
|
202
|
+
pokes its peers. It is not how peer nodes learn about new data; that is the HTTP poke above.
|
|
203
|
+
Loss is acceptable — sync reconciles correctly even if some pokes are dropped.
|
|
204
|
+
|
|
205
|
+
Pokes are **throttled per `(env, type)`**: the first change sends immediately, further changes
|
|
206
|
+
inside the window collapse into one trailing send, so a sustained writer emits ~one datagram per
|
|
207
|
+
window per type rather than one per changed document. The window is
|
|
208
|
+
`OKDB_BUS_POKE_WINDOW_MS` (default `10`; `0` = unthrottled). Control signals (DRAIN,
|
|
209
|
+
ENV_RELEASE, PROC, TYPE_DROP) are never throttled.
|
|
210
|
+
|
|
211
|
+
The bus is **enabled whenever the shmbuf native binding is present**, but a poke only goes on
|
|
212
|
+
the wire when another instance holds the env: at each send the writer checks the env's shared
|
|
213
|
+
writer cells, and if it is the only holder the poke is delivered in-process instead (same-process
|
|
214
|
+
listeners — changefeed, `~sub` reload — still fire; no datagram). A peer that opens the env
|
|
215
|
+
mid-window receives the trailing poke. Packets are framed with a per-store identity, so other
|
|
216
|
+
okdb processes on the host drop a store's packets without acting on them. The `bus`
|
|
217
|
+
constructor option was removed in 2.0 (it is load-bearing for POKE/DRAIN coherence and so is
|
|
159
218
|
derived, not asked). See [Upgrading to 2.0](upgrade-2.0.md).
|
package/docs/time-machine.md
CHANGED
|
@@ -4,10 +4,12 @@ Time Machine tracks the change history of individual documents. For each tracked
|
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
Time Machine works at the **type** level. You opt in a type for tracking; from that moment,
|
|
7
|
+
Time Machine works at the **type** level. You opt in a type for tracking; from that moment, writes to that type are recorded as diffs against the previous state. Tracking is non-blocking and asynchronous — a per-type `single` processor drains the change log after the fact (debounced by `flushDelayMs`, default 1000 ms), so writes are not slowed down. Because it drains the deduplicated change log in batches, several writes to the same key that land within one drain collapse into a single diff.
|
|
8
|
+
|
|
9
|
+
Time Machine is an **opt-in constructor feature**: `env.timeMachine` exists only when the instance is constructed with `timeMachine` set (`true`, or an options object). Without it, `env.timeMachine` is `undefined` and the HTTP routes answer 404 ("Time machine not available"). The `okdb` CLI server turns it on by default (set `"timeMachine": false` in the config file to disable). It is also skipped for `~`-prefixed system envs and when the active license does not include the `timeMachine` feature.
|
|
8
10
|
|
|
9
11
|
```js
|
|
10
|
-
const db = new OKDB('./mydb');
|
|
12
|
+
const db = new OKDB('./mydb', { timeMachine: true }); // or { timeMachine: { flushDelayMs: 1000 } }
|
|
11
13
|
await db.open();
|
|
12
14
|
const env = db.env('myenv');
|
|
13
15
|
|
|
@@ -46,27 +48,30 @@ When `enableAll()` is called, all types registered at that moment are enabled. A
|
|
|
46
48
|
### Full history for a key
|
|
47
49
|
|
|
48
50
|
```js
|
|
49
|
-
const
|
|
50
|
-
limit: 50, // max entries (optional)
|
|
51
|
-
|
|
51
|
+
const { diffs, head } = env.timeMachine.getHistory('Order', 'order-42', {
|
|
52
|
+
limit: 50, // max entries, oldest first (optional)
|
|
53
|
+
maxClock: someClock, // only diffs at or before this clock (optional)
|
|
52
54
|
after: someClock, // only diffs after this clock (optional)
|
|
53
55
|
});
|
|
54
|
-
//
|
|
56
|
+
// diffs: [{ clock, fromClock, timestamp, put: {...}, delete: [...] }, ...] (ascending by clock)
|
|
57
|
+
// head: { value, clock } — the latest tracked value, or null (deleted / never tracked)
|
|
55
58
|
```
|
|
56
59
|
|
|
57
|
-
|
|
60
|
+
The JS option for the upper bound is `maxClock` (the HTTP route's `before` query param maps to it — passing `before` to the JS method is ignored).
|
|
61
|
+
|
|
62
|
+
Each diff entry describes what changed at that clock:
|
|
58
63
|
|
|
59
64
|
- `put` — fields that were set or updated (nested paths for schema-aware types)
|
|
60
65
|
- `delete` — field paths that were removed
|
|
61
|
-
- `clock` —
|
|
62
|
-
- `fromClock` — the previous
|
|
63
|
-
- `timestamp` — wall-clock time the diff was recorded
|
|
66
|
+
- `clock` — the env change-log clock the diff was recorded at (not an HLC value)
|
|
67
|
+
- `fromClock` — the clock of the previous diff (0 for the first one)
|
|
68
|
+
- `timestamp` — wall-clock time (ms) the diff was recorded
|
|
64
69
|
|
|
65
70
|
### Point-in-time reconstruction
|
|
66
71
|
|
|
67
72
|
```js
|
|
68
73
|
const value = env.timeMachine.getStateAt('Order', 'order-42', targetClock);
|
|
69
|
-
// Returns the document value
|
|
74
|
+
// Returns the document value as of `targetClock` (inclusive), or undefined if no diff exists at or before it
|
|
70
75
|
```
|
|
71
76
|
|
|
72
77
|
### Change log across types
|
|
@@ -75,6 +80,7 @@ const value = env.timeMachine.getStateAt('Order', 'order-42', targetClock);
|
|
|
75
80
|
const changes = [...env.timeMachine.getChanges('Order', fromClock, toClock)];
|
|
76
81
|
// Or all types:
|
|
77
82
|
const allChanges = [...env.timeMachine.getChanges(null, fromClock, toClock)];
|
|
83
|
+
// Each: { clock, type, key, put, delete, fromClock, timestamp } — both bounds inclusive
|
|
78
84
|
```
|
|
79
85
|
|
|
80
86
|
## Inspecting status
|
|
@@ -84,23 +90,23 @@ const allChanges = [...env.timeMachine.getChanges(null, fromClock, toClock)];
|
|
|
84
90
|
const status = env.timeMachine.status('Order');
|
|
85
91
|
// {
|
|
86
92
|
// type: 'Order',
|
|
87
|
-
// enabled: true,
|
|
93
|
+
// enabled: true, // a processor is registered in this instance
|
|
88
94
|
// startClock: 1234567,
|
|
89
95
|
// lastProcessedClock: 9876543,
|
|
90
96
|
// headCount: 412,
|
|
91
|
-
//
|
|
97
|
+
// ...processorStatus // state, lag, lastClock, mode, … (when enabled)
|
|
92
98
|
// }
|
|
93
99
|
|
|
94
100
|
// Summary across all types
|
|
95
101
|
const summary = env.timeMachine.status();
|
|
96
|
-
// { enabled
|
|
102
|
+
// { enabled, autoEnable, startClock, lastProcessedClock, envClock, headCount, disabledAt, types: [...] }
|
|
97
103
|
|
|
98
104
|
// Check if a type is enabled
|
|
99
105
|
env.timeMachine.isEnabled('Order'); // boolean
|
|
100
106
|
|
|
101
|
-
// List
|
|
107
|
+
// List every non-system type in the env with its tracking state (sorted by name)
|
|
102
108
|
env.timeMachine.list();
|
|
103
|
-
// [{ type: 'Order', enabled: true,
|
|
109
|
+
// [{ type: 'Order', enabled: true, state: 'online', lastClock: ..., lag: 0 }, ...]
|
|
104
110
|
```
|
|
105
111
|
|
|
106
112
|
## HTTP API
|
|
@@ -128,7 +134,7 @@ env.timeMachine.list();
|
|
|
128
134
|
|
|
129
135
|
History and point-in-time routes return 409 if the requested type is not currently enabled.
|
|
130
136
|
|
|
131
|
-
Query params for `/history`: `limit`, `before` (max clock, inclusive), `after` (min clock, exclusive).
|
|
137
|
+
Query params for `/history`: `limit`, `before` (max clock, inclusive), `after` (min clock, exclusive). The response is the `{ diffs, head }` object. `/at/:clock` returns `{ value, clock }`, or 404 when no state exists at that clock. `/changes` defaults `from` to 0 and `to` to the end of the log.
|
|
132
138
|
|
|
133
139
|
## Schema-aware diffs
|
|
134
140
|
|
package/docs/transactions.md
CHANGED
|
@@ -46,7 +46,7 @@ txn.rollback();
|
|
|
46
46
|
|
|
47
47
|
### How it works
|
|
48
48
|
|
|
49
|
-
`OKDBTransaction` accumulates operations in memory. On `commit()`, it replays them all inside a single
|
|
49
|
+
`OKDBTransaction` accumulates operations in memory. On `commit()`, it replays them all inside a single LMDB write transaction, run through the env's write funnel (`OKDBWriter`). Every secondary index update and change-log entry for all operations is written atomically in that one transaction — there is no partial state.
|
|
50
50
|
|
|
51
51
|
---
|
|
52
52
|
|
|
@@ -94,15 +94,14 @@ await txn.commit(); // both succeed or both fail
|
|
|
94
94
|
| `OKDBTypeNotRegisteredError` | `TYPE_NOT_REGISTERED` | write to unknown type |
|
|
95
95
|
| `OKDBIndexNotRegisteredError` | `INDEX_NOT_REGISTERED` | query on unknown index |
|
|
96
96
|
|
|
97
|
-
All errors extend `OKDBError` and carry a `code` property.
|
|
97
|
+
All errors extend `OKDBError` and carry a `code` property. The error classes are not exported
|
|
98
|
+
from the package — branch on `err.code`:
|
|
98
99
|
|
|
99
100
|
```javascript
|
|
100
|
-
const { OKDBError, OKDBVersionMismatchError } = require('okdb/okdb-error');
|
|
101
|
-
|
|
102
101
|
try {
|
|
103
102
|
await okdb.put('products', 'p42', newValue, { ifVersion: oldVersion });
|
|
104
103
|
} catch (err) {
|
|
105
|
-
if (err
|
|
104
|
+
if (err.code === 'VERSION_MISMATCH') {
|
|
106
105
|
// retry logic
|
|
107
106
|
} else {
|
|
108
107
|
throw err;
|
|
@@ -114,10 +113,10 @@ try {
|
|
|
114
113
|
|
|
115
114
|
## HTTP API — bulk writes
|
|
116
115
|
|
|
117
|
-
The `/transaction` endpoint is the right tool for bulk ingest over HTTP. It accepts an array of operations and commits them all in a single LMDB transaction.
|
|
116
|
+
The `/transaction` endpoint is the right tool for bulk ingest over HTTP. It accepts an array of operations and commits them all in a single LMDB transaction. The route is `POST /api/transaction` for the default env, or `POST /api/env/<env>/transaction` for any env (see [HTTP API](./http-api.md#environment-prefix)). The target types must already be registered.
|
|
118
117
|
|
|
119
118
|
```bash
|
|
120
|
-
curl -X POST http://localhost:8080/api/default/transaction \
|
|
119
|
+
curl -X POST http://localhost:8080/api/env/default/transaction \
|
|
121
120
|
-H "Authorization: Bearer my-token" \
|
|
122
121
|
-H "Content-Type: application/json" \
|
|
123
122
|
-d '{
|
|
@@ -138,19 +137,12 @@ Each operation is `{ action, type, key, … }`:
|
|
|
138
137
|
| `patch` | `patch` (required), `ifVersion` | Same patch format as `PATCH /item/:key` |
|
|
139
138
|
| `remove` | `ifVersion` | Throws if key absent |
|
|
140
139
|
|
|
141
|
-
The body
|
|
142
|
-
|
|
143
|
-
```bash
|
|
144
|
-
curl -X POST http://localhost:8080/api/default/transaction \
|
|
145
|
-
-H "Authorization: Bearer my-token" \
|
|
146
|
-
-H "Content-Type: application/json" \
|
|
147
|
-
-d '[
|
|
148
|
-
{ "action": "put", "type": "prices", "key": "sku-001", "value": { "price": 9.99 } },
|
|
149
|
-
{ "action": "put", "type": "prices", "key": "sku-002", "value": { "price": 19.99 } }
|
|
150
|
-
]'
|
|
151
|
-
```
|
|
140
|
+
The body may be the `{ "operations": [...] }` wrapper shown above or the bare operations array
|
|
141
|
+
(`[ { "action": "put", … }, … ]`); both are validated the same way.
|
|
152
142
|
|
|
153
|
-
Responds `
|
|
143
|
+
Responds `200` with `data: null` on success. If any operation fails (validation, unknown type,
|
|
144
|
+
`ifVersion` mismatch, `remove` of a missing key, …), the entire transaction is aborted and
|
|
145
|
+
nothing is written.
|
|
154
146
|
|
|
155
147
|
---
|
|
156
148
|
|