You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.2 KiB
51 lines
1.2 KiB
import * as sqlite3 from "sqlite3"; |
|
|
|
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 default { |
|
name: "PawSQLiteNodeAdapter", |
|
open: async (dbName) => { |
|
if (!databases.has(dbName)) { |
|
const db = new DatabaseWrapper( |
|
new sqlite3.Database(dbName) |
|
); |
|
databases.set(dbName, db); |
|
} |
|
|
|
return ResponseWrapper.success(); |
|
}, |
|
|
|
close: async (dbName) => { |
|
const db = databases.get(dbName); |
|
if (!db) { |
|
throw new NodeAdapterError("Database not open"); |
|
} |
|
|
|
await db.close(); |
|
|
|
databases.delete(dbName); |
|
return ResponseWrapper.success(); |
|
}, |
|
|
|
sql: async (dbName, sql, ...args) => { |
|
const db = databases.get(dbName); |
|
if (!db) { |
|
throw new NodeAdapterError("Database not open"); |
|
} |
|
|
|
const result = await db.sql(sql, ...args); |
|
|
|
return ResponseWrapper.success(result); |
|
}, |
|
|
|
delete: async (dbName) => { |
|
throw new NodeAdapterError("Delete not implemented"); |
|
}, |
|
|
|
debug: enableDebug |
|
};
|
|
|