Skip to content

Commit 0134a95

Browse files
authored
fix: Check for empty row (#18)
Fixes a compiler error. Fixes #6
1 parent 27833be commit 0134a95

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

examples/bun-postgres/src/db/query_sql.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise<GetAutho
2222
return null;
2323
}
2424
const row = rows[0];
25+
if (!row) {
26+
return null;
27+
}
2528
return {
2629
id: row[0],
2730
name: row[1],
@@ -72,6 +75,9 @@ export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<Cr
7275
return null;
7376
}
7477
const row = rows[0];
78+
if (!row) {
79+
return null;
80+
}
7581
return {
7682
id: row[0],
7783
name: row[1],

examples/node-postgres/src/db/query_sql.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise<GetAutho
2222
return null;
2323
}
2424
const row = rows[0];
25+
if (!row) {
26+
return null;
27+
}
2528
return {
2629
id: row[0],
2730
name: row[1],
@@ -72,6 +75,9 @@ export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise<Cr
7275
return null;
7376
}
7477
const row = rows[0];
78+
if (!row) {
79+
return null;
80+
}
7581
return {
7682
id: row[0],
7783
name: row[1],

src/drivers/postgres.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -417,21 +417,25 @@ export function manyDecl(
417417
),
418418
]
419419
),
420-
factory.createIdentifier("values"),
420+
factory.createIdentifier("values")
421421
),
422422
undefined,
423-
undefined,
423+
undefined
424424
)
425425
),
426-
factory.createIdentifier("map"),
426+
factory.createIdentifier("map")
427427
),
428428
undefined,
429429
[
430430
factory.createArrowFunction(
431431
undefined,
432432
undefined,
433433
[
434-
factory.createParameterDeclaration(undefined, undefined, "row"),
434+
factory.createParameterDeclaration(
435+
undefined,
436+
undefined,
437+
"row"
438+
),
435439
],
436440
undefined,
437441
factory.createToken(SyntaxKind.EqualsGreaterThanToken),
@@ -510,7 +514,9 @@ export function oneDecl(
510514
params.map((param, i) =>
511515
factory.createPropertyAccessExpression(
512516
factory.createIdentifier("args"),
513-
factory.createIdentifier(argName(i, param.column))
517+
factory.createIdentifier(
518+
argName(i, param.column)
519+
)
514520
)
515521
),
516522
false
@@ -520,7 +526,7 @@ export function oneDecl(
520526
factory.createIdentifier("values")
521527
),
522528
undefined,
523-
undefined,
529+
undefined
524530
)
525531
)
526532
),
@@ -550,12 +556,31 @@ export function oneDecl(
550556
),
551557
factory.createVariableStatement(
552558
undefined,
553-
factory.createVariableDeclarationList([
554-
factory.createVariableDeclaration("row", undefined, undefined, factory.createElementAccessExpression(
555-
factory.createIdentifier("rows"),
556-
factory.createNumericLiteral("0")
557-
)),
558-
], NodeFlags.Const)
559+
factory.createVariableDeclarationList(
560+
[
561+
factory.createVariableDeclaration(
562+
"row",
563+
undefined,
564+
undefined,
565+
factory.createElementAccessExpression(
566+
factory.createIdentifier("rows"),
567+
factory.createNumericLiteral("0")
568+
)
569+
),
570+
],
571+
NodeFlags.Const
572+
)
573+
),
574+
factory.createIfStatement(
575+
factory.createPrefixUnaryExpression(
576+
SyntaxKind.ExclamationToken,
577+
factory.createIdentifier("row")
578+
),
579+
factory.createBlock(
580+
[factory.createReturnStatement(factory.createNull())],
581+
true
582+
),
583+
undefined
559584
),
560585
factory.createReturnStatement(
561586
factory.createObjectLiteralExpression(

0 commit comments

Comments
 (0)