node.js function that reads clob data type in oracle database

79 views Asked by At

I have a stored procedure on Oracle db and this stored procedure returns the result of a select query as SYS_REFCURSOR under certain conditions. The rows returned in this select query are like this;

    v_sql_stmt := 'SELECT REQUEST_DATA, REQUEST_DATE, RESPONSE_DATA, RESPONSE_DATE FROM service_log WHERE service_name = ''boSearchInvoiceLog''';

When I run the procedure in oracle db, I get the result I want, but when I call it in node.js, the data in response_data returns thousands of rows in a similar structure. Of these four returned fields, REQUEST_DATA and RESPONSE_DATA are stored in clob data type and json format. There is something wrong with my node.js functions and can you help me fix this.

My node.js function;

async function GetSearchInvoiceLogsByParams(params) {
  const connection = await getConnection();

  try {
    const result = await connection.execute(
      `BEGIN ZPAYSP_BO_SEARCH_INVOICE_LOG(:P_WALLET_NO, :P_TCKN, :P_COMPANY_TYPE, :P_COMPANY_CODE, :P_BEGIN_DATE, :P_END_DATE, :P_SUBSCRIBER_CODE, :O_RESPONSECODE, :O_RESPONSECODEDESC, :O_REC_LIST); END;`,
      {
        P_WALLET_NO: params.walletNo || null,
        P_TCKN: params.tckn || null,
        P_COMPANY_TYPE: params.companyType || null,
        P_COMPANY_CODE: params.companyCode || null,
        P_BEGIN_DATE: params.beginDate || null,
        P_END_DATE: params.endDate || null,
        P_SUBSCRIBER_CODE: params.subscriberCode || null,
        O_RESPONSECODE: { dir: oracledb.BIND_OUT, type: oracledb.STRING },
        O_RESPONSECODEDESC: { dir: oracledb.BIND_OUT, type: oracledb.STRING },
        O_REC_LIST: { dir: oracledb.BIND_OUT, type: oracledb.CURSOR }
      }
    );

    if (result.outBinds.O_RESPONSECODE === "000") {
      const cursor = result.outBinds.O_REC_LIST;
      const rows = await readClobData(cursor);
      await cursor.close();

      let res = {};
      res.RESPONSECODE = result.outBinds.O_RESPONSECODE;
      res.RESPONSECODEDESC = result.outBinds.O_RESPONSECODEDESC;
      res.DATA = rows;
      return res;
    } else {
      return {
        RESPONSECODE: result.outBinds.O_RESPONSECODE,
        RESPONSECODEDESC: result.outBinds.O_RESPONSECODEDESC,
      };
    }
  } catch (error) {
    return {
      RESPONSECODE: "999",
      RESPONSECODEDESC: error.message,
    };
  } finally {
    await connection.close();
  }
}

async function readClobData(cursor) {
  const rows = await cursor.getRows();
  return Promise.all(rows.map(row => {
    const requestDate = row[1]; // REQUEST_DATE sütunu
    const responseDate = row[3]; // RESPONSE_DATE sütunu

    const requestDataPromise = new Promise((resolve, reject) => {
      const requestStream = row[0]; // REQUEST_DATA CLOB verisini okuyan stream
      let requestData = '';
      requestStream.on('data', chunk => requestData += chunk);
      requestStream.on('end', () => resolve(JSON.parse(requestData)));
      requestStream.on('error', reject);
    });

    const responseDataPromise = new Promise((resolve, reject) => {
      const responseStream = row[2]; // RESPONSE_DATA CLOB verisini okuyan stream
      let responseData = '';
      responseStream.on('data', chunk => responseData += chunk);
      responseStream.on('end', () => resolve(JSON.parse(responseData)));
      responseStream.on('error', reject);
    });

    return Promise.all([requestDataPromise, responseDataPromise]).then(values => {
      return {
        request_data: values[0],
        request_date: requestDate,
        response_data: values[1],
        response_date: responseDate
      };
    });
  }));
}

When I call the Stored Procedure with this function, it seems that request_data, request_date, response_date return true. But response_data returns thousands of lines with similar structure but irrelevant. For example 500,231,432 instead of data with id 203.

0

There are 0 answers