evenframe_core 0.1.6

Core functionality for Evenframe - TypeScript type generation and database schema synchronization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! SurrealDB Database Provider Implementation
//!
//! This module implements the DatabaseProvider trait for SurrealDB,
//! containing all SurrealQL query generation and execution logic.

pub mod access;
pub mod assert;
pub mod define;
pub mod execute;
pub mod insert;
pub mod remove;
mod type_mapper;
pub mod upsert;
pub mod value;

use async_trait::async_trait;
use std::collections::BTreeMap;
use surrealdb::{
    Surreal,
    engine::remote::http::{Client, Http},
    opt::auth::Root,
};
use tracing::{debug, info, trace};

use crate::error::{EvenframeError, Result};
use crate::schemasync::{EdgeConfig, TableConfig};
use crate::types::{FieldType, ForeignTypeRegistry, StructConfig, StructField, TaggedUnion};

use self::define::generate_define_statements;
use self::value::to_surreal_string;

use super::{
    DatabaseConfig, DatabaseProvider, ProviderType, Relationship, RelationshipDirection,
    SchemaExport, TableInfo, Transaction,
};

pub use type_mapper::SurrealdbTypeMapper;

/// SurrealDB database provider implementation.
///
/// This provider wraps the existing SurrealDB functionality and implements
/// the DatabaseProvider trait for use with the abstracted SchemaSync system.
pub struct SurrealdbProvider {
    /// The SurrealDB client connection
    client: Option<Surreal<Client>>,
    /// Connection configuration
    config: Option<DatabaseConfig>,
    /// Foreign type registry for type mapping
    registry: ForeignTypeRegistry,
}

impl SurrealdbProvider {
    /// Create a new SurrealDB provider instance
    pub fn new() -> Self {
        Self {
            client: None,
            config: None,
            registry: ForeignTypeRegistry::default(),
        }
    }

    /// Create a new SurrealDB provider instance with a foreign type registry
    pub fn with_registry(registry: ForeignTypeRegistry) -> Self {
        Self {
            client: None,
            config: None,
            registry,
        }
    }

    /// Get the type mapper
    fn type_mapper(&self) -> SurrealdbTypeMapper<'_> {
        SurrealdbTypeMapper::new(&self.registry)
    }

    /// Get a reference to the underlying SurrealDB client.
    ///
    /// This is useful for backward compatibility with existing code
    /// that needs direct access to the client.
    pub fn client(&self) -> Option<&Surreal<Client>> {
        self.client.as_ref()
    }

    /// Get a mutable reference to the underlying SurrealDB client.
    pub fn client_mut(&mut self) -> Option<&mut Surreal<Client>> {
        self.client.as_mut()
    }

    /// Take ownership of the underlying client.
    ///
    /// After calling this, the provider will be disconnected.
    pub fn take_client(&mut self) -> Option<Surreal<Client>> {
        self.client.take()
    }
}

impl Default for SurrealdbProvider {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl DatabaseProvider for SurrealdbProvider {
    fn name(&self) -> &'static str {
        "surrealdb"
    }

    fn supports_graph_queries(&self) -> bool {
        true
    }

    fn supports_embedded_mode(&self) -> bool {
        true
    }

    async fn connect(&mut self, config: &DatabaseConfig) -> Result<()> {
        if config.provider != ProviderType::SurrealDb {
            return Err(EvenframeError::config(format!(
                "SurrealDB provider cannot connect with provider type: {}",
                config.provider
            )));
        }

        info!("Connecting to SurrealDB at {}", config.url);

        let client = Surreal::new::<Http>(&config.url).await.map_err(|e| {
            EvenframeError::database(format!("Failed to create SurrealDB HTTP client: {e}"))
        })?;

        // Sign in if credentials provided
        if let (Some(username), Some(password)) = (&config.username, &config.password) {
            debug!("Signing in to SurrealDB as {}", username);
            client
                .signin(Root {
                    username: username.to_string(),
                    password: password.to_string(),
                })
                .await
                .map_err(|e| {
                    EvenframeError::database(format!("Failed to sign in to SurrealDB: {e}"))
                })?;
        }

        // Select namespace and database
        if let (Some(ns), Some(db)) = (&config.namespace, &config.database) {
            debug!("Using namespace '{}' and database '{}'", ns, db);
            client.use_ns(ns).use_db(db).await.map_err(|e| {
                EvenframeError::database(format!("Failed to select namespace/database: {e}"))
            })?;
        }

        self.client = Some(client);
        self.config = Some(config.clone());
        info!("Successfully connected to SurrealDB");

        Ok(())
    }

