const test_module_query = text SELECT FROM publictests AS t1 INNER JOIN modules_has_tests AS mht ON t1uuid = mhttest_id ; const getAlleyData = await clientquerytest_module_q
To perform the query in a single query and fetch the data from the module_table for all module_ids, you can use the following modified code:
const test_module_query = {
text: 'SELECT t1.*, mht.module_id FROM public.tests AS t1 INNER JOIN modules_has_tests AS mht ON t1.uuid = mht.test_id',
};
const getAlleyData = await client.query(test_module_query);
console.log("getAlleyData",getAlleyData.rows);
const testModuleIds = getAlleyData.rows.map(row => row.module_id);
async function fetchDataForAllModules() {
const moduleDataQuery = {
text: 'SELECT * FROM modules WHERE uuid = ANY($1::uuid[])',
values: [testModuleIds],
};
const moduleDataResult = await client.query(moduleDataQuery);
console.log("moduleDataResult",moduleDataResult.rows);
const combinedData = [];
for (const row of getAlleyData.rows) {
const moduleId = row.module_id;
const moduleData = moduleDataResult.rows.find(data => data.uuid === moduleId);
combinedData.push({
test: row,
module: moduleData,
});
}
return combinedData;
}
const allData = await fetchDataForAllModules();
In this modified code, the initial query test_module_query retrieves both the test data and the corresponding module_ids in a single query. Then, the fetchDataForAllModules function uses the retrieved module_ids to fetch the module data from the module_table using the ANY operator in the query. The module data is then combined with the test data to create the desired combinedData array
原文地址: http://www.cveoy.top/t/topic/ikuQ 著作权归作者所有。请勿转载和采集!