ruvector-node 0.1.29

Node.js bindings for Ruvector via NAPI-RS
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//! Node.js bindings for Ruvector via NAPI-RS
//!
//! High-performance Rust vector database with zero-copy buffer sharing,
//! async/await support, and complete TypeScript type definitions.

#![deny(clippy::all)]
#![warn(clippy::pedantic)]

use napi::bindgen_prelude::*;
use napi_derive::napi;
use ruvector_core::{
    types::{DbOptions, HnswConfig, QuantizationConfig},
    DistanceMetric, SearchQuery, SearchResult, VectorDB as CoreVectorDB, VectorEntry,
};
use std::sync::Arc;
use std::sync::RwLock;
use std::time::{SystemTime, UNIX_EPOCH};

// Import new crates
use ruvector_collections::CollectionManager as CoreCollectionManager;
use ruvector_filter::FilterExpression;
use ruvector_metrics::{gather_metrics, HealthChecker, HealthStatus};
use std::path::PathBuf;

/// Distance metric for similarity calculation
#[napi(string_enum)]
#[derive(Debug)]
pub enum JsDistanceMetric {
    /// Euclidean (L2) distance
    Euclidean,
    /// Cosine similarity (converted to distance)
    Cosine,
    /// Dot product (converted to distance for maximization)
    DotProduct,
    /// Manhattan (L1) distance
    Manhattan,
}

impl From<JsDistanceMetric> for DistanceMetric {
    fn from(metric: JsDistanceMetric) -> Self {
        match metric {
            JsDistanceMetric::Euclidean => DistanceMetric::Euclidean,
            JsDistanceMetric::Cosine => DistanceMetric::Cosine,
            JsDistanceMetric::DotProduct => DistanceMetric::DotProduct,
            JsDistanceMetric::Manhattan => DistanceMetric::Manhattan,
        }
    }
}

/// Quantization configuration
#[napi(object)]
#[derive(Debug, Clone)]
pub struct JsQuantizationConfig {
    /// Quantization type: "none", "scalar", "product", "binary"
    pub r#type: String,
    /// Number of subspaces (for product quantization)
    pub subspaces: Option<u32>,
    /// Codebook size (for product quantization)
    pub k: Option<u32>,
}

impl From<JsQuantizationConfig> for QuantizationConfig {
    fn from(config: JsQuantizationConfig) -> Self {
        match config.r#type.as_str() {
            "none" => QuantizationConfig::None,
            "scalar" => QuantizationConfig::Scalar,
            "product" => QuantizationConfig::Product {
                subspaces: config.subspaces.unwrap_or(16) as usize,
                k: config.k.unwrap_or(256) as usize,
            },
            "binary" => QuantizationConfig::Binary,
            _ => QuantizationConfig::Scalar,
        }
    }
}

/// HNSW index configuration
#[napi(object)]
#[derive(Debug, Clone)]
pub struct JsHnswConfig {
    /// Number of connections per layer (M)
    pub m: Option<u32>,
    /// Size of dynamic candidate list during construction
    pub ef_construction: Option<u32>,
    /// Size of dynamic candidate list during search
    pub ef_search: Option<u32>,
    /// Maximum number of elements
    pub max_elements: Option<u32>,
}

impl From<JsHnswConfig> for HnswConfig {
    fn from(config: JsHnswConfig) -> Self {
        HnswConfig {
            m: config.m.unwrap_or(32) as usize,
            ef_construction: config.ef_construction.unwrap_or(200) as usize,
            ef_search: config.ef_search.unwrap_or(100) as usize,
            max_elements: config.max_elements.unwrap_or(10_000_000) as usize,
        }
    }
}

/// Database configuration options
#[napi(object)]
#[derive(Debug)]
pub struct JsDbOptions {
    /// Vector dimensions
    pub dimensions: u32,
    /// Distance metric
    pub distance_metric: Option<JsDistanceMetric>,
    /// Storage path
    pub storage_path: Option<String>,
    /// HNSW configuration
    pub hnsw_config: Option<JsHnswConfig>,
    /// Quantization configuration
    pub quantization: Option<JsQuantizationConfig>,
}

impl From<JsDbOptions> for DbOptions {
    fn from(options: JsDbOptions) -> Self {
        DbOptions {
            dimensions: options.dimensions as usize,
            distance_metric: options
                .distance_metric
                .map(Into::into)
                .unwrap_or(DistanceMetric::Cosine),
            storage_path: options
                .storage_path
                .unwrap_or_else(|| "./ruvector.db".to_string()),
            hnsw_config: options.hnsw_config.map(Into::into),
            quantization: options.quantization.map(Into::into),
        }
    }
}

