first commit
This commit is contained in:
22
backend/src/stores/dataStore.js
Normal file
22
backend/src/stores/dataStore.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import { db } from "../db.js";
|
||||
import { INITIAL_DATA } from "../constants/initialData.js";
|
||||
|
||||
export const dataStore = {
|
||||
async load(userId) {
|
||||
const { rows } = await db.query("SELECT data FROM user_data WHERE user_id = $1", [userId]);
|
||||
return rows[0] ? JSON.parse(rows[0].data) : structuredClone(INITIAL_DATA);
|
||||
},
|
||||
async save(userId, payload) {
|
||||
await db.query(
|
||||
"INSERT INTO user_data (user_id, data) VALUES ($1, $2) ON CONFLICT (user_id) DO UPDATE SET data = $2",
|
||||
[userId, JSON.stringify(payload)]
|
||||
);
|
||||
return payload;
|
||||
},
|
||||
async reset(userId) {
|
||||
await db.query(
|
||||
"INSERT INTO user_data (user_id, data) VALUES ($1, $2) ON CONFLICT (user_id) DO UPDATE SET data = $2",
|
||||
[userId, JSON.stringify(structuredClone(INITIAL_DATA))]
|
||||
);
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user