Skip to content

even713/mysql-promise

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status

Introduction

this is a fork from mysql-pro and refine it by release connection when transactio done.

this is a wrapper for node-mysql that wraps function calls with promise.you can use generator/yield or async/await with it.

Install

npm install mysql-pro

How to use

init

var Client = require("mysql-promise-transcation");
var client = new Client({
  mysql: {
    host: "127.0.0.1",
    port: 3306,
    database: "db",
    user: "root",
    password: "123456",
  },
});

basic query

client.query("select * from user where id = ?;", [1]).then(
  function (result) {
    // use result
  },
  function (error) {
    // error
  }
);

transaction

// start
client.startTransaction(
  function (result) {
    // execute sql
    client.executeTransaction("select * from user", []).then(
      function (result) {
        // stop
        client.stopTransaction();
      },
      function (error) {}
    );
  },
  function (error) {
    // error
  }
);

generator/yield

function*() {
    yield client.startTransaction();
    yield client.executeTransaction("select * from user;", []);
    yield client.executeTransaction("update user set age = ? where id = ?;", [2, 1]);
    yield client.stopTransaction();
}

async/await

async function() {
    await client.startTransaction();
    await client.executeTransaction("select * from user;", []);
    await client.executeTransaction("update user set age = ? where id = ?;", [2, 1]);
    await client.executeTransaction("update user set name = ? where id = ?;", ['tom', 2]);
    await client.stopTransaction();
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%