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

@knaadh/pg-raw

v0.2.0

Published

A schemaless raw query builder for PostgreSQL

Downloads

4

Readme

pg-header

Pg-Raw

NPM Version Codecov CodeFactor Grade GitHub branch status

A modern library for easily generating PostgreSQL raw queries through a clean and simple API.

This isn't an ORM or query executor - it focuses solely on generating SQL strings, allowing you to execute these queries using Knex, Drizzle, Prisma, or your preferred PostgreSQL client or tool.

Installation

You can install the library using your preferred package manager:

# NPM 
npm install @knaadh/pg-raw

# Yarn
yarn add @knaadh/pg-raw

# Bun
bun add @knaadh/pg-raw

Usage

Here’s a simple example showing how to use the pg-raw library:

import { findMany, FindManyParams } from '@knaadh/pg-raw';

// Basic usage example
const params: FindManyParams = {
  table: 'users',
  query: {
    select: {
      id: true,
      name: true,
    },
    where: {
      email: '[email protected]',
    },
    limit: 1,
  },
};

console.log(findMany(params));
// Output: SELECT "id", "name" FROM "users" WHERE "email" = '[email protected]' LIMIT 1

Additionally, you can also define types to ensure code completion and partial type safety

import { findMany, FindManyParams } from '@knaadh/pg-raw';

interface User {
  id: number;
  name: string;
  email: string;
}

const params: FindManyParams<User> = {
  table: 'users',
  query: {
    select: {
      id: true,
      name: true,
    },
    where: {
      email: '[email protected]',
    },
    limit: 1,
  },
};

console.log(findMany(params));
// Output: SELECT "id", "name" FROM "users" WHERE "email" = '[email protected]' LIMIT 1

The examples demonstrate how to construct a query to find a user by their email address, selecting only their id and name, and limiting the result to one row. The first example does not use types, while the second example utilizes types for added code completion and partial type safety.

Executing Generated Queries

To execute the generated queries, you can use any Postgres client such as node-postgres, postgres.js, or ORMs like Prisma that support raw queries.

Here's an example using node-postgres:


import { findMany, type FindManyParams } from "@knaadh/pg-raw";
import { Client } from "pg";

const client = new Client({
	connectionString: process.env.DATABASE_URL,
});
await client.connect();

const params: FindManyParams = {
	table: "employee",
	query: {
		select: {
			id: true,
			first_name: true,
			last_name: true,
		},
		where: {
			gender: "$1",
		},
		limit: 10,
	},
};

const employeesQuery = findMany(params);

const data = await client.query(employeesQuery, ["F"]);

console.log(data.rows);

You can find the full documentation in the repository available on GitHub.

Features

  • Simple and intuitive API for generating PostgreSQL queries
  • Supports generation of SELECT, INSERT, UPDATE, and DELETE queries
  • Flexible query building with support for complex conditions and joins
  • Automatically generates SQL queries to fetch and aggregate relational data into a single column
  • Compatible with all PostgreSQL clients such as node-postgres, postgres.js or ORMs that support raw queries such as Drizzle, Prisma.

License

This package is MIT licensed