From b51a3b1c8ac6e4cc3fe0e8eb968b24a3179af958 Mon Sep 17 00:00:00 2001 From: SWQXDBA <983110853@qq.com> Date: Fri, 22 Nov 2024 12:03:52 +0800 Subject: [PATCH 1/2] enum document update --- docs/mapping/advanced/enum.mdx | 90 ++++++++++++++++++- .../current/mapping/advanced/enum.mdx | 86 +++++++++++++++++- 2 files changed, 174 insertions(+), 2 deletions(-) diff --git a/docs/mapping/advanced/enum.mdx b/docs/mapping/advanced/enum.mdx index 6c98a53a5..c13f2edc5 100644 --- a/docs/mapping/advanced/enum.mdx +++ b/docs/mapping/advanced/enum.mdx @@ -204,4 +204,92 @@ Next, we show how to override the global configuration: ``` - \ No newline at end of file + + +## Work with TypeScript Client and Jackson + +The serialization of a type to JSON and its storage in the database are two independent matters. The content described above focuses on how Jimmer handles enum mapping in the database, which is unrelated to JSON. + +By default, Jackson converts enums to its name, so when generating the TypeScript Client, Jimmer maps the enum according to its name. + + + + + + ```java + enum Gender { + MAN, + WOMAN + } + ``` + + + + + ```kotlin + enum class Gender { + MAN, + WOMAN + } + ``` + + + + +The generated TypeScript code is as follows: + + ```TypeScript + export const GenderEnum_CONSTANTS = [ + 'MAN', + 'WOMAN' + ] as const; + export type GenderEnum = typeof GenderEnum_CONSTANTS[number]; + + //usage + export type PersonDto = { + gender: GenderEnum; + } + ``` + +If you want to customize the JSON serialization, you need to use the @JsonValue annotation from Jackson. + + + + + ```java + enum Gender { + MAN, + WOMAN; + + @JsonValue + public String getValue() { + return name().toLowerCase(); + } + } + ``` + + + + + ```kotlin + enum class Gender { + MAN, + WOMAN; + + @JsonValue + fun getValue(): String { + return name().toLowerCase(); + } + } + ``` + + + + +When detects the presence of annotations like @JsonValue, Jimmer not knowing how the enum will be converted during JSON serialization, so jimmer will translates the enum type to a string. The generated TypeScript code in this case is as follows: + + ```TypeScript + export type PersonDto = { + gender: string; + } + ``` \ No newline at end of file diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/mapping/advanced/enum.mdx b/i18n/zh/docusaurus-plugin-content-docs/current/mapping/advanced/enum.mdx index 1ce35e6fb..98ddc5a56 100644 --- a/i18n/zh/docusaurus-plugin-content-docs/current/mapping/advanced/enum.mdx +++ b/i18n/zh/docusaurus-plugin-content-docs/current/mapping/advanced/enum.mdx @@ -204,4 +204,88 @@ enum class Gender { ``` - \ No newline at end of file + + +## 与 Typescript Client and Jackson一起使用 + +一个类型的Json序列化与数据库中的存储是独立的两件事。上述所描述的内容只是Jimmer在数据库中如何处理枚举映射,与Json无关。 + +在默认情况下,Jackson将枚举转换为枚举名字符串,所以Jimmer会在生成TypeScript Client的时候按照枚举的Name映射 + + + + + ```java + enum Gender { + MAN, + WOMAN + } + ``` + + + + + ```kotlin + enum class Gender { + MAN, + WOMAN + } + ``` + + + + + 生成的TypeScript代码如下: + ```TypeScript + export const GenderEnum_CONSTANTS = [ + 'MAN', + 'WOMAN' + ] as const; + export type GenderEnum = typeof GenderEnum_CONSTANTS[number]; + + //使用 + export type PersonDto = { + gender: GenderEnum; + } + ``` +如果想要自定义Json序列化,需要使用Jackson的@JsonValue注解 + + + + ```java + enum Gender { + MAN, + WOMAN; + + @JsonValue + public String getValue() { + return name().toLowerCase(); + } + } + ``` + + + + + ```kotlin + enum class Gender { + MAN, + WOMAN; + + @JsonValue + fun getValue(): String { + return name().toLowerCase(); + } + } + ``` + + + + +在检测到存在@JsonValue等注解后,因为jimmer不知道在Json序列化时,枚举会被转换为什么,所以Jimmer会把枚举类型翻译成string,生成的TypeScript代码如下: + + ```TypeScript + export type PersonDto = { + gender: string; + } + ``` From c2f3833443173b2f28da82f99fff80ff1e4375fa Mon Sep 17 00:00:00 2001 From: SWQXDBA <983110853@qq.com> Date: Mon, 25 Nov 2024 10:11:09 +0800 Subject: [PATCH 2/2] fix chinese document --- .../current/mutation/save-command/association/owner.mdx | 2 +- .../current/mutation/save-command/data-classification.mdx | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/mutation/save-command/association/owner.mdx b/i18n/zh/docusaurus-plugin-content-docs/current/mutation/save-command/association/owner.mdx index 69f4d9b14..c6212aa53 100644 --- a/i18n/zh/docusaurus-plugin-content-docs/current/mutation/save-command/association/owner.mdx +++ b/i18n/zh/docusaurus-plugin-content-docs/current/mutation/save-command/association/owner.mdx @@ -8,7 +8,7 @@ import TabItem from '@theme/TabItem'; ## 基本概念 -所畏拥有方,只具备`mappedBy`的`@OneToMany`或`@OneToOne`对象。以`@OneToMany`为例 +所谓拥有方,指具备`mappedBy`的`@OneToMany`或`@OneToOne`对象。以`@OneToMany`为例 diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/mutation/save-command/data-classification.mdx b/i18n/zh/docusaurus-plugin-content-docs/current/mutation/save-command/data-classification.mdx index 748bba31d..8aa2c4cbc 100644 --- a/i18n/zh/docusaurus-plugin-content-docs/current/mutation/save-command/data-classification.mdx +++ b/i18n/zh/docusaurus-plugin-content-docs/current/mutation/save-command/data-classification.mdx @@ -142,7 +142,7 @@ Jimmer支持`UPSERT`操作,即,用户并不明确指定`insert`或`update` ### 3. Key-specified对象 对于一个对象而言,如果`@Id`属性 *(本文的`Book.id`)* 没有被指定,但是其`@Key`属性 *(本文的`Book.name`和`Book.edition`)* 被指定了, -那么该对爱对象为key-specified对象,例如 +那么该对象为key-specified对象,例如 @@ -208,7 +208,7 @@ val book = Book { ### 5. Key-only对象 -对于id-specified对象而言,如果除了id外没有任何属性被指定,称为id-only对象,例如 +对于key-specified对象而言,如果除了id外没有任何属性被指定,称为id-only对象,例如