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

node-ejsql

v1.0.2

Published

EJSQL is a light weight cross-database interface that creates templatized SQL across MySQL, SQLite, MSSQL and Postgres.

Downloads

15

Readme

EJSQL

Package Version License pipeline status coverage report dependencies GitHub top language

EJSQL is a light weight cross-database interface that creates templatized SQL across MySQL, SQLite, MSSQL and Postgres. API documentation is available here.

Background

Over time as an engineer, we like to write good old SQL queries without the overhead or restrictions of a complex ORM. ORMs can introduce complexities where simple aggregation becomes troublesome to implement because it has to conform to a way the ORM can understand.

Over time though, as features are implemented and queries become more spread out over multiple functions, it can become difficult to see how changes affect the big picture of what the query is supposed to look like.

This project aims to simplify the process of creating complex queries without sacrificing code readability. Better readability in how SQL is generated means fewer errors would be missed during code review.

Installation

Open your terminal and type in

via npm,

$ npm install --save node-ejsql

or via Yarn,

$ yarn add node-ejsql

Usage

EJSQL leverages Embedded JavaScript to handle templating of the SQL with SqlString providing escaping and formatting for JavaScript objects.

Features of EJSQL

  • uses { and } instead of < and > for command delemiters
  • escape template values using {%- 'test' %}
  • creates format objects syntax using {%: [1,2,3] %}

Command Line Interface

EJSQL comes with a command line interface that allows for an sql.ejs file to be ran from command line. If the package is installed globally, ejsql should be available via command line and if installed locally, it can be accessed through npx ejsql.

Usage: ejsql [options] <filename> [...filenames]

Runs a templatized sql.ejs file against a database. When using an adapter, make sure the adapter's companion module is installed (mssql, mysql, sqlite3, pg). See https://rdfedor.gitlab.io/node-ejsql/ for additional details.

Options:
  -V, --version                              output the version number
  -a, --adapter <database_adapter>           The database type mssql, mysql, pg, sqlite (default: "sqlite")
  -h, --host <database_host>                 The path to the database host (default: "localhost")
  -n, --dbname <database_name>               The name of the default database (default: "master")
  -u, --user <database_user>                 The username associated with the database login (default: "root")
  -p, --pass <database_pass>                 The password associated with the database login (default: "")
  -d, --data [var1=val1,var2=val2,...]       Pass data to the template as a name value pair (default: "")
  -c, --connector [var1=val1,var2=val2,...]  Add additional options for the database connection (default: "")
  -s, --skipHeaders                          Disables the output of the headers (default: false)
  --help                                     display help for command

Quickstart

This quickstart assumes that there's an existing database and table called users with data.

  • Create an ejsql directory in your source directory.
  • Create list-users.sql.ejs in the ejsql directory with the following contents,
SELECT 
{% if (typeof count !== 'undefined') { %}
    count(*) as countUsers
{% } else { %}
    * 
{% } %}
FROM users
WHERE 1=1
{% if (typeof email !== 'undefined') { %}
    AND email LIKE {%: `%${email}%` %}
{% } %}
  • Create index.js in the root of the source directory with the following contents,
const mysql = require("mysql")
const { join } = require('path')
const { Adapters, Loader } = require("node-ejsql")

// Create a connection the adapter will use
const connection = mysql.createConnection({
  host : '127.0.0.1',
  port : 3307,
  database : 'test-database'
})
// Create the adapter
const adapter = new Adapters.MySQLConnection(connection)

// Load our queries
const queries = Loader(join(__dirname, './ejsql'), adapter)

// Do something
async function foo() {
  const rows = await queries.listUsers({
      count: true,
      email: '@gmail.com'
  })
  rows.map(row => {
    console.log('Total Users: ' + row.countUsers)
  })
}
foo()

Additional examples can be found here and API documentation can be found here.

Supported Adapters

Bugs

If you have questions, feature requests or a bug you want to report, please click here to file an issue.

Author

Support

Like what you see? Keep me awake at night by buying me a coffee or two.

License

Copyright (c) 2020 Roger Fedor.

Usage is provided under the MIT License. See LICENSE for the full details.