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

prep-composer

v1.2.0

Published

A lightweight JavaScript utility that facilitates convenient and safe composition of SQL statements by automatically managing and escaping variable parts.

Downloads

296

Readme

prep-composer

prep-composer is a lightweight JavaScript utility that facilitates convenient and safe composition of SQL statements by automatically managing and escaping parameters.

Features

  • Inline Parameter Values: Simplifies SQL writing by automatically managing the order and number of parameter values. No placeholders and separate parameter list.
  • Modular SQL Composition: Allows the creation of complex SQL statements through independent, reusable segments.
  • Identifier Escaping: Supports escaping of identifiers for enhanced security.
  • Flexible Integration: Compatible with established escaping utilities like sqlstring, and supports ? placeholders for prepared statements.
  • Dialect Compatibility: Designed to work with all SQL dialects; identifier escaping is currently optimized for MySQL and compatible databases using `backtick` escaping.

Usage

const {sql, $} = require('prep-composer');
const SqlString = require('sqlstring'); // optional

const name = "O'Brien";
const newJobTitle = 'Senior Developer';

const updateTable = sql('UPDATE', $['my_db']['employees']); // identifier escaping is optional
const fieldsToUpdate = {
    job_title: 'Designer',
    salary: 200000,
};
const condition = sql('name =', $(name));

const query = sql(updateTable, 'SET', $(fieldsToUpdate), 'WHERE', condition);

console.log(query.toString());
// UPDATE `my_db`.`employees` SET `job_title` = ?, `salary` = ? WHERE name = ?

console.log(query.parameters);
// [ "Designer", 200000, "O'Brien" ]

console.log(query.toString(SqlString.escape));
// UPDATE `my_db`.`employees` SET `job_title` = 'Designer', `salary` = 200000 WHERE name = 'O\'Brien'

Installation

You can install prep-composer using npm:

npm install prep-composer

API documentation

sql(...args: (Fragment | Fragment[])[]): SqlSegment

Creates a new SqlSegment composed of the provided fragments. Fragments can be strings, literals, identifiers, or values. Raw SQL is typically represented by a string fragment. It will be turned into a Literal internally. An array of fragments is flattened into the SQL segment, separated by commas.

Fragment

Type definition for an SQL fragment. A fragment can be a Literal, a string (will be turned into a Literal), a Value, an Identifier or an SqlSegment.

$(value: any): Value|SqlSegment[]

Helper function to create a new Value fragment for the given value. Values represent the variable fragments of a statement that are later escaped or replaced with placeholders.

If the passed value is an array, it is turned into an array of SqlSegments by converting all the elements to values if they are not of type Literal or SqlSegment. This is practical for creating IN conditions.

If an object is passed it will be turned into a series of escaped-key=escaped-value assignments. If values are already literals, values or identifiers, they are used as is. So to set a field to a static raw SQL value, you have to wrap the value in an sql function call (e.g. { hire_date: sql('NOW()') }) otherwise it is interpreted as a parameter that has to be escaped.

$.<identifier> or $[<identifier>]

Helper to create a new Identifier fragment for the given identifier. The identifier is immediately escaped with backticks when passed to the sql function. Identifiers can be chained: $['db']['field'] (yields `db`.`field`). It is recommended to use the square brackets syntax $[<identifier>] for consistency. Dots within identifiers are preserved ($['db.field'] yields `db.field`).

SqlSegment

Represents a composite SQL segment composed of multiple fragments. A segment can be used as a part of another segment, allowing the creation of complex SQL statements.

Members:

  • toString(escapeFn?: (value: any) => string): string: Returns the SQL statement as a string. If escapeFn is provided, it will be used to escape values. Otherwise ? placeholders are output.
  • parameters: any[]: An array of parameter values extracted from the SQL segment.
  • append(fragment: Fragment | Fragment[]): SqlSegment: Returns a new SqlSegment by appending the provided fragment(s) to the current segment. As in the sql function, an array of fragments is flattened into the SQL and separated by commas. Spaces are automatically added between fragments if required.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Feel free to contribute to this project by submitting issues or pull requests. Your feedback and contributions are welcome!