Skip to content

Commit 9add50b

Browse files
authored
feat(obj): Objm added open() for retrieving an ObjectStore instance (#158)
Signed-off-by: Alberto Ricart <[email protected]>
1 parent 9f047fb commit 9add50b

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

Diff for: obj/src/objectstore.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ export class Objm {
103103
}
104104

105105
/**
106-
* Creates and opens the specified ObjectStore. If the Object already exists, it opens the existing ObjectStore.
106+
* Creates and opens the specified ObjectStore. If the ObjectStore already exists,
107+
* it opens the existing ObjectStore.
107108
* @param name
108109
* @param opts
109110
*/
@@ -114,6 +115,21 @@ export class Objm {
114115
return this.#maybeCreate(name, opts);
115116
}
116117

118+
/**
119+
* Opens the specified ObjectStore
120+
* @param name
121+
* @param check - if set to false, it will not check if the ObjectStore exists.
122+
*/
123+
async open(name: string, check = true): Promise<ObjectStore> {
124+
const jsm = await this.js.jetstreamManager();
125+
const os = new ObjectStoreImpl(name, jsm, this.js);
126+
os.stream = objectStoreStreamName(name);
127+
if (check) {
128+
await os.status();
129+
}
130+
return Promise.resolve(os);
131+
}
132+
117133
#maybeCreate(
118134
name: string,
119135
opts: Partial<ObjectStoreOptions> = {},

Diff for: obj/tests/objectstore_test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -1226,3 +1226,42 @@ Deno.test("os - os rejects in older servers", async () => {
12261226
await t("2.6.3", true);
12271227
await cleanup(ns, nc);
12281228
});
1229+
1230+
Deno.test("os - objm open", async () => {
1231+
const { ns, nc } = await setup(jetstreamServerConf({}));
1232+
1233+
const objm = new Objm(nc);
1234+
await assertRejects(
1235+
() => {
1236+
return objm.open("hello");
1237+
},
1238+
Error,
1239+
"object store not found",
1240+
);
1241+
1242+
let obj = await objm.open("hello", false);
1243+
1244+
await assertRejects(
1245+
() => {
1246+
return obj.get("hello");
1247+
},
1248+
Error,
1249+
"stream not found",
1250+
);
1251+
1252+
await assertRejects(
1253+
() => {
1254+
return obj.put({ name: "hi" }, readableStreamFrom(Empty));
1255+
},
1256+
Error,
1257+
"stream not found",
1258+
);
1259+
1260+
await objm.create("hello");
1261+
1262+
obj = await objm.open("hello");
1263+
const oi = await obj.put({ name: "hello" }, readableStreamFrom(Empty));
1264+
assertEquals(oi.name, "hello");
1265+
1266+
await cleanup(ns, nc);
1267+
});

0 commit comments

Comments
 (0)