Compare commits

..

No commits in common. "7b9095ffa6506095beb0ea7e955e8bbe06dc8566" and "1886fe4f2c22089cc13f31f6e9ac455e51205ae3" have entirely different histories.

2 changed files with 13 additions and 54 deletions

View File

@ -2,8 +2,9 @@ import { Database } from "./database.mjs";
import { PawSQLiteError } from "./pawsqlite_error.mjs"; import { PawSQLiteError } from "./pawsqlite_error.mjs";
import { enableDebug } from "./log.mjs"; import { enableDebug } from "./log.mjs";
// Adapters
const adapters = []; import { PSQLAdapter } from "./adapters/cordova_pawsqlite/psql_adapter.mjs";
import { WebSQLAdapter } from "./adapters/websql/websql_adapter.mjs";
export default { export default {
@ -19,58 +20,22 @@ export default {
return adapter.delete(dbName); return adapter.delete(dbName);
}, },
registerAdapter: function(adapter) {
adapters.push(wrapAdapter(adapter));
},
debug: enableDebug, debug: enableDebug,
Error: PawSQLiteError Error: PawSQLiteError
}; };
function wrapAdapter() {
const wrapped = {};
["name", "open", "close", "delete", "sql"].forEach((prop) => {
if (!(prop in adapter)) {
throw new PawSQLiteError(`Invalid adapter: missing property: ${ prop }`);
}
if (
prop === "name" && !(
typeof adapter[prop] === "string" ||
adapter[prop] instanceof String
) ||
prop !== "name" && (
typeof adapter[prop] !== "function"
)
) {
throw new PawSQLiteError("Invalid adapter: invalid type for property: " +
prop);
}
if (typeof adapter[prop] === "function") {
wrapped[prop] = async (...args) => {
try {
return await adapter[prop](...args);
} catch (err) {
throw PawSQLiteError.from(err);
}
};
} else {
wrapped[prop] = adapter[prop];
}
});
return wrapped;
}
function getAdapter(adapterName) { function getAdapter(adapterName) {
const adapter = adapters.find((a) => a.name === adapterName); if (!adapterName) {
adapterName = "pawsqlite";
if (!adapter) { } else {
adapterName = adapterName.toLowerCase();
}
if (adapterName === "pawsqlite") {
return PSQLAdapter;
} else if (adapterName === "websql") {
return WebSQLAdapter;
} else {
throw new PawSQLiteError(`Unknown Adapter: ${adapterName}`); throw new PawSQLiteError(`Unknown Adapter: ${adapterName}`);
} }
return adapter;
} }

View File

@ -1,10 +1,4 @@
export class PawSQLiteError extends Error { export class PawSQLiteError extends Error {
static from(err) {
const p_err = new PawSQLiteError(err.toString());
p_err.cause = err;
return p_err;
}
constructor(message) { constructor(message) {
super(message); super(message);
this.name = "PawSQLiteError"; this.name = "PawSQLiteError";