@tinypudding/mysql-connector

1.1.11 • Public • Published

Discord server NPM version NPM downloads Patreon Ko-Fi

npm installnfo

MySQL-Connector

Script to connect to MySQL databases with Google Cloud Proxy support.

Example

index.js (Server File)

This is the final result inside your server's file.

// Get Module
const mysql = require('./connect');

// Start the MySQL Connection
mysql('server-database-name').then(async db => {

    // Print
    db.query('SELECT * FROM test')

        // Success
        .then(data => {

            // Print
            console.log(data);

            // Complete
            return;

        })

        // Fail
        .catch(err => {

            // Print
            console.error(err);

            // Complete
            return;

        });

    // Complete
    return;      

}).catch(err => {
    console.error(err);
});

// Start the MySQL Connection with multiple databases
mysql(['server-database-name-1', 'server-database-name-2']).then(dbs => {

    // Get Database 1
    const db = dbs['server-database-name-1'];

    // Complete
    return;      

}).catch(err => {
    console.error(err);
});

connect.js (Module)

You will use this module to create a totally easy to use method to create a connection to your database. You just need to insert your database name into the method. Totally simple and easy!

module.exports = function (database = 'main', insertSSL = true) {
    return new Promise(async function (resolve, reject) {

        // Modules
        const mysql = require('@tinypudding/mysql-connector/create');
        const getMySQL = require('./get');

        // Single Database
        if (typeof database === "string") {

            // Get MySQL Connection
            // If you want to use only the normal connection. Change the "firebase" option to "default".
            mysql(require('mysql'), 'firebase', getMySQL(database, insertSSL)).then(resolve).catch(reject);

        }

        // Nope
        else {

            // Prepapre Module
            const sameUser = require('@tinypudding/mysql-connector/sameUser');

            // Prepare Keys
            let key;
            const keys = [];

            // Get Keys
            for (const item in database) {

                if (Number(item) > 0) {
                    keys.push(sameUser(database[item], key));
                } else { key = getMySQL(database[item], insertSSL); keys.push(key); }

            }

            // start Connection
            mysql(require('mysql'), 'firebase', keys).then(resolve).catch(reject);

        }

        // Complete
        return;

    });
};

get.js (Module)

This module will get the json from your database settings.

module.exports = function (database = 'main', insertSSL = true) {

    // Get Key
    const key = require('clone')(require('./keys.json'));

    // Insert Database Value
    key.data.database = database;

    // Insert SSL
    if(insertSSL) {key.data.ssl = require('./getSSL')();}

    // Complete
    return key;

};

getSSL.js (Module)

If you are using SSL. This file will help you to get your SSL data.

module.exports = function (readType) {

    // Prepare FS
    const fs = require('fs');
    const path = require('path');

    // Prepare Items
    const result = {
        ca: fs.readFileSync(path.join(__dirname, '/server-ca.pem'), readType),
        cert: fs.readFileSync(path.join(__dirname, '/client-cert.pem'), readType),
        key: fs.readFileSync(path.join(__dirname, '/client-key.pem'), readType),
        rejectUnauthorized: true
    };

    // Send Result
    return result;

};

keys.json (JSON)

This is the file that will contain all the default connection settings for your database.

{
    "google_cloud": {
        "socketPath": "/cloudsql/{project-name}:{region}:{database-server-id}"
    },
    "default": {
        "host": "public-database-server-ip",
        "port": 3306
    },
    "data": {
        "database": "server-database-name",
        "user": "root",
        "password": "example123"
    }
}

Package Sidebar

Install

npm i @tinypudding/mysql-connector

Weekly Downloads

1

Version

1.1.11

License

MIT

Unpacked Size

18.1 kB

Total Files

11

Last publish

Collaborators

  • jasmindreasond