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

sql-pg

v10.4.1

Published

Complex queries can be written with normal SQL, including the values needs to be bound and prefixed with the `sql` tag.

Downloads

85

Readme

Build Status Coverage Status

Latest Release: v10.4.1, Latest Major Release: v10.0.0, All Releases

SQL-PG

Complex queries can be written with normal SQL, including the values needs to be bound and prefixed with the sql tag.

Features

  • Built on top of pg as database driver
  • Simple data manipulation and selection methods without the need to write SQL
  • SQL Tag and Tag Helpers to write queries looks like native SQL and be secure by design
  • Write easy unit testable queries
  • Possibility to add own Tag Helpers to extend the functionality

Installation

npm install --save pg sql-pg

Initialisation

Use it in your project:

const sql = require('sql-pg')()

The connection use per default the env var DATABASE_URL.

Usage

Manipulation Methods

Simple data manipulation can be done without writing any SQL Statements.

E.g. some user data manipulation:

const id = await sql.insert(
  'users',
  { name: 'Sharaal', email: '[email protected]', passwordhash: '...' }
)

await sql.update('users', { validated: 1 }, { id })

await sql.delete('users', { id })

More complex data manipulation can be done with the SQL Tag.

Selection Methods

Often needed convenient methods to check and extract query results are available with the Selection Methods.

E.g. select all not validated users:

const users = await sql.any('users', ['name', 'email'], { validated: 0 })

Also the Selection Methods supports SQL Tag as parameter for more complex selections. Because they are highly inspired by pg-promise, there are the Selection Methods any/manyOrNone, many, oneOrNone and one available.

SQL Tag and Tag Helpers

If it becomes more complex the SQL Tag and Tag Helpers are the way to go.

They are as near as possible to native SQL queries to be readable and easy to write. All variables can be directly used and will be exchanged via placeholders and given to the database separately as values. For non native values like lists, for table/column names and conditions there are Tag Helpers.

E.g. list of not activated users filtered by name:

const name = 'raa'

const users = await sql.any(
  sql`
    SELECT "name", "email" FROM "users"
      WHERE
        "validated" = 0
        AND
        "name" LIKE ${`%${name}%`}
  `
)

There are a lot more Tag Helpers available like .identifier, .table, .column(s), .value(s), .valuesList, .assignments, .conditions, .limit, .offset, .pagination, .if, .jsonColumnObject and .jsonColumnText.

More

Alternative initialization, available Tag Helpers, Nested Queries, Transaction, Writing Tag Helpers, Migrations, Syntax Highlighting in Atom... All additional documentation can be found in the Wiki.

Contact

Found a bug or missing a feature? -> Create a new Issue

Found a security issue? -> Look at the Security Policy

Having questions, want to give feedback or talk to me? -> E-Mail me [email protected]