question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exports mysql js sync method

See original GitHub issue

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. 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
CodeFoodPixelscommented, Jul 27, 2017

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

1reaction
CodeFoodPixelscommented, Jul 26, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js Synchronous queries with MySQL - Stack Overflow
I know I am late to this party but I feel I can help people like me that needed a way to use...
Read more >
How to Run Synchronous Queries using sync-sql Module in ...
Now open MySQL and create a database for example 'demo'. ... So this is how you can run synchronized SQL queries in node...
Read more >
Node.js, MySQL and async/await - codeburst
It means that you have to wait for the results of a query, but while waiting, your program will continue to execute. For...
Read more >
MySQL Connection With Node.js Using Sequelize and Express
exports = db; The user should not forget to summon the sync() method in the server.js. const app = express(); app.use(....); const db...
Read more >
Nodejs tutorial: using async await with module.exports
Here's a quick standalone tutorial covering something that caused me a great deal of difficulty yesterday.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found