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
/*!
# 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:
```
extern crate actix;
extern crate futures;
extern crate tokio;
#[macro_use]
extern crate serde_derive;
// This allows inserting JSON documents
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate ledb;
#[macro_use]
extern crate ledb_actix;
// This allows define typed documents easy
#[macro_use]
extern crate ledb_derive;
extern crate ledb_types;
#[macro_use]
extern crate log;
extern crate pretty_env_logger;
use actix::System;
use futures::Future;
use ledb_actix::{Storage, Options, StorageAddrExt, Primary, Identifier, Document};
use serde_json::from_value;
use std::env;
use tokio::spawn;
#[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,
}
fn main() {
env::set_var("RUST_LOG", "info");
pretty_env_logger::init().unwrap();
let _ = std::fs::remove_dir_all("example_db");
System::run(|| {
let addr = Storage::new("example_db", Options::default()).unwrap().start(1);
spawn(
addr.clone().send_query(query!(
insert into blog {
"title": "Absurd",
"tags": ["absurd", "psychology"],
"content": "Still nothing..."
}
)).and_then({ let addr = addr.clone(); move |id| {
info!("Inserted document id: {}", id);
assert_eq!(id, 1);
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."
}
))
} }).and_then({ let addr = addr.clone(); move |id| {
info!("Inserted document id: {}", id);
assert_eq!(id, 2);
addr.send_query(query!(
index for blog tags string
))
} }).and_then({ let addr = addr.clone(); move |_| {
info!("Indexing is ok");
addr.send_query(query!(
find BlogPost in blog
where tags == "psychology"
order asc
))
} }).map(|mut docs| {
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());
System::current().stop();
}).map_err(|err| {
error!("Error: {:?}", err);
})
);
});
}
```
## 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_
*/
extern crate actix;
#[allow(unused_imports)]
#[macro_use(_query_impl, _query_extr)]
extern crate ledb;
extern crate serde;
extern crate futures;
#[cfg(test)]
extern crate tokio;
#[cfg(any(test, feature = "web"))]
#[macro_use]
extern crate serde_derive;
#[cfg(test)]
#[macro_use]
extern crate serde_json;
#[cfg(feature = "web")]
extern crate serde_with;
#[cfg(feature = "web")]
extern crate actix_web;
mod actor;
mod extra;
mod macros;
#[cfg(feature = "web")]
mod scope;
pub use ledb::{
Action, Comp, Cond, Document, DocumentsIterator, Filter, Identifier, IndexKind, Info, KeyData,
KeyField, KeyFields, KeyType, Modify, Options, Order, OrderKind, Primary, Stats, Value,
};
pub use actor::*;
pub use extra::*;
#[cfg(feature = "web")]
pub use scope::*;