CodeFoodPixels / node-promise-mysql

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

Exports mysql js sync method #58

Closed sj82516 closed 7 years ago

sj82516 commented 7 years ago

Hi, Is that possible to expose sync methods of mysql js ? Because I would like to define connection in one js file and export the connection. However I cannot exports connection in async way. Here is my connection.js file

const mysql = require("promise-mysql");
const config = require("../../config/local")
let conn
async function init() {
    conn = await mysql.createPool({
        host: config.model.mysql_psk_events.options.host,
        user: config.model.mysql_psk_events.user,
        password: config.model.mysql_psk_events.passwd,
        database: config.model.mysql_psk_events.database,
        multipleStatements: true,
        connectionLimit: 20,
    })
}
init()
exports = conn

the conn variable is undefined.

in other mysql models file I would like to continue using the promised base sql, such as

exports.findEventByUid = function(uid){
    return conn.query(`
        SELECT _id, uid FROM Event WHERE uid = ?
    `, [uid])
}

any suggestion or solution?

Thanks for the great tool. :)

CodeFoodPixels commented 7 years ago

Just to let you know I've seen this, but not had a chance to respond yet. I'll respond properly as soon as I can.

sj82516 commented 7 years ago

Would you wrap the mysql sync methods then export it ? I would like to try to contribute to the project by PR though I haven't PRed to open source before. Could you tell me about what you plan to do on this problem and the I can try to PR? Is that possible ? :P

CodeFoodPixels commented 7 years ago

It's more a change in how you're using it. I've not tested any of the following, but this should hopefully get the idea across:

const mysql = require("promise-mysql");
const config = require("../../config/local")

function init() {
    return mysql.createPool({
        host: config.model.mysql_psk_events.options.host,
        user: config.model.mysql_psk_events.user,
        password: config.model.mysql_psk_events.passwd,
        database: config.model.mysql_psk_events.database,
        multipleStatements: true,
        connectionLimit: 20,
    })
}
exports = init()

Then when you want to use it, something like:

exports.findEventByUid = function(uid){
    const conn = await connector;
    return conn.query(`
        SELECT _id, uid FROM Event WHERE uid = ?
    `, [uid])
}

In this example connector would be the export from your first file

CodeFoodPixels commented 7 years ago

@sj82516 Can you let me know if that worked so I can close the issue?

sj82516 commented 7 years ago

please close the issue. Thanks

riwu commented 4 years ago

@CodeFoodPixels we would need to do const conn = await connector in every function that imports the connector.

It would be much more convenient if we have the option to import a synchronous version of .createPool().