Skip to content

Commit ed01350

Browse files
authored
Revise readme
1 parent c8abffd commit ed01350

File tree

1 file changed

+48
-46
lines changed

1 file changed

+48
-46
lines changed

README.md

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ instance with an `oracle` property that is a connection pool instance.
1010
When the Fastify server is shutdown, this plugin invokes the `.close()` method
1111
on the connection pool.
1212

13-
## Example
13+
## Examples
14+
15+
The plugin provides the basic functionality for creating a connection and executing statements such as
1416

1517
```js
1618
const fastify = require('fastify')()
@@ -51,6 +53,51 @@ fastify.listen(3000, (err) => {
5153
})
5254
```
5355

56+
The `transact` feature can be used for convenience to avoid the repetetive task of setting up a connection, committing, releasing etc. as follows:
57+
58+
```js
59+
const fastify = require('fastify')
60+
61+
fastify.register(require('fastify-oracle'), {
62+
pool: {
63+
user: 'travis',
64+
password: 'travis',
65+
connectString: 'localhost/xe'
66+
}
67+
})
68+
69+
fastify.post('/user/:username', (req, reply) => {
70+
// will return a promise, fastify will send the result automatically
71+
return fastify.oracle.transact(async conn => {
72+
// will resolve to commit, or rollback with an error
73+
return conn.execute(`INSERT INTO USERS (NAME) VALUES('JIMMY')`)
74+
})
75+
})
76+
77+
/* or with a transact callback
78+
79+
fastify.oracle.transact(conn => {
80+
return conn.execute('SELECT * FROM DUAL')
81+
},
82+
function onResult (err, result) {
83+
reply.send(err || result)
84+
}
85+
})
86+
87+
*/
88+
89+
/* or with a commit callback
90+
91+
fastify.oracle.transact((conn, commit) => {
92+
conn.execute('SELECT * FROM DUAL', (err, res) => {
93+
commit(err, res)
94+
});
95+
})
96+
97+
*/
98+
99+
```
100+
54101
## Options
55102

56103
`fastify-oracle` requires an options object with at least one of the following
@@ -131,51 +178,6 @@ fastify.get('/db_data', async function (req, reply) {
131178

132179
If needed `pool` instance can be accessed via `fastify.oracle[.dbname].pool`
133180

134-
## Use of `transact`
135-
136-
```js
137-
const fastify = require('fastify')
138-
139-
fastify.register(require('fastify-oracle'), {
140-
pool: {
141-
user: 'travis',
142-
password: 'travis',
143-
connectString: 'localhost/xe'
144-
}
145-
})
146-
147-
fastify.post('/user/:username', (req, reply) => {
148-
// will return a promise, fastify will send the result automatically
149-
return fastify.oracle.transact(async conn => {
150-
// will resolve to commit, or reject with an error
151-
return conn.execute(`INSERT INTO USERS (NAME) VALUES('JIMMY')`)
152-
})
153-
})
154-
155-
/* or with a transact callback
156-
157-
fastify.oracle.transact(conn => {
158-
return conn.execute('SELECT * FROM DUAL')
159-
},
160-
function onResult (err, result) {
161-
reply.send(err || result)
162-
}
163-
})
164-
165-
*/
166-
167-
/* or with a commit callback
168-
169-
fastify.oracle.transact((conn, commit) => {
170-
conn.execute('SELECT * FROM DUAL', (err, res) => {
171-
commit(err, res)
172-
});
173-
})
174-
175-
*/
176-
177-
```
178-
179181
## License
180182

181183
[MIT License](http://jsumners.mit-license.org/)

0 commit comments

Comments
 (0)