Search code examples
node.jsoracle-databaseblobnode-oracledb

Read BLOB from Oracle Database in Node.js


I am trying to read a BLOB data type from Oracle using a nodejs from npm package called oracledb and this is my select statement, it hold picture in it:

select media from test_data

I have tried using a few solution but it doesn't work.

Here is my response from the res.json:

{
        "MEDIA": {
            "_readableState": {
                "objectMode": false,
                "highWaterMark": 16384,
                "buffer": {
                    "head": null,
                    "tail": null,
                    "length": 0
                },
                "length": 0,
                "pipes": [],
                "flowing": null,
                "ended": false,
                "endEmitted": false,
                .....
   
    },

How do I get the hex value of it?


Solution

  • You are seeing an instance of the node-oracledb Lob class which you can stream from.

    However, it can be easiest to get a Buffer directly. Do this by setting oracledb.fetchAsBuffer, for example:

    oracledb.fetchAsBuffer = [ oracledb.BLOB ];
    
    const result = await connection.execute(`SELECT b FROM mylobs WHERE id = 2`);
    
    if (result.rows.length === 0)
      console.error("No results");
    else {
      const blob = result.rows[0][0];
      console.log(blob.toString());  // assuming printable characters
    }
    

    Refer to the documentation Simple LOB Queries and PL/SQL OUT Binds.