import { Database } from "./database.mjs"; import { wrapAdapter } from "./adapter_wrapper.mjs"; import { PawSQLiteError } from "./pawsqlite_error.mjs"; import { enableDebug, log } from "./log.mjs"; const adapters = []; export default { open: async function(dbName, options = {}) { const adapter = getAdapter(options.adapter); let db = new Database(dbName, adapter); await db.open(); return db; }, delete: function(dbName, options = {}) { const adapter = getAdapter(options.adapter); return adapter.delete(dbName); }, registerAdapter: function(adapter) { if (!adapter) { throw new PawSQLiteError("Invalid adapter"); } const wrapped = wrapAdapter(adapter); if (getAdapter(wrapped.name, true)) { throw new PawSQLiteError(`An adapter with the name: ${ wrapped.name } ` + `Already exists`); } log(`Registered adapter: ${ wrapped.name }`); adapters.push(wrapped); }, debug: enableDebug, Error: PawSQLiteError }; function getAdapter(adapterName, silent=false) { const adapter = adapters.find((a) => a.name === adapterName); if (!(silent || adapter)) { throw new PawSQLiteError(`Unknown Adapter: ${adapterName}`); } return adapter; }