Skip to content

Commit c93f08e

Browse files
authored
Merge pull request #16 from softrams/add-transaction-capabilities
add transaction methods
2 parents f68a78a + cb5c6e9 commit c93f08e

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,30 @@ SSL: {
2626
}
2727
```
2828

29-
#Data Typing Options
29+
## Data Typing Options
3030

3131
This connector gives you a few options for configuring how data is returned from the connector. 'typeCast' defaults to true, and converts
3232
data from the database to its javascript equivalent. For example, it will convert DATETIME SQL objects to a DATE javascript type.
3333
You can also set 'dateStrings' which defaults to false. If you set it to true it will override typeCast and force date returns to be a string instead of a DATE type.
34+
35+
## Working within a transaction
36+
As of version 0.1.0 you can utilize sql transactions. Simply call the transactionConnection method to get a transaction connection and then begin the transaction.
37+
Then, write as many queries as you want, and when you are done, you can commit the transaction and all of your queries will be saved to the database or you can roll back the transaction and nothing done while inside that transaction will be saved. Some pseudo-code for how you might do that is below:
38+
```
39+
someMethod = async () => {
40+
const pool = await dataSource.connect(DATABASE_POOL);
41+
const transactionConnection = await dataSource.transactionConnection(pool);
42+
await transactionConnection.beginTransaction();
43+
44+
try {
45+
await transactionConnection.execute(SOME_QUERY, []);
46+
await transactionConnection.execute(SOME_QUERY, []);
47+
await transactionConnection.execute(SOME_QUERY, []);
48+
49+
await transactionConnection.commitTransaction();
50+
} catch (err) {
51+
await transactionConnection.rollbackTransaction();
52+
throw err;
53+
}
54+
};
55+
```

index.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,88 @@ exports.closePool = async (poolAlias) => {
156156
return false;
157157
}
158158
};
159+
160+
exports.transactionConnection = (pool) => {
161+
return new Promise((resolve, reject) => {
162+
pool.getConnection((err, connection) => {
163+
let query;
164+
if (err) reject(err);
165+
if (connection) {
166+
console.log("MySQL Pool connected with threadId " + connection.threadId);
167+
query = (sql, params) => {
168+
return new Promise((resolve, reject) => {
169+
connection.query(sql, params, (err, result) => {
170+
console.debug("Executing query " + sql);
171+
if (params) {
172+
console.debug(JSON.stringify(params));
173+
}
174+
if (err) reject(err);
175+
resolve(result);
176+
});
177+
});
178+
};
179+
}
180+
181+
const TRANSACTIONS = Object.freeze({
182+
START: 'START TRANSACTION',
183+
COMMIT: 'COMMIT',
184+
ROLLBACK: 'ROLLBACK'
185+
});
186+
187+
const release = () => {
188+
return new Promise((resolve, reject) => {
189+
if (err) reject(err);
190+
if (connection) {
191+
console.log("MySQL pool released the thread " + connection.threadId);
192+
resolve(connection.release());
193+
}
194+
});
195+
};
196+
197+
const beginTransaction = () => {
198+
return new Promise((resolve, reject) => {
199+
if (err) reject(err);
200+
if (connection) {
201+
console.log("MySQL beginning transaction with connection thread: " + connection.threadId);
202+
resolve(connection.query(TRANSACTIONS.START));
203+
}
204+
});
205+
};
206+
207+
const commitTransaction = () => {
208+
return new Promise((resolve, reject) => {
209+
if (err) reject(err);
210+
if (connection) {
211+
console.log("MySQL commiting transaction for connection thread: " + connection.threadId);
212+
resolve(connection.query(TRANSACTIONS.COMMIT));
213+
}
214+
});
215+
};
216+
217+
const rollbackTransaction = () => {
218+
return new Promise((resolve, reject) => {
219+
if (err) reject(err);
220+
if (connection) {
221+
console.log("MySQL rolling back the transaction of connection thread: " + connection.threadId);
222+
resolve(connection.query(TRANSACTIONS.ROLLBACK));
223+
}
224+
});
225+
};
226+
227+
const execute = (sql, params) => {
228+
return new Promise((resolve, reject) => {
229+
connection.query(sql, params, (err, result) => {
230+
console.debug("Executing query " + sql);
231+
if (params) {
232+
console.debug(JSON.stringify(params));
233+
}
234+
if (err) reject(err);
235+
resolve(result);
236+
});
237+
});
238+
}
239+
240+
resolve({ query, execute, release, beginTransaction, commitTransaction, rollbackTransaction});
241+
});
242+
});
243+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@softrams/nodejs-mysql-connector",
3-
"version": "0.0.13",
3+
"version": "0.1.0",
44
"description": "Database connector wrapper to work with MySQL database from nodejs applications",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)