Skip to content

v5.0.0 NAPI node api

Compare
Choose a tag to compare
@TimelordUK TimelordUK released this 28 Jun 15:14
· 439 commits to master since this release

This release is in pre release stages of testing - it is a massive change

  1. we have moved from nan to napi node api to buld the cpp
  2. added extensive logging that can be switched on to see exactly what driver is doing
  3. now building and testing with git actions.
  4. we only support ubuntu glib 2204 or later, if you run on earlier linux you have to build the driver yourself
  5. as much as possible drop in replacement to previous version < 5 of the driver.
  6. we no longer support anything less than node 20, electron 30
  7. we no longer support win32
  8. we build for windows, linux and mac os latest versions
  9. alpine linux will be a best effort but this is no longer considered in scope for supported builds.
image

This adds some much needed support for doing transactions from a pool with a few helper utilities.
Notable new API methods:

pool.beginTransaction(function (err, description) { /* description.connection.query() */ })

Pulls a connection information out of the pool and gives it to the caller. This connection becomes busy for the duration until either of next two functions are called:

pool.commitTransaction(description, function(err) { /* err logging */ })

Calls IF (@@TRANCOUNT > 0) COMMIT TRANSACTION on the connection in the description and releases it back into the pool allowing it to be used by others.

pool.rollbackTransaction(description, function(err) { /* err logging */ })

Calls IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION on the connection in the description and releases it back into the pool allowing it to be used by others.

Both functions are also available in the promised pool:

let description = await pool.promises.beginTransaction()
try {
  await description.connection.promises.query(`
    SELECT * from my_table WITH (updlock, holdlock)
  `)
  // do some other stuff
  await pool.promises.commitTransaction(description)
} catch (err) {
  await pool.promises.rollbackTransaction(description)
  // log err
}

Lastly, a nice little promise-specific utlity has been added:

pool.promises.transaction(async function (description) {
  /* I'm inside a transaction */
  await description.connection.promises.query(`Do transaction query here`)
})

This calls the callback and wraps it inside of a transaction with auto commit on success and auto rollback on any thrown errors.

description link
next js example, note cant run driver on UI thread. todo-with-nextjs_msnodesqlv8
using in typescript msnodesqlv8_ts_sample
js example typings in IDE msnodesqlv8_yarn_sample
using sequelize msnodesqlv8-sequelize
using mssql msnodesqlv8_mssql_sample
using electron msnodesqlv8-electron
using react msnodesqlv8-react
import sql from 'msnodesqlv8'
import Connection = MsNodeSqlV8.Connection
import ConnectionPromises = MsNodeSqlV8.ConnectionPromises
async function t() {
    const connectionString  = "Driver={ODBC Driver 17 for SQL Server};Server=(localdb)\\node;Database=scratch;Trusted_Connection=yes;"
    const con:Connection = await sql.promises.open(connectString)
    const promises:ConnectionPromises  = con.promises
    const res = await promises.query('select @@servername as server')
    console.log(JSON.stringify(res, null, 4))
    await con.promises.close()
}

t().then(() => {
    console.log('closed')
})
const connectionString  = "Driver={ODBC Driver 17 for SQL Server};Server=(localdb)\\node;Database=scratch;Trusted_Connection=yes;"

const sql = require('msnodesqlv8')

const query = 'SELECT top 2 * FROM syscolumns'

async function runner() {
    console.log(`using connection '${connectionString}' run query '${query}'`)
    const res = await sql.promises.query(connectionString, query)
    console.log(JSON.stringify(res, null, 4))
}

runner().then(() => {
    console.log('done.')
}).catch(e => {
    console.error(e)
})

image