/// Vector entry
#[napi(object)]
pub struct JsVectorEntry {
    /// Optional ID (auto-generated if not provided)
    pub id: Option<String>,
    /// Vector data as Float32Array or array of numbers
    pub vector: Float32Array,
    /// Optional metadata as JSON string (use JSON.stringify on objects)
    pub metadata: Option<String>,
}

impl JsVectorEntry {
    fn to_core(&self) -> Result<VectorEntry> {
        // Parse JSON string to HashMap<String, serde_json::Value>
        let metadata = self.metadata.as_ref().and_then(|s| {
            serde_json::from_str::<std::collections::HashMap<String, serde_json::Value>>(s).ok()
        });

        Ok(VectorEntry {
            id: self.id.clone(),
            vector: self.vector.to_vec(),
            metadata,
        })
    }
}

/// Search query parameters
#[napi(object)]
pub struct JsSearchQuery {
    /// Query vector as Float32Array or array of numbers
    pub vector: Float32Array,
    /// Number of results to return (top-k)
    pub k: u32,
    /// Optional ef_search parameter for HNSW
    pub ef_search: Option<u32>,
    /// Optional metadata filter as JSON string (use JSON.stringify on objects)
    pub filter: Option<String>,
}

impl JsSearchQuery {
    fn to_core(&self) -> Result<SearchQuery> {
        // Parse JSON string to HashMap<String, serde_json::Value>
        let filter = self.filter.as_ref().and_then(|s| {
            serde_json::from_str::<std::collections::HashMap<String, serde_json::Value>>(s).ok()
        });

        Ok(SearchQuery {
            vector: self.vector.to_vec(),
            k: self.k as usize,
            filter,
            ef_search: self.ef_search.map(|v| v as usize),
        })
    }
}

/// Search result with similarity score
#[napi(object)]
#[derive(Clone)]
pub struct JsSearchResult {
    /// Vector ID
    pub id: String,
    /// Distance/similarity score (lower is better for distance metrics)
    pub score: f64,
    /// Vector data (if requested)
    pub vector: Option<Float32Array>,
    /// Metadata as JSON string (use JSON.parse to convert to object)
    pub metadata: Option<String>,
}

impl From<SearchResult> for JsSearchResult {
    fn from(result: SearchResult) -> Self {
        // Convert Vec<f32> to Float32Array
        let vector = result.vector.map(|v| Float32Array::new(v));

        // Convert HashMap to JSON string
        let metadata = result.metadata.and_then(|m| serde_json::to_string(&m).ok());

        JsSearchResult {
            id: result.id,
            score: f64::from(result.score),
            vector,
            metadata,
        }
    }
}

/// High-performance vector database with HNSW indexing
#[napi]
pub struct VectorDB {
    inner: Arc<RwLock<CoreVectorDB>>,
}

#[napi]
impl VectorDB {
    /// Create a new vector database with the given options
    ///
    /// # Example
    /// ```javascript
    /// const db = new VectorDB({
    ///   dimensions: 384,
    ///   distanceMetric: 'Cosine',
    ///   storagePath: './vectors.db',
    ///   hnswConfig: {
    ///     m: 32,
    ///     efConstruction: 200,
    ///     efSearch: 100
    ///   }
    /// });
    /// ```
    #[napi(constructor)]
    pub fn new(options: JsDbOptions) -> Result<Self> {
        let core_options: DbOptions = options.into();
        let db = CoreVectorDB::new(core_options)
            .map_err(|e| Error::from_reason(format!("Failed to create database: {}", e)))?;

        Ok(Self {
            inner: Arc::new(RwLock::new(db)),
        })
    }

    /// Create a vector database with default options
    ///
    /// # Example
    /// ```javascript
    /// const db = VectorDB.withDimensions(384);
    /// ```
    #[napi(factory)]
    pub fn with_dimensions(dimensions: u32) -> Result<Self> {
        let db = CoreVectorDB::with_dimensions(dimensions as usize)
            .map_err(|e| Error::from_reason(format!("Failed to create database: {}", e)))?;

        Ok(Self {
            inner: Arc::new(RwLock::new(db)),
        })
    }

