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
/*!

# LEDB Storage actor and REST interface

An implementation of storage actor for [Actix](https://actix.rs/).

*NOTE: Use `features = ["web"]` to enable an optional scoped REST-interface for **actix-web**.*

## Storage actor

Usage example:

```rust
use std::env;

use serde::{Deserialize, Serialize};
use serde_json::json;

use ledb_actix::{query, Document, Options, Primary, Storage, StorageAddrExt};
use log::info;
use serde_json::from_value;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Document)]
struct BlogPost {
    #[document(primary)]
    pub id: Option<Primary>,
    pub title: String,
    pub tags: Vec<String>,
    pub content: String,
}

#[actix_rt::main]
async fn main() {
    env::set_var("RUST_LOG", "info");
    pretty_env_logger::init();

    let _ = std::fs::remove_dir_all("example_db");

    let addr = Storage::new("example_db", Options::default())
        .unwrap()
        .start(1);

    let id = addr
        .send_query(query!(
            insert into blog {
                "title": "Absurd",
                "tags": ["absurd", "psychology"],
                "content": "Still nothing..."
            }
        ))
        .await
        .unwrap();

    info!("Inserted document id: {}", id);
    assert_eq!(id, 1);

    let id = addr.send_query(query!(
        insert into blog {
            "title": "Lorem ipsum",
            "tags": ["lorem", "ipsum"],
            "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        }
    )).await.unwrap();

    info!("Inserted document id: {}", id);
    assert_eq!(id, 2);

    addr.send_query(query!(
        index for blog tags string
    ))
    .await
    .unwrap();

    info!("Indexing is ok");

    let mut docs = addr
        .send_query(query!(
            find BlogPost in blog
            where tags == "psychology"
                order asc
        ))
        .await
        .unwrap();

    info!("Number of found documents: {}", docs.size_hint().0);

    assert_eq!(docs.size_hint(), (1, Some(1)));

    let doc = docs.next().unwrap().unwrap();

    info!("Found document: {:?}", doc);

    let doc_data: BlogPost = from_value(json!({
        "id": 1,
        "title": "Absurd",
        "tags": ["absurd", "psychology"],
        "content": "Still nothing..."
    }))
    .unwrap();

    assert_eq!(&doc, &doc_data);
    assert!(docs.next().is_none());
}
```

## REST-interface

*LEDB HTTP interface 0.1.0*

### Storage API

#### get database info

__GET__ /info

#### get database statistics

__GET__ /stats

### Collection API

#### get list of collections

__GET__ /collection

#### create new empty collection

__POST__ /collection?name=_$collection_name_

#### drop collection with all documents

__DELETE__ /collection/_$collection_name_

### Index API

#### get indexes of collection

__GET__ /collection/_$collection_name_/index

#### create new index for collection

__POST__ /collection/_$collection_name_/index?path=_$field_name_&kind=_$index_kind_&key=_$key_type_

#### drop index of collection

__DELETE__ /collection/_$collection_name_/document/_$index_name_

### Document API

#### find documents using query

__GET__ /collection/_$collection_name_/document?filter=_$query_&order=_$ordering_&offset=_$skip_&length=_$take_

__GET__ /collection/_$collection_name_?filter=_$query_&order=_$ordering_&offset=_$skip_&length=_$take_

#### modify documents using query

__PUT__ /collection/_$collection_name_/document?filter=_$query_&modify=_$modifications_

__PATCH__ /collection/_$collection_name_?filter=_$query_&modify=_$modifications_

#### remove documents using query

__DELETE__ /collection/_$collection_name_/document?filter=_$query_

__PUT__ /collection/_$collection_name_?filter=_$query_

#### insert new document

__POST__ /collection/_$collection_name_/document

__POST__ /collection/_$collection_name_

#### get document by id

__GET__ /collection/_$collection_name_/document/_$document_id_

__GET__ /collection/_$collection_name_/_$document_id_

#### replace document

__PUT__ /collection/_$collection_name_/document/_$document_id_

__PUT__ /collection/_$collection_name_/_$document_id_

#### remove document

__DELETE__ /collection/_$collection_name_/document/_$document_id_

__DELETE__ /collection/_$collection_name_/_$document_id_

*/

mod actor;
mod extra;
mod macros;
#[cfg(feature = "web")]
mod scope;

pub use ledb::{
    KeyType, Modify, Options, Order, OrderKind, Primary, Stats, _query_impl, query_extr, Action,
    Comp, Cond, Document, DocumentsIterator, Filter, Identifier, IndexKind, Info, KeyData,
    KeyField, KeyFields, Value,
};

pub use actor::*;
pub use extra::*;

#[cfg(feature = "web")]
pub use scope::*;