CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 64 forks source link

Ho to do synchronous operations using promise-mysql, like task in pg-promise? #48

Closed lakshmipriyamukundan closed 7 years ago

CodeFoodPixels commented 7 years ago

What are you trying to do?

lakshmipriyamukundan commented 7 years ago

I am new to mysql. My previous work was in postgres. To do my project in node , I had used a library 'pg-promise'. There I could found options like 'task' to do series operations.

For an example, Operations:

  1. I have to insert some data to a table 'Employee'.
  2. And I need the Primary key of this added row of 'Employee' to save as Foreign Key in next table ,'Employee Address'.

These are series operations actually, how do I implement this with promise in promise-mysql?

Can you please explain it with some example?

CodeFoodPixels commented 7 years ago

Assuming the primary key is an autoincrementing integer:

var mysql = require('promise-mysql');
var connection;

mysql.createConnection({
    host: 'localhost',
    user: 'user',
    password: 'password',
    database: 'database'
}).then(function(conn){
    connection = conn;

    return connection.query('insert into Employee...');
}).then(function(results){
var id = results.insertId;
    return connection.query('insert into EmployeeAddress...');
});
lakshmipriyamukundan commented 7 years ago

Thanks for the answer. :star:

vitaly-t commented 7 years ago

The interesting part is that two inserts like this must be executed inside a transaction, in order to keep the data integrity. Perhaps this is what the author meant to ask about, when referencing to tasks/transactions support that pg-promise does :wink:

CodeFoodPixels commented 7 years ago

I'm not here to teach people how to develop, I'm giving up my free time to maintain this library.

On Tue, 9 May 2017 at 14:58 Vitaly Tomilov notifications@github.com wrote:

Perhaps the interesting part is that two inserts like this must be executed inside a transaction. Perhaps this is what the author meant to ask about... 😉

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/lukeb-uk/node-promise-mysql/issues/48#issuecomment-300173163, or mute the thread https://github.com/notifications/unsubscribe-auth/AEjy1C1e1IHN7NTlmXg9UgSmsxKC_1Laks5r4HEhgaJpZM4NR0ox .