-
Notifications
You must be signed in to change notification settings - Fork 0
/
replit_store.js
55 lines (51 loc) · 1.23 KB
/
replit_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
import Database from "@replit/database";
/**
* DB class for interacting with Replit Database
* @class
* @implements {Ezdb}
*/
class ReplitStore {
/**
* Creates an instance of ReplitStore
*/
constructor() {
this.db = new Database();
}
/**
* 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 result = await this.db.set(key, value);
if (!result.ok) {
throw new Error(result.error);
}
} 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 result = await this.db.get(key);
if (!result.ok) {
throw new Error(result.error);
}
return result.value;
} catch (error) {
console.error("Error getting value:", error);
throw error;
}
}
}
export default ReplitStore;