serverless-mysql icon indicating copy to clipboard operation
serverless-mysql copied to clipboard

Add: transaction's end function

Open code-xhyun opened this issue 4 years ago • 9 comments
trafficstars

const transaction = () => {

    let queries = [] // keep track of queries
    let rollback = () => {} // default rollback event

    return {
      query: function(...args) {
        if (typeof args[0] === 'function') {
          queries.push(args[0])
        } else {
          queries.push(() => [...args])
        }
        return this
      },
      rollback: function(fn) {
        if (typeof fn === 'function') { rollback = fn }
        return this
      },
      commit: async function() { return await commit(queries,rollback) },
      end: function(){ return end() } //  <-- i want add this!!!!!!! 
    }
  }

i want add this function like use this

let results = await mysql.transaction()
  .query('INSERT INTO table (x) VALUES(?)', [1])
  .query('UPDATE table SET x = 1')
  .rollback(e => { /* do something with the error */ }) 
  .commit() 
  .end() // <-- THIS

code-xhyun avatar May 06 '21 12:05 code-xhyun

Shouldn't there be a catch in there somewhere?

What is wrong with this way? ( snippet from #85 )

const transaction = await db.transaction()

try {
  transaction.query(() => ["INSERT INTO items (created_at) VALUES(?)", [new Date()]])
  transaction.commit()
catch (e) {
  transaction.rollback()
}

transaction.end()

trasherdk avatar May 06 '21 23:05 trasherdk

@trasherdk

If you want to be like you, We have to write it like this

const transaction = await db.transaction()

try {
  transaction.query(() => ["INSERT INTO items (created_at) VALUES(?)", [new Date()]])
  transaction.commit()
catch (e) {
  transaction.rollback()
}

/*transaction.end()*/ //<-- we can't wirte this  transaction.end is not a function
db.end() //<-- We have to write it like this

so we need to add end function in transaction function

code-xhyun avatar May 07 '21 01:05 code-xhyun

Okay, still... Should your rollback() not be wrapped in a catch() ?

trasherdk avatar May 07 '21 09:05 trasherdk

i think you don't need rollback wrapped in a catch() This will receive the error object, allowing you to add additional logging or perform some other action

code-xhyun avatar May 07 '21 09:05 code-xhyun

Wut? I thought rollback() actually did a rollback. Isn't that the hole purpose of that command?

trasherdk avatar May 07 '21 09:05 trasherdk

I don't understand what you're trying to say.

The reason I open this issue is because "DBpool"disconnect smoothly with end()

code-xhyun avatar May 07 '21 10:05 code-xhyun

In your example snippet above

let results = await mysql.transaction()
  .query('INSERT INTO table (x) VALUES(?)', [1])
  .query('UPDATE table SET x = 1')
  .rollback(e => { /* do something with the error */ }) 
  .commit() 
  .end()

you have a rollback() before a commit(). So, if rollback() is actually rolling back data, what's left for commit() to do? That was what I was wondering about.

trasherdk avatar May 07 '21 14:05 trasherdk

It is just an example In the example, the focus is on the end function.

also Don't you need a commit() to run query() and rollback() on transaction?

code-xhyun avatar May 07 '21 16:05 code-xhyun

Wut? I thought rollback() actually did a rollback. Isn't that the hole purpose of that command?

@trasherdk I was confused too - turns out the rollback method doesn't trigger a rollback, it's just a callback helper which gets called if your transaction rolls back.

dan-kwiat avatar Feb 24 '22 22:02 dan-kwiat

Hey @trasherdk, thanks for opening this issue!

I personally don't expect a transaction to have an end command, it's a part of the DB connection manager.

db.end() should do the trick like you mentioned here.

I'll be closing this issue because I don't see a reason for this to be implemented. Feel free to ping me if you need any further help.

naorpeled avatar Jun 06 '23 22:06 naorpeled