Get the App
SLTechnology News&Howtos  ›  Database  › 

Node Mysql transaction encapsulation

Shulou Source: shulou.com Published: 2022-06-01 11:41:06 11月27日 Update

Node callback function makes database transactions do not seem to be as simple as java and php. Find some transaction packages on the Internet that are not as simple as you expected, so let's package one yourself.

The general idea of encapsulation is very simple: the function accepts a transaction function, and the logic of how to handle the transaction is determined by this function, rather than assembling multiple preprocessing SQL and parameters as the transaction encapsulated on the web. The transaction function requires that Promise be returned. In this way, we can use the Promise to determine whether to commit or roll back the transaction.

According to the above idea, the implementation code:

Let mysql = require ('mysql') let config = require ('.. / config') let pool = mysql.createPool (config.database) / / config.databa database configuration let trans = (tran) = > {return new Promise ((resolve, reject) = > {/ / returns the interface pool.getConnection ((err) that provides the success and failure of the transaction by promise Conn) = > {if (err) {reject (err)} else {conn.beginTransaction ((err) = > {/ / start transaction if (err) {conn.release () reject (err)) } else {let promise = tran (conn) / / call transaction function promise.then (result = > {conn.commit (err = > {/ / transaction function resolve) commit transaction if (err) { Reject (err)} else {resolve (result)})}) .catch (err = > { Conn.rollback (() = > {/ / transaction function reject rolls back the transaction conn.release () reject (err)}) })})}

The transaction function accepts the database connection object conn, where I encapsulate the database execution statement:

Trans.query = (conn, sql, params) = > {return new Promise ((resolve, reject) = > {conn.query (sql, params, (err, result) = > {if (err) {reject (err)} else {resolve (result)}

After the above transaction is encapsulated, the application is the same as the normal Promise:

Trans ((conn) = > {return trans.query (conn,db_ user.register.user, [username, password, new Date (). GetTime ()]) .then (result = > {return trans.query (conn,db_user.login) [username])}) .then (result = > {return trans.query (conn,db_user.register.profile, [result [0] .id Nickname])}) .then (result= > {console.log (transaction commit successful)}) .catch (err = > {console.log (transaction commit failed)})

The above case is a piece of code I intercepted from the project, from which it is obvious that the code can easily encapsulate its own logic and execute transaction processes with different paths.

Tags: Transaction processing function encapsulation data database code success idea or logic different same parameter can pass multiple object interface way case Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information OPPO Reno MariaDB Apple NVidia