Hello from MCP server
import { getDb } from "@/dataAccess/getDb";
/**
* Get a preference value by key
*
* @param key - The preference key
* @returns The value or null if not found
*/
export async function get(key: string): Promise<string | null> {
const db = await getDb();
const result = await db.dbConn.query(
"SELECT value FROM userPrefs WHERE key = ?",
[key]
);
if (!result.values || result.values.length === 0) {
return null;
}
return result.values[0].value;
}
/**
* Get all preferences matching a prefix
*
* @param prefix - The key prefix (e.g., "speedDial" matches "speedDial.0", "speedDial.1", etc.)
* @returns Record of key-value pairs
*/
export async function getByPrefix(prefix: string): Promise<Record<string, string>> {
const db = await getDb();
const result = await db.dbConn.query(
"SELECT key, value FROM userPrefs WHERE key LIKE ?",
[`${prefix}%`]
);
const prefs: Record<string, string> = {};
for (const row of result.values || []) {
prefs[row.key] = row.value;
}
return prefs;
}
/**
* Set a preference value
*
* @param key - The preference key
* @param value - The value to store
*/
export async function set(key: string, value: string): Promise<void> {
const db = await getDb();
const stmt = `
INSERT INTO userPrefs (key, value) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value
`;
await db.dbConn.run(stmt, [key, value]);
await db.saveDb();
}
/**
* Set multiple preferences at once
*
* @param prefs - Record of key-value pairs to store
*/
export async function setMany(prefs: Record<string, string>): Promise<void> {
const db = await getDb();
const stmt = `
INSERT INTO userPrefs (key, value) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = excluded.value
`;
for (const [key, value] of Object.entries(prefs)) {
await db.dbConn.run(stmt, [key, value]);
}
await db.saveDb();
}
/**
* Remove a preference by key
*
* @param key - The preference key to remove
*/
export async function remove(key: string): Promise<void> {
const db = await getDb();
await db.dbConn.run("DELETE FROM userPrefs WHERE key = ?", [key]);
await db.saveDb();
}
/**
* Remove all preferences matching a prefix
*
* @param prefix - The key prefix to match
*/
export async function removeByPrefix(prefix: string): Promise<void> {
const db = await getDb();
await db.dbConn.run("DELETE FROM userPrefs WHERE key LIKE ?", [`${prefix}%`]);
await db.saveDb();
}