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

seduce

v0.0.4

Published

a package for writing isolated SQL

Downloads

6

Readme

seduce

Seduce is a Node module for keeping your SQL out of your code. With seduce, you write parametrized SQL in .sql files. Seduce parses it and builds your queries as functions.

Seduce is a way of writing SQL queries. It does not provide database logic.

Install

npm install seduce

Usage

Start by writing your SQL queries like this, in a file like queries.sql:

-- name: findByNameAndModel
-- Queries the cars table by a car name
SELECT *
FROM cars
WHERE cars.brand = :name AND cars.model = :model

-- name: findByNames
SELECT *
FROM cars
WHERE cars.brand IN :names

Notice the name: notation. This is required, and the name that you define will be the name of the function that seduce will generate for you. You can add any number of comment lines describing what the query does after that line.

Next, write your query normally. For any portion that needs to take a parameter, indicate your parameter like :name.

Write any number of queries that you want -- each should be separated by an empty line.

To use this query that you've just defined, do this:

var seduce = require('seduce'),
    q = seduce('queries.sql');

seduce(...) has the following signature: paths {String|Array} and opt {Object}. Paths are file paths to your sql files; opt is optional but highly recommended. It supports a key escape to attach a function to escape your parameters to prevent injection attacks. Here's an example if you're using node-mysql: seduce(['one.sql', 'two.sql], { escape: connection.escape })

You can call your functions by referring to them by name. Any of these are equivalent:

var carQuery = q.findByNameAndModel('Ford', 'Explorer');

or

var carQuery = q.findByNameAndModel({ name: 'Ford', model: 'Explorer' });

or

var myParams = ['Ford, 'Explorer];
var carQuery = q.findByNameAndModel.apply(null, myParams);

This will return a String like this:

SELECT * FROM cars WHERE cars.brand = "Ford" AND cars.model = "Explorer"

If you pass an array as an argument (like if you're doing an IN query), it'll produce something like this:

SELECT * FROM cars WHERE cars.brand IN ("Ford", "Honda")

If you have multiple parameters with the same name, Seduce will take care of that for you and duplicate the value you provide in your query.

Example

Starting with queries.sql like above...

var mysql = require('mysql'),
  connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'me',
    password : 'secret'
  }),
  seduce = require('seduce'),
  q = seduce('queries.sql');

connection.connect();

connection
.query(q.findByNameAndModel('Ford', 'Explorer'), function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

connection.end();

Example taken from the documentation for node-mysql.

Advantages

  • Writing SQL in Javascript code is awful; anything remotely complex will be on multiple lines, and multiline strings in Javascript usually means dumping your strings into an array and joining them, or concatenating them across multiple lines.
  • Your editor likely has solid support for SQL syntax. By putting your queries in sql files, you get the benefits of syntax highlighting and indentation rules.
  • You can copy and paste your queries elsewhere easier, like into your favorite database client, without having to deal with stripping quotes or brackets.
  • Someone else can write your queries without having to muck around in code.

Status

Brand new.

Credits

Seduce is heavily inspired by a Clojure library by Kris Jenkins Yesql.