import { Database } from "./database.mjs"; import { wrapAdapter } from "./adapter_wrapper.mjs"; import { PawSQLiteError } from "./pawsqlite_error.mjs"; import { enableDebug } 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) { const wrapped = wrapAdapter(adapter); if (getAdapter(wrapped.name)) { throw new PawSQLiteError(`An adapter with the name: ${ wrapped.name } ` + `Already exists`); } 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; }