forked from dao-xyz/peerbit-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
47 lines (35 loc) · 1.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { field, variant } from "@dao-xyz/borsh";
import { Program } from "@dao-xyz/peerbit-program";
import { Documents, DocumentIndex } from "@dao-xyz/peerbit-document";
import { nanoid } from 'nanoid';
// Abstract document definition we can create many kinds of document types from
export class BaseDocument { }
// We version our documents with a single byte, so in the future, if we want to upgrade our document definition
// we can do @variant(1) which allows us to store and manage both Documents of type variant 0 and variant 1 in our Document database
// (This is not needed but recommended)
@variant(0)
export class TextDocument extends BaseDocument {
@field({ type: 'string' })
id: string
@field({ type: 'string' })
text: string
constructor(text: string) {
super()
this.id = nanoid()
this.text = text;
}
}
// MyDatabase needs to extends Program so we later can "open" it using the Peerbit client
@variant("my-database")
export class MyDatabase extends Program {
@field({ type: Documents })
documents: Documents<BaseDocument>
constructor(properties?: { id: string }) {
super(properties)
this.documents = new Documents({ index: new DocumentIndex({ indexBy: 'id' }) })
}
async setup() {
// this will be invoked on startup
await this.documents.setup({ type: BaseDocument })
}
}