dbgate-tools
Version:
Auxiliary tools for other DbGate packages.
47 lines (46 loc) • 2.21 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.enrichWithPreloadedRows = void 0;
const lodash_1 = __importDefault(require("lodash"));
async function enrichWithPreloadedRows(dbModel, dbTarget, conn, driver) {
var _a, _b, _c;
// const res = { ...dbTarget, tables: [...(dbTarget.tables || [])] };
const repl = {};
for (const tableTarget of dbTarget.tables) {
const tableModel = dbModel.tables.find(x => x.pairingId == tableTarget.pairingId);
if ((((_a = tableModel === null || tableModel === void 0 ? void 0 : tableModel.preloadedRows) === null || _a === void 0 ? void 0 : _a.length) || 0) == 0)
continue;
const keyColumns = tableModel.preloadedRowsKey || ((_c = (_b = tableModel.primaryKey) === null || _b === void 0 ? void 0 : _b.columns) === null || _c === void 0 ? void 0 : _c.map(x => x.columnName));
if (((keyColumns === null || keyColumns === void 0 ? void 0 : keyColumns.length) || 0) == 0)
continue;
const dmp = driver.createDumper();
if (keyColumns.length == 1) {
dmp.putCmd('^select * ^from %f ^where %i ^in (%,v)', tableTarget, keyColumns[0], tableModel.preloadedRows.map(x => x[keyColumns[0]]));
}
else {
dmp.put('^select * ^from %f ^where', tableTarget);
dmp.putCollection(' ^or ', tableModel.preloadedRows, row => {
dmp.put('(');
dmp.putCollection(' ^and ', keyColumns, col => dmp.put('%i=%v', col, row[col]));
dmp.put(')');
});
dmp.endCommand();
}
const resp = await driver.query(conn, dmp.s);
repl[tableTarget.pairingId] = {
...tableTarget,
preloadedRows: resp.rows,
preloadedRowsKey: keyColumns,
};
}
if (lodash_1.default.isEmpty(repl))
return dbTarget;
return {
...dbTarget,
tables: dbTarget.tables.map(x => repl[x.pairingId] || x),
};
}
exports.enrichWithPreloadedRows = enrichWithPreloadedRows;