|
|
|
import { NodeAdapterError } from "./node_adapter_error.mjs";
|
|
|
|
import { DatabaseWrapper } from "./database_wrapper.mjs";
|
|
|
|
import { ResponseWrapper } from "./response_wrapper.mjs";
|
|
|
|
import { enableDebug, log } from "./log.mjs";
|
|
|
|
|
|
|
|
const databases = new Map();
|
|
|
|
|
|
|
|
export const PawSQLiteNodeAdapter = {
|
|
|
|
name: "PawSQLiteNodeAdapter",
|
|
|
|
open: async (dbName) => {
|
|
|
|
if (!databases.has(dbName)) {
|
|
|
|
const db = new DatabaseWrapper(dbName);
|
|
|
|
await db.open();
|
|
|
|
databases.set(dbName, db);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResponseWrapper.success();
|
|
|
|
},
|
|
|
|
|
|
|
|
close: async (dbName) => {
|
|
|
|
const db = getDatabase(dbName);
|
|
|
|
await db.close();
|
|
|
|
databases.delete(dbName);
|
|
|
|
|
|
|
|
return ResponseWrapper.success();
|
|
|
|
},
|
|
|
|
|
|
|
|
sql: async (dbName, sql, ...args) => {
|
|
|
|
const db = getDatabase(dbName);
|
|
|
|
const result = await db.sql(sql, ...args);
|
|
|
|
|
|
|
|
return ResponseWrapper.success(result);
|
|
|
|
},
|
|
|
|
|
|
|
|
delete: async (dbName) => {
|
|
|
|
throw new NodeAdapterError("Delete not implemented");
|
|
|
|
},
|
|
|
|
|
|
|
|
debug: enableDebug
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function getDatabase(dbName) {
|
|
|
|
const db = databases.get(dbName);
|
|
|
|
if (!db) {
|
|
|
|
throw new NodeAdapterError("Database not open");
|
|
|
|
}
|
|
|
|
return db;
|
|
|
|
}
|