export interface Query {
id: string;
name: string;
timestamp: string;
query: string;
result?: any;
success?: boolean;
error?: string;
}
class QueryStore {
private queries: Map<string, Query> = new Map();
addQuery(name: string, query: string, result: any = null, success: boolean = true, error: string = ''): Query {
const timestamp = new Date().toISOString();
// Create id using the provided name and the current timestamp (numeric for uniqueness)
const safeName = name.replace(/\s+/g, '_');
const id = `${safeName}-${Date.now()}`;
const newQuery: Query = { id, name, timestamp, query, result, success, error };
this.queries.set(id, newQuery);
return newQuery;
}
listQueries(limit: number = 10): Query[] {
return Array.from(this.queries.values())
.sort((a, b) => b.timestamp.localeCompare(a.timestamp))
.slice(0, limit);
}
getQuery(id: string): Query | undefined {
return this.queries.get(id);
}
}
export const queryStore = new QueryStore();