camelToSnake and snakeCamelMapper() drop the underscore when a digit precedes a camelCase boundary

未关闭 适合新手
#4,855 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
postgresql, typescript
领域
api

调研方向

问题出在 packages/typescript-client/src/column-mapper.ts 中的 camelToSnake,其边界正则 /([a-z_])([A-Z])/g 漏掉了数字到大写字母的转换。运行 issue 中的复现步骤,可以看到 sha256Hash 变成了 sha256hash。当 camelToSnake 返回 sha256_hash、oauth2_token、address1_line 和 v2_payload,并且 column-mapper 的测试覆盖了数字边界时,即为完成。

由索引模型根据 Issue 内容生成。

描述

Versions

  • Client: typescript, @electric-sql/client 1.5.28 (also on main 3f9809fb0b, Node v26.13.0)
  • Electric: any (happens client-side before the request reaches Electric)

Bug description

snakeToCamel converts a column like sha256_hash into sha256Hash, and its doc comment states that the transform is round-trippable via camelToSnake. However, camelToSnake in packages/typescript-client/src/column-mapper.ts only inserts _ before an uppercase letter that follows [a-z_], so camelToSnake("sha256Hash") returns "sha256hash".

When ShapeStream or compileExpression uses snakeCamelMapper(), incoming rows are decoded with key sha256Hash, while columns, where, and subset expressions encode sha256Hash as sha256hash, sending a column name that does not exist in Postgres.

// repro.mjs
// npm i @electric-sql/[email protected] && node repro.mjs
import {
  ShapeStream,
  snakeCamelMapper,
  snakeToCamel,
  camelToSnake,
  compileExpression,
} from "@electric-sql/client"

for (const col of ["sha256_hash", "oauth2_token", "address1_line", "v2_payload"]) {
  const camel = snakeToCamel(col)
  console.log(`${col} -> ${camel} -> ${camelToSnake(camel)}`)
}

console.log(
  "compileExpression:",
  compileExpression({ type: "ref", column: "sha256Hash" }, snakeCamelMapper().encode)
)

const fetchClient = async (url) => {
  const u = new URL(url)
  console.log("columns sent:", u.searchParams.get("columns"))
  console.log("where sent:", u.searchParams.get("where"))
  return new Response("[]", { status: 400 })
}

const stream = new ShapeStream({
  url: "http://localhost:3000/v1/shape",
  params: {
    table: "files",
    columns: ["id", "sha256Hash"],
    where: "sha256Hash = $1",
    params: ["abc"],
  },
  columnMapper: snakeCamelMapper(),
  subscribe: false,
  fetchClient,
  onError: () => stream.unsubscribeAll(),
})
stream.subscribe(() => {})

Actual output:

sha256_hash -> sha256Hash -> sha256hash
oauth2_token -> oauth2Token -> oauth2token
address1_line -> address1Line -> address1line
v2_payload -> v2Payload -> v2payload
compileExpression: "sha256hash"
columns sent: "id","sha256hash"
where sent: sha256hash = $1

Expected behavior

camelToSnake should return sha256_hash, oauth2_token, address1_line, and v2_payload, compileExpression should return "sha256_hash", columns should be "id","sha256_hash", and where should be sha256_hash = $1. Changing the first boundary regex in camelToSnake from /([a-z_])([A-Z])/g to /([a-z0-9_])([A-Z])/g preserves the underscore after digits.

Credit: https://ucsbplse.github.io/

主要语言
TypeScript
星标
10.4k
派生
399
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

electric-sql/electric 的其他 Issue

查看 electric-sql/electric 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。