Skip to content

Commit b3c798e

Browse files
authored
fix(postgres): Support snake case columns (#8)
1 parent 71b021e commit b3c798e

File tree

3 files changed

+133
-59
lines changed

3 files changed

+133
-59
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ export interface GetAuthorRow{
1515
}
1616

1717
exportasyncfunctiongetAuthor(sql: Sql,args: GetAuthorArgs): Promise<GetAuthorRow|null>{
18-
constrows=awaitsql.unsafe<GetAuthorRow[]>(getAuthorQuery,[args.id]);
18+
constrows=awaitsql.unsafe(getAuthorQuery,[args.id]).values();
1919
if(rows.length!==1){
2020
returnnull;
2121
}
22-
returnrows[0];
22+
constrow=rows[0];
23+
return{
24+
id: row[0],
25+
name: row[1],
26+
bio: row[2]
27+
};
2328
}
2429

2530
exportconstlistAuthorsQuery=`-- name: ListAuthors :many
@@ -33,7 +38,11 @@ export interface ListAuthorsRow{
3338
}
3439

3540
exportasyncfunctionlistAuthors(sql: Sql): Promise<ListAuthorsRow[]>{
36-
returnawaitsql.unsafe<ListAuthorsRow[]>(listAuthorsQuery,[]);
41+
return(awaitsql.unsafe(listAuthorsQuery,[]).values()).map(row=>({
42+
id: row[0],
43+
name: row[1],
44+
bio: row[2]
45+
}));
3746
}
3847

3948
exportconstcreateAuthorQuery=`-- name: CreateAuthor :one
@@ -56,11 +65,16 @@ export interface CreateAuthorRow{
5665
}
5766

5867
exportasyncfunctioncreateAuthor(sql: Sql,args: CreateAuthorArgs): Promise<CreateAuthorRow|null>{
59-
constrows=awaitsql.unsafe<CreateAuthorRow[]>(createAuthorQuery,[args.name,args.bio]);
68+
constrows=awaitsql.unsafe(createAuthorQuery,[args.name,args.bio]).values();
6069
if(rows.length!==1){
6170
returnnull;
6271
}
63-
returnrows[0];
72+
constrow=rows[0];
73+
return{
74+
id: row[0],
75+
name: row[1],
76+
bio: row[2]
77+
};
6478
}
6579

6680
exportconstdeleteAuthorQuery=`-- name: DeleteAuthor :exec

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ export interface GetAuthorRow{
1515
}
1616

1717
exportasyncfunctiongetAuthor(sql: Sql,args: GetAuthorArgs): Promise<GetAuthorRow|null>{
18-
constrows=awaitsql.unsafe<GetAuthorRow[]>(getAuthorQuery,[args.id]);
18+
constrows=awaitsql.unsafe(getAuthorQuery,[args.id]).values();
1919
if(rows.length!==1){
2020
returnnull;
2121
}
22-
returnrows[0];
22+
constrow=rows[0];
23+
return{
24+
id: row[0],
25+
name: row[1],
26+
bio: row[2]
27+
};
2328
}
2429

2530
exportconstlistAuthorsQuery=`-- name: ListAuthors :many
@@ -33,7 +38,11 @@ export interface ListAuthorsRow{
3338
}
3439

3540
exportasyncfunctionlistAuthors(sql: Sql): Promise<ListAuthorsRow[]>{
36-
returnawaitsql.unsafe<ListAuthorsRow[]>(listAuthorsQuery,[]);
41+
return(awaitsql.unsafe(listAuthorsQuery,[]).values()).map(row=>({
42+
id: row[0],
43+
name: row[1],
44+
bio: row[2]
45+
}));
3746
}
3847

3948
exportconstcreateAuthorQuery=`-- name: CreateAuthor :one
@@ -56,11 +65,16 @@ export interface CreateAuthorRow{
5665
}
5766

5867
exportasyncfunctioncreateAuthor(sql: Sql,args: CreateAuthorArgs): Promise<CreateAuthorRow|null>{
59-
constrows=awaitsql.unsafe<CreateAuthorRow[]>(createAuthorQuery,[args.name,args.bio]);
68+
constrows=awaitsql.unsafe(createAuthorQuery,[args.name,args.bio]).values();
6069
if(rows.length!==1){
6170
returnnull;
6271
}
63-
returnrows[0];
72+
constrow=rows[0];
73+
return{
74+
id: row[0],
75+
name: row[1],
76+
bio: row[2]
77+
};
6478
}
6579

6680
exportconstdeleteAuthorQuery=`-- name: DeleteAuthor :exec

‎src/drivers/postgres.ts‎

Lines changed: 95 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import{SyntaxKind,NodeFlags,TypeNode,factory}from"typescript";
22

33
import{Parameter,Column}from"../gen/plugin/codegen_pb";
4-
import{argName}from"./utlis";
4+
import{argName,colName}from"./utlis";
55
import{log}from"../logger";
66

77
exportfunctioncolumnType(column?: Column): TypeNode{
@@ -393,33 +393,62 @@ export function manyDecl(
393393
factory.createBlock(
394394
[
395395
factory.createReturnStatement(
396-
factory.createAwaitExpression(
397-
factory.createCallExpression(
398-
factory.createPropertyAccessExpression(
399-
factory.createIdentifier("sql"),
400-
factory.createIdentifier("unsafe")
396+
factory.createCallExpression(
397+
factory.createPropertyAccessExpression(
398+
factory.createAwaitExpression(
399+
factory.createCallExpression(
400+
factory.createPropertyAccessExpression(
401+
factory.createCallExpression(
402+
factory.createPropertyAccessExpression(
403+
factory.createIdentifier("sql"),
404+
factory.createIdentifier("unsafe")
405+
),
406+
undefined,
407+
[
408+
factory.createIdentifier(queryName),
409+
factory.createArrayLiteralExpression(
410+
params.map((param,i)=>
411+
factory.createPropertyAccessExpression(
412+
factory.createIdentifier("args"),
413+
factory.createIdentifier(argName(i,param.column))
414+
)
415+
),
416+
false
417+
),
418+
]
419+
),
420+
factory.createIdentifier("values"),
421+
),
422+
undefined,
423+
undefined,
424+
)
401425
),
402-
[
403-
factory.createArrayTypeNode(
404-
factory.createTypeReferenceNode(
405-
factory.createIdentifier(returnIface),
406-
undefined
407-
)
408-
),
409-
],
410-
[
411-
factory.createIdentifier(queryName),
412-
factory.createArrayLiteralExpression(
413-
params.map((param,i)=>
414-
factory.createPropertyAccessExpression(
415-
factory.createIdentifier("args"),
416-
factory.createIdentifier(argName(i,param.column))
426+
factory.createIdentifier("map"),
427+
),
428+
undefined,
429+
[
430+
factory.createArrowFunction(
431+
undefined,
432+
undefined,
433+
[
434+
factory.createParameterDeclaration(undefined,undefined,"row"),
435+
],
436+
undefined,
437+
factory.createToken(SyntaxKind.EqualsGreaterThanToken),
438+
factory.createObjectLiteralExpression(
439+
columns.map((col,i)=>
440+
factory.createPropertyAssignment(
441+
factory.createIdentifier(colName(i,col)),
442+
factory.createElementAccessExpression(
443+
factory.createIdentifier("row"),
444+
factory.createNumericLiteral(`${i}`)
445+
)
417446
)
418447
),
419-
false
420-
),
421-
]
422-
)
448+
true
449+
)
450+
),
451+
]
423452
)
424453
),
425454
],
@@ -469,29 +498,29 @@ export function oneDecl(
469498
factory.createAwaitExpression(
470499
factory.createCallExpression(
471500
factory.createPropertyAccessExpression(
472-
factory.createIdentifier("sql"),
473-
factory.createIdentifier("unsafe")
474-
),
475-
[
476-
factory.createArrayTypeNode(
477-
factory.createTypeReferenceNode(
478-
factory.createIdentifier(returnIface),
479-
undefined
480-
)
481-
),
482-
],
483-
[
484-
factory.createIdentifier(queryName),
485-
factory.createArrayLiteralExpression(
486-
params.map((param,i)=>
487-
factory.createPropertyAccessExpression(
488-
factory.createIdentifier("args"),
489-
factory.createIdentifier(argName(i,param.column))
490-
)
501+
factory.createCallExpression(
502+
factory.createPropertyAccessExpression(
503+
factory.createIdentifier("sql"),
504+
factory.createIdentifier("unsafe")
491505
),
492-
false
506+
undefined,
507+
[
508+
factory.createIdentifier(queryName),
509+
factory.createArrayLiteralExpression(
510+
params.map((param,i)=>
511+
factory.createPropertyAccessExpression(
512+
factory.createIdentifier("args"),
513+
factory.createIdentifier(argName(i,param.column))
514+
)
515+
),
516+
false
517+
),
518+
]
493519
),
494-
]
520+
factory.createIdentifier("values")
521+
),
522+
undefined,
523+
undefined,
495524
)
496525
)
497526
),
@@ -519,10 +548,27 @@ export function oneDecl(
519548
),
520549
undefined
521550
),
551+
factory.createVariableStatement(
552+
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+
),
522560
factory.createReturnStatement(
523-
factory.createElementAccessExpression(
524-
factory.createIdentifier("rows"),
525-
factory.createNumericLiteral("0")
561+
factory.createObjectLiteralExpression(
562+
columns.map((col,i)=>
563+
factory.createPropertyAssignment(
564+
factory.createIdentifier(colName(i,col)),
565+
factory.createElementAccessExpression(
566+
factory.createIdentifier("row"),
567+
factory.createNumericLiteral(`${i}`)
568+
)
569+
)
570+
),
571+
true
526572
)
527573
),
528574
],

0 commit comments

Comments
(0)