@@ -10,7 +10,9 @@ instance with an `oracle` property that is a connection pool instance.
10
10
When the Fastify server is shutdown, this plugin invokes the ` .close() ` method
11
11
on the connection pool.
12
12
13
- ## Example
13
+ ## Examples
14
+
15
+ The plugin provides the basic functionality for creating a connection and executing statements such as
14
16
15
17
``` js
16
18
const fastify = require (' fastify' )()
@@ -51,6 +53,51 @@ fastify.listen(3000, (err) => {
51
53
})
52
54
```
53
55
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
+
54
101
## Options
55
102
56
103
` 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) {
131
178
132
179
If needed ` pool ` instance can be accessed via ` fastify.oracle[.dbname].pool `
133
180
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
-
179
181
## License
180
182
181
183
[ MIT License] ( http://jsumners.mit-license.org/ )
0 commit comments