-
Notifications
You must be signed in to change notification settings - Fork 0
/
vercel_store.js
49 lines (45 loc) · 1.14 KB
/
vercel_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
import { kv } from "@vercel/kv";
/**
* DB class for interacting with Vercel KV Database
* @class
* @implements {Ezdb}
*/
class VercelStore {
/**
* Creates an instance of VercelStore
*/
constructor() {
// No need to initialize anything as kv is globally available
}
/**
* 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 {
await kv.set(key, JSON.stringify(value));
} 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 kv.get(key);
return result !== null ? JSON.parse(result) : null;
} catch (error) {
console.error("Error getting value:", error);
throw error;
}
}
}
export default VercelStore;