Skip to content

Commit f57b44d

Browse files
committed
feat: singularize embedded field names
1 parent 3dc8fe4 commit f57b44d

File tree

4 files changed

+7
-3
lines changed

4 files changed

+7
-3
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
"author": "",
1313
"license": "ISC",
1414
"devDependencies": {
15+
"@types/inflected": "^2.1.3",
1516
"esbuild": "^0.19.5",
1617
"typescript": "^5.2.2"
1718
},
1819
"workspaces": ["test"],
1920
"dependencies": {
2021
"@bufbuild/protobuf": "^1.4.2",
22+
"inflected": "^2.1.0",
2123
"javy": "^0.1.2"
2224
}
2325
}

src/app.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929

3030
import { Driver as PostgresDriver } from "./drivers/postgres";
3131
import { argName, colName } from "./drivers/utlis";
32+
import { singularize } from "inflected";
3233

3334
// Read input from stdin
3435
const input = readInput();
@@ -354,7 +355,7 @@ function rowDecl(
354355

355356
return factory.createPropertySignature(
356357
undefined,
357-
factory.createIdentifier(column.name),
358+
factory.createIdentifier(singularize(column.name)),
358359
undefined,
359360
factory.createTypeLiteralNode(
360361
embed.map((column, j) =>

src/drivers/postgres.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010

1111
import type { Column, Parameter } from "../gen/plugin/codegen_pb";
1212
import { argName, colName } from "./utlis";
13+
import { singularize } from "inflected";
1314

1415
const typeMapping = {
1516
string: [
@@ -476,7 +477,7 @@ export class Driver {
476477
return factory.createObjectLiteralExpression(
477478
columns.map((col, i) =>
478479
factory.createPropertyAssignment(
479-
factory.createIdentifier(colName(i, col)),
480+
factory.createIdentifier(singularize(colName(i, col))),
480481
!embeds.has(col.name)
481482
? this.buildColumnAccessExpression(col, "row", i)
482483
: factory.createObjectLiteralExpression(

test/cases/embed.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("sqlc.embed", () => {
1414
console.log(result);
1515
expect(result.em1).toBe("[email protected]");
1616
expect(result.em1).toBeTypeOf("string");
17-
expect(result.authors).toMatchObject({
17+
expect(result.author).toMatchObject({
1818
id: expect.any(Number),
1919
name: "John Doe",
2020
bio: "A mysterious author",

0 commit comments

Comments
 (0)