Skip to content

Commit 5e1397b

Browse files
committed
Update API
1 parent fb69fc9 commit 5e1397b

File tree

5 files changed

+20
-40
lines changed

5 files changed

+20
-40
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ As `Nested` database is like a [`KeyValue`](https://github.com/orbitdb/orbitdb/b
1616
A simple example with `Nested`:
1717
```ts
1818
import { createOrbitDB } from "@orbitdb/core";
19-
import { registerNested, toNested } from "@orbitdb/nested-db";
19+
import { Nested } from "@orbitdb/nested-db";
2020

2121
// Register nested database type. IMPORTANT - must call before creating orbit instance !
22-
registerNested();
22+
useDatabaseType(Nested);
2323

2424
const orbitdb = await createOrbitDB({ ipfs })
2525

@@ -29,8 +29,7 @@ await db.put("a", 1);
2929
await db.put("b/c", 2);
3030
await db.put(["b", "d"], 3) // Alternative syntax
3131

32-
const all = await db.all(); // [{ key: "a", value: 1}, { key: "b/c", value: 2 }, { key: "b/d", value: 3 }]
33-
toNested(all) // { a: 1, b: { c: 2, d: 3 } }
32+
const all = await db.all(); // { a: 1, b: { c: 2, d: 3 } }
3433

3534
await db.get("b") // { c: 2, d: 3 }
3635

@@ -41,10 +40,10 @@ await db.all(); // { "a": 1 }
4140
A more complex example with object types:
4241
```ts
4342
import { createOrbitDB } from "@orbitdb/core";
44-
import { registerNested, toNested } from "@orbitdb/nested-db";
43+
import { Nested } from "@orbitdb/nested-db";
4544

4645
// Register nested database type. IMPORTANT - must call before creating orbit instance !
47-
registerNested();
46+
useDatabaseType(Nested);
4847

4948
const orbit = await createOrbitDB({ ipfs })
5049

@@ -53,8 +52,7 @@ const db = await orbitdb.open({ type: "nested" });
5352
await db.putNested({ a: { b: 1, c: 2 } });
5453
await db.putNested({ d: 3 });
5554

56-
const all = await db.all(); // [{ key: "a/b", value: 1 }, { key: "a/c", value: 2 }, { key: "d", value: 3 }]
57-
toNested(all) // { a: { b: 1, c: 2}, d: 3 }
55+
const all = await db.all(); // { a: { b: 1, c: 2}, d: 3 }
5856

5957
```
6058

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export { default as Nested, NestedDatabaseType } from "@/nested.js";
22

33
export {
4-
registerNested,
54
flatten,
65
toNested,
76
splitKey,

src/nested.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,12 @@ const Nested =
150150
}
151151
};
152152

153-
const all = async (): Promise<
154-
{
155-
key: string;
156-
value: DagCborEncodable;
157-
hash: string;
158-
}[]
159-
> => {
153+
const all = async (): Promise<NestedValue> => {
160154
const values = [];
161155
for await (const entry of iterator()) {
162156
values.unshift(entry);
163157
}
164-
return values;
158+
return toNested(values);
165159
};
166160

167161
return {

src/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { DagCborEncodable, useDatabaseType } from "@orbitdb/core";
33
import Nested from "@/nested.js";
44
import { NestedValue, PossiblyNestedValue } from "./types";
55

6-
export const registerNested = () => useDatabaseType(Nested);
7-
86
export const splitKey = (key: string): string[] => key.split("/");
97
export const joinKey = (key: string[]): string => key.join("/");
108

test/nested.spec.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,11 @@ describe("Nested Database", () => {
162162
});
163163

164164
it("add a nested value", async () => {
165-
const hash1 = await db.put("a/b", 1);
166-
const hash2 = await db.put("a/c", 2);
165+
await db.put("a/b", 1);
166+
await db.put("a/c", 2);
167167

168168
const actual = await db.all();
169-
expect(actual).to.have.deep.members([
170-
{ key: "a/b", value: 1, hash: hash1 },
171-
{ key: "a/c", value: 2, hash: hash2 },
172-
]);
169+
expect(actual).to.deep.equal({ a: { b: 1, c: 2 }});
173170
});
174171

175172
it("get a nested value", async () => {
@@ -195,14 +192,11 @@ describe("Nested Database", () => {
195192
});
196193

197194
it("add a nested value - list syntax", async () => {
198-
const hash1 = await db.put(["a", "b"], 1);
199-
const hash2 = await db.put(["a", "c"], 2);
195+
await db.put(["a", "b"], 1);
196+
await db.put(["a", "c"], 2);
200197

201198
const actual = await db.all();
202-
expect(actual).to.have.deep.members([
203-
{ key: "a/b", value: 1, hash: hash1 },
204-
{ key: "a/c", value: 2, hash: hash2 },
205-
]);
199+
expect(actual).to.deep.equal({ a: { b: 1, c: 2 }});
206200
});
207201

208202
it("remove root key", async () => {
@@ -219,35 +213,32 @@ describe("Nested Database", () => {
219213
await db.put("a/b", 1);
220214
await db.put("a/c", 2);
221215

222-
const hash = await db.put("a", 3);
216+
await db.put("a", 3);
223217

224218
const actual = await db.all();
225-
expect(actual).to.have.deep.members([{ key: "a", value: 3, hash }]);
219+
expect(actual).to.deep.equal({ a: 3 });
226220
});
227221

228222
it("put nested", async () => {
229-
const hashes = await db.putNested({ a: { b: 1, c: 2 } });
223+
await db.putNested({ a: { b: 1, c: 2 } });
230224

231225
const actual = await db.all();
232-
expect(actual).to.have.deep.members([
233-
{ key: "a/b", value: 1, hash: hashes[0] },
234-
{ key: "a/c", value: 2, hash: hashes[1] },
235-
]);
226+
expect(actual).to.deep.equal({ a: { b: 1, c: 2 }});
236227
});
237228

238229
it("put key nested value", async () => {
239230
await db.put("a", { b: 2, c: 3 });
240231
await db.put("a", { b: 1 });
241232

242-
const actual = toNested(await db.all());
233+
const actual = await db.all();
243234
expect(actual).to.deep.equal({ a: { b: 1 } });
244235
});
245236

246237
it("put nested value merges with previous values", async () => {
247238
await db.put("a", { b: 2, c: 3 });
248239
await db.putNested("a", { b: 1 });
249240

250-
const actual = toNested(await db.all());
241+
const actual = await db.all();
251242
expect(actual).to.deep.equal({ a: { b: 1, c: 3 } });
252243
});
253244

0 commit comments

Comments
 (0)