Skip to content

Commit

Permalink
fix: Check for empty row (#18)
Browse files Browse the repository at this point in the history
Fixes a compiler error.
Fixes #6
  • Loading branch information
kyleconroy authored Mar 3, 2024
1 parent 27833be commit 0134a95
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
6 changes: 6 additions & 0 deletions examples/bun-postgres/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise<GetAutho
return null;
}
const row = rows[0];
if (!row) {
return null;
}
return {
id: row[0],
name: row[1],
Expand Down Expand Up @@ -72,6 +75,9 @@ export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<Cr
return null;
}
const row = rows[0];
if (!row) {
return null;
}
return {
id: row[0],
name: row[1],
Expand Down
6 changes: 6 additions & 0 deletions examples/node-postgres/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise<GetAutho
return null;
}
const row = rows[0];
if (!row) {
return null;
}
return {
id: row[0],
name: row[1],
Expand Down Expand Up @@ -72,6 +75,9 @@ export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<Cr
return null;
}
const row = rows[0];
if (!row) {
return null;
}
return {
id: row[0],
name: row[1],
Expand Down
49 changes: 37 additions & 12 deletions src/drivers/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,21 +417,25 @@ export function manyDecl(
),
]
),
factory.createIdentifier("values"),
factory.createIdentifier("values")
),
undefined,
undefined,
undefined
)
),
factory.createIdentifier("map"),
factory.createIdentifier("map")
),
undefined,
[
factory.createArrowFunction(
undefined,
undefined,
[
factory.createParameterDeclaration(undefined, undefined, "row"),
factory.createParameterDeclaration(
undefined,
undefined,
"row"
),
],
undefined,
factory.createToken(SyntaxKind.EqualsGreaterThanToken),
Expand Down Expand Up @@ -510,7 +514,9 @@ export function oneDecl(
params.map((param, i) =>
factory.createPropertyAccessExpression(
factory.createIdentifier("args"),
factory.createIdentifier(argName(i, param.column))
factory.createIdentifier(
argName(i, param.column)
)
)
),
false
Expand All @@ -520,7 +526,7 @@ export function oneDecl(
factory.createIdentifier("values")
),
undefined,
undefined,
undefined
)
)
),
Expand Down Expand Up @@ -550,12 +556,31 @@ export function oneDecl(
),
factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList([
factory.createVariableDeclaration("row", undefined, undefined, factory.createElementAccessExpression(
factory.createIdentifier("rows"),
factory.createNumericLiteral("0")
)),
], NodeFlags.Const)
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
"row",
undefined,
undefined,
factory.createElementAccessExpression(
factory.createIdentifier("rows"),
factory.createNumericLiteral("0")
)
),
],
NodeFlags.Const
)
),
factory.createIfStatement(
factory.createPrefixUnaryExpression(
SyntaxKind.ExclamationToken,
factory.createIdentifier("row")
),
factory.createBlock(
[factory.createReturnStatement(factory.createNull())],
true
),
undefined
),
factory.createReturnStatement(
factory.createObjectLiteralExpression(
Expand Down

0 comments on commit 0134a95

Please sign in to comment.