Crate ledb_types

Source
Expand description

§Types and traits for storable documents

§Document trait

The basic trait which should be implemented for structs which designed to be handled as documents.

use serde::{Serialize, Deserialize};
use ledb_types::{Document, Identifier, Primary, KeyFields, KeyType, IndexKind};

#[derive(Serialize, Deserialize)]
struct MyDoc {
    // define optional primary key field
    id: Option<Primary>,
    // define other fields
    title: String,
    tag: Vec<String>,
    timestamp: u32,
    // define nested document
    meta: MetaData,
}

#[derive(Serialize, Deserialize)]
struct MetaData {
    // define index field
    keywords: Vec<String>,
    // define other fields
    description: String,
}

impl Document for MyDoc {
    // declare primary key field name
    fn primary_field() -> Identifier {
        "id".into()
    }

    // declare other key fields
    fn key_fields() -> KeyFields {
        KeyFields::new()
            // add key fields of document
            .with_field(("title", KeyType::String, IndexKind::Unique))
            .with_field(("tag", KeyType::String, IndexKind::Index))
            .with_field(("timestamp", KeyType::Int, IndexKind::Unique))
            // add key fields from nested document
            .with_fields(MetaData::key_fields().with_parent("meta"))
    }
}

impl Document for MetaData {
    // declare key fields for index
    fn key_fields() -> KeyFields {
        KeyFields::new()
            // add key fields of document
            .with_field(("keywords", KeyType::String, IndexKind::Index))
    }
}

§DocumentKeyType trait

This trait maps rust types to key types.

Structs§

KeyField
Indexed field definition
KeyFields
Indexed fields definition

Enums§

Identifier
Generic string indentifier
IndexKind
The kind of index
KeyType
The type of key

Traits§

Document
Identified document representation
DocumentKeyType
Field key type inference

Type Aliases§

Primary
Primary key (document identifier)