Azri
Azri

Reputation: 85

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?

Upvotes: 1

Views: 3089

Answers (1)

Christopher Jones
Christopher Jones

Reputation: 10516

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.

Upvotes: 3

Related Questions