Skip to content

Latest commit

Β 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

psqljs 🐘

A lightweight Node.js utility for working with PostgreSQL without the boilerplate.

psqljs wraps pg (Pool) with simple, reusable helpers for querying, CRUD, and table management β€” so you don't have to rewrite connection and query logic for every project.

⚠️ Beta: psqljs is currently under development. The API may change and is not yet recommended for production.

Features

  • πŸ”Œ Zero-config pool via dotenv + pg (db.js:7)
  • πŸ’¬ Raw SQL via Query(sql, values) with parameterized values
  • πŸ” Select helpers: findAll, findOne, findMany
  • ✏️ CRUD helpers: Insert, Update, Remove (with RETURNING *)
  • πŸ—„οΈ Table helpers: CreateTable, DeleteTable, TableExists, GetTables, GetColumns
  • πŸ“¦ ESM-first (package.json:13 β€” "type": "module")

Installation

npm install psqljs
# not published yet β€” for local development:
git clone https://github.com/your-org/psqljs.git
cd psqljs
npm install

Requirements: Node.js >= 18, PostgreSQL, pg@^8.23.0, dotenv@^17.4.2.

Configuration

psqljs reads connection info from environment variables via dotenv in db.js:1-13.

Create a .env in the project root:

host=localhost
port=5432
database=mydb
user=postgres
password=your_password

These map directly to new Pool({ host, port, database, user, password }) in db.js:7-13. db.js:15-16 exports both named pool and default for advanced use.

.env is gitignored ( .gitignore:2).

Quick Start

import psql from "./Func/Main.js";

// 1. Test connection
if (await psql.testConnection()) {
  console.log("Connection successful");
}

// 2. Raw query
const rows = await psql.Query("SELECT * FROM users WHERE id = $1", [1]);

// 3. CRUD helpers
const user = await psql.Insert("users", { name: "Ada", email: "ada@example.com" });
const found = await psql.findOne("users", "email", "ada@example.com");
const updated = await psql.Update("users", { name: "Ada Lovelace" }, "id", user.id);
const removed = await psql.Remove("users", "id", user.id);

Or import helpers directly:

import { Query } from "./Func/Query.js";
import { Insert } from "./Func/Insert.js";
import { CreateTable } from "./Func/Table.js";

API Reference

All helpers are re-exported from Func/Main.js:19-36.

Core

Query(sql, values = []) β€” Func/Query.js:3

Execute raw parameterized SQL. Returns result.rows.

import { Query } from "./Func/Query.js";

await Query("SELECT * FROM users WHERE id = $1", [1]);
await Query("SELECT 1"); // -> [{ "?column?": 1 }]

testConnection() β€” Func/Connection.js:4

Runs SELECT 1 via Query. Returns true if rows returned, false on error.

import { testConnection } from "./Func/Connection.js";
await testConnection(); // true | false

Note: testConnection calls pool.end() in finally (Func/Connection.js:14). After calling it, the pool is closed and subsequent queries will fail unless you recreate the pool. Avoid using it in long-lived servers β€” use it for CLI checks / index.js:4.

Select Helpers β€” Func/Select.js

Function Signature Description Returns
findAll findAll(table) SELECT * FROM table rows[]
findOne findOne(table, column, value) SELECT ... WHERE column = $1 LIMIT 1 row | null
findMany findMany(table, column, value) SELECT ... WHERE column = $1 rows[]
await findAll("users");
await findOne("users", "id", 42);        // null if not found
await findMany("users", "role", "admin");

Table/column names are interpolated (Func/Select.js:4,11,20). Pass trusted values only β€” they are not parameterized.

Write Helpers

Insert(table, data) β€” Func/Insert.js:3

Inserts one row. data is an object { column: value }. Builds $1, $2... placeholders and returns RETURNING *.

await Insert("users", { name: "Grace", email: "grace@example.com" });
// -> { id: 1, name: "Grace", email: "grace@example.com", ... }

Update(table, data, whereColumn, whereValue) β€” Func/Update.js:3

Updates rows matching whereColumn = whereValue. data is the SET object. Returns first updated row or null.

await Update("users", { name: "Grace Hopper", role: "admin" }, "id", 1);

Remove(table, column, value) β€” Func/Delete.js:3

Deletes rows matching column = $1. Returns deleted row or null.

await Remove("users", "id", 1);

Table Helpers β€” Func/Table.js

Function Signature Description
CreateTable CreateTable(table, columns) CREATE TABLE "table" (...) β€” columns is { name: "TEXT NOT NULL", age: "INT" } (Func/Table.js:3)
DeleteTable DeleteTable(table) DROP TABLE "table" (Func/Table.js:15)
TableExists TableExists(table) Checks information_schema.tables (Func/Table.js:19), returns boolean
GetTables GetTables() Lists BASE TABLEs in public schema (Func/Table.js:31)
GetColumns GetColumns(table) Lists column_name, data_type, is_nullable, column_default for table (Func/Table.js:41)
await CreateTable("users", {
  id: "SERIAL PRIMARY KEY",
  name: "TEXT NOT NULL",
  email: "TEXT UNIQUE NOT NULL",
  created_at: "TIMESTAMP DEFAULT NOW()"
});

await TableExists("users"); // true
await GetTables();          // [{ table_name: "users" }, ...]
await GetColumns("users");  // [{ column_name: "id", data_type: "integer", ... }]
await DeleteTable("users");

Project Structure

psqljs/
β”œβ”€β”€ db.js              # pg Pool setup from .env
β”œβ”€β”€ index.js           # Demo: testConnection()
β”œβ”€β”€ Func/
β”‚   β”œβ”€β”€ Main.js        # Default export aggregating all helpers
β”‚   β”œβ”€β”€ Connection.js  # testConnection()
β”‚   β”œβ”€β”€ Query.js       # Query()
β”‚   β”œβ”€β”€ Select.js      # findAll / findOne / findMany
β”‚   β”œβ”€β”€ Insert.js      # Insert()
β”‚   β”œβ”€β”€ Update.js      # Update()
β”‚   β”œβ”€β”€ Delete.js      # Remove()
β”‚   └── Table.js       # CreateTable / DeleteTable / TableExists / GetTables / GetColumns
β”œβ”€β”€ package.json       # ESM, pg, dotenv
└── .env               # host, port, database, user, password (gitignored)

How It Works

  1. db.js creates a singleton Pool from env vars.
  2. Every helper imports pool and calls pool.query(...).
  3. Query is the primitive β€” all other helpers build on top of it or pool directly.
  4. See index.js:1-11 for a minimal connection check.

Status & Roadmap

🚧 Beta / Early Development β€” API is stabilizing.

  • Raw query + select helpers
  • Insert / Update / Delete
  • Table management
  • Transactions / BEGIN/COMMIT helper
  • Input validation & safer identifier escaping
  • Connection retry / pool config exposure
  • Tests & CI
  • npm publish

License

MIT β€” see package.json:11 (currently ISC, will be aligned to MIT).

Contributing

PRs welcome. Please keep helpers parameterized where possible and add JSDoc for new functions.

About

Connecting and manipulating your postgresql db using node js

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages