Node.js,MariaDB : Module for Transaction

1.目的
・トランザクションの記述でのコールバックのネストをシンプルにする

2.code

"use script";

const fs = require('fs');
const mysql = require('mysql');

const configStr = fs.readFileSync('config.json').toString();
const config = JSON.parse(configStr);

module.exports = {
    beginTransaction : _beginTransaction,
    commit          : _commit,
    connection      : _connection,
    delete          : _delete,
    insert          : _insert,
    query           : _query,
    rollback        : _rollback,
    selectOne       : _selectOne,
    selectAll       : _selectAll,
    update          : _update
}

function _beginTransaction(connection) {
    return new Promise((resolve, reject) => {
        connection.beginTransaction((err) => {
            if (err) {
                return reject(err);
            }
            resolve();
        });
    });
}

function _commit (connection) {
    return new Promise((resolve, reject) => {
        connection.commit((err) => {
            if (err) {
                return reject(err);
            }
            resolve();
        });
    });
}

function _connection () {
    return mysql.createConnection(config.DATABASE);
}

function _delete (connection, sql, args) {
    return new Promise ((resolve, reject) => {
        connection.query(sql, args, (err, result) => {
            if(err) {
                return reject (err);
            }
            resolve(result);
        });
    });
}

function _insert (connection, sql, args) {
    return new Promise ((resolve, reject) => {
        connection.query(sql, args, (err, result) => {
            if(err) {
                return reject (err);
            }
            resolve(result);
        });
    });
}

function _query (connection, statement, params) {
    return new Promise((resolve, reject) => {
        connection.query(statement, params, (err, results, fields) => {
            if (err) {
                return reject(err);
            }
            resolve([results, fields]);
        });
    });
}

function _rollback (connection, err) {
    return new Promise((resolve, reject) => {
        connection.rollback(() => {
            if (err) {
                return reject(err);
            }
            resolve();
        });
    });
}

function _selectOne (connection, sql, args) {
    return new Promise ((resolve, reject) => {
        connection.query(sql, args, (err, row) => {
            if(err) {
                return reject (err);
            }
            resolve(row[0]);
        });
    });
}

function _selectAll (connection, sql, args) {
    return new Promise( (resolve, reject) => {
        connection.query(sql, args, (err, rows) => {
            if(err) {
                return reject (err);
            }
            resolve(rows);
        });
    });
}

function _update (connection, sql, args) {
    return new Promise ((resolve, reject) => {
        connection.query(sql, args, (err, result) => {
            if(err) {
                return reject (err);
            }
            resolve(result);
        });
   });
}