@the-neon/pg
v0.2.4
Published
PostgreSQL client for Neon with transaction support
Readme
@the-neon/pg
PostgreSQL client for Neon with transaction support.
Install
npm install @the-neon/pgUsage
import PostgresDB from "@the-neon/pg";
const db = new PostgresDB("postgresql://user:pass@host:5432/db");
// Transactions
await db.start();
await db.commit();
await db.rollback();
// Query
await db.query("SELECT * FROM users WHERE id = $1", [1]);
await db.queryAsJson("SELECT * FROM users");
// CRUD
await db.insert<User>("users", { name: "Alice", email: "[email protected]" });
await db.update<User>("users", { id: 1 }, { name: "Alice Updated" });
await db.delete<User>("users", { id: 1 });
// Fetch
await db.getOne<User>("SELECT * FROM users WHERE id = $1", [1]);
await db.getMany<User>("SELECT * FROM users");
await db.getById<User>("users", 1);
await db.getByIds<User>("users", [1, 2, 3]);
await db.getCount("users", { tenantId: "abc" });API
Transactions
start()— Begin transactioncommit()— Commit transactionrollback()— Rollback transaction
Query
query(sql, args?)— Execute raw SQL, returns rowCount and rowsqueryAsJson(sql, args?)— Execute and return results as JSONexecute(sql, args?)— Execute without return (INSERT/UPDATE/DELETE), returns rowCount
CRUD
insert(table, columns)— Insert row, returns inserted recordupdate(table, filter, columns)— Update rows matching filter, returns updated recorddelete(table, filter)— Delete rows matching filter, returns deleted record
Fetch
getOne(sql, args?)— Fetch single rowgetMany(sql, args?)— Fetch multiple rowsgetById(table, id, filter?)— Fetch by id with optional filtergetByIds(table, ids, filter?)— Fetch by multiple ids with optional filtergetCount(table, filter)— Get row count with filter
