6.0.7 • Published 7 days ago

@peerbit/document v6.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
7 days ago

Documents

Distributed document store.

This store is built on top of the base store. This store allows for type-safe document storage and retrieval accross peers.

As of now, go through the tests for documentation on how to use the module.

Example

import { field, option, serialize, variant } from "@dao-xyz/borsh";
import { Program } from "@peerbit/program";
import { Peerbit } from "peerbit";
import {
	Documents,
	DocumentIndex,
	SearchRequest,
	StringMatch,
	StringMatchMethod,
	Results,
} from "@peerbit/document";


@variant("document")
class Document {
    @field({ type: "string" })
    id: string;

    @field({ type: option("string") })
    name?: string;

    @field({ type: option("u64") })
    number?: bigint;

    constructor(opts?: Document) {
        if (opts) {
            Object.assign(this, opts);
        }
    }
}

@variant("test_documents")
class TestStore extends Program {
    @field({ type: Documents })
    docs: Documents<Document>;

    constructor(properties?: { docs: Documents<Document> }) {
        super();
        if (properties) {
            this.docs = properties.docs;
        }
    }
    async open(): Promise<void> {
        await this.docs.open({ 
			type: Document, 
			index: {
				fields: (obj) => obj // here you can filter and transform what fields you want to index
			}})
    }
}

// later 

const peer = await Peerbit.create ({libp2p: your_libp2p_instance})
const store = peer.open(new TestStore());
console.log(store.address) /// this address can be opened by another peer 


// insert
let doc = new Document({
    id: "1",
    name: "hello world",
    number: 1n,
});
let doc2 = new Document({
    id: "2",
    name: "hello world",
    number: 2n,
});

let doc3 = new Document({
    id: "3",
    name: "foo",
    number: 3n,
});

await store.docs.put(doc);
await store.docs.put(doc2);
await store.docs.put(doc3);


// search for documents from another peer
const peer2 = await Peerbit.create ({libp2: another_libp2p_instance})
const store2 = peer2.open(store.address);

let responses: Document[] = await store2.docs.index.search(
    new SearchRequest({
        query: [
          new StringMatch({
                key: "name",
                value: "ello",
				method: StringMatchMethod.contains
            }),
        ],
    })
);
expect(responses]).to.have.length(2);
expect(responses.map((x) => x.value.id)).to.deep.equal(["1", "2"]);
6.0.7-aa577a5

7 days ago

6.0.7-0691c73

7 days ago

6.0.7-218a5bb

7 days ago

6.0.7-efee9d3

1 month ago

6.0.7-a9206a8

1 month ago

6.0.7-a4f88b6

1 month ago

6.0.7

2 months ago

6.0.6

3 months ago

6.0.3

3 months ago

6.0.2

3 months ago

6.0.5

3 months ago

6.0.4

3 months ago

6.0.1

3 months ago

6.0.0

3 months ago

5.0.8

3 months ago

5.0.7

3 months ago

5.0.6

3 months ago

5.0.5

4 months ago

5.0.4

4 months ago

5.0.3

4 months ago

5.0.2

4 months ago

5.0.1

4 months ago

4.1.9

4 months ago

5.0.0

4 months ago

4.1.8

4 months ago

4.1.7

4 months ago

4.1.6

4 months ago

4.1.5

4 months ago

4.1.4

4 months ago

4.1.3

4 months ago

4.1.2

4 months ago

4.1.1

4 months ago

4.0.12

4 months ago

4.0.11

4 months ago

4.1.0

4 months ago

4.0.10

4 months ago

4.0.9

4 months ago

4.0.8

4 months ago

4.0.5

4 months ago

4.0.7

4 months ago

4.0.6

4 months ago

4.0.4

4 months ago

4.0.1

4 months ago

4.0.3

4 months ago

4.0.2

4 months ago

3.2.0

8 months ago

3.1.11

8 months ago

3.1.10

8 months ago

3.1.9

8 months ago

3.1.8

8 months ago

3.1.7

8 months ago

3.1.6

8 months ago

3.1.5

8 months ago

3.1.4

8 months ago

3.1.3

8 months ago

3.1.2

8 months ago

3.1.1

8 months ago

3.1.0

8 months ago

3.0.8

8 months ago

3.0.7

8 months ago

3.0.6

8 months ago

3.0.5

8 months ago

3.0.4

9 months ago

3.0.3

9 months ago

3.0.2

9 months ago

3.0.1

9 months ago

3.0.0

9 months ago

2.0.1

10 months ago

2.0.0

10 months ago

1.1.9

10 months ago

1.1.8

10 months ago

1.1.7

10 months ago

1.1.6

10 months ago

1.1.5

10 months ago

1.1.4

10 months ago

1.1.3

10 months ago

1.1.2

10 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago