Queries in SQL files
Having installed and configured PgTyped it is now time to write some queries.
Lets create our first query in books/queries.sql
:
books/queries.sql
Notice the comment above the SQL query. PgTyped uses such comments to give generated query functions meaningful names.
If PgTyped is running in watch mode, it will automatically parse the SQL file on each change, extracting all queries and generating strictly typed TS queries in books/queries.ts
:
books/queries.ts
Query findBookById
is now statically typed, with types inferred from the PostgreSQL schema.
This generated query can be imported and executed as follows:
index.ts
For more information on writing queries in SQL files check out the Annotated SQL guide.