-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample-hyperdrive.ts
More file actions
77 lines (64 loc) · 2.09 KB
/
example-hyperdrive.ts
File metadata and controls
77 lines (64 loc) · 2.09 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Easybase } from "./index.ts";
import Corestore from "corestore";
// Define action types for better type safety
type HyperdriveActions = {
uploadFile: (value: any, context: { view: any; base: any }) => Promise<void>;
updateMetadata: (
value: any,
context: { view: any; base: any }
) => Promise<void>;
};
async function example() {
// Create a corestore
const store = new Corestore("./data");
// Create Easybase with Hyperdrive view and typed actions
const easybase = new Easybase<HyperdriveActions>(store, {
viewType: "hyperdrive",
actions: {
// Custom action for handling file uploads
uploadFile: async (value, { view, base }) => {
const { filename, content } = value;
await view.put(filename, content);
console.log(`File ${filename} uploaded successfully`);
},
// Custom action for handling metadata
updateMetadata: async (value, { view, base }) => {
const { key, metadata } = value;
await view.put(`metadata/${key}`, metadata);
console.log(`Metadata for ${key} updated`);
},
},
});
await easybase.ready();
// Access Hyperdrive components
const drive = easybase.hyperdriveView;
const db = easybase.hyperbeeDb;
const blobs = easybase.hyperblobs;
if (drive && db && blobs) {
console.log("Hyperdrive view is ready!");
// Example: Use the dynamically created action methods
await easybase.uploadFile({
filename: "example.txt",
content: "Hello, Hyperdrive!",
});
// Example: Update metadata using the dynamic method
await easybase.updateMetadata({
key: "user-profile",
metadata: { name: "Alice", age: 30 },
});
// Example: Add a blob
const blobId = await blobs.put(Buffer.from("This is a blob"));
await easybase.uploadFile({
filename: "blob-data.bin",
content: blobId,
});
}
// Create an invite for pairing
const invite = await easybase.createInvite();
console.log("Invite:", invite);
// Clean up
await easybase.close();
await store.close();
}
// Run the example
example().catch(console.error);