jeremydaly / serverless-mysql

A module for managing MySQL connections at SERVERLESS scale
MIT License
1.2k stars 82 forks source link

transactions arguments showing undefined #152

Closed matt-wiz closed 1 year ago

matt-wiz commented 1 year ago

I am trying to run a transaction but r is always showing undefined. Whether it's a SELECT or INSERT, I can't access things like r.affectedRows or r.insertId. Is it because I am using the mysql2 library?

let results = await mysql.transaction() .query('INSERT INTO table (x) VALUES(?)', [1]) .query((r) => { console.log(r.insertId)// always shows undefined but row is added. }) .commit() // execute the queries

or

let results = await mysql .transaction() .query('SELECT * FROM table WHERE name = ?', ['serverless']) .query((r) => { console.log(r.affectedRows) }) .commit() // execute the queries

naorpeled commented 1 year ago

Hey @matt-wiz, sorry for the big delay.

The following should work.

let results = await mysql.transaction()
.query('INSERT INTO table (x) VALUES(?)', [1])
// always shows undefined but row is added.
.query((_, r) => {
   console.log(r.insertId)
  })
// execute the queries
.commit() 

the current implementation of query in transactions lets you specify a function that its first argument is an object with a rollback function and the second argument contains the result of the previous query.

Let me know if you need anything else.

naorpeled commented 1 year ago

Hey, I'm closing this issue as completed. Feel free to ping me if you need anything else.