Skip to content

Latest commit

 

History

History
101 lines (80 loc) · 4.74 KB

codegen-alt.md

File metadata and controls

101 lines (80 loc) · 4.74 KB

Item 58: Consider Codegen as an Alternative to Complex Types

Things to Remember

  • While type-level TypeScript is an impressively powerful tool, it's not always the best tool for the job.
  • For complex type manipulations, consider generating code and types as an alternative to writing type-level code. Your code generation tool can be written in ordinary TypeScript or any other language.
  • Run codegen and git diff on your continuous integration system to make sure generated code stays in sync.## Code Samples
async function getBooks(db: Database) {
  const result = await db.query(
    `SELECT title, author, year, publisher FROM books`
  );
  return result.rows;
}

💻 playground


async function getLatestBookByAuthor(db: Database, publisher: string) {
  const result = await db.query(
    `SELECT author, MAX(year) FROM books GROUP BY author WHERE publisher=$1`,
    [publisher]
  );
  return result.rows;
}

💻 playground


// books-queries.ts
import { sql } from '@pgtyped/runtime';
const selectLatest = sql`
    SELECT author, MAX(year)
    FROM books
    GROUP BY author
    WHERE publisher=$publisher
`;

async function getLatestBookByAuthor(db: Database, publisher: string) {
  const result = await selectLatest.run({publisher}, db);
  //    ^? const result: any[]
  return result;
}

💻 playground


// books-queries.types.ts
/** Types generated for queries found in "books-queries.ts" */

/** 'selectLatest' parameters type */
export interface selectLatestParams {
  publisher: string;
}

/** 'selectLatest' return type */
export interface selectLatestResult {
  author: string;
  year: number;
}

/** 'selectLatest' query type */
export interface selectLatestQuery {
  params: selectLatestParams;
  result: selectLatestResult;
}

💻 playground


// books-queries.ts
import { sql } from '@pgtyped/runtime';
import { selectLatestQuery } from './books-queries.types';
export const selectLatestBookByAuthor = sql<selectLatestQuery>`
    SELECT author, MAX(year)
    FROM books
    GROUP BY author
    WHERE publisher=$publisher
`;

async function getLatestBookByAuthor(db: Database, publisher: string) {
  const result = await selectLatestBookByAuthor.run({publisher}, db);
  //    ^? const result: selectLatestResult[]
  return result;
}

💻 playground