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

mysqlconnector

v2.0.6

Published

MySQL connector

Downloads

172

Readme

mysql-connector

Build Status Coverage Status David-dm

NodeJs mysql helper. This library will help you to manage connection and operation with your MySQL server. Base on mysql module, this library add helpers to simply you interaction with the data layer of your application

Introduction

To install simply run the npm install command

npm i mysqlconnector

The library offer two main approach of a SQL operation: query and transaction
To connect to the database you have to use the MySqlConnection class

    // const connection = new MySqlConnection('localhost', 'username', 'password');
    const connection = new MySqlConnection({
        hostname: "localhost",
        username: "root",
        password: "",
        port: 3306,
    });  
     
    connection.connectAsync().then(() => { 
        
        connection.queryAsync('SELECT * FROM users').then((results) => {
        
            results.forEach(user => { console.log(user); });
            
            connection.closeAsync().then(() => { console.log('connection closed'); })
          });
     });

The use of Promise simplify the use of the MySqlConnection object

    const connection = new MySqlConnection({
            hostname: "localhost",
            username: "root",
            password: "root"
        });
     
    connection.connectAsync()
        .then(() => { })
        .catch((exception) => {
            console.error('something bad happened', exception)
        });

The same goes for the use of a query

    const connection = new MySqlConnection({
        hostname: "localhost",
        username: "root",
        password: "root"
    });
     
     ...
     
    connection.queryAsync('SELECT * FROM user')
        .then((results) => {
            ...
         })
        .catch((exception) => {
            console.error('something bad happened',exception)
        });

Helpers

To simplify basic queries like create, drop, insert, select ... The library contains some query builder than can be use in a fluent way

Example of select

    const query = Select.Table("users");
    console.log(query.toString());
    // SELECT * FROM users
    
    const select = Select.Properties("username", "email").table("users").where("email = '[email protected]'");
    console.log(select.toString());
    // SELECT username, email FROM users WHERE email = '[email protected]'

Example of update

    
    let user = { username : 'user', email: '[email protected]' };
    
    user.email = '[email protected]';
    
    const update = Update.Table("user").fromModel(user).where("email = '[email protected]");
    console.log(update.toString());
    // UPDATE user SET username = 'user', email = '[email protected]' WHERE email = '[email protected]'

Transaction

The library contains also a helper for transaction. The transaction will be automatically commit if no error occurred. If any thing goes wrong the rollback will be executed. Using TypeScript with async / await it allows you to have a nicer way to make sure all your operation happen in the same transaction

        const mySql = new MySqlConnection({
            hostname: "localhost",
            username: "root",
            password: "root",
            database: "test"
        });
        const dbContext = new DbContext(mySql);
        
        await dbContext.inTransactionAsync(async () => {
            
           await dbContext.executeAsync(Insert.InTo("user").fromModel({
                Email: "[email protected]",
                FirstName: "toto",
                LastName: "toto",
                username: "toto",
            }).toString());
        });

The dbContext can be pass to other function and the inTransactionAsync function can be call again with impacting the first call, it still will be one transaction

        const mySql = new MySqlConnection({
            hostname: "localhost",
            username: "root",
            password: "root",
            database: "test"
        });
        const dbContext = new DbContext(mySql);
        
        
        function async addUserAsync(dbContext, user) {
            
            await dbContext.inTransactionAsync(async (dbContext) => {
                
               await dbContext.executeAsync(Insert.InTo("user").fromModel(user).toString());
               
               await this.addPasswordToUserAsync(dbContext)
            });
        }
        
        function async addPasswordToUserAsync(dbContext, user) {
            
            await dbContext.inTransactionAsync(async (dbContext) => {
            
                // Generation of complex password hash
               await dbContext.executeAsync(Insert.InTo("user").property('password', user.password).toString());
               
            });
        }