node.js oracledb - Get one record per group with an array

587 views Asked by At

I'm using node.js and OracleDB to join 2 tables, with a one to many relationship.

For Example:

RECORDS Table:
RECORD_ID   COMMENTS
1000        Comment 1
1001        Comment 2

DEVICE Table:

DEVICE_ID   REF_RECORD_ID   DEVICE_NAME
2000        1000            iPhone 6
2001        1000            iPhone 7
2003        1001            Samsung Galaxy S6
2004        1001            Samsung Galaxy S7

I can retrieve a join using

SELECT A.RECORD_ID, A.COMMENT, B.DEVICE_ID, B.DEVICE_NAME FROM RECORDS A, DEVICES B WHERE A.RECORD_ID = B.REF_RECORD_ID;

Current Result:

[{
    RECORD_ID: 1000, COMMENT: "Comment 1", DEVICE_ID: 2000, DEVICE_NAME: "iPhone 6"
},
{
    RECORD_ID: 1000, COMMENT: "Comment 1", DEVICE_ID: 2001, DEVICE_NAME: "iPhone 7"
},
{
    RECORD_ID: 1001, COMMENT: "Comment 2", DEVICE_ID: 2003, DEVICE_NAME: "Samsung Galaxy S6"
},
{
    RECORD_ID: 1001, COMMENT: "Comment 2", DEVICE_ID: 2004, DEVICE_NAME: "Samsung Galaxy S7"
}]

But I would like a result as shown below:

[{
    RECORD_ID: 1000, COMMENT: "Comment 1",
    DEVICES: [{ DEVICE_ID: 2000, DEVICE_NAME: "iPhone 6" }, { DEVICE_ID: 2001, DEVICE_NAME: "iPhone 7" }]
},
{
    RECORD_ID: 1001, COMMENT: "Comment 2",
    DEVICES: [{ DEVICE_ID: 2003, DEVICE_NAME: "Samsung Galaxy S6" }, { DEVICE_ID: 2004, DEVICE_NAME: "Samsung Galaxy S7" }]
}]

Is there a better way to do this rather than looping through the array of objects and then finding duplicate RECORD_ID's and creating an array to push the sub items?

1

There are 1 answers

0
Dan McGhan On

The output you've specified could be done directly when nested cursor expressions are supported. You can see that others are looking for the same thing here: https://github.com/oracle/node-oracledb/issues/565

In the mean time, here's an example of some code that could do this in Node.js once you get the result set:

var resultRowsIdx;
var resultRows = [{
    RECORD_ID: 1000, COMMENT: "Comment 1", DEVICE_ID: 2000, DEVICE_NAME: "iPhone 6"
},
{
    RECORD_ID: 1000, COMMENT: "Comment 1", DEVICE_ID: 2001, DEVICE_NAME: "iPhone 7"
},
{
    RECORD_ID: 1001, COMMENT: "Comment 2", DEVICE_ID: 2003, DEVICE_NAME: "Samsung Galaxy S6"
},
{
    RECORD_ID: 1001, COMMENT: "Comment 2", DEVICE_ID: 2004, DEVICE_NAME: "Samsung Galaxy S7"
}];
var records = [];
var recordsMap = {};
var currentRecordId;

for (resultRowsIdx = 0; resultRowsIdx < resultRows.length; resultRowsIdx += 1) {
  currentRecordId = resultRows[resultRowsIdx].RECORD_ID;

  if (!recordsMap[currentRecordId]) {
    recordsMap[currentRecordId] = {};
    recordsMap[currentRecordId].RECORD_ID = currentRecordId;
    recordsMap[currentRecordId].COMMENT = resultRows[resultRowsIdx].COMMENT;
    recordsMap[currentRecordId].DEVICES = [];

    records.push(recordsMap[currentRecordId]);
  }

  recordsMap[currentRecordId].DEVICES.push({
    DEVICE_ID: resultRows[resultRowsIdx].DEVICE_ID,
    DEVICE_NAME: resultRows[resultRowsIdx].DEVICE_NAME
  });
}

console.log(records);