    async fn disconnect(&mut self) -> Result<()> {
        self.client = None;
        self.config = None;
        Ok(())
    }

    fn is_connected(&self) -> bool {
        self.client.is_some()
    }

    async fn export_schema(&self) -> Result<SchemaExport> {
        let client = self
            .client
            .as_ref()
            .ok_or_else(|| EvenframeError::database("Not connected to SurrealDB"))?;

        // Use SurrealDB's export functionality
        let mut export_stream = client
            .export(())
            .await
            .map_err(|e| EvenframeError::database(format!("Failed to export schema: {e}")))?;

        use futures::StreamExt;
        let mut raw_statements = String::new();
        while let Some(chunk) = export_stream.next().await {
            let chunk = chunk.map_err(|e| {
                EvenframeError::database(format!("Error reading export stream: {e}"))
            })?;
            raw_statements.push_str(&String::from_utf8_lossy(&chunk));
        }

        Ok(SchemaExport {
            tables: vec![],
            indexes: vec![],
            relationships: vec![],
            raw_statements: Some(raw_statements),
        })
    }

    async fn apply_schema(&self, statements: &[String]) -> Result<()> {
        let client = self
            .client
            .as_ref()
            .ok_or_else(|| EvenframeError::database("Not connected to SurrealDB"))?;

        for stmt in statements {
            trace!("Executing schema statement: {}", stmt);
            client.query(stmt).await.map_err(|e| {
                EvenframeError::database(format!(
                    "Failed to execute schema statement: {e}\nStatement: {stmt}"
                ))
            })?;
        }

        Ok(())
    }

    async fn get_table_info(&self, table_name: &str) -> Result<Option<TableInfo>> {
        let client = self
            .client
            .as_ref()
            .ok_or_else(|| EvenframeError::database("Not connected to SurrealDB"))?;

        let query = format!("INFO FOR TABLE {}", table_name);
        let response: surrealdb::IndexedResults = client
            .query(&query)
            .await
            .map_err(|e| EvenframeError::database(format!("Failed to get table info: {e}")))?;

        // This method is not currently used — the schema comparison pipeline uses
        // export-based parsing (SchemaImporter::import_schema_only) instead of
        // per-table INFO queries. Reserved for future use (e.g., per-table
        // introspection or SQL provider parity).
        let _ = response;
        Ok(None)
    }

    async fn list_tables(&self) -> Result<Vec<String>> {
        let client = self
            .client
            .as_ref()
            .ok_or_else(|| EvenframeError::database("Not connected to SurrealDB"))?;

        let query = "INFO FOR DB";
        let mut response = client
            .query(query)
            .await
            .map_err(|e| EvenframeError::database(format!("Failed to list tables: {e}")))?;

        // Parse the response to extract table names
        let result: Option<serde_json::Value> = response
            .take(0)
            .map_err(|e| EvenframeError::database(format!("Failed to parse table list: {e}")))?;

        let mut tables = Vec::new();
        if let Some(value) = result
            && let Some(tb) = value.get("tables").and_then(|v| v.as_object())
        {
            tables.extend(tb.keys().cloned());
        }

        Ok(tables)
    }

    async fn execute(&self, query: &str) -> Result<Vec<serde_json::Value>> {
        let client = self
            .client
            .as_ref()
            .ok_or_else(|| EvenframeError::database("Not connected to SurrealDB"))?;

        let mut response = client
            .query(query)
            .await
            .map_err(|e| EvenframeError::database(format!("Failed to execute query: {e}")))?;

        let results: Vec<serde_json::Value> = response
            .take(0)
            .map_err(|e| EvenframeError::database(format!("Failed to parse query results: {e}")))?;

        Ok(results)
    }

