As a workaround, maybe something like this could work:
db.js:
const { PGlite } = require('@electric-sql/pglite');
const db = new PGlite();
async function execSQL(sql) {
await db.exec(sql);
return { success: true };
}
async function querySQL(sql, params) {
const ret = await db.query(sql, params);
return ret.rows;
}
// Command-line interface logic
const action = process.argv[2];
const sql = process.argv[3];
const params = process.argv.slice(4);
if (action === 'exec') {
execSQL(sql).then(result => {
console.log(JSON.stringify(result));
process.exit();
});
} else if (action === 'query') {
querySQL(sql, params).then(result => {
console.log(JSON.stringify(result));
process.exit();
});
}
main.py:
import subprocess
import json
def execute_sql(sql: str) -> dict:
result = subprocess.run(
['node', 'db.js', 'exec', sql],
stdout=subprocess.PIPE,
text=True
)
return json.loads(result.stdout)
def query_sql(sql: str, params: list) -> list:
result = subprocess.run(
['node', 'db.js', 'query', sql] + params,
stdout=subprocess.PIPE,
text=True
)
return json.loads(result.stdout)
# Example Usage
create_table_sql = """
CREATE TABLE IF NOT EXISTS todo (
id SERIAL PRIMARY KEY,
task TEXT,
done BOOLEAN DEFAULT false
);
"""
execute_sql(create_table_sql)
query_result = query_sql("SELECT * FROM todo WHERE id = $1", ["1"])
print(query_result)