Search code examples
node.jsoraclenode-oracledb

Fetching LOB as String


trying to make a query against a view that returns a CLOB field

that CLOB is a json that i would like to access

due to nature of architecture i would like to avoid streaming

import * as oracleDB from 'oracledb';

oracleDB.fetchAsString = [ oracleDB.CLOB ];

const connection = await oracleDB.getConnection({
                user: process.env.ORACLE_USER,
                password: process.env.ORACLE_PW,
                connectString: process.env.ORACLE_HOST
            });

const result = await connection.execute(queryString);
await connection.close();

this is what i have found in the oracle docs

it seems to not work however as there are 2 problems

  1. oracleDB.fetchAsString = [ oracleDB.CLOB ];
    

    is not possible (without ugly override) as fetchAsString is flagged as read only

  2. even then i still get LOB

there is a second option found in the docs

const result = await connection.execute(
    `SELECT c FROM mylobs WHERE id = 1`,
    [], // no binds
    { fetchInfo: {"C": {type: oracledb.STRING}} }
);

this is not possible as well as type is expecting number and STRING is an object

EDIT: I made it work by using the fetchinfo method and adding // @ts-ignore // eslint-disable-next-line prettier/prettier to the code to statisfy both, the typemismatch for the execute function and eslint trying to remove "" from the column name


Solution

  • To fetch a CLOB field as a string in OracleDB without streaming, you can use the fetchInfo option in the execute() method. However, there seems to be a misunderstanding about the usage.