node.jspromisenode-oracledbexecutemany

Node.js + Oracledb + executeMany + batchErrors + Promise: does not execute all if one error


iam using BatchError with executeMany

if no errors.. everything is ok

but if one error or more .. it doesn't execute. it gives rowsAffected = error row number

my code:

const oracledb = require('oracledb');

async function post(req, res, next) {
    try {
      let user = {};
      user = await create();
  
      res.status(201).json(user);
    } catch (err) {
      next(err);
    }
  }
  
  module.exports.post = post;


  const sql =
  `INSERT INTO TABLE (
     USER_CODE, 
     USER_NAME,
     STAFF_ID,
     ROLE_ID,
     TEAM_CODE,
     GRP_ID,
     MOBILE_NUMBER
   ) VALUES (
     :USER_CODE,
     :USER_NAME,
     :STAFF_ID,
     :ROLE_ID,
     :TEAM_CODE,
     :GRP_ID,
     :MOBILE_NUMBER
   )`;

  async function create() {

    const data = [
      {"USER_CODE":600, "user_name": "att1", "staff_id": 660, "role_id": 1, "team_code": 20, "grp_id": 1, "mobile_number": "0101"},
      {"USER_CODE":600, "user_name": "att2", "staff_id": 661, "role_id": 1, "team_code": 20, "grp_id": 1, "mobile_number": "0102"},
      {"USER_CODE":602, "user_name": "att3", "staff_id": 662, "role_id": 1, "team_code": 20, "grp_id": 1, "mobile_number": "0103"}
   ];

    let opts = {};

    let result = await manyExecute(sql, data, opts);
 
    return result;
  }
  
  module.exports.create = create;

function manyExecute(statement, binds, opts) {
    return new Promise(async (resolve, reject) => {
      let conn;
  
      opts.outFormat = oracledb.OBJECT;
      opts.autoCommit = true;
      opts.batchErrors = true;

      console.log(opts);
  
      try {
        conn = await oracledb.getConnection();
  
        result = await conn.executeMany(statement, binds, opts);
  
        resolve(result);
      } catch (err) {
        reject(err);
      } finally {
        if (conn) { // conn assignment worked, need to close
          try {
            await conn.close();
          } catch (err) {
            console.log(err);
          }
        }
      }
    });
  }
  
  module.exports.manyExecute = manyExecute;

if i executed above code, this error appears (USER_CODE is pk):

{ "rowsAffected": 2, "batchErrors": [ { "errorNum": 1, "offset": 1 } ] }

i expected rowsAffected = 3 also, first item not added to TABLE

Thanks


Solution

  • Adding commit() to my code solved my problem

    function manyExecute(statement, binds, opts) {
        return new Promise(async (resolve, reject) => {
          let conn;
      
          opts.outFormat = oracledb.OBJECT;
          opts.autoCommit = true;
          opts.batchErrors = true;
    
          console.log(opts);
      
          try {
            conn = await oracledb.getConnection();
      
            result = await conn.executeMany(statement, binds, opts);
      
            resolve(result);
            await conn.commit();
    
          } catch (err) {
            reject(err);
          } finally {
            if (conn) { // conn assignment worked, need to close
              try {
                await conn.close();
              } catch (err) {
                console.log(err);
              }
            }
          }
        });
      }
      
      module.exports.manyExecute = manyExecute;
    

    Thanks to Dan McGhan, thank you all for help :)