Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

Mô-đun mysql được quảng bá sẽ hoạt động như thế nào với NodeJS?

Nếu một phương thức là một nút "errback" với một đối số - nó sẽ được giải quyết mà không có tham số nào trong then hoặc bị từ chối với err chuyển cho nó. Trong trường hợp quảng bá, bạn có thể bắt nó bằng .error hoặc sử dụng một hàm với Promise.OperationalError .

Đây là một cách tiếp cận đơn giản:

function getConnection(){
    var connection = mysql.createConnection({
      host     : 'localhost',
      user     : 'me',
      password : 'secret'
    });
    return connection.connectAsync().return(connection); // <- note the second return
}

getConnection().then(function(db){
    return db.queryAsync(....);
}).error(function(){
   // could not connect, or query error
});

Nếu điều này là để quản lý kết nối - tôi sẽ sử dụng Promise.using - đây là một mẫu từ API:

var mysql = require("mysql");
// uncomment if necessary
// var Promise = require("bluebird");
// Promise.promisifyAll(mysql);
// Promise.promisifyAll(require("mysql/lib/Connection").prototype);
// Promise.promisifyAll(require("mysql/lib/Pool").prototype);
var pool  = mysql.createPool({
    connectionLimit: 10,
    host: 'example.org',
    user: 'bob',
    password: 'secret'
});

function getSqlConnection() {
    return pool.getConnectionAsync().disposer(function(connection) {
        try {
            connection.release();
        } catch(e) {};
    });
}

module.exports = getSqlConnection;

Điều gì sẽ cho phép bạn làm:

Promise.using(getSqlConnection(), function(conn){
    // handle connection here, return a promise here, when that promise resolves
    // the connection will be automatically returned to the pool.
});



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cách trả lại vị trí của một mục danh sách trong MySQL

  2. NodeJS đã phản hồi Múi giờ MySQL khác khi tôi tìm nạp trực tiếp từ MySQL

  3. 3 cách để tìm hàng có chứa chữ hoa trong MySQL

  4. chia nhỏ từ khóa cho bài php mysql

  5. FROM_UNIXTIME () Ví dụ - MySQL