    async fn execute_batch(&self, queries: &[String]) -> Result<Vec<Vec<serde_json::Value>>> {
        let mut results = Vec::with_capacity(queries.len());
        for query in queries {
            let result = self.execute(query).await?;
            results.push(result);
        }
        Ok(results)
    }

    async fn insert(&self, table: &str, records: &[serde_json::Value]) -> Result<Vec<String>> {
        let client = self
            .client
            .as_ref()
            .ok_or_else(|| EvenframeError::database("Not connected to SurrealDB"))?;

        let mut ids = Vec::with_capacity(records.len());

        for record in records {
            let created: Option<serde_json::Value> = client
                .create(table)
                .content(record.clone())
                .await
                .map_err(|e| EvenframeError::database(format!("Failed to insert record: {e}")))?;

            if let Some(created) = created
                && let Some(id) = created.get("id").and_then(|v| v.as_str())
            {
                ids.push(id.to_string());
            }
        }

        Ok(ids)
    }

    async fn upsert(&self, table: &str, records: &[serde_json::Value]) -> Result<Vec<String>> {
        let client = self
            .client
            .as_ref()
            .ok_or_else(|| EvenframeError::database("Not connected to SurrealDB"))?;

        let mut ids = Vec::with_capacity(records.len());

        for record in records {
            // Extract ID if present
            let id = record.get("id").and_then(|v| v.as_str());

            let upserted: Option<serde_json::Value> = if let Some(id) = id {
                client
                    .upsert((table, id))
                    .content(record.clone())
                    .await
                    .map_err(|e| {
                        EvenframeError::database(format!("Failed to upsert record: {e}"))
                    })?
            } else {
                client
                    .create(table)
                    .content(record.clone())
                    .await
                    .map_err(|e| {
                        EvenframeError::database(format!(
                            "Failed to create record during upsert: {e}"
                        ))
                    })?
            };

            if let Some(upserted) = upserted
                && let Some(id) = upserted.get("id").and_then(|v| v.as_str())
            {
                ids.push(id.to_string());
            }
        }

        Ok(ids)
    }

    async fn select(&self, table: &str, filter: Option<&str>) -> Result<Vec<serde_json::Value>> {
        let query = if let Some(f) = filter {
            format!("SELECT * FROM {} WHERE {}", table, f)
        } else {
            format!("SELECT * FROM {}", table)
        };

        self.execute(&query).await
    }

    async fn count(&self, table: &str, filter: Option<&str>) -> Result<u64> {
        let query = if let Some(f) = filter {
            format!("SELECT count() FROM {} WHERE {} GROUP ALL", table, f)
        } else {
            format!("SELECT count() FROM {} GROUP ALL", table)
        };

        let results = self.execute(&query).await?;

        if let Some(first) = results.first()
            && let Some(count) = first.get("count").and_then(|v| v.as_u64())
        {
            return Ok(count);
        }

        Ok(0)
    }

    async fn delete(&self, table: &str, ids: &[String]) -> Result<()> {
        let client = self
            .client
            .as_ref()
            .ok_or_else(|| EvenframeError::database("Not connected to SurrealDB"))?;

        for id in ids {
            // Parse the ID to get table:id format or just id
            let record_id = if id.contains(':') {
                id.clone()
            } else {
                format!("{}:{}", table, id)
            };

            client
                .query(format!("DELETE {}", record_id))
                .await
                .map_err(|e| {
                    EvenframeError::database(format!("Failed to delete record {}: {e}", record_id))
                })?;
        }

        Ok(())
    }

    fn generate_create_table(
        &self,
        table_name: &str,
        config: &TableConfig,
        all_tables: &BTreeMap<String, TableConfig>,
        objects: &BTreeMap<String, StructConfig>,
        enums: &BTreeMap<String, TaggedUnion>,
    ) -> String {
        // Use existing generate_define_statements function
        let default_registry = crate::types::ForeignTypeRegistry::default();
        generate_define_statements(
            table_name,
            config,
            all_tables,
            objects,
            enums,
            false, // full_refresh_mode
            &default_registry,
        )
    }

    fn generate_create_field(
        &self,
        table_name: &str,
        field: &StructField,
        _objects: &BTreeMap<String, StructConfig>,
        _enums: &BTreeMap<String, TaggedUnion>,
    ) -> String {
        // Generate a DEFINE FIELD statement
        let field_type = self.type_mapper().field_type_to_surql(&field.field_type);
        format!(
            "DEFINE FIELD {} ON TABLE {} TYPE {};",
            field.field_name, table_name, field_type
        )
    }

