2023-08-14 19:11:28 +00:00
|
|
|
import { db } from '@/db.ts';
|
|
|
|
|
|
|
|
/** Inserts relays into the database, skipping duplicates. */
|
|
|
|
function addRelays(relays: `wss://${string}`[]) {
|
2023-08-15 00:00:54 +00:00
|
|
|
if (!relays.length) return Promise.resolve();
|
2023-08-14 19:11:28 +00:00
|
|
|
const values = relays.map((url) => ({ url }));
|
|
|
|
|
|
|
|
return db.insertInto('relays')
|
|
|
|
.values(values)
|
|
|
|
.onConflict((oc) => oc.column('url').doNothing())
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get a list of all known good relays. */
|
|
|
|
async function getAllRelays(): Promise<string[]> {
|
2023-08-15 01:41:20 +00:00
|
|
|
const rows = await db
|
|
|
|
.selectFrom('relays')
|
|
|
|
.select('relays.url')
|
|
|
|
.execute();
|
|
|
|
|
2023-08-14 19:11:28 +00:00
|
|
|
return rows.map((row) => row.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
export { addRelays, getAllRelays };
|