-
Notifications
You must be signed in to change notification settings - Fork 0
/
pouch_store.js
65 lines (59 loc) · 1.43 KB
/
pouch_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
56
57
58
59
60
61
62
63
64
65
import PouchDB from "pouchdb";
/**
* DB class for interacting with PouchDB
* @class
* @implements {Ezdb}
*/
class PouchStore {
/**
* Creates an instance of PouchStore
*/
constructor() {
const dbName = process.env.POUCHDB_NAME || "ezdb";
this.db = new PouchDB(dbName);
}
/**
* Sets a value in the database
* @param {string} key - The key to set
* @param {any} value - The value to set
* @returns {Promise<void>}
* @throws {Error} If there's an error setting the value
*/
async set(key, value) {
try {
const existingDoc = await this.db.get(key).catch((err) => {
if (err.name === "not_found") {
return { _id: key };
}
throw err;
});
const updatedDoc = {
...existingDoc,
value: value,
};
await this.db.put(updatedDoc);
} catch (error) {
console.error("Error setting value:", error);
throw error;
}
}
/**
* Gets a value from the database
* @param {string} key - The key to get
* @returns {Promise<any>} The value associated with the key
* @throws {Error} If there's an error getting the value
*/
async get(key) {
try {
const doc = await this.db.get(key);
return doc.value;
} catch (error) {
if (error.name === "not_found") {
return null;
}
console.error("Error getting value:", error);
throw error;
}
}
}
export default PouchStore;