metabase-api-rs 0.1.0-beta.3

A simplified Rust client for the Metabase API (Beta release - feature complete)
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
//! Database model representing Metabase database connections
//!
//! This module provides the core data structures for working with
//! Metabase database connections, including tables and fields.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::common::MetabaseId;

/// Connection source for database
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "lowercase")]
pub enum ConnectionSource {
    #[default]
    Admin,
    Setup,
}

/// Unique identifier for a database field
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct FieldId(pub i64);

/// Unique identifier for a database table
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TableId(pub i64);

/// Represents a Metabase database connection
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Database {
    /// Unique identifier for the database
    pub id: MetabaseId,

    /// Database name
    pub name: String,

    /// Database engine (e.g., "postgres", "mysql", "h2")
    pub engine: String,

    /// Connection details (host, port, database name, etc.)
    pub details: Value,

    /// Whether full sync is enabled
    #[serde(default)]
    pub is_full_sync: bool,

    /// Whether on-demand sync is enabled
    #[serde(default)]
    pub is_on_demand: bool,

    /// Whether the database is a sample database
    #[serde(default)]
    pub is_sample: bool,

    /// Cache field values for this database
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_field_values_schedule: Option<String>,

    /// Metadata sync schedule
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata_sync_schedule: Option<String>,

    /// When the database was created
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<DateTime<Utc>>,

    /// When the database was last updated
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<DateTime<Utc>>,
}

/// Represents a table in a database
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DatabaseTable {
    /// Unique identifier for the table
    pub id: TableId,

    /// Database ID this table belongs to
    pub db_id: MetabaseId,

    /// Table name
    pub name: String,

    /// Database schema name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schema: Option<String>,

    /// Display name for the table
    pub display_name: String,

    /// Whether the table is active
    #[serde(default = "default_true")]
    pub active: bool,

    /// Table description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Entity type (e.g., "entity/GenericTable", "entity/EventTable")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub entity_type: Option<String>,

    /// Visibility type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub visibility_type: Option<String>,

    /// When the table was created
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<DateTime<Utc>>,

    /// When the table was last updated
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<DateTime<Utc>>,
}

/// Represents a field in a database table
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DatabaseField {
    /// Unique identifier for the field
    pub id: FieldId,

    /// Table ID this field belongs to
    pub table_id: TableId,

    /// Field name
    pub name: String,

    /// Display name for the field
    pub display_name: String,

    /// Database-specific type
    pub database_type: String,

    /// Base type (e.g., "type/Text", "type/Integer")
    pub base_type: String,

    /// Semantic type (e.g., "type/Email", "type/URL")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub semantic_type: Option<String>,

    /// Whether this field is active
    #[serde(default = "default_true")]
    pub active: bool,

    /// Field description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Whether this is a primary key
    #[serde(default)]
    pub is_pk: bool,

    /// Foreign key target field ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fk_target_field_id: Option<FieldId>,

    /// Field position in the table
    #[serde(default)]
    pub position: i32,

    /// Visibility type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub visibility_type: Option<String>,

    /// When the field was created
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at: Option<DateTime<Utc>>,

    /// When the field was last updated
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<DateTime<Utc>>,
}

/// Request to create a new database connection
#[derive(Debug, Clone, Serialize)]
pub struct CreateDatabaseRequest {
    /// Database name
    pub name: String,

    /// Database engine
    pub engine: String,

    /// Connection details
    pub details: Value,

    /// Whether to enable full sync
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_full_sync: Option<bool>,

    /// Whether to enable on-demand sync
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_on_demand: Option<bool>,

    /// Schedule for caching field values
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_field_values_schedule: Option<String>,

    /// Schedule for metadata sync
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata_sync_schedule: Option<String>,
}

/// Request to update a database connection
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateDatabaseRequest {
    /// New name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// New connection details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub details: Option<Value>,

    /// Whether to enable full sync
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_full_sync: Option<bool>,

    /// Whether to enable on-demand sync
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_on_demand: Option<bool>,

    /// Schedule for caching field values
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_field_values_schedule: Option<String>,

    /// Schedule for metadata sync
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata_sync_schedule: Option<String>,
}

