Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ fastify.register(require('fastify-postgres'), {
})

fastify.post('/user/:username', (req, reply) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

If we make this async, we can directly return return client.query right?

Copy link
Member Author

Choose a reason for hiding this comment

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

There is no need to use async at all because Fastify understand promises. However that won't be the normal case for applications.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes, thank you for the info!

fastify.pg.transact(async client => {
try {
const id = await client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username])
reply.send(id)
} catch (err) {
reply.send(err)
}
// will return a promise, fastify will send the result automatically
return fastify.pg.transact(async client => {
// will resolve to an id, or reject with an error
const id = await client.query('INSERT INTO users(username) VALUES($1) RETURNING id', [req.params.username])

// potentially do something with id

return id
})
})

Expand Down