Skip to content

Commit

Permalink
Annotate example queries
Browse files Browse the repository at this point in the history
  • Loading branch information
adelsz committed Sep 28, 2019
1 parent ea12da8 commit 0588546
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
14 changes: 11 additions & 3 deletions packages/example/src/books/queries.ts
Original file line number Diff line number Diff line change
@@ -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`;
10 changes: 5 additions & 5 deletions packages/example/src/books/queries.types.ts
Original file line number Diff line number Diff line change
@@ -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;
1 change: 0 additions & 1 deletion packages/example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 14 additions & 4 deletions packages/example/src/users/queries.ts
Original file line number Diff line number Diff line change
@@ -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<ISelectAllUsersResult, ISelectAllUsersParams>`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`;
9 changes: 5 additions & 4 deletions packages/example/src/users/queries.types.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand All @@ -31,6 +34,4 @@ export interface ISelectUserIdsParams {
/** 'selectUserIds' return type */
export interface ISelectUserIdsResult {
id: string;
note: string | null;
age: number;
}

0 comments on commit 0588546

Please sign in to comment.