nodedb-lite 0.1.0

Embedded edge client for NodeDB — offline vector search, graph traversal, and CRDT sync
Documentation
//! DDL handlers for strict document collection operations.

use nodedb_types::result::QueryResult;
use nodedb_types::value::Value;

use crate::error::LiteError;
use crate::query::engine::LiteQueryEngine;
use crate::storage::engine::StorageEngine;

use super::parser::parse_strict_create_sql;

impl<S: StorageEngine> LiteQueryEngine<S> {
    /// Handle: CREATE COLLECTION <name> (<col_defs>) WITH storage = 'strict'
    pub(in crate::query) async fn handle_create_strict(
        &self,
        sql: &str,
    ) -> Result<QueryResult, LiteError> {
        let (name, schema) = parse_strict_create_sql(sql)?;

        self.strict.create_collection(&name, schema).await?;

        // Register the new collection in the query engine.
        self.register_strict_collection(&name);

        Ok(QueryResult {
            columns: vec!["result".into()],
            rows: vec![vec![Value::String(format!(
                "strict collection '{name}' created"
            ))]],
            rows_affected: 0,
            command: None,
        })
    }

    /// Handle: DROP COLLECTION <name> (for strict collections).
    pub(in crate::query) async fn handle_drop_strict(
        &self,
        name: &str,
    ) -> Result<QueryResult, LiteError> {
        // The rows and the indexes over them go in one storage batch.
        let strict = &self.strict;
        self.indexes
            .drop_collection_with(&*self.storage, name, || strict.drop_ops(name))
            .await?;
        self.strict.forget_collection(name)?;

        Ok(QueryResult {
            columns: vec!["result".into()],
            rows: vec![vec![Value::String(format!(
                "strict collection '{name}' dropped"
            ))]],
            rows_affected: 0,
            command: None,
        })
    }
}