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
//! This library provides several traits in order to make database access a lot
//! easier. In addition when using `naphtha`, it is possible to change
//! the database that is used for specific models in your application without
//! the requirement of changing any code.
//!
//! It implements the most common operations on a database like `insert`, `update`
//! and `remove` for you, while also providing the ability to send custom queries
//! to the database.
//! In addition to that, when using the `barrel-XXX` features, you can write your
//! SQL migrations and use them in your application during runtime.
//! See the [examples](#examples) below.
//!
//! ## Features overview
//!
//! * Most common function implementations `insert`, `update`, `remove` for your
//! models.
//! * Custom transactions provided by the `custom` function
//! * [DatabaseUpdateHandler](DatabaseUpdateHandler) enables you to change the models values before and
//! after the `update` transaction to the database.
//! * Change database on specific model in your application without the need to
//! change your code.
//! * Possibility to query a model from the database by using one of its member.
//! * Integrated [barrel] for writing your SQL migrations and the possibility to apply them during
//! runtime.
//! * Thread safe handling of the database connection.
//!
//! ## Supported databases
//!
//! * SQlite3 (using [diesel](diesel) under the hood).
//! * MySQL (using [diesel](diesel) under the hood).
//! * PostgreSQL (using [diesel](diesel) under the hood).
//!
//! ## Examples
//!
//! In this chapter, minimal usages are shown. Please have a look at the examples
//! in the repository for more and detailed use.
//!
//! ### Connecting to a database
//!
//! ```rust
//! use naphtha::{DatabaseConnection, DatabaseConnect};
//! // This is the only line required to be changed to switch database types.
//! type DbBackend = diesel::SqliteConnection;
//! let db: DatabaseConnection<DbBackend> = DatabaseConnection::connect(":memory:").unwrap();
//! // do some database work
//! ```
//!
//! ### Defining a model and use database connection
//!
//! To create a model and its database integration, the following code is required.
//!
//! *Note that this is an excerpt, see the `examples` folder in the repository for
//! a full working example.*
//!
//! ```ignore
//! #[model(table_name = "persons")]
//! pub struct Person {
//! id: i32,
//! pub description: Option<String>,
//! pub updated_at: NaiveDateTime,
//! }
//!
//! pub mod schema {
//! table! {
//! persons (id) {
//! id -> Int4,
//! description -> Nullable<Varchar>,
//! updated_at -> Timestamp,
//! }
//! }
//! }
//!
//! impl DatabaseModel for Person {
//! type PrimaryKey = i32;
//! fn primary_key(&self) -> Self::PrimaryKey {
//! self.id
//! }
//!
//! fn set_primary_key(&mut self, value: &Self::PrimaryKey) {
//! self.id = *value;
//! }
//!
//! fn default_primary_key() -> Self::PrimaryKey {
//! 0
//! }
//!
//! fn table_name() -> &'static str {
//! "persons"
//! }
//! }
//!
//! // Define your custom changes to the model before and after the transactions.
//! impl<T> naphtha::DatabaseUpdateHandler<T> for Person {}
//! impl<T> naphtha::DatabaseRemoveHandler<T> for Person {}
//! impl<T> naphtha::DatabaseInsertHandler<T> for Person {}
//!
//! // This implements your database migration functions.
//! impl DatabaseSqlMigration for Person {
//! fn migration_up(migration: &mut Migration) {
//! use naphtha::DatabaseModel;
//! migration.create_table_if_not_exists(Self::table_name(), |t| {
//! t.add_column("id", types::primary());
//! t.add_column("description", types::text().nullable(true));
//! t.add_column("updated_at", types::custom("timestamp"));
//! });
//! }
//!
//! fn migration_down(migration: &mut Migration) {
//! use naphtha::DatabaseModel;
//! migration.drop_table_if_exists(Self::table_name());
//! }
//! }
//!
//! fn main() {
//! use naphtha::{DatabaseConnection, DatabaseConnect};
//! let db = DatabaseConnection::connect(":memory:").unwrap();
//! // p is to be mutable because the insert function updates the id member
//! // to the one given by the database.
//! let mut p = Person {
//! id: Person::default_primary_key(),
//! description: Some("The new person is registered".into()),
//! };
//! p.insert(&db);
//! // id member is set to the correct number given by the database.
//!
//! // do a custom query to the database
//! db.custom::<diesel::result::QueryResult::<Person>, _>(|c: &DbBackend| {
//! use schema::{persons, persons::dsl::*};
//! persons.filter(id.eq(1)).first(c)
//! });
//!
//! p.remove(&db);
//! // p not available anymore in the database
//! }
//! ```
pub use diesel;
pub extern crate log;
use ;
/// Defines your `struct` as a model and implements the required traits for
/// interacting with the database. Currently only *named* `struct` member are
/// supported.
pub use model;
/// Re-exports the [barrel] crate including small trait additions required by naphtha.
/// Thin wrapper around a [Connection](diesel::Connection).
;
/// Contains functions database connection handling.
/// Defines the relation of the model to the database.
/// Defines functions to modify the stored model instance on the database.
/// Methods that are called before and after the transaction executed when
/// the [insert](DatabaseModelModifier::insert) method is called.
/// Can be used to do custom changes to the database or the model instance.
/// Useful for extending the basic CRUD model.
/// Methods that are called before and after the transaction executed when
/// the [update](DatabaseModelModifier::update) method is called.
/// Can be used to do custom changes to the database or the model instance.
/// Useful for extending the basic CRUD model.
/// Methods that are called before and after the transaction executed when
/// the [remove](DatabaseModelModifier::remove) method is called.
/// Can be used to do custom changes to the database or the model instance.
/// Useful for extending the basic CRUD model.