Import the full power of PostgreSQL as a TypeScript library.
Developer Preview: Typegres is experimental and not production-ready. The API is evolving rapidly. Try the playground and star the repo to follow along!
This project is evolving quickly! The code examples in this README are correct, but the playground contains the very latest, most ergonomic API I'm working on.
# Install Typegres
npm install typegres
// Import the typegres library
import { typegres, select } from "typegres";
// Import your schema definition
import db from "./schema";
const tg = typegres({
/* Your db connection options */
});
const activeUsers = await select(
(u) => ({
upper: u.name.upper(),
isAdult: u.age[">"](18),
}),
{
from: db.users,
where: (u) => u.isActive,
}
).execute(tg);
console.log(activeUsers);
// Output: [{ upper: 'ALICE', isAdult: true }, { upper: 'CHARLIE', isAdult: false }]
See the examples directory for complete working examples.
While traditional ORMs and query builders abstract over multiple SQL dialects, Typegres goes all-in on PostgreSQL to provide the most powerful and type-safe experience possible. In a single import, you can access the full power of Postgres with complete TypeScript type safety.
Focus on learning Postgres itself โ Typegres just gives you autocomplete, type-checking, and all other benefits of TypeScript.
// Find all authors who have published more than 10 posts
const authorCounts = select(
(p) => ({
author_id: p.author_id,
postCount: p.id.count(),
}),
{
from: db.posts,
groupBy: (p) => [p.author_id],
}
);
const prolificAuthors = await select(
(ac, { u }) => ({
id: u.id,
name: u.name,
totalPosts: ac.postCount,
}),
{
from: authorCounts.asFromItemjoin(db.users, "u", (ac, { u }) => ac.author_id["="](u.id)),
where: (ac) => ac.postCount[">"](10),
}
).execute(tg);
// Type of prolificAuthors is { id: number; name: string; totalPosts: bigint }[]
๐งช Current Features (Developer Preview)
The project is currently in an early but powerful state. The core foundation is in place:
๐ Road to v1.0: Production Readiness
The immediate priority is building a rock-solid foundation to make Typegres stable and ready for production use. This includes:
๐ญ Long-Term Vision: A New Data Layer
Once that stable v1.0 foundation is in place, the roadmap will focus on solving deeper, more fundamental problems that can make significant headway into resolving the object-relational impedance mismatch.
src/
- Main library source codesrc/gen/
- Auto-generated PostgreSQL types and functionssite/
- Documentation website and interactive playgroundRequirements:
nix
package managerdirenv
for automatic environment setupTo contribute, clone the repository (run nix develop
if you don't have direnv
set up) and run:
# Install dependencies
npm install
# Start custom PostgreSQL instance:
./start_postgres.sh
# Run the codegen script to generate types and functions
npm run codegen
# Run tests
npm test
# Type check the code
npm run typecheck
# Build the library
npm run build
MIT ยฉ Ryan Rasti