From cc3351c3a0b87234db9f3c4cc75ad2d6d3a06c0a Mon Sep 17 00:00:00 2001 From: Nao Yonashiro Date: Mon, 25 Dec 2023 21:00:00 +0900 Subject: [PATCH] fix(postgres): support snake case --- examples/bun-postgres/src/db/query_sql.ts | 24 +++- examples/node-postgres/src/db/query_sql.ts | 24 +++- src/drivers/postgres.ts | 144 ++++++++++++++------- 3 files changed, 133 insertions(+), 59 deletions(-) diff --git a/examples/bun-postgres/src/db/query_sql.ts b/examples/bun-postgres/src/db/query_sql.ts index ddb6c66..fc99497 100644 --- a/examples/bun-postgres/src/db/query_sql.ts +++ b/examples/bun-postgres/src/db/query_sql.ts @@ -15,11 +15,16 @@ export interface GetAuthorRow { } export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise { - const rows = await sql.unsafe(getAuthorQuery, [args.id]); + const rows = await sql.unsafe(getAuthorQuery, [args.id]).values(); if (rows.length !== 1) { return null; } - return rows[0]; + const row = rows[0]; + return { + id: row[0], + name: row[1], + bio: row[2] + }; } export const listAuthorsQuery = `-- name: ListAuthors :many @@ -33,7 +38,11 @@ export interface ListAuthorsRow { } export async function listAuthors(sql: Sql): Promise { - return await sql.unsafe(listAuthorsQuery, []); + return (await sql.unsafe(listAuthorsQuery, []).values()).map(row => ({ + id: row[0], + name: row[1], + bio: row[2] + })); } export const createAuthorQuery = `-- name: CreateAuthor :one @@ -56,11 +65,16 @@ export interface CreateAuthorRow { } export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise { - const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]); + const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values(); if (rows.length !== 1) { return null; } - return rows[0]; + const row = rows[0]; + return { + id: row[0], + name: row[1], + bio: row[2] + }; } export const deleteAuthorQuery = `-- name: DeleteAuthor :exec diff --git a/examples/node-postgres/src/db/query_sql.ts b/examples/node-postgres/src/db/query_sql.ts index ddb6c66..fc99497 100644 --- a/examples/node-postgres/src/db/query_sql.ts +++ b/examples/node-postgres/src/db/query_sql.ts @@ -15,11 +15,16 @@ export interface GetAuthorRow { } export async function getAuthor(sql: Sql, args: GetAuthorArgs): Promise { - const rows = await sql.unsafe(getAuthorQuery, [args.id]); + const rows = await sql.unsafe(getAuthorQuery, [args.id]).values(); if (rows.length !== 1) { return null; } - return rows[0]; + const row = rows[0]; + return { + id: row[0], + name: row[1], + bio: row[2] + }; } export const listAuthorsQuery = `-- name: ListAuthors :many @@ -33,7 +38,11 @@ export interface ListAuthorsRow { } export async function listAuthors(sql: Sql): Promise { - return await sql.unsafe(listAuthorsQuery, []); + return (await sql.unsafe(listAuthorsQuery, []).values()).map(row => ({ + id: row[0], + name: row[1], + bio: row[2] + })); } export const createAuthorQuery = `-- name: CreateAuthor :one @@ -56,11 +65,16 @@ export interface CreateAuthorRow { } export async function createAuthor(sql: Sql, args: CreateAuthorArgs): Promise { - const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]); + const rows = await sql.unsafe(createAuthorQuery, [args.name, args.bio]).values(); if (rows.length !== 1) { return null; } - return rows[0]; + const row = rows[0]; + return { + id: row[0], + name: row[1], + bio: row[2] + }; } export const deleteAuthorQuery = `-- name: DeleteAuthor :exec diff --git a/src/drivers/postgres.ts b/src/drivers/postgres.ts index 09635e2..44c4a47 100644 --- a/src/drivers/postgres.ts +++ b/src/drivers/postgres.ts @@ -1,7 +1,7 @@ import { SyntaxKind, NodeFlags, TypeNode, factory } from "typescript"; import { Parameter, Column } from "../gen/plugin/codegen_pb"; -import { argName } from "./utlis"; +import { argName, colName } from "./utlis"; import { log } from "../logger"; export function columnType(column?: Column): TypeNode { @@ -393,33 +393,62 @@ export function manyDecl( factory.createBlock( [ factory.createReturnStatement( - factory.createAwaitExpression( - factory.createCallExpression( - factory.createPropertyAccessExpression( - factory.createIdentifier("sql"), - factory.createIdentifier("unsafe") + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createAwaitExpression( + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier("sql"), + factory.createIdentifier("unsafe") + ), + undefined, + [ + factory.createIdentifier(queryName), + factory.createArrayLiteralExpression( + params.map((param, i) => + factory.createPropertyAccessExpression( + factory.createIdentifier("args"), + factory.createIdentifier(argName(i, param.column)) + ) + ), + false + ), + ] + ), + factory.createIdentifier("values"), + ), + undefined, + undefined, + ) ), - [ - factory.createArrayTypeNode( - factory.createTypeReferenceNode( - factory.createIdentifier(returnIface), - undefined - ) - ), - ], - [ - factory.createIdentifier(queryName), - factory.createArrayLiteralExpression( - params.map((param, i) => - factory.createPropertyAccessExpression( - factory.createIdentifier("args"), - factory.createIdentifier(argName(i, param.column)) + factory.createIdentifier("map"), + ), + undefined, + [ + factory.createArrowFunction( + undefined, + undefined, + [ + factory.createParameterDeclaration(undefined, undefined, "row"), + ], + undefined, + factory.createToken(SyntaxKind.EqualsGreaterThanToken), + factory.createObjectLiteralExpression( + columns.map((col, i) => + factory.createPropertyAssignment( + factory.createIdentifier(colName(i, col)), + factory.createElementAccessExpression( + factory.createIdentifier("row"), + factory.createNumericLiteral(`${i}`) + ) ) ), - false - ), - ] - ) + true + ) + ), + ] ) ), ], @@ -469,29 +498,29 @@ export function oneDecl( factory.createAwaitExpression( factory.createCallExpression( factory.createPropertyAccessExpression( - factory.createIdentifier("sql"), - factory.createIdentifier("unsafe") - ), - [ - factory.createArrayTypeNode( - factory.createTypeReferenceNode( - factory.createIdentifier(returnIface), - undefined - ) - ), - ], - [ - factory.createIdentifier(queryName), - factory.createArrayLiteralExpression( - params.map((param, i) => - factory.createPropertyAccessExpression( - factory.createIdentifier("args"), - factory.createIdentifier(argName(i, param.column)) - ) + factory.createCallExpression( + factory.createPropertyAccessExpression( + factory.createIdentifier("sql"), + factory.createIdentifier("unsafe") ), - false + undefined, + [ + factory.createIdentifier(queryName), + factory.createArrayLiteralExpression( + params.map((param, i) => + factory.createPropertyAccessExpression( + factory.createIdentifier("args"), + factory.createIdentifier(argName(i, param.column)) + ) + ), + false + ), + ] ), - ] + factory.createIdentifier("values") + ), + undefined, + undefined, ) ) ), @@ -519,10 +548,27 @@ export function oneDecl( ), undefined ), + factory.createVariableStatement( + undefined, + factory.createVariableDeclarationList([ + factory.createVariableDeclaration("row", undefined, undefined, factory.createElementAccessExpression( + factory.createIdentifier("rows"), + factory.createNumericLiteral("0") + )), + ], NodeFlags.Const) + ), factory.createReturnStatement( - factory.createElementAccessExpression( - factory.createIdentifier("rows"), - factory.createNumericLiteral("0") + factory.createObjectLiteralExpression( + columns.map((col, i) => + factory.createPropertyAssignment( + factory.createIdentifier(colName(i, col)), + factory.createElementAccessExpression( + factory.createIdentifier("row"), + factory.createNumericLiteral(`${i}`) + ) + ) + ), + true ) ), ],