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

mql-mysql

v1.0.21

Published

Build and run mysql queries

Downloads

7

Readme

mql-mysql

Build sql dynamically

npm dependencies appveyor

Install

npm i --save mql-mysql

This package depends on ext-mysql which is used to make requests to the database.

Why should you use a SQL builder?

Just to be clear, IMO, you should avoid that. Whenever is possible, write down your raw sqls. But, maybe you're are building a custom report or forms and lists based in custom values, flags and your SQL starts to look something similar to:

var sql = "SELECT ";
for( var i in columns )
  sql += columns[i];
// ...
if( hasJoins )
  sql += joins.join( " " );

if( where )
//...

You may find this useful.

Matrix Query Language

It's not exactly a language, but a matrix object. It explains how the data you want to retrieve or set should be found. It could a json file, but there is MQL class to help you do that. Let's set up an example.

const { MQL, MQLtoMySQL } = require('mql-mysql');
const MySQL = require('ext-mysql');

// Set your MySQL db
process.env.ENCODE = "utf8";
process.env.MYSQL_HOSTNAME = "localhost";
process.env.MYSQL_USER = "root";
process.env.MYSQL_PASSWORD = "";
process.env.MYSQL_DATABASE = "test";
MySQL.CREATE_POOL();

// Create a MySQL connection
const conn = new MySQL();
await conn.init();

Our first MQL

const mql = new MQL();

// Set a table called persons and add two columns, id and name.
mql.addTable( 'persons' );
mql.persons.addColumn( 'id' );
mql.persons.addColumn( 'name' );

// Now you use this mql object to select from database.
var [sql, binds] = await MQLtoMySQL.select( mql );
console.log( sql );
// SELECT persons.id id, persons.name name FROM persons persons

// Or you can run to database
var [rows, fields] = await MQLtoMySQL.select( mql );

MQL.addTable( key, name = key, primary = 'id' )

The key works as an alias, but if you don't provide a (table) name, it's going to be that too. Primary is the column name of the primary key of the table.

MQL.addColumn( table, key, name = key, value = null, flag = MQL.GET )

The table is the alias you gave before (you have to set the table before the column), key works as an alias for the column, name of the column same rule of setTable. Value is used for set, where, join. And flag sets what you want to do about the column. You can access the table and set a column like mmql.myTableAlias.addColumn( 'colName' ).

Examples

This mql below create a row for person table with name and age. After it gets the insert id, it will replace the person.id with the correct value and add the address.

mql = new MQL();

const mql = new MQL();

mql.addTable( 'person' );
mql.person.addColumn( 'id' );
mql.person.addColumn( 'name', 'name', "John", MQL.SET );
mql.person.addColumn( 'age', 'age', 27, MQL.SET );

mql.addTable( 'address' );
mql.address.addColumn( 'id' );
mql.address.addColumn( 'person_id', 'person_id', 'person.id', MQL.JOIN );
mql.address.addColumn( 'line1', 'line1', "Street Sol VI, 123", MQL.SET );

var [ids, results] = MQLtoMySQL.insert( mql );

Update

This example does the same as before, but updating value. Now we have to set a column with MQL.EQUAL_TO in order to create a where rule. The address will be found because of the MQL.JOIN. It updates and get the person row, look for a address that fits the MQL.JOIN if found, updates, when not, inserts a new row.

mql = new MQL();

const mql = new MQL();

mql.addTable( 'person' );
mql.person.addColumn( 'id', 'id', 15, MQL.EQUAL_TO );
mql.person.addColumn( 'name', 'name', "John", MQL.SET );
mql.person.addColumn( 'age', 'age', 27, MQL.SET );

mql.addTable( 'address' );
mql.address.addColumn( 'id' );
mql.address.addColumn( 'person_id', 'person_id', 'person.id', MQL.JOIN );
mql.address.addColumn( 'line1', 'line1', "Street Sol VI, 123", MQL.SET );

var [ids, results] = MQLtoMySQL.update( mql );

Delete

This is the same than before, I just remove the MQL.SET columns. You could keep those there, they are ignored.

mql = new MQL();

const mql = new MQL();

mql.addTable( 'person' );
mql.person.addColumn( 'id', 'id', 15, MQL.EQUAL_TO );

mql.addTable( 'address' );
mql.address.addColumn( 'person_id', 'person_id', 'person.id', MQL.JOIN );

var [ids, results] = MQLtoMySQL.update( mql );

// TODO: More examples.