简体   繁体   English

我如何将mysql-promise导出为节点Js中的模块?

[英]how can i export the mysql-promise as a module in node Js?

Here I have a simple config file for a postgres db: 在这里,我有一个用于postgres db的简单配置文件:

postresql.config.js postresql.config.js

const pg = require('pg-promise')()

const connectionString = {
    host: 'localhost',
    port: 8080,
    database: 'test',
    user: 'test',
    password: 'test123678!'
}

let db = pg(connectionString);

module.exports = {
    db
}

This file is used in the DataAccess file to do some query, like this: 该文件在DataAccess文件中用于执行一些查询,如下所示:

let db = require('../configs/postgresql.config').db;

let getAll = function() {
    return db.manyOrNone('my query')
        .then(function (res) {
            console.log(res);
            return res;
        });
};

Using the pg-promise module is very fast and easy. 使用pg-promise模块非常快速和容易。
But i get in trouble when i try to do the samething with a mysql database. 但是,当我尝试对mysql数据库执行相同操作时会遇到麻烦。
How can I achieve the same result with the promise-mysql module? 如何使用promise-mysql模块达到相同的结果?

Use something universal, pg-promise is exclusive for PostgreSQL database. 使用通用的东西,pg-promise是PostgreSQL数据库专有的。 There is this awesome called bluebird , which can be integrated into any instance of a function. 有一个很棒的叫做bluebird,可以集成到函数的任何实例中。 http://bluebirdjs.com/docs/getting-started.html http://bluebirdjs.com/docs/getting-started.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM