Skip to the content.

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.

Table of Contents

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);

Features

API

This section provides detailed documentation for each API method available in the pg-raw library. These methods allow you to construct SQL queries for interacting with PostgreSQL databases.

findMany

Overview

The findMany function generates a PostgreSQL SELECT query to retrieve multiple records from a database table. It allows for specifying selection criteria, filtering conditions, and various query modifiers to fine-tune the data retrieval process.

Syntax

findMany(params: FindManyParams): string

Parameters

params (FindManyParams) is an object with the following structure:

insertOne

Overview

The insertOne function generates a PostgreSQL INSERT query to add a single record to a database table.

Syntax

insertOne(params: InsertOneParams): string

Parameters

insertMany

Overview

The insertMany function generates a PostgreSQL INSERT query to add multiple records to a database table in a single operation.

Syntax

insertMany(params: InsertManyParams): string

Parameters

Overview

The updateMany generates a PostgreSQL UPDATE query for modifying records in a database table. It allows users to specify new data for records and conditions for selecting which records to update.

Syntax

updateMany(params: UpdateManyParams): string

Parameters

deleteMany

Overview

The deleteMany function generates a PostgreSQL DELETE query to remove multiple records from a database table based on specified conditions.

Syntax

deleteMany(params: DeleteManyParams): string

Parameters

License

This package is MIT licensed