-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathdbTools.js
More file actions
30 lines (28 loc) · 859 Bytes
/
dbTools.js
File metadata and controls
30 lines (28 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sqlite3pkg from 'node-sqlite3-wasm';
const { Database } = sqlite3pkg;
import { resolvePath } from "../utils/pathHelper.js";
export const sql_query = async (args) => {
const { query } = args;
if (!query || !query.trim().toLowerCase().startsWith("select")) {
return { isError: true, content: [{ type: "text", text: "Only SELECT queries are allowed." }] };
}
const dbPath = resolvePath("database.db");
let db;
try {
db = new Database(dbPath);
const rows = db.all(query);
return {
content: [{
type: "text",
text: JSON.stringify(rows, null, 2)
}]
};
} catch (e) {
return {
isError: true,
content: [{ type: "text", text: `SQL Error: ${e.message}` }]
};
} finally {
if (db) db.close();
}
};