/// Database sync status
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DatabaseSyncStatus {
    /// Current sync status
    pub status: String,

    /// Sync progress (0-100)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub progress: Option<f32>,

    /// Error message if sync failed
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,

    /// When the sync started
    #[serde(skip_serializing_if = "Option::is_none")]
    pub started_at: Option<DateTime<Utc>>,

    /// When the sync completed
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completed_at: Option<DateTime<Utc>>,
}

fn default_true() -> bool {
    true
}

impl Database {
    /// Creates a new database builder
    pub fn builder(name: impl Into<String>, engine: impl Into<String>) -> DatabaseBuilder {
        DatabaseBuilder::new(name, engine)
    }
}

/// Builder for creating Database instances
pub struct DatabaseBuilder {
    name: String,
    engine: String,
    details: Value,
    is_full_sync: bool,
    is_on_demand: bool,
    cache_field_values_schedule: Option<String>,
    metadata_sync_schedule: Option<String>,
}

impl DatabaseBuilder {
    /// Creates a new database builder
    pub fn new(name: impl Into<String>, engine: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            engine: engine.into(),
            details: Value::Object(serde_json::Map::new()),
            is_full_sync: true,
            is_on_demand: false,
            cache_field_values_schedule: None,
            metadata_sync_schedule: None,
        }
    }

    /// Sets the connection details
    pub fn details(mut self, details: Value) -> Self {
        self.details = details;
        self
    }

    /// Sets whether full sync is enabled
    pub fn full_sync(mut self, enabled: bool) -> Self {
        self.is_full_sync = enabled;
        self
    }

    /// Sets whether on-demand sync is enabled
    pub fn on_demand_sync(mut self, enabled: bool) -> Self {
        self.is_on_demand = enabled;
        self
    }

    /// Sets the cache field values schedule
    pub fn cache_schedule(mut self, schedule: impl Into<String>) -> Self {
        self.cache_field_values_schedule = Some(schedule.into());
        self
    }

    /// Sets the metadata sync schedule
    pub fn sync_schedule(mut self, schedule: impl Into<String>) -> Self {
        self.metadata_sync_schedule = Some(schedule.into());
        self
    }

    /// Builds the Database instance
    pub fn build(self) -> Database {
        Database {
            id: MetabaseId(0), // Will be set by the server
            name: self.name,
            engine: self.engine,
            details: self.details,
            is_full_sync: self.is_full_sync,
            is_on_demand: self.is_on_demand,
            is_sample: false,
            cache_field_values_schedule: self.cache_field_values_schedule,
            metadata_sync_schedule: self.metadata_sync_schedule,
            created_at: None,
            updated_at: None,
        }
    }

    /// Builds a CreateDatabaseRequest
    pub fn build_request(self) -> CreateDatabaseRequest {
        CreateDatabaseRequest {
            name: self.name,
            engine: self.engine,
            details: self.details,
            is_full_sync: Some(self.is_full_sync),
            is_on_demand: Some(self.is_on_demand),
            cache_field_values_schedule: self.cache_field_values_schedule,
            metadata_sync_schedule: self.metadata_sync_schedule,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_database_creation() {
        let database = Database::builder("Test DB", "postgres")
            .details(json!({
                "host": "localhost",
                "port": 5432,
                "dbname": "testdb",
                "user": "testuser"
            }))
            .full_sync(true)
            .on_demand_sync(false)
            .build();

        assert_eq!(database.name, "Test DB");
        assert_eq!(database.engine, "postgres");
        assert!(database.is_full_sync);
        assert!(!database.is_on_demand);
    }

    #[test]
    fn test_database_table() {
        let table = DatabaseTable {
            id: TableId(1),
            db_id: MetabaseId(1),
            name: "users".to_string(),
            schema: Some("public".to_string()),
            display_name: "Users".to_string(),
            active: true,
            description: Some("User accounts".to_string()),
            entity_type: Some("entity/UserTable".to_string()),
            visibility_type: None,
            created_at: None,
            updated_at: None,
        };

        assert_eq!(table.name, "users");
        assert_eq!(table.display_name, "Users");
        assert!(table.active);
    }

    #[test]
    fn test_database_field() {
        let field = DatabaseField {
            id: FieldId(1),
            table_id: TableId(1),
            name: "email".to_string(),
            display_name: "Email".to_string(),
            database_type: "VARCHAR(255)".to_string(),
            base_type: "type/Text".to_string(),
            semantic_type: Some("type/Email".to_string()),
            active: true,
            description: None,
            is_pk: false,
            fk_target_field_id: None,
            position: 2,
            visibility_type: None,
            created_at: None,
            updated_at: None,
        };

        assert_eq!(field.name, "email");
        assert_eq!(field.base_type, "type/Text");
        assert_eq!(field.semantic_type, Some("type/Email".to_string()));
        assert!(!field.is_pk);
    }

    #[test]
    fn test_create_database_request() {
        let request = Database::builder("Production DB", "mysql")
            .details(json!({
                "host": "db.example.com",
                "port": 3306,
                "dbname": "production"
            }))
            .cache_schedule("0 0 * * *")
            .build_request();

        assert_eq!(request.name, "Production DB");
        assert_eq!(request.engine, "mysql");
        assert_eq!(request.is_full_sync, Some(true));
        assert_eq!(
            request.cache_field_values_schedule,
            Some("0 0 * * *".to_string())
        );
    }

    #[test]
    fn test_update_database_request() {
        let request = UpdateDatabaseRequest {
            name: Some("Updated DB".to_string()),
            is_full_sync: Some(false),
            ..Default::default()
        };

        assert_eq!(request.name, Some("Updated DB".to_string()));
        assert_eq!(request.is_full_sync, Some(false));
        assert!(request.details.is_none());
    }
}

// ==================== Database Metadata Models ====================

/// Database metadata including tables and fields
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseMetadata {
    /// Database ID
    pub id: MetabaseId,

    /// Database name
    pub name: String,

    /// Database engine
    pub engine: String,

    /// List of tables in the database
    pub tables: Vec<TableMetadata>,

    /// Database features
    #[serde(default)]
    pub features: Vec<String>,

    /// Native query permissions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub native_permissions: Option<String>,
}

/// Table metadata including fields
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableMetadata {
    /// Table ID
    pub id: TableId,

    /// Table name
    pub name: String,

    /// Table schema
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schema: Option<String>,

    /// Display name
    pub display_name: String,

    /// Table description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Entity type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub entity_type: Option<String>,

    /// List of fields in the table
    pub fields: Vec<FieldMetadata>,

    /// Whether the table is active
    #[serde(default = "default_true")]
    pub active: bool,
}

/// Field metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldMetadata {
    /// Field ID
    pub id: FieldId,

    /// Field name
    pub name: String,

    /// Display name
    pub display_name: String,

    /// Database type
    pub database_type: String,

    /// Base type (Metabase type)
    pub base_type: String,

    /// Semantic type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub semantic_type: Option<String>,

    /// Field description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Whether this is a primary key
    #[serde(default)]
    pub is_pk: bool,

    /// Foreign key target field ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fk_target_field_id: Option<FieldId>,

    /// Field position in the table
    pub position: i32,

    /// Whether the field is active
    #[serde(default = "default_true")]
    pub active: bool,
}

/// Database sync result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncResult {
    /// Sync task ID
    pub id: String,

    /// Sync status
    pub status: String,

    /// Status message
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,

    /// When the sync started
    #[serde(skip_serializing_if = "Option::is_none")]
    pub started_at: Option<DateTime<Utc>>,

    /// When the sync completed
    #[serde(skip_serializing_if = "Option::is_none")]
    pub completed_at: Option<DateTime<Utc>>,
}