-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstore.js
55 lines (50 loc) · 1.26 KB
/
store.js
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
const IPFS = require("ipfs");
const OrbitDB = require("orbit-db");
// Configuration for IPFS instance
const ipfsConfig = {
repo: "/orbitdb/examples/todomvc/ipfs/0.27.0",
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
// Use IPFS dev signal server
"/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star"
]
}
}
};
// Configuration for the database
const dbConfig = {
// If database doesn't exist, create it
create: true,
// Don't wait to load from the network
sync: false,
// Load only the local version of the database
localOnly: true,
// Allow anyone to write to the database,
// otherwise only the creator of the database can write
admin: ["*"],
write: ["*"]
};
const store = async name => {
return new Promise((resolve, reject) => {
// Create IPFS instance
const ipfs = new IPFS(ipfsConfig);
ipfs.on("error", e => console.error(e));
ipfs.on("ready", async () => {
try {
// Create an OrbitDB instance
const orbitdb = new OrbitDB(ipfs);
// Open (or create) database
const db = await orbitdb.docs(name, dbConfig);
// Done
resolve(db);
} catch (e) {
reject(e);
}
});
});
};
export default store;