    /// Insert a vector entry into the database
    ///
    /// Returns the ID of the inserted vector (auto-generated if not provided)
    ///
    /// # Example
    /// ```javascript
    /// const id = await db.insert({
    ///   vector: new Float32Array([1.0, 2.0, 3.0]),
    ///   metadata: { text: 'example' }
    /// });
    /// ```
    #[napi]
    pub async fn insert(&self, entry: JsVectorEntry) -> Result<String> {
        let core_entry = entry.to_core()?;
        let db = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let db = db.read().expect("RwLock poisoned");
            db.insert(core_entry)
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Insert failed: {}", e)))
    }

    /// Insert multiple vectors in a batch
    ///
    /// Returns an array of IDs for the inserted vectors
    ///
    /// # Example
    /// ```javascript
    /// const ids = await db.insertBatch([
    ///   { vector: new Float32Array([1, 2, 3]) },
    ///   { vector: new Float32Array([4, 5, 6]) }
    /// ]);
    /// ```
    #[napi]
    pub async fn insert_batch(&self, entries: Vec<JsVectorEntry>) -> Result<Vec<String>> {
        let core_entries: Result<Vec<VectorEntry>> = entries.iter().map(|e| e.to_core()).collect();
        let core_entries = core_entries?;
        let db = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let db = db.read().expect("RwLock poisoned");
            db.insert_batch(core_entries)
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Batch insert failed: {}", e)))
    }

    /// Search for similar vectors
    ///
    /// Returns an array of search results sorted by similarity
    ///
    /// # Example
    /// ```javascript
    /// const results = await db.search({
    ///   vector: new Float32Array([1, 2, 3]),
    ///   k: 10,
    ///   filter: { category: 'example' }
    /// });
    /// ```
    #[napi]
    pub async fn search(&self, query: JsSearchQuery) -> Result<Vec<JsSearchResult>> {
        let core_query = query.to_core()?;
        let db = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let db = db.read().expect("RwLock poisoned");
            db.search(core_query)
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Search failed: {}", e)))
        .map(|results| results.into_iter().map(Into::into).collect())
    }

    /// Delete a vector by ID
    ///
    /// Returns true if the vector was deleted, false if not found
    ///
    /// # Example
    /// ```javascript
    /// const deleted = await db.delete('vector-id');
    /// ```
    #[napi]
    pub async fn delete(&self, id: String) -> Result<bool> {
        let db = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let db = db.read().expect("RwLock poisoned");
            db.delete(&id)
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Delete failed: {}", e)))
    }

    /// Get a vector by ID
    ///
    /// Returns the vector entry if found, null otherwise
    ///
    /// # Example
    /// ```javascript
    /// const entry = await db.get('vector-id');
    /// if (entry) {
    ///   console.log('Found:', entry.metadata);
    /// }
    /// ```
    #[napi]
    pub async fn get(&self, id: String) -> Result<Option<JsVectorEntry>> {
        let db = self.inner.clone();

        let result = tokio::task::spawn_blocking(move || {
            let db = db.read().expect("RwLock poisoned");
            db.get(&id)
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Get failed: {}", e)))?;

        Ok(result.map(|entry| {
            // Convert HashMap to JSON string
            let metadata = entry.metadata.and_then(|m| serde_json::to_string(&m).ok());

            JsVectorEntry {
                id: entry.id,
                vector: Float32Array::new(entry.vector),
                metadata,
            }
        }))
    }

    /// Get the number of vectors in the database
    ///
    /// # Example
    /// ```javascript
    /// const count = await db.len();
    /// console.log(`Database contains ${count} vectors`);
    /// ```
    #[napi]
    pub async fn len(&self) -> Result<u32> {
        let db = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let db = db.read().expect("RwLock poisoned");
            db.len()
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Len failed: {}", e)))
        .map(|len| len as u32)
    }

    /// Check if the database is empty
    ///
    /// # Example
    /// ```javascript
    /// if (await db.isEmpty()) {
    ///   console.log('Database is empty');
    /// }
    /// ```
    #[napi]
    pub async fn is_empty(&self) -> Result<bool> {
        let db = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let db = db.read().expect("RwLock poisoned");
            db.is_empty()
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("IsEmpty failed: {}", e)))
    }
}

/// Get the version of the Ruvector library
#[napi]
pub fn version() -> String {
    env!("CARGO_PKG_VERSION").to_string()
}

