Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added transaction helper #24

Merged
merged 10 commits into from
Oct 4, 2018
Merged

Conversation

cemremengu
Copy link
Contributor

@cemremengu cemremengu commented Sep 20, 2018

Addresses #5 and #26

Based on this example

@cemremengu cemremengu force-pushed the transaction-helper branch 7 times, most recently from ae82f20 to 5ee6545 Compare September 20, 2018 23:54
index.js Outdated
@@ -3,6 +3,39 @@
const fp = require('fastify-plugin')
var pg = require('pg')

function transactionHelper (query, values) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you support a double callback and promise interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean something like:

function transactionHelper (query, values, cb = null)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

index.js Outdated
client.query('COMMIT', (err) => {
done()
if (err) {
reject(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should return after rejecting.

index.js Outdated
client.query('BEGIN', (err) => {
if (shouldAbort(err)) reject(err)
client.query(query, values, (err, res) => {
if (shouldAbort(err)) reject(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should return after rjecting

index.js Show resolved Hide resolved
index.js Outdated
@@ -3,6 +3,38 @@
const fp = require('fastify-plugin')
var pg = require('pg')

function transactionHelper (query, values, cb = null) {
return new Promise((resolve, reject) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should not return a promise if there is a callback. Just use the callback version and if there is no callback return a promise.

if (!cb) {
  return new Promise((resolve, reject) => {
    this.transact(query, values, function (err, res) {
       if (err) { return reject(err) }
      return resolve(res)
    })
  })
}

Copy link
Contributor Author

@cemremengu cemremengu Sep 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please have another look?

Sorry, I lack experience with promises and callbacks. Another issue is that I am having trouble building the project since I am on windows so just using here to run the tests 😄 It's a walk in the dark.

Apologies for spamming

index.js Outdated Show resolved Hide resolved
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some docs in the README as well?

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

index.js Outdated
client.query('BEGIN', (err) => {
if (shouldAbort(err)) return cb(err)

fn(client).then(res => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you pass a callback as well and support the dual promise/callback pattern?

Copy link
Contributor Author

@cemremengu cemremengu Sep 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry I thought over this but am still confused. What will that callback look like? How do I know the user wants a callback?Could you please give me some pointers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cemremengu
Copy link
Contributor Author

I know the last change is wrong but I am close to figuring it out 😄

@mcollina
Copy link
Member

I know the last change is wrong but I am close to figuring it out 😄

Why you say so? Looks ok to me.

@cemremengu
Copy link
Contributor Author

I made it but then the usage didn't make sense to me. If I am understanding correct, will it look like:

fastify.pg.transact((client, commit) => { 

const id = client.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['brianc']);
commit(null, id)

I am too used to async/await usage so I am having a hard time getting my head around this 😄

@mcollina
Copy link
Member

It should be:

fastify.pg.transact((client, commit) => { 
  client.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['brianc'], commit);
})

or

fastify.pg.transact((client, commit) => { 
  client.query('INSERT INTO users(username) VALUES($1) RETURNING id', ['brianc'], (err, id) => {
    commit(err, id)
  });
})

@cemremengu
Copy link
Contributor Author

Ahh ok now it makes sense! I was thinking in terms of async usage mixed together with callback usage 😄

Thank you for taking your time on this and bearing with me. I will add a test case for that as well

@mcollina
Copy link
Member

Thank you for working on this. I wanted to add this for a long time!

@cemremengu cemremengu force-pushed the transaction-helper branch 2 times, most recently from 3d4f833 to c6e5516 Compare September 24, 2018 23:01
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of nits. This is good work!

README.md Outdated Show resolved Hide resolved
test.js Outdated Show resolved Hide resolved
@temsa
Copy link
Contributor

temsa commented Oct 4, 2018

Should this be merged now @mcollina ? :)

@mcollina mcollina merged commit d2158b7 into fastify:master Oct 4, 2018
@mcollina
Copy link
Member

mcollina commented Oct 4, 2018

Yes!

@cemremengu cemremengu deleted the transaction-helper branch October 4, 2018 09:40
@cemremengu
Copy link
Contributor Author

This concludes #5 and #26 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants