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-rewrapped

v1.3.3

Published

A wrapper for MySQL on node to make querying a database as easy as possible

Downloads

81

Readme

MySQL-Rewrapped

Build Status GitHub npm npm bundle size npm

WARNING

The where function has changed since version 1.2.6, make sure to read Common Functions to see how the where function now works

Description

MySQL wrapped is a nodejs library built to make querying a database without any knowledge of sql easy, it depends on:

  • mysql

Setup

To install this module, from the command line run

npm install --save mysql-rewrapped

to include this package in your software, include this in your source code

const mysql = require("mysql-rewrapped");

To initialise an instance of mysql-wrapped, you need to give it config in the form of a json object, as well as this, the initialisation is asynchronous, so you will need a callback e.g:

const mysqlRewrapped = require("mysql-rewrapped");
let mysql;
mysqlRewrapped({
 "host": "127.0.0.1",
 "user": "root",
 "password": "password",
 "database": "EXAMPLE_SCHEMA",
 "connectionLimit": 10
}, (data) => {
    mysql = data;
});

Usage

mysql-wrapped has 3 levels of abstraction, and it can be used at any level, with the lowest level being the default mysql package, so in ascending levels of abstraction, it could be used like this

Level 1 - No abstraction

this would be used in the same way as the mysql package, and most of the best documentation would be found at that package, specifically on connection pools, the way the connection pool is accessed is

mysql.Database.connectionPool

Level 2 - Query generation

This level is where queries are generated, and can generate the 4 main types of query, which all have a common structure, with common functions

new mysql.QUERYTYPE(
    
)

then you add functions on to the end depending on what you want to do with the query, but at to execute every query, you do:

new mysql.QUERYTYPE().exec(
    //you place a callback in here, which returns your data
)

SELECT

To use SELECT statements with this library, you would do something along the lines of

new mysql.Select(
    //field names here, as an array, or an asterisk to indicate a wildcard
)

The select statement has a special function called join, which is used as shown below

join(
    /*
    Joining one table onto another, usage as shown:
    [{type: type of join. target: target table, from: field on main table, to: field on joining table}]
     */
)

The select statement has another function called distinct, which when called will make the query only select distinct fields, there are no parameters

Update

Update is very similar to the Select statement, the only difference being the constructor

new mysql.Update(
    //json object of fields to update, with their values as shown below
    //{field: value} - e.g
    {firstname: "Bob"}
    
)

Insert

For insert, the constructor is the same as for Update, so

new mysql.Insert(
    //json object of fields to insert, with their values as shown below
    //{field: value} - e.g
    {firstname: "Bob"}
    
)

Delete

For insert, the constructor does not take any parameters

Common Functions

new mysql.QUERY.table(
    //the name of the table to select from
).where(
    /*
    Filtering the table done as shown:
    fieldname: the name of the field - string,
    value: the value to search by - string / array
    operator - string, operators are "=", "<=>", "<>", "!=", ">", ">=", "<", "<=", "like"
    can only be used to start the where, can't be used multiple times in a query
     */
).orWhere(
    /*
    fieldname: the name of the field - string,
    value: the value to search by - string / array
    operator - string, operators are "=", "<=>", "<>", "!=", ">", ">=", "<", "<=", "like"
     */
).andWhere(
    /*
    fieldname: the name of the field - string,
    value: the value to search by - string / array
    operator - string, operators are "=", "<=>", "<>", "!=", ">", ">=", "<", "<=", "like"
     */
).exec(
    /*
    This function executes the generated query, and returns in via a callback function
     */
)

Level 3 - Table generation + field checks

so for level 3, the library will generate an instance of a table class for every table it finds in your database, it will use this to check your parameters, to check the fields exist in the tables.

To use this functionality, all you need to do is:

TABLENAME.Select()
TABLENAME.Insert()

the advantage of this is you also dont need to use the .table function when generating the query, as it is automatically done

in actual use the code would look like this

Users.select("*")
    .where("groupname", "Admin", "=")
    .join(
        [
            {
                type: "INNER",
                target: "UserGroups",
                from: "Users.id",
                to: "UserGroups.userid"
            },
            {
                type: "LEFT",
                target: "Groups",
                from: "UserGroups.groupid",
                to: "Groups.id"
            }
        ]
    ).exec(data => {
        console.log(data);
    }
);