This commit is contained in:
seb
2026-09-22 07:25:16 +02:00
parent 4ed6260e91
commit cca131d83a
148 changed files with 5343 additions and 3466 deletions

View File

@@ -1,15 +1,15 @@
/**
* Resolves the object a store method should query through.
*
* Converted store modules call `runner.query(sql, params)`. During the
* incremental migration (see TODO.md §1) they may still be constructed with a
* Converted store modules call `runner(sql, params)` or `runner.query(sql, params)`.
* During the incremental migration they may still be constructed with a
* raw `mssql` pool, or called with a raw `mssql` Transaction as `tx`. This
* adapter exposes the provider contract over both so mixed call sites keep
* adapter exposes the unified runner contract over both so mixed call sites keep
* working until every module is converted.
*
* @param {import('../providers/provider.js').DatabaseProvider} provider
* @param {object} [tx] Optional provider runner or legacy mssql Transaction.
* @returns {import('../providers/provider.js').DatabaseProvider}
* @returns {((sql: string, params?: object) => Promise<import('../providers/provider.js').QueryResult>) & { query: Function, dialect?: string }}
*/
/** Wraps a raw mssql pool/transaction in the provider query contract. */
@@ -33,8 +33,7 @@ function wrapMssql(target) {
function normalize(target) {
if (!target) return null;
// Provider instances expose `dialect`; legacy mssql pools also have a
// `query()` method, so check for the marker before trusting `query`.
if (typeof target === 'function' && typeof target.query === 'function') return target;
if (target.dialect) return target;
if (typeof target.request === 'function') return wrapMssql(target);
if (typeof target.query === 'function') return target;
@@ -43,7 +42,34 @@ function normalize(target) {
export default function makeRunner(provider, tx) {
if (tx && tx !== provider) {
return normalize(tx) ?? normalize(provider) ?? provider;
if (typeof tx === 'function' && typeof tx.query === 'function') return tx;
const normTx = normalize(tx);
if (normTx) return createCallableRunner(normTx);
}
return normalize(provider) ?? provider;
if (typeof provider === 'function' && typeof provider.query === 'function') return provider;
const normProvider = normalize(provider);
if (normProvider) return createCallableRunner(normProvider);
return null;
}
function createCallableRunner(base) {
if (typeof base === 'function' && typeof base.query === 'function') return base;
const run = async (sql, params) => {
return base.query(sql, params);
};
run.query = run;
run.exec = base.exec ? base.exec.bind(base) : undefined;
run.dialect = base.dialect;
run.provider = base.provider || base;
run.writeFileContent = base.writeFileContent?.bind(base);
run.readFileContent = base.readFileContent?.bind(base);
run.writeThumbAvif = base.writeThumbAvif?.bind(base);
run.readThumbAvif = base.readThumbAvif?.bind(base);
run.writePicFull = base.writePicFull?.bind(base);
run.readPicFull = base.readPicFull?.bind(base);
run.writePicThumb = base.writePicThumb?.bind(base);
run.readPicThumb = base.readPicThumb?.bind(base);
run.deleteFile = base.deleteFile?.bind(base);
run.deletePic = base.deletePic?.bind(base);
return run;
}