All Projects → adelsz → Pgtyped

adelsz / Pgtyped

Licence: mit
pgTyped - Typesafe SQL in TypeScript

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Pgtyped

Sqliterally
Lightweight SQL query builder
Stars: ✭ 231 (-86.68%)
Mutual labels:  query, postgres
Stator
Stator, your go-to template for the perfect stack. 😍🙏
Stars: ✭ 217 (-87.49%)
Mutual labels:  postgres, generator
Jsonb accessor
Adds typed jsonb backed fields to your ActiveRecord models.
Stars: ✭ 558 (-67.82%)
Mutual labels:  query, postgres
Query Translator
Query Translator is a search query translator with AST representation
Stars: ✭ 165 (-90.48%)
Mutual labels:  query, generator
Database-Web-API
Dynamically generate RESTful APIs from the contents of a database table. Provides JSON, XML, and HTML. Supports most popular databases
Stars: ✭ 37 (-97.87%)
Mutual labels:  postgres, query
Loukoum
A simple SQL Query Builder
Stars: ✭ 305 (-82.41%)
Mutual labels:  query, postgres
Laravel Graphql
GraphQL implementation with power of Laravel
Stars: ✭ 56 (-96.77%)
Mutual labels:  query, generator
Nancy
Fully automated database experiments. THIS IS A MIRROR OF https://gitlab.com/postgres.ai/nancy
Stars: ✭ 100 (-94.23%)
Mutual labels:  postgres
Simple recommender
A simple recommendation engine for Rails/Postgres
Stars: ✭ 101 (-94.18%)
Mutual labels:  postgres
Pgcli
Postgres CLI with autocompletion and syntax highlighting
Stars: ✭ 9,985 (+475.84%)
Mutual labels:  postgres
Teleport
Certificate authority and access plane for SSH, Kubernetes, web apps, databases and desktops
Stars: ✭ 10,602 (+511.42%)
Mutual labels:  postgres
Quiz
Example real time quiz application with .NET Core, React, DDD, Event Sourcing, Docker and built-in infrastructure for CI/CD with k8s, jenkins and helm
Stars: ✭ 100 (-94.23%)
Mutual labels:  postgres
Morpheus
Reactive type-safe Scala driver for SQL databases
Stars: ✭ 101 (-94.18%)
Mutual labels:  postgres
Oxidizer
📦 A Rust ORM based on tokio-postgres and refinery
Stars: ✭ 100 (-94.23%)
Mutual labels:  postgres
Ehealth.api
Index page and integration layer for projects that related to Ukrainian Health Services government institution
Stars: ✭ 103 (-94.06%)
Mutual labels:  postgres
Node Postgres
PostgreSQL client for node.js.
Stars: ✭ 10,061 (+480.22%)
Mutual labels:  postgres
Pg flame
A flamegraph generator for Postgres EXPLAIN ANALYZE output.
Stars: ✭ 1,391 (-19.78%)
Mutual labels:  postgres
Npgsql.fsharp.analyzer
F# analyzer that provides embedded SQL syntax analysis, type-checking for parameters and result sets and nullable column detection when writing queries using Npgsql.FSharp.
Stars: ✭ 103 (-94.06%)
Mutual labels:  postgres
Laravel Table
Generate tables from Eloquent models.
Stars: ✭ 101 (-94.18%)
Mutual labels:  generator
Thispersondoesnotexist Js
Api for https://thispersondoesnotexist.com Generates an image of a person that does not exist in real life
Stars: ✭ 101 (-94.18%)
Mutual labels:  generator

PgTyped

Version Actions Status

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:

  1. Automatically generates TS types for parameters/results of SQL queries of any complexity.
  2. Supports extracting and typing queries from both SQL and TS files.
  3. Generate query types as you write them, using watch mode.
  4. Useful parameter interpolation helpers for arrays and objects.
  5. No need to define your DB schema in TypeScript, your running DB is the live source of type data.
  6. 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.

Documentation

Visit our new documentation page at https://pgtyped.now.sh/

Getting started

  1. npm install @pgtyped/cli @pgtyped/query typescript (typescript is a required peer dependency for pgtyped)
  2. Create a PgTyped config.json file.
  3. Run npx pgtyped -w -c config.json to start PgTyped in watch mode.

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

  1. Configuring Pgtyped
  2. Writing queries in SQL files
  3. Advanced queries and parameter expansions in SQL files
  4. Writing queries in TS files
  5. 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

MIT

Copyright (c) 2019-present, Adel Salakh

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].