Refactor database access to use a unified dbAdapter for SQLite and PostgreSQL

- Replaced direct database calls with dbAdapter methods in fieldRoutes, itemRoutes, listRoutes, syncRoutes, and webRoutes.
- Implemented a new db.ts file to handle database interactions for both SQLite and PostgreSQL.
- Added WebSocket support for real-time synchronization in WebSocketSyncClient and ws.ts.
- Updated TypeScript configuration to include WebSocket types.
- Created a new WebSocket server for handling sync requests and responses.
- Ensured compatibility with both SQLite and PostgreSQL by using parameterized queries.
This commit is contained in:
Gabriel20xx committed 2025-10-25 17:02:42 +02:00
1 parent 22ec511ca7
commit e5e72a39ac
24 files changed
+1183 -395

No files matched your search

+16 -19
View File
@@ -1,12 +1,12 @@
import { Router, Request, Response } from 'express';
import { db } from '../database';
import { dbAdapter } from '../db';
const router = Router();
// Get fields for a list
router.get('/list/:listId', async (req: Request, res: Response) => {
try {
const fields = db.prepare('SELECT * FROM fields WHERE listId = ? AND isDeleted = 0 ORDER BY `order` ASC').all(req.params.listId);
const fields = await dbAdapter.queryAll('SELECT * FROM fields WHERE listId = ? AND isDeleted = 0 ORDER BY "order" ASC', [req.params.listId]);
const formattedFields = (fields as any[]).map(field => ({ ...field, isDeleted: !!field.isDeleted }));
res.json(formattedFields);
} catch (error) {
@@ -18,12 +18,11 @@ router.get('/list/:listId', async (req: Request, res: Response) => {
router.post('/', async (req: Request, res: Response) => {
try {
const { id, name, fieldType, fieldOptions, listId, order, createdAt, updatedAt, isDeleted } = req.body;
db.prepare(`
INSERT INTO fields (id, name, fieldType, fieldOptions, listId, \`order\`, createdAt, updatedAt, isDeleted)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(id, name, fieldType, fieldOptions, listId, order, createdAt, updatedAt, isDeleted ? 1 : 0);
const field = db.prepare('SELECT * FROM fields WHERE id = ?').get(id);
await dbAdapter.execute(
'INSERT INTO fields (id, name, fieldType, fieldOptions, listId, "order", createdAt, updatedAt, isDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
[id, name, fieldType, fieldOptions, listId, order, createdAt, updatedAt, isDeleted ? 1 : 0]
);
const field = await dbAdapter.queryOne('SELECT * FROM fields WHERE id = ?', [id]);
res.status(201).json({ ...(field as any), isDeleted: !!(field as any).isDeleted });
} catch (error) {
res.status(500).json({ error: 'Failed to create field' });
@@ -35,17 +34,16 @@ router.put('/:id', async (req: Request, res: Response) => {
try {
const updatedAt = Date.now();
const { name, fieldType, fieldOptions, order } = req.body;
const result = db.prepare(`
UPDATE fields
SET name = ?, fieldType = ?, fieldOptions = ?, \`order\` = ?, updatedAt = ?
WHERE id = ?
`).run(name, fieldType, fieldOptions, order, updatedAt, req.params.id);
const result = await dbAdapter.execute(
'UPDATE fields SET name = ?, fieldType = ?, fieldOptions = ?, "order" = ?, updatedAt = ? WHERE id = ?',
[name, fieldType, fieldOptions, order, updatedAt, req.params.id]
);
if (result.changes === 0) {
return res.status(404).json({ error: 'Field not found' });
}
const field = db.prepare('SELECT * FROM fields WHERE id = ?').get(req.params.id);
const field = await dbAdapter.queryOne('SELECT * FROM fields WHERE id = ?', [req.params.id]);
res.json({ ...(field as any), isDeleted: !!(field as any).isDeleted });
} catch (error) {
res.status(500).json({ error: 'Failed to update field' });
@@ -56,11 +54,10 @@ router.put('/:id', async (req: Request, res: Response) => {
router.delete('/:id', async (req: Request, res: Response) => {
try {
const updatedAt = Date.now();
const result = db.prepare(`
UPDATE fields
SET isDeleted = 1, updatedAt = ?
WHERE id = ?
`).run(updatedAt, req.params.id);
const result = await dbAdapter.execute(
'UPDATE fields SET isDeleted = 1, updatedAt = ? WHERE id = ?',
[updatedAt, req.params.id]
);
if (result.changes === 0) {
return res.status(404).json({ error: 'Field not found' });