Compare commits
No commits in common. "cd8db56678fd6d92b0f7998f4d93c0259e4c2a32" and "32e50f783c339434c7d6130d7e028a1aa419341f" have entirely different histories.
cd8db56678
...
32e50f783c
@ -1,36 +0,0 @@
|
||||
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;
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
import { Database } from "./database.mjs";
|
||||
import { wrapAdapter } from "./adapter_wrapper.mjs";
|
||||
import { PawSQLiteError } from "./pawsqlite_error.mjs";
|
||||
import { enableDebug } from "./log.mjs";
|
||||
|
||||
@ -21,25 +20,55 @@ export default {
|
||||
return adapter.delete(dbName);
|
||||
},
|
||||
registerAdapter: function(adapter) {
|
||||
const wrapped = wrapAdapter(adapter);
|
||||
|
||||
if (getAdapter(wrapped.name)) {
|
||||
throw new PawSQLiteError(`An adapter with the name: ${ wrapped.name } ` +
|
||||
`Already exists`);
|
||||
}
|
||||
|
||||
adapters.push(wrapped);
|
||||
adapters.push(wrapAdapter(adapter));
|
||||
},
|
||||
debug: enableDebug,
|
||||
Error: PawSQLiteError
|
||||
};
|
||||
|
||||
|
||||
function wrapAdapter(adapter) {
|
||||
const wrapped = {};
|
||||
|
||||
function getAdapter(adapterName, silent=false) {
|
||||
["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) {
|
||||
const adapter = adapters.find((a) => a.name === adapterName);
|
||||
|
||||
if (!(silent || adapter)) {
|
||||
if (!adapter) {
|
||||
throw new PawSQLiteError(`Unknown Adapter: ${adapterName}`);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user