    fn map_field_type(&self, field_type: &FieldType) -> String {
        self.type_mapper().field_type_to_surql(field_type)
    }

    fn format_value(&self, field_type: &FieldType, value: &serde_json::Value) -> String {
        to_surreal_string(field_type, value, &self.registry)
    }

    fn generate_relationship_table(&self, edge: &EdgeConfig) -> Vec<String> {
        // For SurrealDB, edges are defined as tables with special structure
        // The actual RELATE statements create relationships
        let mut statements = Vec::new();

        statements.push(format!("DEFINE TABLE {} SCHEMAFULL;", edge.edge_name));

        // Define in and out fields for edges
        statements.push(format!(
            "DEFINE FIELD in ON TABLE {} TYPE record;",
            edge.edge_name
        ));
        statements.push(format!(
            "DEFINE FIELD out ON TABLE {} TYPE record;",
            edge.edge_name
        ));

        statements
    }

    async fn create_relationship(
        &self,
        edge_table: &str,
        from_id: &str,
        to_id: &str,
        data: Option<&serde_json::Value>,
    ) -> Result<String> {
        let client = self
            .client
            .as_ref()
            .ok_or_else(|| EvenframeError::database("Not connected to SurrealDB"))?;

        let query = if let Some(data) = data {
            format!(
                "RELATE {}->{}->{} CONTENT {}",
                from_id,
                edge_table,
                to_id,
                serde_json::to_string(data).unwrap_or_default()
            )
        } else {
            format!("RELATE {}->{}->{}", from_id, edge_table, to_id)
        };

        let mut response = client
            .query(&query)
            .await
            .map_err(|e| EvenframeError::database(format!("Failed to create relationship: {e}")))?;

        let results: Vec<serde_json::Value> = response.take(0).map_err(|e| {
            EvenframeError::database(format!("Failed to parse relationship result: {e}"))
        })?;

        if let Some(first) = results.first()
            && let Some(id) = first.get("id").and_then(|v| v.as_str())
        {
            return Ok(id.to_string());
        }

        Ok(String::new())
    }

    async fn delete_relationship(
        &self,
        edge_table: &str,
        from_id: &str,
        to_id: &str,
    ) -> Result<()> {
        let query = format!(
            "DELETE {} WHERE in = {} AND out = {}",
            edge_table, from_id, to_id
        );
        self.execute(&query).await?;
        Ok(())
    }

    async fn get_relationships(
        &self,
        edge_table: &str,
        record_id: &str,
        direction: RelationshipDirection,
    ) -> Result<Vec<Relationship>> {
        let query = match direction {
            RelationshipDirection::Outgoing => {
                format!("SELECT * FROM {} WHERE in = {}", edge_table, record_id)
            }
            RelationshipDirection::Incoming => {
                format!("SELECT * FROM {} WHERE out = {}", edge_table, record_id)
            }
            RelationshipDirection::Both => {
                format!(
                    "SELECT * FROM {} WHERE in = {} OR out = {}",
                    edge_table, record_id, record_id
                )
            }
        };

        let results = self.execute(&query).await?;

        let relationships = results
            .into_iter()
            .filter_map(|v| {
                let id = v.get("id")?.as_str()?.to_string();
                let from_id = v.get("in")?.as_str()?.to_string();
                let to_id = v.get("out")?.as_str()?.to_string();
                Some(Relationship {
                    id,
                    from_id,
                    to_id,
                    data: Some(v),
                })
            })
            .collect();

        Ok(relationships)
    }

    async fn begin_transaction(&self) -> Result<Box<dyn Transaction>> {
        Err(EvenframeError::database(
            "SurrealDB transactions are not yet implemented in the provider abstraction",
        ))
    }

    async fn create_embedded_instance(&self) -> Result<Option<Box<dyn DatabaseProvider>>> {
        // SurrealDB supports embedded mode via Surreal<Mem>
        // For now, return None - this will be implemented later
        // when we refactor the comparator to use the provider abstraction
        Ok(None)
    }
}