Skip to content

Commit

Permalink
Merge pull request #16 from softrams/add-transaction-capabilities
Browse files Browse the repository at this point in the history
add transaction methods
  • Loading branch information
mkmurali authored Apr 11, 2024
2 parents f68a78a + cb5c6e9 commit c93f08e
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,30 @@ SSL: {
}
```

#Data Typing Options
## Data Typing Options

This connector gives you a few options for configuring how data is returned from the connector. 'typeCast' defaults to true, and converts
data from the database to its javascript equivalent. For example, it will convert DATETIME SQL objects to a DATE javascript type.
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.

## Working within a transaction
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.
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:
```
someMethod = async () => {
const pool = await dataSource.connect(DATABASE_POOL);
const transactionConnection = await dataSource.transactionConnection(pool);
await transactionConnection.beginTransaction();
try {
await transactionConnection.execute(SOME_QUERY, []);
await transactionConnection.execute(SOME_QUERY, []);
await transactionConnection.execute(SOME_QUERY, []);
await transactionConnection.commitTransaction();
} catch (err) {
await transactionConnection.rollbackTransaction();
throw err;
}
};
```
85 changes: 85 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,88 @@ exports.closePool = async (poolAlias) => {
return false;
}
};

exports.transactionConnection = (pool) => {
return new Promise((resolve, reject) => {
pool.getConnection((err, connection) => {
let query;
if (err) reject(err);
if (connection) {
console.log("MySQL Pool connected with threadId " + connection.threadId);
query = (sql, params) => {
return new Promise((resolve, reject) => {
connection.query(sql, params, (err, result) => {
console.debug("Executing query " + sql);
if (params) {
console.debug(JSON.stringify(params));
}
if (err) reject(err);
resolve(result);
});
});
};
}

const TRANSACTIONS = Object.freeze({
START: 'START TRANSACTION',
COMMIT: 'COMMIT',
ROLLBACK: 'ROLLBACK'
});

const release = () => {
return new Promise((resolve, reject) => {
if (err) reject(err);
if (connection) {
console.log("MySQL pool released the thread " + connection.threadId);
resolve(connection.release());
}
});
};

const beginTransaction = () => {
return new Promise((resolve, reject) => {
if (err) reject(err);
if (connection) {
console.log("MySQL beginning transaction with connection thread: " + connection.threadId);
resolve(connection.query(TRANSACTIONS.START));
}
});
};

const commitTransaction = () => {
return new Promise((resolve, reject) => {
if (err) reject(err);
if (connection) {
console.log("MySQL commiting transaction for connection thread: " + connection.threadId);
resolve(connection.query(TRANSACTIONS.COMMIT));
}
});
};

const rollbackTransaction = () => {
return new Promise((resolve, reject) => {
if (err) reject(err);
if (connection) {
console.log("MySQL rolling back the transaction of connection thread: " + connection.threadId);
resolve(connection.query(TRANSACTIONS.ROLLBACK));
}
});
};

const execute = (sql, params) => {
return new Promise((resolve, reject) => {
connection.query(sql, params, (err, result) => {
console.debug("Executing query " + sql);
if (params) {
console.debug(JSON.stringify(params));
}
if (err) reject(err);
resolve(result);
});
});
}

resolve({ query, execute, release, beginTransaction, commitTransaction, rollbackTransaction});
});
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@softrams/nodejs-mysql-connector",
"version": "0.0.13",
"version": "0.1.0",
"description": "Database connector wrapper to work with MySQL database from nodejs applications",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit c93f08e

Please sign in to comment.