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

jm-ez-mysql

v4.0.0

Published

Easy MySQL Wrapper

Downloads

657

Readme

jm-ez-mysql

Overview

Easy MySQL Wrapper for NodeJS

Installation

npm install jm-ez-mysql --save

Examples

var My = require("jm-ez-mysql");

// Init DB Connection
My.init({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'psu',
});

// Select All
My.findAll("psu_project", ["id"], "1=1").then(function (r) {
    console.log(r)
})

// Select All with count
My.findAllWithCount("psu_project", "id", ["id"], "1=1", "LIMIT 0, 5").then(function (r) {
    console.log(r)
})

// Select First
My.first("psu_project", ["id"], "1=1 ").then(function (r) {
    console.log(r);
});

// Insert
My.insert("temp", {
    name: 'Jay'
}).then(function (result) {
    console.log(result.insertId)
})

// Insert Multiple Rows
My.insertMany("temp", [{
    name: 'Jay'
}, {
    name: 'Subham'
}]).then(function (result) {
    console.log(result.insertId)
})

// Update
My.update("temp", {
    name: 'Jayu'
}, "id = 2").then(function (result) {
    console.log(My.lQ);
})

// Update First
My.updateFirst("temp", {
    name: 'Jayu'
}, "id = 2").then(function (result) {
    console.log(My.lQ);
})

// Delete
My.delete("temp", "id = 6").then(function () {
    console.log(My.lQ);
})

// Pure MySQL Query
var id = "'4";
My.findRaw("select * from psu_project where id = " + My.escape(id))
    .then(function (results) {
        console.log('My query results', results);
        console.log('Lq', My.lQ);
    })
    .catch(function (err) {
        console.log(err);
    });

// Pure MySQL Query as Formatted String
var id = "'4";
My.query("select * from psu_project where id = ?", [id])
    .then(function (results) {
        console.log('My query results', results);
        console.log('Lq', My.lQ);
    })
    .catch(function (err) {
        console.log(err);
    });

// Select All using prepared statement
My.findAll("psu_project", ["id"], "id=?", [id]).then(function (r) {
    console.log(r)
})

// Select All with count using prepared statement
My.findAllWithCount("psu_project", "id", ["id"], "id=?", "LIMIT 0, 5", ["id"]).then(function (r) {
    console.log(r)
})

// Select First using prepared statement
My.first("psu_project", ["id"], "1=? ", [id]).then(function (r) {
    console.log(r);
});

// Update using prepared statement
My.update("temp", {
    name: 'Jayu'
}, "id = ?", [id]).then(function (result) {
    console.log(My.lQ);
})

// Update First using prepared statement
My.updateFirst("temp", {
    name: 'Jayu'
}, "id = ?", [id]).then(function (result) {
    console.log(My.lQ);
})

// Delete using prepared statement
My.delete("temp", "id = ?", [id]).then(function () {
    console.log(My.lQ);
})

//Select Query with where condition
const selectQuery = My.initQuery();
selectQuery.where('id', 1);
selectQuery.execute("table");

//Select Query with multiple where condition
const selectQuery = My.initQuery();
selectQuery.where('id', 1);
selectQuery.where('name', 'somename');
selectQuery.execute("table");


//Select Query with multiple(AND/OR) where condition
const selectQuery = My.initQuery();
selectQuery.where('id', 1);
selectQuery.orWhere('name', 'searchContent');
selectQuery.execute("table");
//query : SELECT  *  FROM table  WHERE id = 1   OR  name = 'searchContent';

//Select Query with multiple(AND/OR) with multple condition where condition
const selectQuery = My.initQuery();
selectQuery.where('(id = ? OR id = ?)', [1, 50]);
selectQuery.orWhere('name', 'searchContent');
selectQuery.execute("table");

