pg-promise - Combine multiple nested loop queries to parent array result
Asked Answered
A

2

0

My question is based on Combine nested loop queries to parent array result - pg-promise. I'm having a similar scenario but have multiple queries to be combined to get my final results. Following is my code with which I tried to implement my requirement. But I was not able to get the results from second query combined with the main query. I'm a beginner and would like to know the correct way of implementation.

  db.task(t => {
    const a = studies => t.any ('SELECT facility_contacts.name, facility_contacts.email, facility_contacts.phone FROM facility_contacts WHERE nct_id = $1', studies.nct_id).then(facility_contacts => {
    studies.facility_contacts = facility_contacts;
    return studies
      });
    const b = studies => t.any ('SELECT eligibilities.criteria, eligibilities.gender FROM eligibilities WHERE nct_id = $1', studies.nct_id).then(eligibilities => {
    studies.eligibilities = eligibilities;
    return studies;
      });
  return t.map('SELECT studies.nct_id, studies.official_title, studies.phase, facilities.country FROM studies, facilities WHERE facilities.country LIKE \'%Ireland%\' LIMIT 10',[], a, b).then(t.batch);
}).then(studies => {
  console.log(studies);
  res.send(studies);
}).catch(error => {
  console.log(error);
});

Thanks in advance.

Aversion answered 23/7, 2018 at 10:19 Comment(2)
You should post the code of what you have tried so far, and explain which part didn't work. Simply asking someone to write your code for you isn't going well with the StackOverflow community. Also, the question title is meaningless in this context.Tergum
@Tergum Sorry for the inconvenience. I've added my code.Aversion
T
1

Here's one possibility:

db.task(t => {
    const a = studies => t.any('SELECT facility_contacts.name, facility_contacts.email, facility_contacts.phone FROM facility_contacts WHERE nct_id = $1', studies.nct_id)
        .then(facility_contacts => {
            studies.facility_contacts = facility_contacts;
            return studies
        });
    const b = studies => t.any('SELECT eligibilities.criteria, eligibilities.gender FROM eligibilities WHERE nct_id = $1', studies.nct_id)
        .then(eligibilities => {
            studies.eligibilities = eligibilities;
            return studies;
        });
    const c = studies => t.batch([a(studies), b(studies)]);
    return t.map('SELECT studies.nct_id, studies.official_title, studies.phase, facilities.country FROM studies, facilities WHERE facilities.country LIKE \'%Ireland%\' LIMIT 10', [], c)
        .then(t.batch);
})
    .then(studies => {
        console.log(studies);
        res.send(studies);
    })
    .catch(error => {
        console.log(error);
    });

There can be many different approaches, some way better performing, like JSON-based.

Tergum answered 23/7, 2018 at 14:43 Comment(0)
M
0

You can use Promise.all to get all result

db.task(t => {
    const queries = [
        t.any('SELECT facility_contacts.name, facility_contacts.email, facility_contacts.phone FROM facility_contacts WHERE nct_id = $1', studies.nct_id),
        t.any('SELECT eligibilities.criteria, eligibilities.gender FROM eligibilities WHERE nct_id = $1', studies.nct_id),
        t.map('SELECT studies.nct_id, studies.official_title, studies.phase, facilities.country FROM studies, facilities WHERE facilities.country LIKE \'%Ireland%\' LIMIT 10', [], a, b)
    ];
    return Promise.all(queries);
}).then((ResultArrayOfQueries) => {
    console.log(ResultArrayOfQueries);
    // Combine as per your requirement
    res.send(ResultArrayOfQueries);
}).catch(error => {
    console.log(error);
});
Morrismorrison answered 23/7, 2018 at 12:32 Comment(3)
I'm getting error related to studies.nct_id propertyAversion
One should never use Promise.all in the context of an open connection, as pg-promise explains: github.com/vitaly-t/pg-promise/wiki/…Tergum
Also, this code doesn't even implement any dependency between query data that's explained in the question.Tergum

© 2022 - 2024 — McMap. All rights reserved.