prism/server/modules/store.ts
2026-01-31 02:00:29 -08:00

72 lines
2 KiB
TypeScript

import { Database } from 'bun:sqlite';
import { mkdirSync } from 'node:fs';
import { dirname } from 'node:path';
import { PRISM_DB } from '@/constants/paths';
import type { NotificationChannel } from '@/types/notifications';
type EndpointMapping = {
endpoint: string;
appName: string;
channel: NotificationChannel;
groupId: string | null;
upEndpoint: string | null;
};
mkdirSync(dirname(PRISM_DB), { recursive: true });
const db = new Database(PRISM_DB);
db.run(`
CREATE TABLE IF NOT EXISTS mappings (
endpoint TEXT PRIMARY KEY,
groupId TEXT,
appName TEXT NOT NULL,
channel TEXT NOT NULL DEFAULT 'signal',
upEndpoint TEXT
)
`);
export function register(
endpoint: string,
appName: string,
channel: 'signal',
options?: { groupId?: string },
): void;
export function register(
endpoint: string,
appName: string,
channel: 'webhook',
options: { upEndpoint: string },
): void;
export function register(
endpoint: string,
appName: string,
channel: NotificationChannel,
options: { groupId?: string; upEndpoint?: string } = {},
) {
const { groupId = null, upEndpoint = null } = options;
db.run(
'INSERT OR IGNORE INTO mappings (endpoint, groupId, appName, channel, upEndpoint) VALUES (?, ?, ?, ?, ?)',
[endpoint, groupId, appName, channel, upEndpoint],
);
}
export const getMapping = (endpoint: string) => {
const row = db
.query(
'SELECT endpoint, groupId, appName, channel, upEndpoint FROM mappings WHERE endpoint = ?',
)
.get(endpoint) as EndpointMapping | undefined;
return row;
};
export const getAllMappings = () =>
db
.query('SELECT endpoint, groupId, appName, channel, upEndpoint FROM mappings')
.all() as EndpointMapping[];
export const updateChannel = (endpoint: string, channel: NotificationChannel) =>
db.run('UPDATE mappings SET channel = ? WHERE endpoint = ?', [channel, endpoint]);
export const removeEndpoint = (endpoint: string) =>
db.run('DELETE FROM mappings WHERE endpoint = ?', [endpoint]);