//Another way to pass condition value
const selectQuery = My.initQuery();
newQuery.where('(id = ? OR id = ?)');
selectQuery.orWhere('name', 'searchContent');
selectQuery.execute("table", [1, 50]);
//query: SELECT  *  FROM table WHERE (id = 1 OR id = 50)  OR  name = 'searchContent';

//Select custom fields
const selectQuery = My.initQuery();
selectQuery.select('name, id, data'); // argument can be String|Array , Default is *
selectQuery.execute("table", [1, 50]);
//Query: SELECT name, id, data FROM table  WHERE 1=1

//Use joins : 

// Ex1. Left Join
const selectQuery = My.initQuery();
selectQuery.leftJoin("table2 as t2", "t2.t1ID = t1.id "); 
selectQuery.execute("table as t1");

// Query: SELECT  *  FROM table1 as t1 LEFT  JOIN table2 as t2 ON t2.t1ID = t1.id  WHERE 1=1;

// Ex2. Left and Right Join
const selectQuery = My.initQuery();
selectQuery.leftJoin("table2 as t2", "t2.t1ID = t1.id "); 
selectQuery.rightJoin("table3 as t3", "t3.t1ID = t1.id "); 
selectQuery.execute("table as t1");
//Query: SELECT  *  FROM table1 as t1 LEFT  JOIN table2 as t2 ON t2.t1ID = t1.id  RIGHT  JOIN table3 as t3 ON t3.t1ID = t1.id  WHERE 1=1;

// Ex3. Left Join with Condition
const selectQuery = My.initQuery();
selectQuery.leftJoin("table2 as t2", "t2.t1ID = t1.id AND t2.name = ? "); 
selectQuery.execute("table as t1", ['somename']);
//query: SELECT  *  FROM table as t1 LEFT  JOIN table2 as t2 ON t2.t1ID = t1.id AND t2.name = 'somename'  WHERE 1=1; 

// Ex4. Inner Join

const selectQuery = My.initQuery();
selectQuery.innerJoin("table2 as t2", "t2.t1ID = t1.id");
selectQuery.execute("table as t1");
// Query: SELECT  *  FROM table1 as t1 INNER  JOIN table2 as t2 ON t2.t1ID = t1.id  WHERE 1=1;

// Ex5.
const selectQuery = My.initQuery();
selectQuery.join("table2 as t2", "t2.t1ID = t1.id", "left"); // third argument will join type ex. (left, right, right outer, ...), default is inner
selectQuery.execute("table as t1");



//Use query as count
const selectQuery = My.initQuery();
selectQuery.select('name, id, data'); // argument can be String|Array , Default is *
selectQuery.where('(id = ? OR id = ?)', [1, 50]);
selectQuery.orWhere('name', 'searchContent');
selectQuery.execute("table", [], true); // If third argument will be true than last condition will remains and used for further process, like for count.
//Qeury: SELECT name, id, data FROM table  WHERE (id = 1 OR id = 50)  OR  name = 'searchContent'

selectQuery.count(); // argument can be any field name, default is id 
selectQuery.execute("table");
//Qeury: SELECT COUNT(table.id) as count  FROM table  WHERE (id = 1 OR id = 50) OR  name = 'searchContent'


//Use order by
const selectQuery = My.initQuery();
selectQuery.orderBy('id', 'ASC');
selectQuery.orderBy('name', 'DESC');
selectQuery.execute("table");
//Query: SELECT  *  FROM table  WHERE 1=1 ORDER BY id ASC, name DESC

//Use group by
const selectQuery = My.initQuery();
selectQuery.groupBy('id'); //
selectQuery.execute("table");
//Query:SELECT  *  FROM table  WHERE 1=1 GROUP BY id 

//Use Limit 
const selectQuery = My.initQuery();
selectQuery.skip(2); // records want to skip
selectQuery.limit(10);
selectQuery.execute("table");
//Query:SELECT  *  FROM table  WHERE 1=1 LIMIT 2, 10


// Get Last fired Query
console.log(My.lQ);

License

The MIT License (MIT)