Skip to content

Commit f350ff8

Browse files
committed
[SeaORM] edit
1 parent a8e3f2b commit f350ff8

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

SeaORM/docs/04-generate-entity/05-newtype.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ You can define a New Type (`T`) and use it as model field. The following traits
77
3. Implement [`sea_query::ValueType`](https://docs.rs/sea-query/*/sea_query/value/trait.ValueType.html) for `T`
88
4. Implement [`sea_query::Nullable`](https://docs.rs/sea-query/*/sea_query/value/trait.Nullable.html) for `T`
99

10-
## Wrapper Type
10+
## Wrapping scalar types
1111

1212
You can create new types wrapping any type supported by SeaORM.
1313

@@ -154,7 +154,7 @@ impl sea_orm::sea_query::Nullable for Integer {
154154

155155
## Wrapping `Vec<T>` (backend generic)
156156

157-
You can also define a backend-generic `Vec<T>` field by serialize / deserialize the object to / from JSON:
157+
You can also wrap a `Vec<T>` field by serialize / deserialize the object to / from JSON. This is a backend-generic way of supporting array types across databases.
158158

159159
```rust
160160
use sea_orm::entity::prelude::*;
@@ -220,6 +220,33 @@ impl sea_orm::sea_query::Nullable for ObjectVec {
220220
```
221221
</details>
222222

223+
## Treat any type as JSON
224+
225+
In addition to wrapping `Vec<T>`, the `FromJsonQueryResult` macro can be used on any type that implements `serde`'s `Serialize` and `Deserialize`, and they will be converted to/from JSON when interacting with databases.
226+
227+
```rust
228+
use sea_orm::FromJsonQueryResult;
229+
use sea_orm::entity::prelude::*;
230+
use serde::{Deserialize, Serialize};
231+
232+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
233+
#[sea_orm(table_name = "json_struct")]
234+
pub struct Model {
235+
#[sea_orm(primary_key)]
236+
pub id: i32,
237+
pub json_value: Metadata,
238+
pub json_value_opt: Option<Metadata>,
239+
}
240+
241+
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, FromJsonQueryResult)]
242+
pub struct Metadata {
243+
pub id: i32,
244+
pub name: String,
245+
pub price: f32,
246+
pub notes: Option<String>,
247+
}
248+
```
249+
223250
## Enum String
224251

225252
Since `1.1.8`, `DeriveValueType` also supports `enum` types. It offers a simpler alternative to `DeriveActiveEnum` for client-side enums backed by string database types.

SeaORM/versioned_docs/version-1.1.x/04-generate-entity/05-newtype.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ You can define a New Type (`T`) and use it as model field. The following traits
77
3. Implement [`sea_query::ValueType`](https://docs.rs/sea-query/*/sea_query/value/trait.ValueType.html) for `T`
88
4. Implement [`sea_query::Nullable`](https://docs.rs/sea-query/*/sea_query/value/trait.Nullable.html) for `T`
99

10-
## Wrapper Type
10+
## Wrapping scalar types
1111

1212
You can create new types wrapping any type supported by SeaORM.
1313

@@ -138,7 +138,7 @@ impl sea_orm::sea_query::Nullable for Integer {
138138

139139
## Wrapping `Vec<T>` (backend generic)
140140

141-
You can also define a backend-generic `Vec<T>` field by serialize / deserialize the object to / from JSON:
141+
You can also wrap a `Vec<T>` field by serialize / deserialize the object to / from JSON. This is a backend-generic way of supporting array types across databases.
142142

143143
```rust
144144
use sea_orm::entity::prelude::*;
@@ -204,6 +204,33 @@ impl sea_orm::sea_query::Nullable for ObjectVec {
204204
```
205205
</details>
206206

207+
## Treat any type as JSON
208+
209+
In addition to wrapping `Vec<T>`, the `FromJsonQueryResult` macro can be used on any type that implements `serde`'s `Serialize` and `Deserialize`, and they will be converted to/from JSON when interacting with databases.
210+
211+
```rust
212+
use sea_orm::FromJsonQueryResult;
213+
use sea_orm::entity::prelude::*;
214+
use serde::{Deserialize, Serialize};
215+
216+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
217+
#[sea_orm(table_name = "json_struct")]
218+
pub struct Model {
219+
#[sea_orm(primary_key)]
220+
pub id: i32,
221+
pub json_value: Metadata,
222+
pub json_value_opt: Option<Metadata>,
223+
}
224+
225+
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, FromJsonQueryResult)]
226+
pub struct Metadata {
227+
pub id: i32,
228+
pub name: String,
229+
pub price: f32,
230+
pub notes: Option<String>,
231+
}
232+
```
233+
207234
## Enum String
208235

209236
Since `1.1.8`, `DeriveValueType` also supports `enum` types. It offers a simpler alternative to `DeriveActiveEnum` for client-side enums backed by string database types.

0 commit comments

Comments
 (0)