PawSQLite/src/pawsqlite.mjs

53 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-03-01 18:47:07 -08:00
import { Database } from "./database.mjs";
import { wrapAdapter } from "./adapter_wrapper.mjs";
2021-03-01 18:47:07 -08:00
import { PawSQLiteError } from "./pawsqlite_error.mjs";
import { enableDebug, log } from "./log.mjs";
2021-03-01 18:47:07 -08:00
2021-03-01 22:19:38 -08:00
const adapters = [];
2021-03-01 18:47:07 -08:00
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);
},
2021-03-01 22:19:38 -08:00
registerAdapter: function(adapter) {
if (!adapter) {
throw new PawSQLiteError("Invalid adapter");
}
const wrapped = wrapAdapter(adapter);
2021-03-02 18:19:18 -08:00
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);
2021-03-01 22:19:38 -08:00
},
2021-03-01 18:47:07 -08:00
debug: enableDebug,
Error: PawSQLiteError
};
2021-03-01 22:19:38 -08:00
function getAdapter(adapterName, silent=false) {
2021-03-01 22:19:38 -08:00
const adapter = adapters.find((a) => a.name === adapterName);
if (!(silent || adapter)) {
2021-03-01 18:47:07 -08:00
throw new PawSQLiteError(`Unknown Adapter: ${adapterName}`);
}
2021-03-01 22:19:38 -08:00
return adapter;
2021-03-01 18:47:07 -08:00
}