@nats-io/kv 3.0.0-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/README.md +158 -0
- package/build/src/internal_mod.ts +25 -0
- package/build/src/kv.ts +1201 -0
- package/build/src/mod.ts +23 -0
- package/build/src/types.ts +377 -0
- package/lib/internal_mod.d.ts +3 -0
- package/lib/internal_mod.js +15 -0
- package/lib/internal_mod.js.map +1 -0
- package/lib/kv.d.ts +121 -0
- package/lib/kv.js +986 -0
- package/lib/kv.js.map +1 -0
- package/lib/mod.d.ts +3 -0
- package/lib/mod.js +12 -0
- package/lib/mod.js.map +1 -0
- package/lib/types.d.ts +323 -0
- package/lib/types.js +21 -0
- package/lib/types.js.map +1 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# kv
|
|
2
|
+
|
|
3
|
+
The kv module implements the NATS KV functionality using JetStream for
|
|
4
|
+
JavaScript clients. JetStream clients can use streams to store and access data.
|
|
5
|
+
KV is materialized view that presents a different _API_ to interact with the
|
|
6
|
+
data stored in a stream using the API for a Key-Value store which should be
|
|
7
|
+
familiar to many application developers.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Note that this library is distributed in two different registries:
|
|
12
|
+
|
|
13
|
+
- npm a node-specific library supporting CJS (`require`) and ESM (`import`)
|
|
14
|
+
- jsr a node and other ESM (`import`) compatible runtimes (deno, browser, node)
|
|
15
|
+
|
|
16
|
+
If your application doesn't use `require`, you can simply depend on the JSR
|
|
17
|
+
version.
|
|
18
|
+
|
|
19
|
+
### NPM
|
|
20
|
+
|
|
21
|
+
The NPM registry hosts a node-only compatible version of the library
|
|
22
|
+
[@nats-io/kv](https://www.npmjs.com/package/@nats-io/kv) supporting both CJS and
|
|
23
|
+
ESM:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @nats-io/kv
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### JSR
|
|
30
|
+
|
|
31
|
+
The JSR registry hosts the ESM-only [@nats-io/kv](https://jsr.io/@nats-io/kv)
|
|
32
|
+
version of the library.
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
deno add @nats-io/kv
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npx jsr add @nats-io/kv
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
yarn dlx jsr add @nats-io/kv
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
bunx jsr add @nats-io/kv
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Referencing the library
|
|
51
|
+
|
|
52
|
+
Once you import the library, you can reference in your code as:
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
import { Kvm } from "@nats-io/kv";
|
|
56
|
+
|
|
57
|
+
// or in node (only when using CJS)
|
|
58
|
+
const { Kvm } = require("@nats-io/kv");
|
|
59
|
+
|
|
60
|
+
// using a nats connection:
|
|
61
|
+
const kvm = new Kvm(nc);
|
|
62
|
+
await kvm.list();
|
|
63
|
+
await kvm.create("mykv");
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
If you want to customize some of the JetStream options when working with KV, you
|
|
67
|
+
can:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { jetStream } from "@nats-io/jetstream";
|
|
71
|
+
import { Kvm } from "@nats-io/kv";
|
|
72
|
+
|
|
73
|
+
const js = jetstream(nc, { timeout: 10_000 });
|
|
74
|
+
// KV will inherit all the options from the JetStream client
|
|
75
|
+
const kvm = new Kvm(js);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// create the named KV or bind to it if it exists:
|
|
80
|
+
const kvm = new Kvm(nc);
|
|
81
|
+
const kv = await kvm.create("testing", { history: 5 });
|
|
82
|
+
// if the kv is expected to exist:
|
|
83
|
+
// const kv = await kvm.open("testing");
|
|
84
|
+
|
|
85
|
+
// create an entry - this is similar to a put, but will fail if the
|
|
86
|
+
// key exists
|
|
87
|
+
const hw = await kv.create("hello.world", "hi");
|
|
88
|
+
|
|
89
|
+
// Values in KV are stored as KvEntries:
|
|
90
|
+
// {
|
|
91
|
+
// bucket: string,
|
|
92
|
+
// key: string,
|
|
93
|
+
// value: Uint8Array,
|
|
94
|
+
// created: Date,
|
|
95
|
+
// revision: number,
|
|
96
|
+
// delta?: number,
|
|
97
|
+
// operation: "PUT"|"DEL"|"PURGE"
|
|
98
|
+
// }
|
|
99
|
+
// The operation property specifies whether the value was
|
|
100
|
+
// updated (PUT), deleted (DEL) or purged (PURGE).
|
|
101
|
+
|
|
102
|
+
// you can monitor values modification in a KV by watching.
|
|
103
|
+
// You can watch specific key subset or everything.
|
|
104
|
+
// Watches start with the latest value for each key in the
|
|
105
|
+
// set of keys being watched - in this case all keys
|
|
106
|
+
const watch = await kv.watch();
|
|
107
|
+
(async () => {
|
|
108
|
+
for await (const e of watch) {
|
|
109
|
+
// do something with the change
|
|
110
|
+
console.log(
|
|
111
|
+
`watch: ${e.key}: ${e.operation} ${e.value ? e.string() : ""}`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
})().then();
|
|
115
|
+
|
|
116
|
+
// update the entry
|
|
117
|
+
await kv.put("hello.world", sc.encode("world"));
|
|
118
|
+
// retrieve the KvEntry storing the value
|
|
119
|
+
// returns null if the value is not found
|
|
120
|
+
const e = await kv.get("hello.world");
|
|
121
|
+
// initial value of "hi" was overwritten above
|
|
122
|
+
console.log(`value for get ${e?.string()}`);
|
|
123
|
+
|
|
124
|
+
const buf: string[] = [];
|
|
125
|
+
const keys = await kv.keys();
|
|
126
|
+
await (async () => {
|
|
127
|
+
for await (const k of keys) {
|
|
128
|
+
buf.push(k);
|
|
129
|
+
}
|
|
130
|
+
})();
|
|
131
|
+
console.log(`keys contains hello.world: ${buf[0] === "hello.world"}`);
|
|
132
|
+
|
|
133
|
+
let h = await kv.history({ key: "hello.world" });
|
|
134
|
+
await (async () => {
|
|
135
|
+
for await (const e of h) {
|
|
136
|
+
// do something with the historical value
|
|
137
|
+
// you can test e.operation for "PUT", "DEL", or "PURGE"
|
|
138
|
+
// to know if the entry is a marker for a value set
|
|
139
|
+
// or for a deletion or purge.
|
|
140
|
+
console.log(
|
|
141
|
+
`history: ${e.key}: ${e.operation} ${e.value ? sc.decode(e.value) : ""}`,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
})();
|
|
145
|
+
|
|
146
|
+
// deletes the key - the delete is recorded
|
|
147
|
+
await kv.delete("hello.world");
|
|
148
|
+
|
|
149
|
+
// purge is like delete, but all history values
|
|
150
|
+
// are dropped and only the purge remains.
|
|
151
|
+
await kv.purge("hello.world");
|
|
152
|
+
|
|
153
|
+
// stop the watch operation above
|
|
154
|
+
watch.stop();
|
|
155
|
+
|
|
156
|
+
// danger: destroys all values in the KV!
|
|
157
|
+
await kv.destroy();
|
|
158
|
+
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
KV,
|
|
3
|
+
KvCodec,
|
|
4
|
+
KvCodecs,
|
|
5
|
+
KvDeleteOptions,
|
|
6
|
+
KvEntry,
|
|
7
|
+
KvLimits,
|
|
8
|
+
KvOptions,
|
|
9
|
+
KvPutOptions,
|
|
10
|
+
KvStatus,
|
|
11
|
+
KvWatchOptions,
|
|
12
|
+
RoKV,
|
|
13
|
+
} from "./types";
|
|
14
|
+
|
|
15
|
+
export { kvPrefix, KvWatchInclude } from "./types";
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
Base64KeyCodec,
|
|
19
|
+
Bucket,
|
|
20
|
+
defaultBucketOpts,
|
|
21
|
+
Kvm,
|
|
22
|
+
NoopKvCodecs,
|
|
23
|
+
validateBucket,
|
|
24
|
+
validateKey,
|
|
25
|
+
} from "./kv";
|