O
O
Oleg Zhvankov2015-06-23 13:10:50
MySQL
Oleg Zhvankov, 2015-06-23 13:10:50

How to convert mysql to pg construct in node.js?

There is a code:

var mysql = require('mysql'),
    cfg = require('./db-config');

function MySQL(config) {

    this.connection = null;

    this.connect = function(){
        this.connection = mysql.createConnection({
            host: config.hostname,
            port: config.port,
            user: config.user,
            password: config.password,
            database: config.db
        });

        this.connection.connect(function(err) {
            if(err){
                console.error('Connection had errors: ', err.code);
                console.error('Connection params used: hostname = ' +  config.hostname + ', username = ' + config.user + ', db = '+  config.db );
                process.exit(1);
            }
        });

        //add alias for debug
        this.connection.debug = this.debug;

        return this.connection;
    };

    this.disconnect = function() {
        this.connection.end();
    };

    this.debug = function(err, callback) {
        // Generate SOFT error, instead of throwing hard error.
        // We send messages with debug info only if in development mode

        if(global.App.env === 'development') {
            callback({
                message: {
                    text: 'Database error',
                    debug: err
                }
            });
        }else{
            callback({
                message: {
                    text: 'Unknown error',
                    debug: null
                }
            });
        }
    }
}

global.App.database = new MySQL(cfg);

I ask for help in translating this piece into pg
, it is the function that interests me,
thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-06-23
@ozhvankov

Format your code
Use an ORM
Assignment, not a question
https://www.npmjs.com/package/pg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question