diff --git a/README.md b/README.md index a5f4ce4..6873abf 100644 --- a/README.md +++ b/README.md @@ -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; + } + }; +``` diff --git a/index.js b/index.js index 2c64b0b..ec40c66 100644 --- a/index.js +++ b/index.js @@ -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}); + }); + }); +}; \ No newline at end of file diff --git a/package.json b/package.json index 6bee5e1..0de92d4 100644 --- a/package.json +++ b/package.json @@ -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": {