@nats-io/obj 3.0.0-1

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 ADDED
@@ -0,0 +1,187 @@
1
+ # obj
2
+
3
+ The obj module implements the NATS ObjectStore functionality using JetStream for
4
+ JavaScript clients. JetStream clients can use streams to store and access data.
5
+ Obj is materialized view that presents a different _API_ to interact with the
6
+ data stored in a stream using the API for a ObjectStore which should be familiar
7
+ 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/obj](https://www.npmjs.com/package/@nats-io/obj) supporting both CJS
23
+ and ESM:
24
+
25
+ ```bash
26
+ npm install @nats-io/obj
27
+ ```
28
+
29
+ ### JSR
30
+
31
+ The JSR registry hosts the EMS-only [@nats-io/obj](https://jsr.io/@nats-io/obj)
32
+ version of the library.
33
+
34
+ ```bash
35
+ deno add @nats-io/obj
36
+ ```
37
+
38
+ ```bash
39
+ npx jsr add @nats-io/obj
40
+ ```
41
+
42
+ ```bash
43
+ yarn dlx jsr add @nats-io/obj
44
+ ```
45
+
46
+ ```bash
47
+ bunx jsr add @nats-io/obj
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 { Objm } from "@nats-io/obj";
56
+
57
+ // or in node (only when using CJS)
58
+ const { Objm } = require("@nats-io/obj");
59
+
60
+ // using a nats connection:
61
+ const objm = new Objm(nc);
62
+ await objm.list();
63
+ await objm.create("myobj");
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 { Objm } from "@nats-io/obj";
72
+
73
+ const js = jetstream(nc, { timeout: 10_000 });
74
+ // KV will inherit all the options from the JetStream client
75
+ const objm = new Objm(js);
76
+ ```
77
+
78
+ ```typescript
79
+ const sc = StringCodec();
80
+ // create the named ObjectStore or bind to it if it exists:
81
+ const objm = new Objm(nc);
82
+ const os = await objm.create("testing", { storage: StorageType.File });
83
+
84
+ // ReadableStreams allows JavaScript to work with large data without
85
+ // necessarily keeping it all in memory.
86
+ //
87
+ // ObjectStore reads and writes to JetStream via ReadableStreams
88
+ // https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream
89
+ // You can easily create ReadableStreams from static data or iterators
90
+
91
+ // here's an example of creating a readable stream from static data
92
+ function readableStreamFrom(data: Uint8Array): ReadableStream<Uint8Array> {
93
+ return new ReadableStream<Uint8Array>({
94
+ pull(controller) {
95
+ // the readable stream adds data
96
+ controller.enqueue(data);
97
+ controller.close();
98
+ },
99
+ });
100
+ }
101
+
102
+ // reading from a ReadableStream is similar to working with an async iterator:
103
+ async function fromReadableStream(
104
+ rs: ReadableStream<Uint8Array>,
105
+ ) {
106
+ let i = 1;
107
+ const reader = rs.getReader();
108
+ while (true) {
109
+ const { done, value } = await reader.read();
110
+ if (done) {
111
+ break;
112
+ }
113
+ if (value && value.length) {
114
+ // do something with the accumulated data
115
+ console.log(`chunk ${i++}: ${sc.decode(value)}`);
116
+ }
117
+ }
118
+ }
119
+
120
+ let e = await os.get("hello");
121
+ console.log(`hello entry exists? ${e !== null}`);
122
+
123
+ // watch notifies when a change in the object store happens
124
+ const watch = await os.watch();
125
+ (async () => {
126
+ for await (const i of watch) {
127
+ // when asking for history you get a null
128
+ // that tells you when all the existing values
129
+ // are provided
130
+ if (i === null) {
131
+ continue;
132
+ }
133
+ console.log(`watch: ${i!.name} deleted?: ${i!.deleted}`);
134
+ }
135
+ })();
136
+
137
+ // putting an object returns an info describing the object
138
+ const info = await os.put({
139
+ name: "hello",
140
+ description: "first entry",
141
+ options: {
142
+ max_chunk_size: 1,
143
+ },
144
+ }, readableStreamFrom(sc.encode("hello world")));
145
+
146
+ console.log(
147
+ `object size: ${info.size} number of chunks: ${info.size} deleted: ${info.deleted}`,
148
+ );
149
+
150
+ // reading it back:
151
+ const r = await os.get("hello");
152
+ // it is possible while we read the ReadableStream that something goes wrong
153
+ // the error property on the result will resolve to null if there's no error
154
+ // otherwise to the error from the ReadableStream
155
+ r?.error.then((err) => {
156
+ if (err) {
157
+ console.error("reading the readable stream failed:", err);
158
+ }
159
+ });
160
+
161
+ // use our sample stream reader to process output to the console
162
+ // chunk 1: h
163
+ // chunk 2: e
164
+ // ...
165
+ // chunk 11: d
166
+ await fromReadableStream(r!.data);
167
+
168
+ // list all the entries in the object store
169
+ // returns the info for each entry
170
+ const list = await os.list();
171
+ list.forEach((i) => {
172
+ console.log(`list: ${i.name}`);
173
+ });
174
+
175
+ // you can also get info on the object store as a whole:
176
+ const status = await os.status();
177
+ console.log(`bucket: '${status.bucket}' size in bytes: ${status.size}`);
178
+
179
+ // you can prevent additional modifications by sealing it
180
+ const final = await os.seal();
181
+ console.log(`bucket: '${final.bucket}' sealed: ${final.sealed}`);
182
+
183
+ // only other thing that you can do is destroy it
184
+ // this gets rid of the objectstore
185
+ const destroyed = await os.destroy();
186
+ console.log(`destroyed: ${destroyed}`);
187
+ ```
@@ -0,0 +1,13 @@
1
+ export type {
2
+ ObjectInfo,
3
+ ObjectResult,
4
+ ObjectStore,
5
+ ObjectStoreLink,
6
+ ObjectStoreMeta,
7
+ ObjectStoreMetaOptions,
8
+ ObjectStoreOptions,
9
+ ObjectStorePutOpts,
10
+ ObjectStoreStatus,
11
+ } from "./types";
12
+
13
+ export { Objm } from "./objectstore";
@@ -0,0 +1,13 @@
1
+ export type {
2
+ ObjectInfo,
3
+ ObjectResult,
4
+ ObjectStore,
5
+ ObjectStoreLink,
6
+ ObjectStoreMeta,
7
+ ObjectStoreMetaOptions,
8
+ ObjectStoreOptions,
9
+ ObjectStorePutOpts,
10
+ ObjectStoreStatus,
11
+ } from "./internal_mod";
12
+
13
+ export { Objm } from "./objectstore";