2021-03-01 18:47:07 -08:00
|
|
|
import { Database } from "./database.mjs";
|
2021-03-02 14:25:40 -08:00
|
|
|
import { wrapAdapter } from "./adapter_wrapper.mjs";
|
2021-03-01 18:47:07 -08:00
|
|
|
import { PawSQLiteError } from "./pawsqlite_error.mjs";
|
2021-03-04 20:07:13 -08:00
|
|
|
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) {
|
2021-03-02 21:53:46 -08:00
|
|
|
if (!adapter) {
|
|
|
|
throw new PawSQLiteError("Invalid adapter");
|
|
|
|
}
|
|
|
|
|
2021-03-02 14:25:40 -08:00
|
|
|
const wrapped = wrapAdapter(adapter);
|
|
|
|
|
2021-03-02 18:19:18 -08:00
|
|
|
if (getAdapter(wrapped.name, true)) {
|
2021-03-02 14:25:40 -08:00
|
|
|
throw new PawSQLiteError(`An adapter with the name: ${ wrapped.name } ` +
|
|
|
|
`Already exists`);
|
|
|
|
}
|
|
|
|
|
2021-03-04 20:07:13 -08:00
|
|
|
log(`Registered adapter: ${ wrapped.name }`);
|
|
|
|
|
2021-03-02 14:25:40 -08:00
|
|
|
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
|
|
|
|
2021-03-02 14:25:40 -08:00
|
|
|
function getAdapter(adapterName, silent=false) {
|
2021-03-01 22:19:38 -08:00
|
|
|
const adapter = adapters.find((a) => a.name === adapterName);
|
|
|
|
|
2021-03-02 14:25:40 -08:00
|
|
|
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
|
|
|
}
|