From 0588546735f03fd4fcb716bff9a352fbc585fd84 Mon Sep 17 00:00:00 2001 From: Adel Salakh Date: Sun, 29 Sep 2019 01:36:42 +0300 Subject: [PATCH] Annotate example queries --- packages/example/src/books/queries.ts | 14 +++++++++++--- packages/example/src/books/queries.types.ts | 10 +++++----- packages/example/src/index.ts | 1 - packages/example/src/users/queries.ts | 18 ++++++++++++++---- packages/example/src/users/queries.types.ts | 9 +++++---- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/packages/example/src/books/queries.ts b/packages/example/src/books/queries.ts index d7041168..486b7776 100644 --- a/packages/example/src/books/queries.ts +++ b/packages/example/src/books/queries.ts @@ -1,5 +1,13 @@ -const sql = (_: TemplateStringsArray) => null; +import sql from "@pg-typed/query"; +import { + ISelectAllBooksParams, ISelectAllBooksResult, + IDeleteBooksResult, IDeleteBooksParams, +} from "./queries.types"; -export const SELECT_ALL_BOOKS = sql`select * from books`; +export const selectAllBooks = sql< + ISelectAllBooksResult, ISelectAllBooksParams + >`select * from books`; -export const DELETE_BOOKS = sql`delete from books * where id = $id`; +export const deleteBooks = sql< + IDeleteBooksResult, IDeleteBooksParams + >`delete from books * where id = $id`; diff --git a/packages/example/src/books/queries.types.ts b/packages/example/src/books/queries.types.ts index 8261ad79..b32cd334 100644 --- a/packages/example/src/books/queries.types.ts +++ b/packages/example/src/books/queries.types.ts @@ -1,19 +1,19 @@ -/** Types generated for queries found in "./src/books/queries.ts" */ +/** Types generated for queries found in "src/books/queries.ts" */ -/** 'SELECT_ALL_BOOKS' parameters type */ +/** 'selectAllBooks' parameters type */ export type ISelectAllBooksParams = void; -/** 'SELECT_ALL_BOOKS' return type */ +/** 'selectAllBooks' return type */ export interface ISelectAllBooksResult { id: string; name: string | null; uid: string | null; } -/** 'DELETE_BOOKS' parameters type */ +/** 'deleteBooks' parameters type */ export interface IDeleteBooksParams { id: string | null; } -/** 'DELETE_BOOKS' return type */ +/** 'deleteBooks' return type */ export type IDeleteBooksResult = void; diff --git a/packages/example/src/index.ts b/packages/example/src/index.ts index 8ae316aa..abebc4b2 100644 --- a/packages/example/src/index.ts +++ b/packages/example/src/index.ts @@ -9,7 +9,6 @@ const client = new Client({ async function main() { await client.connect(); - const x = await client.query("select id, name from users where age in ($1, $2)", [34, 45]); const users = await selectAllUsers.run({ ages: [34, 45], }, client); diff --git a/packages/example/src/users/queries.ts b/packages/example/src/users/queries.ts index 4afaf664..9a87ecf9 100644 --- a/packages/example/src/users/queries.ts +++ b/packages/example/src/users/queries.ts @@ -1,8 +1,18 @@ import sql from "@pg-typed/query"; -import { ISelectAllUsersParams, ISelectAllUsersResult } from "./queries.types"; +import { + ISelectAllUsersParams, ISelectAllUsersResult, + IInsertUsersParams, IInsertUsersResult, + ISelectUserIdsParams, ISelectUserIdsResult, +} from "./queries.types"; -export const selectAllUsers = sql`select id, name from users where age in $$ages`; +export const selectAllUsers = sql< + ISelectAllUsersResult, ISelectAllUsersParams + >`select id, name from users where age in $$ages`; -export const insertUsers = sql`insert into users (name, age) values $users(name, age)`; +export const insertUsers = sql< + IInsertUsersResult, IInsertUsersParams + >`insert into users (name, age) values $users(name, age) returning id, name`; -export const selectUserIds = sql`select id, note, age from users where id = $id and age = $age`; +export const selectUserIds = sql< + ISelectUserIdsResult, ISelectUserIdsParams + >`select id from users where id = $id and age = $age`; diff --git a/packages/example/src/users/queries.types.ts b/packages/example/src/users/queries.types.ts index 50770830..cfd54715 100644 --- a/packages/example/src/users/queries.types.ts +++ b/packages/example/src/users/queries.types.ts @@ -1,4 +1,4 @@ -/** Types generated for queries found in "./src/users/queries.ts" */ +/** Types generated for queries found in "src/users/queries.ts" */ /** 'selectAllUsers' parameters type */ export interface ISelectAllUsersParams { @@ -20,7 +20,10 @@ export interface IInsertUsersParams { } /** 'insertUsers' return type */ -export type IInsertUsersResult = void; +export interface IInsertUsersResult { + id: string; + name: string; +} /** 'selectUserIds' parameters type */ export interface ISelectUserIdsParams { @@ -31,6 +34,4 @@ export interface ISelectUserIdsParams { /** 'selectUserIds' return type */ export interface ISelectUserIdsResult { id: string; - note: string | null; - age: number; }