Moved adapter wrapping to its own file
This commit is contained in:
parent
32e50f783c
commit
c15fa92174
36
src/adapter_wrapper.mjs
Normal file
36
src/adapter_wrapper.mjs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
export function wrapAdapter(adapter) {
|
||||||
|
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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user