Skip to content

Commit

Permalink
[FEAT] [KV] expose stream metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Jun 20, 2023
1 parent b460c31 commit c67091b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions jetstream/kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ export class Bucket implements KV, KvRemove {
} else {
sc.subjects = [this.subjectForBucket()];
}
if (opts.metadata) {
sc.metadata = opts.metadata;
}

const nci = (this.js as unknown as { nc: NatsConnectionImpl }).nc;
const have = nci.getServerVersion();
Expand Down Expand Up @@ -917,6 +920,10 @@ export class KvStatusImpl implements KvStatus {
get size(): number {
return this.si.state.bytes;
}

get metadata(): Record<string, string> | undefined {
return this.si.config.metadata;
}
}

class KvStoredEntryImpl implements KvEntry {
Expand Down
13 changes: 13 additions & 0 deletions jetstream/tests/kv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1767,3 +1767,16 @@ Deno.test("kv - string payloads", async () => {

await cleanup(ns, nc);
});

Deno.test("kv - metadata", async () => {
const { ns, nc } = await setup(jetstreamServerConf({}, true));
if (await notCompatible(ns, nc, "2.10.0")) {
return;
}

const js = nc.jetstream();
const kv = await js.views.kv("K", { metadata: { hello: "world" } });
const status = await kv.status();
assertEquals(status.metadata?.hello, "world");
await cleanup(ns, nc);
});
12 changes: 12 additions & 0 deletions jetstream/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,12 @@ export interface KvStatus extends KvLimits {
* Size of the bucket in bytes
*/
size: number;
/**
* Metadata field to store additional information about the stream. Note that
* keys starting with `_nats` are reserved. This feature only supported on servers
* 2.10.x and better.
*/
metadata?: Record<string, string>;
}

export interface KvOptions extends KvLimits {
Expand All @@ -1055,6 +1061,12 @@ export interface KvOptions extends KvLimits {
* but has the possibility of inconsistency during a read.
*/
allow_direct: boolean;
/**
* Metadata field to store additional information about the kv. Note that
* keys starting with `_nats` are reserved. This feature only supported on servers
* 2.10.x and better.
*/
metadata?: Record<string, string>;
}

/**
Expand Down

0 comments on commit c67091b

Please sign in to comment.