Skip to content

Commit 467853a

Browse files
author
Toni Sharpe
committed
wip
1 parent ea8902b commit 467853a

File tree

2 files changed

+118
-11
lines changed

2 files changed

+118
-11
lines changed

index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,24 @@ function fastifyPostgres (fastify, options, next) {
107107
const useTransaction = routeOptions.options && routeOptions.options.useTransaction
108108

109109
if (useTransaction) {
110-
console.log('>>>>>>>>>>>>>>>>>>>>>> in useTransaction')
111110
fastify.addHook('preHandler', async (req, reply) => {
112-
console.log('>>>>>>>>>>>>>>>>>>>>>> in preHandler')
113-
await fastify.pg.transact(async (client, commit) => {
114-
console.log('>>>>>>>>>>>>>>>>>>>>>> in preHandler transact')
115-
req.pg = client
116-
await req.pg.query('BEGIN')
111+
await fastify.pg.transact(async (client, commit, done) => {
112+
try {
113+
req.pg = client
114+
await req.pg.query('BEGIN')
115+
} catch (e) {
116+
117+
}
117118
})
118119
})
119120
fastify.addHook('onSend', async (req, reply) => {
120-
console.log('>>>>>>>>>>>>>>>>>>>>>> in onSend')
121-
await fastify.pg.transact(async (client, commit) => {
122-
console.log('>>>>>>>>>>>>>>>>>>>>>> in onSend transact')
123-
// TODO only call this if transaction is complete
124-
await req.pg.query('COMMIT')
121+
await fastify.pg.transact(async (client, commit, done) => {
122+
try {
123+
// TODO only call this if transaction is complete
124+
await req.pg.query('COMMIT')
125+
} catch (e) {
126+
await req.pg.query('ROLLBACK', done)
127+
}
125128
})
126129
})
127130
}

test/req-initialization.test.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,107 @@ test('fastify request should have the correct decoration at the custom namespace
7272
t.ok(fastify.hasRequestDecorator('pg'))
7373
})
7474
})
75+
76+
test('when useTransaction is set to true - ', t => {
77+
test('and the transaction is successful - ', t => {
78+
test('fastify request should start a transaction', t => {
79+
t.plan(2)
80+
81+
const fastify = Fastify()
82+
t.teardown(() => fastify.close())
83+
84+
fastify.register(fastifyPostgres, {
85+
connectionString: process.env.DATABASE_TEST_URL || 'postgres://postgres:postgres@localhost/postgres'
86+
})
87+
88+
fastify.route({
89+
method: 'GET',
90+
url: '/users',
91+
options: { useTransaction: true },
92+
handler: (req, reply) => {
93+
reply.send('response!')
94+
t.ok(req.pg)
95+
96+
const result = req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', [
97+
'root-with-callback'
98+
])
99+
100+
const userId = result.rows[0].id
101+
102+
req.pg
103+
.query('SELECT * FROM users WHERE id = $1', [userId])
104+
.then((result) => {
105+
t.is(result.rows[0].username, 'root-with-callback')
106+
})
107+
.catch((err) => {
108+
t.fail(err)
109+
})
110+
}
111+
})
112+
113+
fastify.inject({
114+
method: 'GET',
115+
url: '/users'
116+
}, (err, res) => {
117+
t.error(err)
118+
})
119+
})
120+
121+
test('queries should run inside the handler', t => {
122+
t.plan(2)
123+
124+
const fastify = Fastify()
125+
t.teardown(() => fastify.close())
126+
127+
fastify.register(fastifyPostgres, {
128+
connectionString: process.env.DATABASE_TEST_URL || 'postgres://postgres:postgres@localhost/postgres'
129+
})
130+
131+
fastify.route({
132+
method: 'GET',
133+
url: '/users',
134+
options: { useTransaction: true },
135+
handler: (req, reply) => {
136+
reply.send('response!')
137+
t.ok(req.pg)
138+
139+
const result = req.pg.query('INSERT INTO users(username) VALUES($1) RETURNING id', [
140+
'root-with-callback'
141+
])
142+
143+
const userId = result.rows[0].id
144+
145+
req.pg
146+
.query('SELECT * FROM users WHERE id = $1', [userId])
147+
.then((result) => {
148+
t.is(result.rows[0].username, 'root-with-callback')
149+
})
150+
.catch((err) => {
151+
t.fail(err)
152+
})
153+
}
154+
})
155+
156+
fastify.inject({
157+
method: 'GET',
158+
url: '/users'
159+
}, (err, res) => {
160+
t.error(err)
161+
})
162+
})
163+
164+
test('fastify request should commit the transaction')
165+
166+
t.end()
167+
})
168+
169+
test('and the transaction fails - ', t => {
170+
test('throws the correct error')
171+
172+
test('rolls back the transaction')
173+
174+
t.end()
175+
})
176+
177+
t.end()
178+
})

0 commit comments

Comments
 (0)