I need to coalesce data from three tables into one data structure. One record in the first table has a relation to 103 records in the second table. Each of the 103 records have relations to 0-to-N records in the third table. How can I get all three related tables in fewer than 104 calls, preferably 1 or 2?
Thank you,
Kelly
Hello @Kelly_Oglesby ,
Assuming you have 3 tables: Parent - 1:N (children) → Child - 1:N (grandchilds) → Grandchild,
I can suggest you following solution:
const MAX_PAGE_SIZE = 100
async function findAllRecords(tableName, query) {
const total = await Backendless.Data.of(tableName).getObjectCount(query && query.where)
const itemsList = []
const requests = []
let currentQuery = {
...query,
pageSize: MAX_PAGE_SIZE,
offset : 0
}
while (currentQuery.offset < total) {
requests.push(Backendless.Data.of(tableName).find(currentQuery))
currentQuery = {
...currentQuery,
offset: currentQuery.offset + MAX_PAGE_SIZE
}
}
const results = await Promise.all(requests)
results.forEach(items => {
items.forEach(item => {
itemsList.push(item)
})
})
return itemsList
}
const parent = await Backendless.Data.of('Parent').findFirst() // assume you already have some parent records
const children = await findAllRecords('Child', {
where : `Parent[children].objectId = '${ parent.objectId }'`,
relations : ['grandchilds'],
relationsPageSize: 100
})
// each child will have grandchilds array
Regards,
Stanislaw