Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] [KV] expose stream metadata #545

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
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
Loading