pgbuddy
v1.0.3
Published
A lightweight set of helper functions for Postgres.js to simplify simple CRUD operations.
Maintainers
Readme
PgBuddy
A no-nonsense, type-safe, tiny query builder wrapping postgres.js.
Installation
npm install pgbuddy postgresZod is optional but recommended for runtime validation:
npm install zodQuick start
With Zod (recommended)
import postgres from "postgres";
import { PgBuddy } from "pgbuddy";
import { z } from "zod";
const sql = postgres(process.env.DATABASE_URL);
const db = new PgBuddy(sql);
const UserSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string().email(),
});
const users = db.table("users", UserSchema);
// Create
const user = await users.create({ name: "Alice", email: "[email protected]" });
// Read
const all = await users.findMany();
const one = await users.findFirst({ where: { email: "[email protected]" } });
// Update
await users.update({ where: { id: user.id }, data: { name: "Alice Smith" } });
// Delete
await users.delete({ where: { id: user.id } });Without Zod
import postgres from "postgres";
import { PgBuddy, Insertable } from "pgbuddy";
const sql = postgres(process.env.DATABASE_URL);
const db = new PgBuddy(sql);
interface User {
id: number;
name: string;
email: string;
}
type UserInsert = Insertable<User, "id">;
const users = db.table<User, UserInsert>("users");
// Create
const user = await users.create({ name: "Alice", email: "[email protected]" });
// Read
const all = await users.findMany();
const one = await users.findFirst({ where: { email: "[email protected]" } });
// Update
await users.update({ where: { id: user.id }, data: { name: "Alice Smith" } });
// Delete
await users.delete({ where: { id: user.id } });Tip: Use Kanel to auto-generate TypeScript interfaces directly from your database schema.
