rouchdb 0.3.2

Local-first document database with CouchDB replication protocol support
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
//! Integration tests for new PouchDB parity features against CouchDB.
//! These require a running CouchDB instance:
//!   docker compose up -d
//!   cargo test -p rouchdb --test parity_http -- --ignored

mod common;

use common::{delete_remote_db, fresh_remote_db};
use rouchdb::{
    AllDocsOptions, ChangesOptions, ChangesStreamOptions, Database, FindOptions, IndexDefinition,
    ReplicationOptions, SortField,
};
use std::time::Duration;

// =========================================================================
// db.close() on HTTP
// =========================================================================

#[tokio::test]
#[ignore]
async fn close_http_db() {
    let url = fresh_remote_db("close").await;
    let db = Database::http(&url);
    db.put("doc1", serde_json::json!({})).await.unwrap();
    db.close().await.unwrap(); // No-op for HTTP, should not error
    delete_remote_db(&url).await;
}

// =========================================================================
// db.explain() on HTTP
// =========================================================================

#[tokio::test]
#[ignore]
async fn explain_on_http_without_index() {
    let url = fresh_remote_db("explain").await;
    let db = Database::http(&url);

    db.put("doc1", serde_json::json!({"name": "Alice", "age": 30}))
        .await
        .unwrap();

    let explanation = db
        .explain(FindOptions {
            selector: serde_json::json!({"age": {"$gt": 20}}),
            ..Default::default()
        })
        .await;

    // Should fall back to _all_docs since no Mango index
    assert_eq!(explanation.index.name, "_all_docs");

    delete_remote_db(&url).await;
}

#[tokio::test]
#[ignore]
async fn explain_on_http_with_index() {
    let url = fresh_remote_db("explain_idx").await;
    let db = Database::http(&url);

    db.put("doc1", serde_json::json!({"name": "Alice", "age": 30}))
        .await
        .unwrap();

    db.create_index(IndexDefinition {
        name: String::new(),
        fields: vec![SortField::Simple("age".into())],
        ddoc: None,
    })
    .await
    .unwrap();

    let explanation = db
        .explain(FindOptions {
            selector: serde_json::json!({"age": {"$gt": 20}}),
            ..Default::default()
        })
        .await;

    assert_eq!(explanation.index.name, "idx-age");
    assert_eq!(explanation.index.index_type, "json");

    delete_remote_db(&url).await;
}

// =========================================================================
// Design documents on HTTP
// =========================================================================

#[tokio::test]
#[ignore]
async fn design_doc_crud_on_http() {
    let url = fresh_remote_db("ddoc").await;
    let db = Database::http(&url);

    let ddoc = rouchdb::DesignDocument {
        id: "_design/myapp".into(),
        rev: None,
        views: {
            let mut v = std::collections::HashMap::new();
            v.insert(
                "by_type".into(),
                rouchdb::ViewDef {
                    map: "function(doc) { emit(doc.type, 1); }".into(),
                    reduce: Some("_count".into()),
                },
            );
            v
        },
        filters: std::collections::HashMap::new(),
        validate_doc_update: None,
        shows: std::collections::HashMap::new(),
        lists: std::collections::HashMap::new(),
        updates: std::collections::HashMap::new(),
        language: Some("javascript".into()),
    };

    let result = db.put_design(ddoc).await.unwrap();
    assert!(result.ok);

    let retrieved = db.get_design("myapp").await.unwrap();
    assert_eq!(retrieved.id, "_design/myapp");
    assert!(retrieved.views.contains_key("by_type"));

    // Delete
    let rev = retrieved.rev.unwrap();
    let del = db.delete_design("myapp", &rev).await.unwrap();
    assert!(del.ok);

    // Should be gone
    assert!(db.get_design("myapp").await.is_err());

    delete_remote_db(&url).await;
}

// =========================================================================
// Security document on HTTP
// =========================================================================

#[tokio::test]
#[ignore]
async fn security_document_on_http() {
    let url = fresh_remote_db("security").await;
    let db = Database::http(&url);

    let sec = db.get_security().await.unwrap();
    // CouchDB returns a security doc (may have admin set from URL auth)
    assert!(sec.admins.names.is_empty() || !sec.admins.names.is_empty());

    delete_remote_db(&url).await;
}

// =========================================================================
// Replication with since override (HTTP)
// =========================================================================

#[tokio::test]
#[ignore]
async fn replication_since_override_http() {
    let url = fresh_remote_db("repl_since").await;
    let remote = Database::http(&url);
    let local = Database::memory("local");

    remote
        .put("doc1", serde_json::json!({"v": 1}))
        .await
        .unwrap();
    remote
        .put("doc2", serde_json::json!({"v": 2}))
        .await
        .unwrap();
    remote
        .put("doc3", serde_json::json!({"v": 3}))
        .await
        .unwrap();

    // Get changes to find a since point
    let changes = remote.changes(ChangesOptions::default()).await.unwrap();
    let _since = changes.results[0].seq.clone();

    // Replicate only changes after since
    let result = local.replicate_from(&remote).await;
    // Just verify basic replication works to CouchDB
    assert!(result.is_ok());

    delete_remote_db(&url).await;
}

// =========================================================================
// Replication without checkpoint (HTTP)
// =========================================================================

