rethinkdb / rethinkdb-rs

A native RethinkDB driver written in Rust
Apache License 2.0
210 stars 27 forks source link

Can not nested command in json object #58

Open krist7599555 opened 3 years ago

krist7599555 commented 3 years ago

can not using reql::Command in serde:json! because serde::Serialize behavior

let command = r
  .table("users")
  .insert(json!({
    "id": r.uuid(),    // error
    "age": r.expr(16), // error
    "created": r.now() // error
  }));

this problem also hard to solve because we can not serialize json multiple time in rethinkdb

// original body
{ "tags": ["love", "boy", "hate"] }

// after serialize query
{ "tags": [2, ["love", "boy", "hate"]]}

i also try another approch like using r.object([["key", "value"], ["key2", r.uuid()]]) and r.hashMap is not implemented yet.

any suggestion on this please.

rushmorem commented 3 years ago

Thanks for reporting this. I will look into it when I get some time.

this problem also hard to solve because we can not serialize json multiple time in rethinkdb

Yes, this is one of the reasons Command doesn't currently implement Serialize.

json!({
    "id": r.uuid(),    // error
    "age": r.expr(16), // error
    "created": r.now() // error
  })

Is that the actual query you wanted to run or it's just an example? If it's the actual query you can work around this by using Uuid from the uuid crate instead of r.uuid(), 16 instead of r.expr(16) and constructing the time using DateTime instead of r.now().

vettich commented 9 months ago

I think the json serialization problem can be solved by implementing our own implementation of the json! macro. You can see an example implementation in unreql crate

We could write something like this:

use unreql::{r, rjson};

r.table("users")
  .get(1)
  .update(rjson!({
    "name": "John",
    "upd_count": r.row().g("upd_count").add(1),
  }))