npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tinypudding/mysql-connector

v1.1.11

Published

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

Downloads

13

Readme

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"
    }
}