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
//! <p align="center"><img width="300" src="https://raw.githubusercontent.com/greyblake/joydb/master/art/rust_joydb_embedded_json_file_database.webp" alt="Joydb - a JSON/CSV database for Rust"></p>
//! <h2 align="center">JSON/CSV file database and ORM for quick prototyping.</h2>
//!
//! Joydb is a Rust library that acts like a database and ORM at the same time and provides a simple way to store and retrieve data in JSON, CSV or any other format.
//!
//! # Getting started
//! Install prerequisites:
//!
//! ```sh
//! cargo install serde --features derive
//! cargo install joydb --features json
//! ```
//!
//! ```
//! # let _ = ::std::fs::remove_file("data.json");
//!
//! use joydb::{Joydb, adapters::JsonAdapter, Model};
//! use serde::{Serialize, Deserialize};
//!
//! // Define your model
//! #[derive(Debug, Clone, Serialize, Deserialize, Model)]
//! struct User {
//! // id is mandatory field for every model.
//! // We use integer here, but most likely you will want to use Uuid.
//! id: u32,
//! username: String,
//! }
//!
//! // Define the state
//! joydb::state! {
//! AppState,
//! models: [User],
//! }
//!
//! // Define the database (combination of state and adapter)
//! type Db = Joydb<AppState, JsonAdapter>;
//!
//! // Create a new database or open an existing one
//! let db = Db::open("data.json").unwrap();
//!
//! let alice = User {
//! id: 1,
//! username: "Alice".to_string(),
//! };
//!
//! // Insert a new user
//! db.insert(&alice).unwrap();
//!
//! // Get the user by ID
//! let user = db.get::<User>(&1).unwrap().unwrap();
//! assert_eq!(user.username, "Alice");
//!
//! # let _ = ::std::fs::remove_file("data.json");
//! ```
//!
//! # CRUD operations
//!
//! | Operation | Methods |
//! |-----------|--------------------------------------------------------------------------------------------------------------|
//! | Create | [`insert`](Joydb::insert), [`upsert`](Joydb::upsert) |
//! | Read | [`get`](Joydb::get), [`get_all`](Joydb::get_all), [`get_all_by`](Joydb::get_all_by), [`count`](Joydb::count) |
//! | Update | [`update`](Joydb::update), [`upsert`](Joydb::upsert) |
//! | Delete | [`delete`](Joydb::delete), [`delete_all_by`](Joydb::delete_all_by) |
//!
//! Please refer to [Joydb] for more details.
//!
//! # Adapters
//!
//! There are 2 types of adapters:
//!
//! - _Unified_ - uses a single file to store the state. It writes and reads the entire state at once. Usually requires a file path.
//! - _Partitioned_ - uses multiple files to store the state. It writes and reads each relation separately. Usually requires directory path.
//!
//! The following adapters are implemented out of the box and can be used with the corresponding
//! feature flag enabled.
//!
//! | Adapter | Format | Type | Feature flag |
//! | ----------------------------------------------------------------- | ------ | ----------- | ------------ |
//! | [JsonAdapter](crate::adapters::JsonAdapter) | JSON | Unified | `json` |
//! | [JsonPartitionedAdapter](crate::adapters::JsonPartitionedAdapter) | JSON | Partitioned | `json` |
//! | [RonAdapter](crate::adapters::RonAdapter) | RON | Unified | `ron` |
//! | [RonPartitionedAdapter](crate::adapters::RonPartitionedAdapter) | RON | Partitioned | `ron` |
//! | [CsvAdapter](crate::adapters::CsvAdapter) | CSV | Paritioned | `csv` |
//!
//! # Sync policy
//!
//! Sync policy defines when exactly the data must be written to the file system.
//!
//! Please see [SyncPolicy] for more details.
//!
//! # Motivation
//!
//! While prototyping new projects, I often needed some form of persistent storage.
//! However, setting up a full-fledged database and ORM felt like overkill for the project's scope.
//! So I'd occasionally fall back to a simple JSON file.
//! As this pattern repeated, I decided to solve the problem once and for all by building Joydb.
//!
//! # Limitation
//!
//! Joydb is designed in the way that it writes the entire database state to a file
//! system at once. This means that it is not suitable for high performance applications or for
//! domains where the data is too large to fit in memory.
//!
//! It's highly recommended to switch to a proper database like PostgreSQL before Joydb turns into
//! Paindb.
//!
//! # License
//!
//! MIT © [Serhii Potapov](https://www.greyblake.com)
pub use ;
pub use JoydbError;
pub use Model;
pub use Relation;
pub use ;
/// A macro to derive the [Model] trait for a struct.
/// A struct must have a field named `id`, which is the primary key.
pub use Model;