Handling serialization on Rust SDK variables for FETCH clauses #4176
Unanswered
vitordhers
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Dear folks, I'm having some trouble on finding out the best way to deal with some relations in my Surrealdb project. As you can tell by this discussion title, I'm using Rust SDK.
I have the following structs on
tables
, thereforeentities
:A few questions have risen from my current work.
1. Is there any value on implementing a custom
Serialize
andDeserialize
forenum
s, in order to store data more efficiently (specially when it comes toenum
s)? I mean, by simplyderive
ingSerialize
andDeserialize
traits for myenum
s, I get the following record (as seen onSurrealist
), forCommemorationEntity
entity
:But, by implementing custom
Serialize
andDeserialize
traits, I was able to store the following:From the storing space point of view, it makes little sense to convert
enum
s such asLiturgicalColor
to a number, since as mentioned in documentation for data types,int
should be ai64
, which takes 8 bytes, while theString
values stored from derived traits, usually takes less than 8 bytes. But, what about the followingenum
:I've seen that storing it without custom
Serialize/Deserialize
, anObject(1)
gets stored. In that case, I'd assume that simply storing serialized values ofString
up to 4 chars in length would be more memory efficient, am I wrong?Besides, as mentioned in the documentation, storing
byte array
s is also a possibility, so, assuming that I'd love to be the most efficient as possible, would I get value, in terms of memory usage, on storingenum
s as byte arrays instead of using thederive
d implementations, or am I missing anything fundamental on how data is stored in Surrealdb in this case?2. When it comes to storing relations, I'm using two fields in the the example above,
commemorations
andcommemorations_ids
. The former one is supposed to get the relations, obtained viaSELECT
queries using theFETCH
clause, while the latter simply stores the records.While the data structure above works, it makes me take a non-intuitive approach on how to store data. For instance, in order to create a new
LiturgicalDateEntity
, I need to instantiate the followingstruct
:which creates the record running the code briefly written below:
And in order to retrieve that data, I need to run this
query
:SELECT *, commemorations as commemorations_ids FROM liturgical_dates:⟨11-6-2024⟩ FETCH commemorations
.So far, so good, it works. But I find it non-intuitive to store the ids at
commemorations
field, as ideally, it should be stored atcommemorations_ids
field. Besides, if I don't add theFETCH
clause at the end of thequery
, I get the following error:"Failed to convert `[{ commemorations: [commemorations:st_barnabas_apostle], commemorations_ids: [commemorations:st_barnabas_apostle], day: 11, id: liturgical_dates:⟨11-6-2024⟩, month: 6, time: 0, year: 2024 }]` to `T`: missing field `tb`"
, regardless of setting this field as optional (By having it wrapped in anOption
).I can tell that the struct deserialization is expecting to receive a
Vec<CommemorationEntity>
, not aVec<Thing>
, so that the error. So, a few questions arise.First, is it possible to add an
ALIAS
clause after theFETCH
clause in order to map that value to a custom field? I've searched the issues and didn't find any suggestion in that regard. I believe that Surrealdb would benefit greatly from this implementation, unless there already is a different workaround to achieve this behavior that I'm not aware yet.Second, what approach should I take in order to properly deserialize the
commemorations
field, by being have to receive both aVec
of records (CommemorationEntity
) or references (Thing
), without calling theFETCH
clause? I've tried using two solutions listed below, to no avail:This approach yields:
Failed to convert `[{ commemorations: [commemorations:st_barnabas_apostle], day: 11, id: liturgical_dates:⟨11-6-2024⟩, month: 6, time: 0, year: 2024 }]` to `T`: missing field `tb`
and
In turn, this yields:
Failed to convert `[{ commemorations: [commemorations:st_barnabas_apostle], day: 11, id: liturgical_dates:⟨11-6-2024⟩, month: 6, time: 0, year: 2024 }]` to `T`: invalid value: map, expected map with a single key
I'm assuming that this level of abstraction is possible and certainly I'm not taking the correct approach for cases like that. If so, how can I achieve the expected behavior explained above?
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions