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

modquery

v0.9.0

Published

A module to use for MySQL queries with respect to SQL.

Downloads

3

Readme

modQuery (v 0.5)

A module to use for MySQL queries with respect to SQL.

What is about

modQuery is a module that aims to bridge your JS code with MySQL with a respect to both SQL & Javascript. It's purpose is to wrap your SQL with extra functionality that is often needed. It's fun.

Why to use it

Because..

  • You like using pure SQL but not have a unmaintainable JS code.
  • You don't want flood your database with unnecessary connections & re-connections.
  • You want to wrap your your Query with objects & callbacks that fit better to your JS logic.
  • Your model in your JS code doesn't mirror exactly the Database Schema.
  • You want a better SQL control.
  • You want to have fun & you like beer.

What to have in mind

  • It is not a persistence framework.
  • It currently supports MySQL only.
  • It can be used in production but with caution.
  • Don't use it if you don't like beer.

Getting Started

As usual, include the module

var modQ = new require("modquery");

Initiate it.

var modQuery = new modQ({
	dbArgs: {
		host              : "localhost",
		user              : "bartender",
		password          : "beerIsAwesome",
		database          : "MyPub"
	}
});

Cheers!

// create a massive Query
modQuery.newModQuery()// TIP: use newModQuery() to void collisions & have better connection pooling
	.from("beers") //
	.select(["name"]) //
	.filterBy("beers", "id").in([1,2,3,4]) //
	.addParallel() // parallel execution & result fetch
	//-------------------------- test regex
	.from("beers") //
	.select(['id', 'name']) //
	.filterBy("beers", "name").regex("'^[a-d]'") //
	.addParallel() // parallel execution & result fetch
	//-------------------------- test equals
	.from("beers") //
	.select(['id', 'name']) //
	.filterBy("beers", "id").equals(1) //
	.addParallel() // parallel execution & result fetch
	//-------------------------- test notEquals
	.from("beers") //
	.select(['id', 'name']) //
	.filterBy("beers", "id").notEquals(1) //
	.limit(10, 20)
	.addParallel() // parallel execution & result fetch
	//-------------------------- test contains
	.from("beers") //
	.select(['id', 'name']) //
	.filterBy("beers", "name").contains("old") //
	.limit(1, 10)
	.addParallel() // parallel execution & result fetch
	//-------------------------- test greaterThan
	.from("beers") //
	.select(['id', 'name']) //
	.filterBy("beers", "abv").greaterThan(5) //
	.limit(1, 10)
	.addParallel() // parallel execution & result fetch
	//-------------------------- Build and execute all queries in parallel
	.execute(function(rows, err, sql) {
		if(err){
			// if error, show what the heck was executed
			console.log(sql);
			console.log(err);
		}
		console.log(rows);
	});

Insert Rows:

modQuery.newModQuery()
	.insertInto("beers")
	.insert("name", "test Beer")
	.insert("abv", 5.7)
	.addParallel() // other row
	.insertInto("beers")
	.insert("name", "best Beer")
	.insert("abv", 9)
	.execute(function(rows, err, sql) {
		if(err){
			// if error, show what the heck was executed
			console.log(sql);
			console.log(err);
		}
		console.log(sql);
	});

compiles to

INSERT  INTO `beers`  SET name = 'test Beer', abv = 5.7 ;
INSERT  INTO `beers`  SET name = 'best Beer', abv = 9 ;

A more Advanced

modQuery.newModQuery() //
	.insertInto("beers_mirror") //
	.from("beers") //
	.select(["id", "name", "abv"]) //
	.filterBy("beers", "id").in([1,2,3,4]) //
	.limit(1, 10) //
	.onDuplicate() //
	.set("name","fooBar") //
	.execute(function(rows, err, sql) {
		if(err){
			// if error, show what the heck was executed
			console.log(sql);
			console.log(err);
		}
		console.log(sql);
	});

compiles to

INSERT  INTO `beers_mirror`
	SELECT id, name, abv  FROM `beers`
	WHERE (  `beers`.`id` IN (1,2,3,4)  )
	LIMIT 1,10
ON DUPLICATE KEY UPDATE
	name = 'fooBar';

Update rows

modQuery.newModQuery() //
	.update("beers_mirror") //
	.set("name","fooBar1") //
	.filterBy("beers_mirror", "id").in([1,2]) //
	.execute(function(rows, err, sql) {
		if(err){
			// if error, show what the heck was executed
			console.log(sql);
			console.log(err);
		}
		console.log(sql);
	});

compiles to

UPDATE `beers_mirror`
	SET name = 'fooBar1'
WHERE (  `beers_mirror`.`id` IN (1,2)  );

You can even enqueue heterogeneous Queries and execute them in pipe, callback will then be called for each one of them.

modQuery.newModQuery() //
	.insertInto("beers_mirror") //
	.from("beers") //
	.select(["id", "name", "abv"]) //
	.filterBy("beers", "id").in([1,2,3,4,5,6]) //
	.limit(0, 10) //
	.onDuplicate() //
	.set("name","fooBar") //
	.inAddition() // enQueue query
	.update("beers_mirror") //
	.set("name","fooBar1") //
	.filterBy("beers_mirror", "id").in([1,2]) //
	.inAddition() // enQueue query
	.from("beers_mirror")// a SELECT * query
	// Execute all as they have been piped
	.execute(function(rows, err, sql) {
		if(err){
			// if error, show what the heck was executed
			console.log(sql);
			console.log(err);
		}
		console.log(sql);
	});

compiles to

INSERT  INTO `beers_mirror`
	SELECT id, name, abv
	FROM `beers`
	WHERE (  `beers`.`id` IN (1,2,3,4,5,6)  )
	LIMIT 0,10
ON DUPLICATE KEY UPDATE
	name = 'fooBar';

UPDATE `beers_mirror`
	SET name = 'fooBar1'
WHERE (  `beers_mirror`.`id` IN (1, 2)  );

SELECT  *   FROM `beers_mirror`   ;

I found a bug

  • First drink a beer,
  • then create an issue,
  • then pray & have faith.