/// Test function to verify the bindings are working
#[napi]
pub fn hello() -> String {
    "Hello from Ruvector Node.js bindings!".to_string()
}

/// Filter for metadata-based search
#[napi(object)]
#[derive(Debug, Clone)]
pub struct JsFilter {
    /// Field name to filter on
    pub field: String,
    /// Operator: "eq", "ne", "gt", "gte", "lt", "lte", "in", "match"
    pub operator: String,
    /// Value to compare against (JSON string)
    pub value: String,
}

impl JsFilter {
    fn to_filter_expression(&self) -> Result<FilterExpression> {
        let value: serde_json::Value = serde_json::from_str(&self.value)
            .map_err(|e| Error::from_reason(format!("Invalid JSON value: {}", e)))?;

        Ok(match self.operator.as_str() {
            "eq" => FilterExpression::eq(&self.field, value),
            "ne" => FilterExpression::ne(&self.field, value),
            "gt" => FilterExpression::gt(&self.field, value),
            "gte" => FilterExpression::gte(&self.field, value),
            "lt" => FilterExpression::lt(&self.field, value),
            "lte" => FilterExpression::lte(&self.field, value),
            "match" => FilterExpression::Match {
                field: self.field.clone(),
                text: value.as_str().unwrap_or("").to_string(),
            },
            _ => FilterExpression::eq(&self.field, value),
        })
    }
}

/// Collection configuration
#[napi(object)]
#[derive(Debug, Clone)]
pub struct JsCollectionConfig {
    /// Vector dimensions
    pub dimensions: u32,
    /// Distance metric
    pub distance_metric: Option<JsDistanceMetric>,
    /// HNSW configuration
    pub hnsw_config: Option<JsHnswConfig>,
    /// Quantization configuration
    pub quantization: Option<JsQuantizationConfig>,
}

impl From<JsCollectionConfig> for ruvector_collections::CollectionConfig {
    fn from(config: JsCollectionConfig) -> Self {
        ruvector_collections::CollectionConfig {
            dimensions: config.dimensions as usize,
            distance_metric: config
                .distance_metric
                .map(Into::into)
                .unwrap_or(DistanceMetric::Cosine),
            hnsw_config: config.hnsw_config.map(Into::into),
            quantization: config.quantization.map(Into::into),
            on_disk_payload: true,
        }
    }
}

/// Collection statistics
#[napi(object)]
#[derive(Debug, Clone)]
pub struct JsCollectionStats {
    /// Number of vectors in the collection
    pub vectors_count: u32,
    /// Disk space used in bytes
    pub disk_size_bytes: i64,
    /// RAM space used in bytes
    pub ram_size_bytes: i64,
}

impl From<ruvector_collections::CollectionStats> for JsCollectionStats {
    fn from(stats: ruvector_collections::CollectionStats) -> Self {
        JsCollectionStats {
            vectors_count: stats.vectors_count as u32,
            disk_size_bytes: stats.disk_size_bytes as i64,
            ram_size_bytes: stats.ram_size_bytes as i64,
        }
    }
}

/// Collection alias
#[napi(object)]
#[derive(Debug, Clone)]
pub struct JsAlias {
    /// Alias name
    pub alias: String,
    /// Collection name
    pub collection: String,
}

impl From<(String, String)> for JsAlias {
    fn from(tuple: (String, String)) -> Self {
        JsAlias {
            alias: tuple.0,
            collection: tuple.1,
        }
    }
}

/// Collection manager for multi-collection support
#[napi]
pub struct CollectionManager {
    inner: Arc<RwLock<CoreCollectionManager>>,
}

#[napi]
impl CollectionManager {
    /// Create a new collection manager
    ///
    /// # Example
    /// ```javascript
    /// const manager = new CollectionManager('./collections');
    /// ```
    #[napi(constructor)]
    pub fn new(base_path: Option<String>) -> Result<Self> {
        let path = PathBuf::from(base_path.unwrap_or_else(|| "./collections".to_string()));
        let manager = CoreCollectionManager::new(path).map_err(|e| {
            Error::from_reason(format!("Failed to create collection manager: {}", e))
        })?;

        Ok(Self {
            inner: Arc::new(RwLock::new(manager)),
        })
    }

