@@ -7,10 +7,10 @@ export module Read {
77SELECT id, name, bio FROM authors
88WHERE id = $1 LIMIT 1` ;
99 export interface GetAuthorArgs {
10- id : string ;
10+ id : number ;
1111 }
1212 export interface GetAuthorRow {
13- id : string ;
13+ id : number ;
1414 name : string ;
1515 bio : string | null ;
1616 }
@@ -33,7 +33,7 @@ WHERE id = $1 LIMIT 1`;
3333SELECT id, name, bio FROM authors
3434ORDER BY name` ;
3535 export interface ListAuthorsRow {
36- id : string ;
36+ id : number ;
3737 name : string ;
3838 bio : string | null ;
3939 }
@@ -48,7 +48,7 @@ ORDER BY name`;
4848SELECT name FROM authors
4949where id = $1 limit 1` ;
5050 export interface GetNameByIdArgs {
51- id : string ;
51+ id : number ;
5252 }
5353 export async function getNameById ( sql : Sql , args : GetNameByIdArgs ) : Promise < string > {
5454 const rows = await sql . unsafe ( getNameByIdQuery , [ args . id ] ) . values ( ) ;
@@ -61,12 +61,25 @@ where id = $1 limit 1`;
6161 }
6262 return row [ 0 ] ;
6363 }
64+ const getCountQuery = `-- name: GetCount :one
65+ SELECT count(id) FROM authors` ;
66+ export async function getCount ( sql : Sql ) : Promise < number > {
67+ const rows = await sql . unsafe ( getCountQuery , [ ] ) . values ( ) ;
68+ if ( rows . length !== 1 ) {
69+ throw new Error ( `expected 1 row, got ${ rows . length } ` ) ;
70+ }
71+ const row = rows [ 0 ] ;
72+ if ( ! row ) {
73+ throw new Error ( "query returned empty row" ) ;
74+ }
75+ return row [ 0 ] ;
76+ }
6477 export module Nested {
6578 const listQuery = `-- name: Read_Nested_List :many
6679SELECT id, name, bio FROM authors
6780ORDER BY name` ;
6881 export interface ListRow {
69- id : string ;
82+ id : number ;
7083 name : string ;
7184 bio : string | null ;
7285 }
0 commit comments