Hello from MCP server
import { getDb } from "@/dataAccess/getDb";
export interface LogEntry {
id?: number;
created_at?: string;
log_type: string;
log_data: Record<string, any>;
created_by: string;
created_by_name: string;
}
export interface LogEntryStored {
id: number;
created_at: string;
log_type: string;
log_data: Record<string, any>;
created_by: string;
created_by_name: string;
}
/**
* Save a log entry to the logs table
*
* @param entry - The log entry to save
* @returns The id of the inserted log entry
*/
export async function save(entry: LogEntry): Promise<number> {
const db = await getDb();
const stmt = `
INSERT INTO logs (log_type, log_data, created_by, created_by_name)
VALUES (?, ?, ?, ?)
`;
const result = await db.dbConn.run(stmt, [
entry.log_type,
JSON.stringify(entry.log_data),
entry.created_by,
entry.created_by_name,
]);
await db.saveDb();
return result.changes?.lastId || 0;
}
/**
* List log entries from the logs table
*
* @param options - Query options
* @param options.limit - Maximum number of entries to return (default: 100)
* @param options.offset - Number of entries to skip (default: 0)
* @param options.log_type - Filter by log type (optional)
* @param options.created_by - Filter by creator ID (optional)
* @returns Array of log entries
*/
export async function list(options: {
limit?: number;
offset?: number;
log_type?: string;
created_by?: string;
} = {}): Promise<LogEntryStored[]> {
const db = await getDb();
const { limit = 100, offset = 0, log_type, created_by } = options;
let query = `SELECT * FROM logs`;
const conditions: string[] = [];
const params: any[] = [];
if (log_type) {
conditions.push(`log_type = ?`);
params.push(log_type);
}
if (created_by) {
conditions.push(`created_by = ?`);
params.push(created_by);
}
if (conditions.length > 0) {
query += ` WHERE ${conditions.join(' AND ')}`;
}
query += ` ORDER BY created_at DESC LIMIT ? OFFSET ?`;
params.push(limit, offset);
const result = await db.dbConn.query(query, params);
return (result.values || []).map((row: any) => ({
id: row.id,
created_at: row.created_at,
log_type: row.log_type,
log_data: JSON.parse(row.log_data || '{}'),
created_by: row.created_by,
created_by_name: row.created_by_name,
}));
}