Skip to content

Commit 64d1ce8

Browse files
author
Bennett Hardwick
committed
Update docs
1 parent eedf4ec commit 64d1ce8

File tree

1 file changed

+66
-23
lines changed

1 file changed

+66
-23
lines changed

src/lib.rs

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
//! ## Usage
88
//!
99
//! To use Cryptonamo, you must first create a table in DynamoDB.
10-
//! The table must have a primary key and sort key, both of type String.
10+
//! The table must have a at least partition key, sort key, and term field - all of type String.
11+
//!
12+
//! Cryptonamo also expects a Global Secondary Index called "TermIndex" to exist if you want to
13+
//! search and query against records. This index should project all fields and have a key schema
14+
//! that is a hash on the term attribute.
1115
//!
1216
//! You can use the the `aws` CLI to create a table with an appropriate schema as follows:
1317
//!
@@ -22,14 +26,15 @@
2226
//! AttributeName=pk,KeyType=HASH \
2327
//! AttributeName=sk,KeyType=RANGE \
2428
//! --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
25-
//! --global-secondary-indexes "IndexName=TermIndex,KeySchema=[{AttributeName=term,KeyType=HASH},{AttributeName=pk,KeyType=RANGE}],Projection={ProjectionType=ALL},ProvisionedThroughput={ReadCapacityUnits=5,WriteCapacityUnits=5}"
29+
//! --global-secondary-indexes "IndexName=TermIndex,KeySchema=[{AttributeName=term,KeyType=HASH}],Projection={ProjectionType=ALL},ProvisionedThroughput={ReadCapacityUnits=5,WriteCapacityUnits=5}"
2630
//! ```
2731
//!
2832
//! See below for more information on schema design for Cryptonamo tables.
2933
//!
3034
//! ### Annotating a Cryptanomo Type
3135
//!
32-
//! To use Cryptonamo, you must first annotate a struct with the `Cryptonamo` derive macro.
36+
//! To use Cryptonamo, you must first annotate a struct with the `Encryptable` derive macro, as
37+
//! well as the `Searchable` and `Decryptable` macros if you want to support those features.
3338
//!
3439
//! ```rust
3540
//! use cryptonamo::{Searchable, Decryptable, Encryptable};
@@ -42,7 +47,7 @@
4247
//! }
4348
//! ```
4449
//!
45-
//! The `Cryptonamo` derive macro will generate implementations of the following traits:
50+
//! These derive macros will generate implementations for the following traits of the same name:
4651
//!
4752
//! * `Decryptable` - a trait that allows you to decrypt a record from DynamoDB
4853
//! * `Encryptable` - a trait that allows you to encrypt a record for storage in DynamoDB
@@ -52,8 +57,9 @@
5257
//!
5358
//! ### Controlling Encryption
5459
//!
55-
//! By default, all fields on a `Cryptanomo` type are encrypted and stored in the index.
56-
//! To store a field as a plaintext, use the `plaintext` attribute:
60+
//! By default, all fields on an annotated struct are stored encrypted in the table.
61+
//!
62+
//! To store a field as a plaintext, you can use the `plaintext` attribute:
5763
//!
5864
//! ```rust
5965
//! use cryptonamo::{Searchable, Decryptable, Encryptable};
@@ -69,16 +75,6 @@
6975
//! }
7076
//! ```
7177
//!
72-
//! Most basic rust types will work automatically but you can implement a conversion trait for [Plaintext] to support custom types.
73-
//!
74-
//! ```ignore
75-
//! impl From<MyType> for Plaintext {
76-
//! fn from(t: MyType) -> Self {
77-
//! t.as_bytes().into()
78-
//! }
79-
//! }
80-
//! ```
81-
//!
8278
//! If you don't want a field stored in the the database at all, you can annotate the field with `#[cryptonamo(skip)]`.
8379
//!
8480
//!```rust
@@ -95,9 +91,11 @@
9591
//! }
9692
//! ```
9793
//!
94+
//! If you implement the `Decryptable` trait these skipped fields need to implement `Default`.
95+
//!
9896
//! ### Sort keys
9997
//!
100-
//! Cryptanomo requires every record to have a sort key and it derives it automatically based on the name of the struct.
98+
//! Cryptanomo requires every record to have a sort key. By default this will be derived based on the name of the struct.
10199
//! However, if you want to specify your own, you can use the `sort_key_prefix` attribute:
102100
//!
103101
//!```rust
@@ -115,10 +113,30 @@
115113
//! }
116114
//! ```
117115
//!
116+
//! #### Dynamic Sort keys
117+
//!
118+
//! Cryptonamo also supports specifying the sort key dynamically based on a field on the struct.
119+
//! You can choose the field using the `#[sort_key]` attribute.
120+
//!
121+
//! ```rust
122+
//! #[derive(Debug, Encryptable)]
123+
//! struct User {
124+
//! #[partition_key]
125+
//! email: String,
126+
//! #[sort_key]
127+
//! name: String,
128+
//!
129+
//! #[cryptonamo(skip)]
130+
//! not_required: String,
131+
//! }
132+
//! ```
133+
//!
134+
//! Sort keys will contain that value and will be prefixed by the sort key prefix.
135+
//!
118136
//! ## Indexing
119137
//!
120138
//! Cryptanomo supports indexing of encrypted fields for searching.
121-
//! Exact, prefix and compound match types are all supported.
139+
//! Exact, prefix and compound match types are currently supported.
122140
//! To index a field, use the `query` attribute:
123141
//!
124142
//! ```rust
@@ -136,7 +154,11 @@
136154
//! ```
137155
//!
138156
//! You can also specify a compound index by using the `compound` attribute.
139-
//! All indexes with the same compound name are combined into a single index.
157+
//! Indexes with the same name will be combined into the one index.
158+
//!
159+
//! Compound index names must be a combination of field names separated by a #.
160+
//! Fields mentioned in the compound index name that aren't correctly annottated will result in a
161+
//! compilation error.
140162
//!
141163
//! ```rust
142164
//! use cryptonamo::Encryptable;
@@ -152,8 +174,28 @@
152174
//! }
153175
//! ```
154176
//!
155-
//! **NOTE:** Compound indexes defined using the `compound` attribute are not currently working.
156-
//! Check out [SearchableRecord] for more information on how to implement compound indexes.
177+
//! It's also possible to add more than one query attribute to support querying records in multiple
178+
//! different ways.
179+
//!
180+
//!
181+
//! ```rust
182+
//! use cryptonamo::Encryptable;
183+
//!
184+
//! #[derive(Debug, Encryptable)]
185+
//! struct User {
186+
//! #[cryptonamo(query = "exact")]
187+
//! #[cryptonamo(query = "exact", compound = "email#name")]
188+
//! #[partition_key]
189+
//! email: String,
190+
//!
191+
//! #[cryptonamo(query = "prefix")]
192+
//! #[cryptonamo(query = "exact")]
193+
//! #[cryptonamo(query = "prefix", compound = "email#name")]
194+
//! name: String,
195+
//! }
196+
//! ```
197+
//! It's important to note that the more annotations that are added to a field the more index terms that will be generated. Adding too many attributes could result in a
198+
//! proliferation of terms and data.
157199
//!
158200
//! ## Storing and Retrieving Records
159201
//!
@@ -327,6 +369,9 @@
327369
//! # }
328370
//! ```
329371
//!
372+
//! Note: if you don't have the correct indexes defined this query builder will return a runtime
373+
//! error.
374+
//!
330375
//! ## Table Verticalization
331376
//!
332377
//! Cryptonamo uses a technique called "verticalization" which is a popular approach to storing data in DynamoDB.
@@ -444,8 +489,6 @@
444489
//!
445490
//! ## Issues and TODO
446491
//!
447-
//! - [ ] Support for plaintext types is currently not implemented
448-
//! - [ ] Using the derive macros for compound macros is not working correctly (you can implement the traits directly)
449492
//! - [ ] Sort keys are not currently hashed (and should be)
450493
//!
451494
pub mod crypto;

0 commit comments

Comments
 (0)