From 1db2f0a1db786d848ccc2e31a54db6f4343bf4e2 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 25 Oct 2026 17:21:08 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E4=BF=AE=E6=AD=A3=E6=8A=95=E5=BD=B1?= =?UTF-8?q?=E5=88=AB=E5=90=8D=E9=93=81=E5=BE=8B=EF=BC=88AS=20=E5=BF=85?= =?UTF-8?q?=E9=A1=BB=E7=94=A8=20camelCase=20JSON=20=E9=94=AE=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit row_to_json 用列的输出名当 JSON 键。原 §5.2 示例写的是 `AS creator_time`, 照抄会得到 `"creator_time"` 而 react 是 `"creatorTime"` ⇒ 前端字段全读不到。 §5.2/§5.3/R1.4 补上「每列显式 AS 」并给出正确示例。 Co-Authored-By: Claude Code --- docs/contract.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/contract.md b/docs/contract.md index 57dd7b6..b56530b 100644 --- a/docs/contract.md +++ b/docs/contract.md @@ -34,6 +34,9 @@ 所有 SELECT **必须显式列出列名**。原因:react 版库(TypeORM `synchronize`)只有 entity 里的列, 官方 DDL 有 30~60 列。显式投影让本仓后端**能开 react 建出来的库**,且响应 JSON 与 react 逐字相同。 +🔴 **每列还要显式 `AS `**(camelCase,见 §4)——`row_to_json` 用**列的输出名**当 JSON 键。 +写成 `AS creator_time` 会得到 `"creator_time"`,react 是 `"creatorTime"` ⇒ 前端读不到字段。 + ### R1.5 numeric 列必须序列化成 JSON 字符串 `f_price` 回显 `"9.90"`,不是 `9.9`。手段:**在 SQL 投影里就 cast 成 text**(`f_price::text AS price`)。 `int` 列(`f_delete_mark` / `f_stock` / `f_is_administrator`)**保持数字**。 @@ -221,9 +224,13 @@ pub fn valid_column(name: &str) -> Option; **写入一律 UTC 朴素时间**;**读出投影成 JS ISO 串**: ```sql -to_char(f_creator_time, 'YYYY-MM-DD"T"HH24:MI:SS') || 'Z' AS creator_time +to_char(f_creator_time, 'YYYY-MM-DD"T"HH24:MI:SS') || 'Z' AS creatorTime ``` +🔴 **投影的 `AS` 别名必须就是 §4 的 JSON 键(camelCase)** —— +`row_to_json` 用的是**列的输出名**当 JSON 键。别名写成 `AS creator_time` 会得到 `"creator_time"`, +与 react 的 `"creatorTime"` **对不上** ⇒ 前端所有字段读不到。**每一列**都要显式 `AS `。 + ### 5.3 精确列清单(**投影真源**) 🔴 **SELECT 的列必须逐字取自下表** —— 这是 react 版 entity 的**精确列集**(`~/Desktop/workspace/skills/ssw-jnpf-lite-react/src/backend/src/**/*.entity.ts`), @@ -232,6 +239,12 @@ to_char(f_creator_time, 'YYYY-MM-DD"T"HH24:MI:SS') || 'Z' AS creator_time 格式:`列名 → JSON 键`。列顺序 = react entity 的声明顺序。 +🔴 **投影写法**:每列都写成 `f_xxx AS `(**别名 = 右边的 JSON 键,camelCase**)。 +`row_to_json` 用的就是别名当 JSON 键 —— 别名写错 ⇒ 响应键名对不上 react。 +例:`f_real_name AS realName`、`f_is_administrator AS isAdministrator`、 +`f_creator_time` ⇒ `to_char(f_creator_time, 'YYYY-MM-DD"T"HH24:MI:SS') || 'Z' AS creatorTime`、 +`f_price` ⇒ `f_price::text AS price`(numeric 必须是字符串)。 + **base_authorize**(9 列) ``` f_id → id f_tenant_id → tenantId f_object_type → objectType