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

sqlish

v0.0.12

Published

A small SQL generator for NodeJS, MongoDB shell or web browser.

Downloads

23

Readme

build status sqlish

A SQL generator for for NodeJS, Mongo Shell and web browsers.

Overview

JavaScript's ability to chaining function calls provides an nice semantic for generating SQL statements. sqlish (Californian pronunciation: sk wil' ish) takes this approach and attempts to be portable as well as simple to use.

Advantages?

There are several advantages two taking this approach

  • lower cognitive noise (you're programming in one language not two)
  • your JS tools (e.g. jslint) now cover your assembly of SQL statements
  • assembling complex SQL is just a matter of chaining functions
  • MongoDB like object literals for expressions
  • sqlish is very small without dependancies
  • it works as a NodeJS module
  • it works as a loadable script in MongoDB's shell
  • it works should work web browsers supporting SQLite

installation

To use with NodeJS install via npm.

	npm install sqlish

A NodeJS example

	var sqlish = require("sqlish"),
		dialect = sqlish.Dialect,
		Sql = new sqlish.Sqlish("MySQL 5.5"),
		message = {
			id: 0,
			name: "fred",
			email: "[email protected]",
			messages: 'He Said, "Hello World"',
			sent: new Date("09/01/2012 14:15:00")
		};

	Sql.use_UTC = false;	
	
	// Output:
	// INSERT INTO messages (name, email, msg, sent) VALUES (
	//	"fred", "[email protected]", "He siad, \"Hello World\"", 
	//	"2012-09-01 14:15:00");
	console.log(Sql.insert("test", message).toString());
	
	// Output:
	// SELECT id, name, email, msg, sent FROM messages 
	//	WHERE email LIKE "%@example.com";
	console.log(Sql.select(Object.keys(message))
			.from("messages")
			.where({email: {$like: "%@example.com"}}).toString());
	
	// Output:
	// REPLACE INTO messages (id, name, email, msg, sent) VALUES (
	//	10123, "George", "[email protected]", "He siad, \"Hello World\"", 
	//	"2012-07-01");
	message.id = 10123;
	message.name = "George";
	message.email = "[email protected]";
	message.sent = new Date("07/01/2012");
	console.log(Sql.replace("test", message).toString());

a word about sqlish's toString()

Normally you want SQL statements to end with a semi-colon and by default this is what the toString() at the end of the function chain will do. However their are cases where you may want to render complex SQL statements by parts. In that case you can overwrite the default semi-colon by passing the terminating string (including the empty string) as a paramater to toString().

	var sqlish = require("sqlish"),
		Sql = new sqlish.Sqlish();
    
    // No trailing semi-colon
    console.log(sql.select("count()").toString(""));
    // Trailing semi-colon
    console.log(sql.select("count()").toString());

Running the above will yeald something like-

    > console.log(sql.select("count()").toString(""));
    'SELECT count()'
    > console.log(sql.select("count()").toString());
    'SELECT count();'

If you would like to have some other delimiter used as the end of statement marker you can do so when overwriting Sql.eol at time of object creation or before calling toString().

	var sqlish = require("sqlish"),
		Sql = new sqlish.Sqlish();
		
	Sql.eol = ";\n\n";
    
    // No trailing semi-colon
    console.log(sql.select("count()").toString(""));
    // Trailing semi-colon with two new lines
    console.log(sql.select("count()").toString());

Would yeild something like-

    > console.log(sql.select("count()").toString(""));
    'SELECT count()'
    > console.log(sql.select("count()").toString());
    'SELECT count();'
    
    

MongoDB shell example

Copy sqlish.js and load-sqlish.js to your working directory. Then use MongoDB's shell's load() function to include it. See mongo-example.js:

    load("load-sqlish.js");
    sql = new Sql();
    // Output should look like:
    //  SELECT id, name, email, modified FROM myTable;
    print(sql.select(["id", "name", "email", "modified"]).from("myTable").toString());
    item = {id:1, name: "fred", email: "[email protected]", modified: new Date("09/14/2012 10:21:14")};
    // Output should look like:
    //  REPLACE INTO myTable (id, name, email, modified) VALUES ("fred", "[email protected]", "2012-09-14 10:21:14");
    print(sql.replace("myTable", item).toString());

Save the above example code as mongo-example.js. Run under MongoDB's shell-

	mongo mongo-example.js

Output should look something like-

	MongoDB shell version: 2.2.0
	connecting to: test
	SELECT id, name, email, modified FROM myTable;
	REPLACE INTO myTable (id, name, email, modified) VALUES (1, "fred", "[email protected]", "2012-09-14 10:21:14");

Alternatives?

If you don't need web browser or MongoDB shell support two SQL generators at npmjs.org look very promising-