@nats-io/kv 3.4.0-4 → 3.4.1-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 (2) hide show
  1. package/README.md +97 -0
  2. package/package.json +3 -3
package/README.md CHANGED
@@ -170,3 +170,100 @@ watch.stop();
170
170
  // danger: destroys all values in the KV!
171
171
  await kv.destroy();
172
172
  ```
173
+
174
+ ## Per-Key TTLs
175
+
176
+ Server 2.11+ supports automatic key removal. Enabled by creating the bucket with
177
+ the `markerTTL` option (milliseconds, minimum 1000). Setting it does two things
178
+ on the backing stream:
179
+
180
+ - enables per-message TTLs (`allow_msg_ttl = true`)
181
+ - when a key is removed by `max_age` or a per-key TTL, the server emits a
182
+ tombstone marker; `markerTTL` is how long that auto-emitted marker stays in
183
+ the bucket before the server removes it too
184
+
185
+ Per-key TTL is exposed in two places only:
186
+
187
+ - `kv.create(key, value, "<duration>")` — first write only
188
+ - `kv.purge(key, { ttl: "<duration>" })` — lifetime of the purge marker
189
+
190
+ Duration is a string: `2s`, `2m`, `1.5h`. A bare number means seconds. Minimum
191
+ resolution is one second; See https://pkg.go.dev/time#ParseDuration for more
192
+ information. `put()` does not accept a TTL — if it did, expiry of the latest
193
+ revision could surface an older history entry.
194
+
195
+ ### Purge with a TTL on the marker
196
+
197
+ ```typescript
198
+ // markerTTL on the bucket is required for any per-key/marker TTL to work
199
+ const kv = await new Kvm(js).create("A", { markerTTL: 2_000 });
200
+ await kv.create("k", "hello");
201
+
202
+ // purge rolls up history immediately; the purge marker itself is removed
203
+ // after 2s thanks to the per-call ttl
204
+ await kv.purge("k", { ttl: "2s" });
205
+
206
+ // ~2s later the marker is gone and the key is invisible to new watchers/gets
207
+ ```
208
+
209
+ ### Bucket-wide TTL with auto-emitted markers
210
+
211
+ ```typescript
212
+ // bucket-wide ttl (max_age) makes every entry live at most 1s;
213
+ // markerTTL keeps the auto-emitted tombstone for 2s after expiry
214
+ const kv = await new Kvm(js).create("A", { markerTTL: 2_000, ttl: 1_000 });
215
+
216
+ await kv.create("k", "hello");
217
+
218
+ // deferred can be imported from @nats-io/nats-core
219
+ const d = deferred();
220
+ const now = Date.now();
221
+ const iter = await kv.watch();
222
+ (async () => {
223
+ for await (const e of iter) {
224
+ console.log(Date.now() - now, e.operation, e.key);
225
+ // server emits a marker with Nats-Marker-Reason: MaxAge,
226
+ // which the client maps to PURGE
227
+ if (e.operation === "PURGE") {
228
+ d.resolve();
229
+ }
230
+ }
231
+ })().catch();
232
+ await d;
233
+
234
+ // after markerTTL elapses the marker is also gone
235
+ // delay can be imported from @nats-io/nats-core
236
+ await delay(2500);
237
+ const e = await kv.get("k");
238
+ console.log(e ? "key still found" : "key is gone");
239
+ ```
240
+
241
+ ### Per-key TTL on create
242
+
243
+ ```typescript
244
+ const kv = await new Kvm(js).create("A", { markerTTL: 2_000 });
245
+
246
+ // per-key TTL is a duration string ("5s", "1m", "1.5h"); minimum 1s
247
+ await kv.create("k", "hello", "5s");
248
+
249
+ // deferred can be imported from @nats-io/nats-core
250
+ const d = deferred();
251
+ const now = Date.now();
252
+ const iter = await kv.watch();
253
+ (async () => {
254
+ for await (const e of iter) {
255
+ console.log(Date.now() - now, e.operation, e.key);
256
+ // ~5s after create the server removes the entry and emits a marker
257
+ if (e.operation === "PURGE") {
258
+ d.resolve();
259
+ }
260
+ }
261
+ })().catch();
262
+ await d;
263
+
264
+ // marker itself disappears after the bucket's markerTTL
265
+ // delay can be imported from @nats-io/nats-core
266
+ await delay(2500);
267
+ const e = await kv.get("k");
268
+ console.log(e ? "key still found" : "key is gone");
269
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nats-io/kv",
3
- "version": "3.4.0-4",
3
+ "version": "3.4.1-0",
4
4
  "files": [
5
5
  "lib/",
6
6
  "LICENSE",
@@ -33,8 +33,8 @@
33
33
  },
34
34
  "description": "kv library - this library implements all the base functionality for NATS KV javascript clients",
35
35
  "dependencies": {
36
- "@nats-io/jetstream": "3.4.0-4",
37
- "@nats-io/nats-core": "3.4.0-4"
36
+ "@nats-io/jetstream": "3.4.0",
37
+ "@nats-io/nats-core": "3.4.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@types/node": "^25.6.1",