Compare commits
2 Commits
1886fe4f2c
...
7b9095ffa6
Author | SHA1 | Date | |
---|---|---|---|
7b9095ffa6 | |||
38df7f1053 |
@ -2,9 +2,8 @@ import { Database } from "./database.mjs";
|
||||
import { PawSQLiteError } from "./pawsqlite_error.mjs";
|
||||
import { enableDebug } from "./log.mjs";
|
||||
|
||||
// Adapters
|
||||
import { PSQLAdapter } from "./adapters/cordova_pawsqlite/psql_adapter.mjs";
|
||||
import { WebSQLAdapter } from "./adapters/websql/websql_adapter.mjs";
|
||||
|
||||
const adapters = [];
|
||||
|
||||
|
||||
export default {
|
||||
@ -20,22 +19,58 @@ export default {
|
||||
|
||||
return adapter.delete(dbName);
|
||||
},
|
||||
registerAdapter: function(adapter) {
|
||||
adapters.push(wrapAdapter(adapter));
|
||||
},
|
||||
debug: enableDebug,
|
||||
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) {
|
||||
if (!adapterName) {
|
||||
adapterName = "pawsqlite";
|
||||
} else {
|
||||
adapterName = adapterName.toLowerCase();
|
||||
}
|
||||
if (adapterName === "pawsqlite") {
|
||||
return PSQLAdapter;
|
||||
} else if (adapterName === "websql") {
|
||||
return WebSQLAdapter;
|
||||
} else {
|
||||
const adapter = adapters.find((a) => a.name === adapterName);
|
||||
|
||||
if (!adapter) {
|
||||
throw new PawSQLiteError(`Unknown Adapter: ${adapterName}`);
|
||||
}
|
||||
|
||||
return adapter;
|
||||
}
|
@ -1,4 +1,10 @@
|
||||
export class PawSQLiteError extends Error {
|
||||
static from(err) {
|
||||
const p_err = new PawSQLiteError(err.toString());
|
||||
p_err.cause = err;
|
||||
return p_err;
|
||||
}
|
||||
|
||||
constructor(message) {
|
||||
super(message);
|
||||
this.name = "PawSQLiteError";
|
||||
|
Loading…
Reference in New Issue
Block a user