    /// Create a new collection
    ///
    /// # Example
    /// ```javascript
    /// await manager.createCollection('my_vectors', {
    ///   dimensions: 384,
    ///   distanceMetric: 'Cosine'
    /// });
    /// ```
    #[napi]
    pub async fn create_collection(&self, name: String, config: JsCollectionConfig) -> Result<()> {
        let core_config: ruvector_collections::CollectionConfig = config.into();
        let manager = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let manager = manager.write().expect("RwLock poisoned");
            manager.create_collection(&name, core_config)
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Failed to create collection: {}", e)))
    }

    /// List all collections
    ///
    /// # Example
    /// ```javascript
    /// const collections = await manager.listCollections();
    /// console.log('Collections:', collections);
    /// ```
    #[napi]
    pub async fn list_collections(&self) -> Result<Vec<String>> {
        let manager = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let manager = manager.read().expect("RwLock poisoned");
            manager.list_collections()
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))
    }

    /// Delete a collection
    ///
    /// # Example
    /// ```javascript
    /// await manager.deleteCollection('my_vectors');
    /// ```
    #[napi]
    pub async fn delete_collection(&self, name: String) -> Result<()> {
        let manager = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let manager = manager.write().expect("RwLock poisoned");
            manager.delete_collection(&name)
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Failed to delete collection: {}", e)))
    }

    /// Get collection statistics
    ///
    /// # Example
    /// ```javascript
    /// const stats = await manager.getStats('my_vectors');
    /// console.log(`Vectors: ${stats.vectorsCount}`);
    /// ```
    #[napi]
    pub async fn get_stats(&self, name: String) -> Result<JsCollectionStats> {
        let manager = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let manager = manager.read().expect("RwLock poisoned");
            manager.collection_stats(&name)
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Failed to get stats: {}", e)))
        .map(Into::into)
    }

    /// Create an alias for a collection
    ///
    /// # Example
    /// ```javascript
    /// await manager.createAlias('latest', 'my_vectors_v2');
    /// ```
    #[napi]
    pub async fn create_alias(&self, alias: String, collection: String) -> Result<()> {
        let manager = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let manager = manager.write().expect("RwLock poisoned");
            manager.create_alias(&alias, &collection)
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Failed to create alias: {}", e)))
    }

    /// Delete an alias
    ///
    /// # Example
    /// ```javascript
    /// await manager.deleteAlias('latest');
    /// ```
    #[napi]
    pub async fn delete_alias(&self, alias: String) -> Result<()> {
        let manager = self.inner.clone();

        tokio::task::spawn_blocking(move || {
            let manager = manager.write().expect("RwLock poisoned");
            manager.delete_alias(&alias)
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
        .map_err(|e| Error::from_reason(format!("Failed to delete alias: {}", e)))
    }

    /// List all aliases
    ///
    /// # Example
    /// ```javascript
    /// const aliases = await manager.listAliases();
    /// for (const alias of aliases) {
    ///   console.log(`${alias.alias} -> ${alias.collection}`);
    /// }
    /// ```
    #[napi]
    pub async fn list_aliases(&self) -> Result<Vec<JsAlias>> {
        let manager = self.inner.clone();

        let aliases = tokio::task::spawn_blocking(move || {
            let manager = manager.read().expect("RwLock poisoned");
            manager.list_aliases()
        })
        .await
        .map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?;

        Ok(aliases.into_iter().map(Into::into).collect())
    }
}

/// Health response
#[napi(object)]
#[derive(Debug, Clone)]
pub struct JsHealthResponse {
    /// Status: "healthy", "degraded", or "unhealthy"
    pub status: String,
    /// Version string
    pub version: String,
    /// Uptime in seconds
    pub uptime_seconds: i64,
}

/// Get Prometheus metrics
///
/// # Example
/// ```javascript
/// const metrics = getMetrics();
/// console.log(metrics);
/// ```
#[napi]
pub fn get_metrics() -> String {
    gather_metrics()
}

/// Get health status
///
/// # Example
/// ```javascript
/// const health = getHealth();
/// console.log(`Status: ${health.status}`);
/// console.log(`Uptime: ${health.uptimeSeconds}s`);
/// ```
#[napi]
pub fn get_health() -> JsHealthResponse {
    let checker = HealthChecker::new();
    let health = checker.health();

    JsHealthResponse {
        status: match health.status {
            HealthStatus::Healthy => "healthy".to_string(),
            HealthStatus::Degraded => "degraded".to_string(),
            HealthStatus::Unhealthy => "unhealthy".to_string(),
        },
        version: health.version,
        uptime_seconds: health.uptime_seconds as i64,
    }
}