Search code examples
javascriptnode.jsoraclenode-oracledb

NPM Oracle: Inserting BLOBs


I am trying to use the NPM oracle library and update some tables with BLOB values created from files on my computer. Oracle documentation says to use the createLob() function like the following in order to get a value the database will accept:

conn.createLob(oracledb.BLOB, function(err, templob) {
if (err) { . . . }
// ... else use templob
});

But I have no idea what "use templob" implies...

How do I get my data into these oracledb.BLOB objects?

EDIT: Example of update I am attempting:

const queryString = `UPDATE TABLENAME SET BLOB = :blob WHERE ID = 1234;`;
                this.oracleConnection.execute(queryString, 
                    {blob: await fs.readFileSync('/path/to/image.jpg')}
                )

Solution

  • Based on your use case (small files and limited concurrency), I think the buffer APIs will be the best bet as they are very simple. From the doc:

    Given the table:

    CREATE TABLE mylobs (id NUMBER, c CLOB, b BLOB);
    

    an INSERT example is:

    var fs = require('fs');
    var str = fs.readFileSync('example.txt', 'utf8');
    . . .
    
    conn.execute(
      `INSERT INTO mylobs (id, myclobcol) VALUES (:idbv, :cbv)`,
      { idbv: 1,
        cbv: str },  // type and direction are optional for IN binds
      function(err, result) {
        if (err)
          console.error(err.message);
        else
          console.log('CLOB inserted from example.txt');
    . . .
    

    Also, in case you ever need it, I'm wrapping up a mini-series on this topic but it's framed more around web server uploads: https://jsao.io/2019/06/uploading-and-downloading-files-with-node-js-and-oracle-database/