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

mysql-js-wrapper

v1.0.10

Published

Javascript Database wrapper for MySql

Downloads

16

Readme

Bigg MySql DB Wrapper

A fully featured database wrapper for MySql, based on the wrapper included in Codeigniter.

Using this database wrapper with Node frameworks enables data objects to be passed and the Sql queries automatically constructed based on the key:value pairs, meaning less time defining what data should be inserted, updated or deleted.

Installation

npm install mysql-js-wrapper

Prerequisites

Config File

Database connection settings should be held in a config file and then imported to your code.

 // dbConfig.js
 
 export const connections = {
 
 connection1:
     {
         host: 'Localhost',
         user: 'local_user1',
         password: 'my_password1',
         port: 3306,
         database: 'my_db1'
     },
 connection2:
     {
         host: 'Localhost',
         user: 'local_user2',
         password: 'my_password2',
         port: 3306,
         database: 'my_db2'
     },
 };
Importing
import sqlBuilder from 'mysql-js-wrapper'
import { connections } from '../config/dbConfig.js' 
const db = new sqlBuilder();

Useage

Selecting records
db.conn(connections.connection1);						
db.where({field1: 'value1', field2: 'value2'});									
db.limit(1);											
db.orderBy({createdDate: 'ASC'});						
db.get('my_table').then((result) => {					

	// Do something with the result

}).catch((error) => {

    // Do something with the error
});
Updating records
const dataObject = JSON.parse(event.body); 				

db.conn(connections.connection1); 						
db.data(dataObject); 									
db.where({id: 2}); 										
db.update('my_table').then((result) => { 				

	// Do something with the result

}).catch((error) => {

    // Do something with the error
});
Inserting records
const dataObject = JSON.parse(event.body); 				

db.conn(connections.connection1); 						
db.data(dataObject); 									
db.insert('my_table').then((result) => { 				

	// Do something with the result

}).catch((error) => {

    // Do something with the error
});
Deleting records
db.conn(connections.connection1); 						
db.where({id:, 3});										
db.delete('my_table').then((result) => { 				
        
   // Do something with the result

}).catch((error) => {

    // Do something with the error
});

Functions List

#####conn(MySQLConnection conn) Set the connection to use from your config file

#####data(Object key/value) Set a data object containing key/value pairs to match table fields e.g,

db.data({
    first_name:   'joe',
    last_name:    'blogs'
})

#####where(Object key/value) Set a where statement. Can be used multiple times e.g.

db.where({first_name:, 'joe', last_name: 'blogs'});

#####select(String select) Not required unless you want to manually choose what records to select. Defaults to * e.g.

db.select('first_name as firstName, last_name as lastName');

#####join(Object key/value) Join another table. Defaults to left outer join if join type not specified e.g.

db.join({table: orders, condition: 'order.id = customers.id'})

or

db.join({table: orders, condition: 'order.id = customers.id', type: 'inner'})

or

db.join(
    [
        {
            table: orders, condition: 'order.id = customers.id'
        },
        {
            table: addresses, condition: 'customers.id = addresses.id'
        }
    ]
)

#####limit() Limit the results of a select statement e.g.

db.limit(10);

#####groupBy() Group the results e.g.

db.groupBy('firstName');

or

db.groupBy(['firstName', 'age']);

#####orderBy() Order the results of a select statement String fieldName e.g.

db.orderBy({'first_name': 'desc'});

or

db.orderBy(
    [
        {
            'first_name': 'desc'
        },
        {
            'age': 'asc'
        }
    ]
);

#####get() Get results from a table e.g.

db.get('customers');

#####update() Update the data in a table e.g.

db.update('customers');

#####insert() Insert data into a table e.g.

db.insert('customers');

#####delete() Delete table rows. Be sure to include a where statement first e.g.

db.where('customerId', 1);
db.delete('customers');