Skip to content

Does native_db support nested structures? #207

Answered by vincent-herlemont
Raj2032 asked this question in Q&A
Discussion options

You must be logged in to vote

Here is an example using ToKey (read the documentation carefully).

use native_db::*;
use native_model::{native_model, Model};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[native_model(id = 1, version = 1)]
#[native_db]
struct Item {
    #[primary_key]
    id: u32,
    #[secondary_key]
    name: Name,
}

#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
struct Name {
    first: String,
    last: String,
}

impl ToKey for &Name {
    fn to_key(&self) -> Key {
        // Concatenate the first and last name
        let mut key = self.first.clone();
        key.push_str(&self.last);
        Key::new(key.as_bytes().t…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Raj2032
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #205 on August 03, 2024 06:12.