Skip to content

Commit

Permalink
feat(obj): Objm added get() for retrieving an ObjectStore instance
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart committed Dec 2, 2024
1 parent 499edac commit e4b8f0f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
15 changes: 15 additions & 0 deletions obj/src/objectstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ export class Objm {
return this.#maybeCreate(name, opts);
}

/**
* Opens the specified ObjectStore
* @param name
* @param check - if true (the default) a check is made to verify the stream exists
*/
async get(name: string, check = true): Promise<ObjectStore> {
const jsm = await this.js.jetstreamManager();
const os = new ObjectStoreImpl(name, jsm, this.js);
os.stream = objectStoreStreamName(name);
if (check) {
await os.status();
}
return Promise.resolve(os);
}

#maybeCreate(
name: string,
opts: Partial<ObjectStoreOptions> = {},
Expand Down
39 changes: 39 additions & 0 deletions obj/tests/objectstore_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1226,3 +1226,42 @@ Deno.test("os - os rejects in older servers", async () => {
await t("2.6.3", true);
await cleanup(ns, nc);
});

Deno.test("os - objm get", async () => {
const { ns, nc } = await setup(jetstreamServerConf({}));

const objm = new Objm(nc);
await assertRejects(
() => {
return objm.get("hello");
},
Error,
"object store not found",
);

let obj = await objm.get("hello", false);

await assertRejects(
() => {
return obj.get("hello");
},
Error,
"stream not found",
);

await assertRejects(
() => {
return obj.put({ name: "hi" }, readableStreamFrom(Empty));
},
Error,
"stream not found",
);

await objm.create("hello");

obj = await objm.get("hello");
const oi = await obj.put({ name: "hello" }, readableStreamFrom(Empty));
assertEquals(oi.name, "hello");

await cleanup(ns, nc);
});

0 comments on commit e4b8f0f

Please sign in to comment.