#[tokio::test]
#[ignore]
async fn replication_no_checkpoint_http() {
    let url = fresh_remote_db("repl_nockpt").await;
    let remote = Database::http(&url);
    let local = Database::memory("local");

    local
        .put("doc1", serde_json::json!({"v": 1}))
        .await
        .unwrap();

    let result = local
        .replicate_to_with_opts(
            &remote,
            ReplicationOptions {
                checkpoint: false,
                ..Default::default()
            },
        )
        .await
        .unwrap();

    assert!(result.ok);
    assert_eq!(result.docs_written, 1);

    let doc = remote.get("doc1").await.unwrap();
    assert_eq!(doc.data["v"], 1);

    delete_remote_db(&url).await;
}

// =========================================================================
// allDocs with conflicts option on HTTP
// =========================================================================

#[tokio::test]
#[ignore]
async fn all_docs_conflicts_http() {
    let url = fresh_remote_db("alldocs_conflicts").await;
    let db = Database::http(&url);

    db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();
    db.put("doc2", serde_json::json!({"v": 2})).await.unwrap();

    let result = db
        .all_docs(AllDocsOptions {
            include_docs: true,
            conflicts: true,
            ..AllDocsOptions::new()
        })
        .await
        .unwrap();

    assert_eq!(result.rows.len(), 2);

    delete_remote_db(&url).await;
}

// =========================================================================
// Changes with selector filter on HTTP
// =========================================================================

#[tokio::test]
#[ignore]
async fn changes_selector_filter_http() {
    let url = fresh_remote_db("ch_sel_http").await;
    let db = Database::http(&url);

    db.put(
        "user1",
        serde_json::json!({"type": "user", "name": "Alice"}),
    )
    .await
    .unwrap();
    db.put(
        "inv1",
        serde_json::json!({"type": "invoice", "amount": 100}),
    )
    .await
    .unwrap();
    db.put("user2", serde_json::json!({"type": "user", "name": "Bob"}))
        .await
        .unwrap();

    let changes = db
        .changes(ChangesOptions {
            selector: Some(serde_json::json!({"type": "user"})),
            include_docs: true,
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(changes.results.len(), 2);
    for event in &changes.results {
        let doc = event.doc.as_ref().unwrap();
        assert_eq!(doc["type"], "user");
    }

    delete_remote_db(&url).await;
}

// =========================================================================
// Live changes on HTTP with events
// =========================================================================

#[tokio::test]
#[ignore]
async fn live_changes_events_http() {
    let url = fresh_remote_db("ch_events").await;
    let db = Database::http(&url);

    db.put("doc1", serde_json::json!({"v": 1})).await.unwrap();

    let (mut rx, handle) = db.live_changes_events(ChangesStreamOptions {
        include_docs: true,
        poll_interval: Duration::from_millis(200),
        ..Default::default()
    });

    let mut got_change = false;
    let timeout = tokio::time::sleep(Duration::from_secs(5));
    tokio::pin!(timeout);

    loop {
        tokio::select! {
            event = rx.recv() => {
                match event {
                    Some(rouchdb::ChangesEvent::Change(ce)) => {
                        assert_eq!(ce.id, "doc1");
                        got_change = true;
                        break;
                    }
                    Some(_) => continue,
                    None => break,
                }
            }
            _ = &mut timeout => break,
        }
    }

    assert!(got_change);
    handle.cancel();
    delete_remote_db(&url).await;
}

// =========================================================================
// Mango find with index on HTTP
// =========================================================================

#[tokio::test]
#[ignore]
async fn mango_find_with_index_http() {
    let url = fresh_remote_db("mango_idx").await;
    let db = Database::http(&url);

    db.put(
        "alice",
        serde_json::json!({"name": "Alice", "age": 30, "type": "user"}),
    )
    .await
    .unwrap();
    db.put(
        "bob",
        serde_json::json!({"name": "Bob", "age": 25, "type": "user"}),
    )
    .await
    .unwrap();
    db.put(
        "charlie",
        serde_json::json!({"name": "Charlie", "age": 35, "type": "user"}),
    )
    .await
    .unwrap();

    // Create index
    db.create_index(IndexDefinition {
        name: String::new(),
        fields: vec![SortField::Simple("age".into())],
        ddoc: None,
    })
    .await
    .unwrap();

    // Find using index
    let result = db
        .find(FindOptions {
            selector: serde_json::json!({"age": {"$gte": 30}}),
            ..Default::default()
        })
        .await
        .unwrap();

    assert_eq!(result.docs.len(), 2);

    delete_remote_db(&url).await;
}

// =========================================================================
// Replication with events on HTTP
// =========================================================================

#[tokio::test]
#[ignore]
async fn replication_events_http() {
    let url = fresh_remote_db("repl_events").await;
    let remote = Database::http(&url);
    let local = Database::memory("local");

    local
        .put("doc1", serde_json::json!({"v": 1}))
        .await
        .unwrap();
    local
        .put("doc2", serde_json::json!({"v": 2}))
        .await
        .unwrap();

    let (result, mut rx) = local
        .replicate_to_with_events(&remote, ReplicationOptions::default())
        .await
        .unwrap();

    assert!(result.ok);

    let mut events = Vec::new();
    while let Ok(event) = rx.try_recv() {
        events.push(event);
    }

    assert!(
        events
            .iter()
            .any(|e| matches!(e, rouchdb::ReplicationEvent::Active))
    );

    delete_remote_db(&url).await;
}