Matt Crysler
Matt Crysler

Reputation: 885

NodeJS ODBC library not returning affected/inserted rows as result

I am resorting to asking the minds of StackOverflow to help me identify what it is I'm doing wrong. I have written a NodeJS app that connects to a Microsoft Fabric database. I am using an ODBC connection via the npm ODBC library. I cannot, for the life of me, seem to get the number of rows inserted after an insert statement runs. The connection is fine and the row gets inserted but the results are always empty. Here is the sample code directly from the ODBC npm page:

const odbc = require('odbc');

async function commitTransaction() {
    const connection = await odbc.connect(`${process.env.CONNECTION_STRING}`);
    await connection.beginTransaction();
    const insertResult = await connection.query('INSERT INTO MY_TABLE VALUES(1, \'Name\')');
    await connection.commit();
    // INSERT query has now been committed
}

commitTransaction();

My expectation would be the insertResult variable contains a relevant response which includes the number of rows inserted. The relevant part of the connection string is that I'm using this driver: ODBC Driver 18 for SQL Server as Microsoft Fabric documentation states the ODBC Driver must be version 18 or higher.

Any help in identifying what it is I am doing wrong would be greatly appreciated! Thank you!

Upvotes: 0

Views: 13

Answers (1)

Matt Crysler
Matt Crysler

Reputation: 885

I am an idiot. The log statement was converting the result to a string which truncates the metadata that comes with it and only returns the array of results, of which there are none. I am able to access the result directly via

...
const insertResult = await connection.query('INSERT INTO MY_TABLE VALUES(1, \'Name\')');
console.log(insertResult.count);
...

Apologies for the misunderstanding but sometimes talking out the problem with a text editor is helpful :)

Upvotes: 0

Related Questions