adelsz/pgtyped
adelsz/pgtyped has 3,284 GitHub stars and 120 forks. GitHot measured 0 stars in 24 hours and -2 in 7 days. Last checked 2026-11-11 06:34 UTC. Last push 2026-10-31.
pgTyped - Typesafe SQL in TypeScript
What you can do
- Automatically generates TS types for parameters/results of SQL queries of any complexity.
- Supports extracting and typing queries from both SQL and TS files.
- Generate query types as you write them, using watch mode.
- Useful parameter interpolation helpers for arrays and objects.
Crecimiento de estrellas medido
- in 24h
- 0
- in 7 days
- -2
- in 15 days
- Recopilando historial
- in 1 month
- Recopilando historial
Cambios netos en períodos que se solapan, no totales diarios. Los datos no disponibles se dejan en blanco. Las estrellas indican interés, no calidad.
README.md
Original
PgTyped
PgTyped makes it possible to use raw SQL in TypeScript with guaranteed type-safety.
No need to map or translate your DB schema to TypeScript, PgTyped automatically generates types and interfaces for your SQL queries by using your running Postgres database as the source of type information.
Features:
- Automatically generates TS types for parameters/results of SQL queries of any complexity.
- Supports extracting and typing queries from both SQL and TS files.
- Generate query types as you write them, using watch mode.
- Useful parameter interpolation helpers for arrays and objects.
- No need to define your DB schema in TypeScript, your running DB is the live source of type data.
- Prevents SQL injections by not doing explicit parameter substitution. Instead, queries and parameters are sent separately to the DB driver, allowing parameter substitution to be safely done by the PostgreSQL server.
- Native ESM support. Runtime dependencies are also provided as CommonJS.
Documentation
Visit our documentation page at https://pgtyped.dev/
Getting started
npm install -D @pgtyped/cli typescript(typescript is a required peer dependency for pgtyped)npm install @pgtyped/runtime(@pgtyped/runtimeis the only required runtime dependency of pgtyped)- Create a PgTyped
config.jsonfile. - Run
npx pgtyped -w -c config.jsonto start PgTyped in watch mode.
More info on getting started can be found in the Getting Started page. You can also refer to the example app for a preconfigured example.
Example
Lets save some queries in books.sql:
/* @name FindBookById */
SELECT * FROM books WHERE id = :bookId;
PgTyped parses the SQL file, extracting all queries and generating strictly typed TS queries in books.queries.ts:
/** Types generated for queries found in "books.sql" */
//...
/** 'FindBookById' parameters type */
export interface IFindBookByIdParams {
bookId: number | null;
}
/** 'FindBookById' return type */
export interface IFindBookByIdResult {
id: number;
rank: number | null;
name: string | null;
author_id: number | null;
}
/**
* Query generated from SQL:
* SELECT * FROM books WHERE id = :bookId
*/
export const findBookById = new PreparedQuery<
IFindBookByIdParams,
IFindBookByIdResult
>(...);
Query findBookById is now statically typed, with types inferred from the PostgreSQL schema.
This generated query can be imported and executed as follows:
import { Client } from 'pg';
import { findBookById } from './books.queries';
export const client = new Client({
host: 'localhost',
user: 'test',
password: 'example',
database: 'test',
});
async function main() {
await client.connect();
const books = await findBookById.run(
{
bookId: 5,
},
client,
);
console.log(`Book name: ${books[0].name}`);
await client.end();
}
main();
Resources
- Configuring pgTyped
- Writing queries in SQL files
- Advanced queries and parameter expansions in SQL files
- Writing queries in TS files
- Advanced queries and parameter expansions in TS files
Project state:
This project is being actively developed and its APIs might change. All issue reports, feature requests and PRs appreciated.
License
Copyright (c) 2019-present, Adel Salakh
Explorar proyectos relacionados
Asabeneh/30-Days-Of-Python
El reto de programación 30 Days of Python es una guía paso a paso para aprender el lenguaje de programación Python en 30 días. Este reto puede llevar más de 100 días. Sigue tu propio ritmo. Estos vídeos también pueden ayudarte: https://www.youtube.com/channel/UC7PNRuno1rzYPb1xLa4yktw
78,560 estrellasgrafana/grafana
Plataforma abierta y componible de observabilidad y visualización de datos. Visualiza métricas, registros y trazas de múltiples fuentes, como Prometheus, Loki, Elasticsearch, InfluxDB, Postgres y muchas más.
77,830 estrellasredis/redis
Para los desarrolladores que crean aplicaciones en tiempo real basadas en datos, Redis es la opción preferida, la más rápida y la de más funciones como caché, servidor de estructuras de datos y motor de consultas de documentos y vectores.
77,395 estrellasapache/superset
Apache Superset es una plataforma de visualización y exploración de datos.
75,473 estrellas