db code, check if database is up before any select operation

This commit is contained in:
Sam Hadow 2025-03-05 13:37:03 +01:00
parent 10f25b8810
commit 16b6b4edf9

236
src/db.js
View File

@ -92,95 +92,187 @@ const database = {
console.error("Pubkey is required"); console.error("Pubkey is required");
return; return;
} }
try { const maxRetries = 5;
await pool.query( let retries = 0;
'INSERT INTO "users" (uuid, pubkey) VALUES (DEFAULT, $1)', while (retries < maxRetries) {
[pubkey,] const dbUp = await database.checkIfDatabaseIsUp();
); if (dbUp) {
console.log(`Added user with the public key ${pubkey} .`); try {
} catch (err) { await pool.query(
console.error('Error adding user:', err); 'INSERT INTO "users" (uuid, pubkey) VALUES (DEFAULT, $1)',
[pubkey]
);
console.log(`Added user with the public key ${pubkey}.`);
return;
} catch (err) {
console.error('Error adding user:', err);
throw err;
}
} else {
console.log('Waiting for the database to start...');
retries += 1;
if (retries < maxRetries) {
await new Promise(resolve => setTimeout(resolve, 10000));
} else {
console.error(`Failed to connect to the database after ${maxRetries} attempts.`);
}
}
} }
}, },
createRoom: async (pubkey1, pubkey2) => { createRoom: async (pubkey1, pubkey2) => {
try { const maxRetries = 5;
const userQuery = 'SELECT uuid FROM users WHERE pubkey = $1'; let retries = 0;
const uuidRes1 = await pool.query(userQuery, [pubkey1]); while (retries < maxRetries) {
const uuidRes2 = await pool.query(userQuery, [pubkey2]); const dbUp = await database.checkIfDatabaseIsUp();
if (dbUp) {
try {
const userQuery = 'SELECT uuid FROM users WHERE pubkey = $1';
const uuidRes1 = await pool.query(userQuery, [pubkey1]);
const uuidRes2 = await pool.query(userQuery, [pubkey2]);
if (!uuidRes1.rows[0] || !uuidRes2.rows[0]) { if (!uuidRes1.rows[0] || !uuidRes2.rows[0]) {
throw new Error('One or both users not found'); throw new Error('One or both users not found');
}
const uuid1 = uuidRes1.rows[0].uuid;
const uuid2 = uuidRes2.rows[0].uuid;
const roomRes = await pool.query(
'INSERT INTO room (uuid) VALUES (DEFAULT) RETURNING uuid'
);
const roomid = roomRes.rows[0].uuid;
await pool.query(
`INSERT INTO room_members (room_uuid, user_uuid)
VALUES ($1, $2), ($1, $3)`,
[roomid, uuid1, uuid2]
);
return roomid;
} catch (err) {
console.error('Error creating the room:', err);
throw err;
}
} else {
console.log('Waiting for the database to start...');
retries += 1;
if (retries < maxRetries) {
await new Promise(resolve => setTimeout(resolve, 10000));
} else {
console.error(`Failed to connect to the database after ${maxRetries} attempts.`);
}
} }
const uuid1 = uuidRes1.rows[0].uuid;
const uuid2 = uuidRes2.rows[0].uuid;
const roomRes = await pool.query(
'INSERT INTO room (uuid) VALUES (DEFAULT) RETURNING uuid'
);
const roomid = roomRes.rows[0].uuid;
await pool.query(
`INSERT INTO room_members (room_uuid, user_uuid)
VALUES ($1, $2), ($1, $3)`,
[roomid, uuid1, uuid2]
);
return roomid;
} catch (err) {
console.error('Error creating the room:', err);
throw err; // Re-throw to handle in calling code
} }
}, },
getRooms: async (pubkey) => { getRooms: async (pubkey) => {
try { const maxRetries = 5;
const roomsRes = await pool.query( let retries = 0;
'SELECT m.room_uuid FROM room_members m, users u WHERE m.user_uuid = u.uuid AND u.pubkey = $1', while (retries < maxRetries) {
[pubkey] const dbUp = await database.checkIfDatabaseIsUp();
); if (dbUp) {
return roomsRes.rows.map(row => row.room_uuid); try {
} catch (err) { const roomsRes = await pool.query(
console.error('Error retrieving rooms:', err); 'SELECT m.room_uuid FROM room_members m, users u WHERE m.user_uuid = u.uuid AND u.pubkey = $1',
throw err; [pubkey]
);
return roomsRes.rows.map(row => row.room_uuid);
} catch (err) {
console.error('Error retrieving rooms:', err);
throw err;
}
} else {
console.log('Waiting for the database to start...');
retries += 1;
if (retries < maxRetries) {
await new Promise(resolve => setTimeout(resolve, 10000));
} else {
console.error(`Failed to connect to the database after ${maxRetries} attempts.`);
}
}
} }
}, },
getRoomMembers: async (roomid) => { getRoomMembers: async (roomid) => {
try { const maxRetries = 5;
const members = await pool.query( let retries = 0;
'SELECT u.pubkey FROM room_members r, users u WHERE r.user_uuid = u.uuid AND r.room_uuid = $1', while (retries < maxRetries) {
[roomid] const dbUp = await database.checkIfDatabaseIsUp();
); if (dbUp) {
return members.rows.map(row => row.pubkey); try {
} catch (err) { const members = await pool.query(
console.error('Error retrieving rooms:', err); 'SELECT u.pubkey FROM room_members r, users u WHERE r.user_uuid = u.uuid AND r.room_uuid = $1',
throw err; [roomid]
);
return members.rows.map(row => row.pubkey);
} catch (err) {
console.error('Error retrieving rooms:', err);
throw err;
}
} else {
console.log('Waiting for the database to start...');
retries += 1;
if (retries < maxRetries) {
await new Promise(resolve => setTimeout(resolve, 10000));
} else {
console.error(`Failed to connect to the database after ${maxRetries} attempts.`);
}
}
} }
}, },
getPeers: async (pubkey) => { getPeers: async (pubkey) => {
try { const maxRetries = 5;
const peers = await pool.query( let retries = 0;
`SELECT u1.pubkey while (retries < maxRetries) {
FROM room_members r1, room_members r2, users u1, users u2 const dbUp = await database.checkIfDatabaseIsUp();
WHERE r1.user_uuid = u1.uuid if (dbUp) {
AND r2.user_uuid = u2.uuid try {
AND r1.room_uuid = r2.room_uuid const peers = await pool.query(
AND u2.pubkey != u1.pubkey `SELECT u1.pubkey
AND u2.pubkey = $1`, FROM room_members r1, room_members r2, users u1, users u2
[pubkey] WHERE r1.user_uuid = u1.uuid
); AND r2.user_uuid = u2.uuid
return peers.rows.map(row => row.pubkey); AND r1.room_uuid = r2.room_uuid
} catch (err) { AND u2.pubkey != u1.pubkey
console.error('Error retrieving peers:', err); AND u2.pubkey = $1`,
throw err; [pubkey]
);
return peers.rows.map(row => row.pubkey);
} catch (err) {
console.error('Error retrieving peers:', err);
throw err;
}
} else {
console.log('Waiting for the database to start...');
retries += 1;
if (retries < maxRetries) {
await new Promise(resolve => setTimeout(resolve, 10000));
} else {
console.error(`Failed to connect to the database after ${maxRetries} attempts.`);
}
}
} }
}, },
getPublicKeys: async () => { getPublicKeys: async () => {
try { const maxRetries = 5;
const result = await pool.query('SELECT pubkey FROM users'); let retries = 0;
const publicKeys = result.rows.map(row => row.pubkey); while (retries < maxRetries) {
return publicKeys; const dbUp = await database.checkIfDatabaseIsUp();
} catch (err) { if (dbUp) {
console.error('Error retrieving public keys:', err); try {
throw new Error('Error retrieving public keys'); const result = await pool.query('SELECT pubkey FROM users');
const publicKeys = result.rows.map(row => row.pubkey);
return publicKeys;
} catch (err) {
console.error('Error retrieving public keys:', err);
throw new Error('Error retrieving public keys');
}
} else {
console.log('Waiting for the database to start...');
retries += 1;
if (retries < maxRetries) {
await new Promise(resolve => setTimeout(resolve, 10000));
} else {
console.error(`Failed to connect to the database after ${maxRetries} attempts.`);
}
}
} }
} }
}; };