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

pgeon

v0.0.6

Published

Type-safe Postgres client.

Downloads

16

Readme

pgeon

This library is:

  1. A zero-dependencies, decently fast Postgres client.
  2. A webpack loader that type-checks your SQL queries at compile time (seriously).

Installation

npm install --save pgeon

Example

For a first look, check out the small example application and its corresponding webpack configuration. Provided Docker is running, you can try starting it with the command below. Notice that there is a type error which is caught at compile time!

./docker-npm run example

Postgres client

A from-scratch implementation of the Postgres protocol covering most common use cases. Its features include:

  • Connection pooling
  • Secure database connections
  • Support for most common data types (and more coming soon)
  • Strict one-to-one mapping between Postgres and JavaScript types
  • Query cancellation
  • Basic transaction management
  • Usage of Postgres' binary data format
  • Query preparation for SQL injection prevention

Connection pool

The first step towards executing useful queries is starting a connection pool.

import { newPool } from './postgres-client'

export const db = newPool()

If no options are provided, standard Postgres environmental variables will be read and default values used. Explicit parameters can be provided to configure the database connection and pool limits.

import { newPool } from './postgres-client'

export const db = newPool({
  host: 'https://example.org',
  port: 41100,
  username: 'john_doe',
  password: 'aV27FGH!!bVxpQyyBukKyQ5&#TzX^)mg5%JzDuZKuA*xi(uh5s)%zZ!2CCY&(@5T',
  minConnections: 1,
  maxConnections: 16
})

Query execution

Through a connection pool, queries can be run(). Queries with no dynamic parts on them (save their parameters) should be defined using the sql template literal tag. This allows for static type checking later on, such that the types of the columns returned by the query are taken into account at compile time, just like for any regular TypeScript function. For this reason, it is important that sql is not aliased, and that no other template literal tag named the same exists elsewhere in the codebase.

To prevent SQL injection, template literal placeholders are replaced with Postgres query placeholders, and the query is prepared and executed in separate steps.

import { sql } from 'pgeon'
import { db } from './db'

const name = 'mirror'

db
  .run(sql`select * from things where name = ${name}`)
  .then(queryResult => console.debug(queryResult))

The same method accepts dynamic queries too. Parameter SQL injection is prevented in the same way as for static queries.

import { db } from './db'

const dynamicCriteria = true ? 'name = $1' : 'description = $1'

const name = 'mirror'

db
  .run({
    sql: `select * from things where ${dynamicCriteria}`,
    params: [name]
  })
  .then(queryResult => console.debug(queryResult))

webpack loader

In order to enable compile-time checks of static SQL queries, the webpack loader must be run before your TypeScript loader of choice. In webpack, this means placing it after said TypeScript loader in the webpack configuration. Note that in order to write the webpack configuration in TypeScript, as well as to be able to reference loaders written in TypeScript directly, ts-node is needed as a dependency.

import { Configuration } from 'webpack'

const webpackConfig: Configuration = {
  entry: './example.ts',
  target: 'node',
  module: {
    rules: [{
      test: /\.ts$/,
      use: ['ts-loader', './webpack-loader.ts']
    }]
  },
  resolve: {
    extensions: ['.ts']
  }
}

export default webpackConfig