Aragog
aragog is a simple lightweight ODM library for ArangoDB using the arangors driver.
The main concept is to provide behaviors allowing to synchronize documents and structs as simply an lightly as possible.
In the future versions aragog will also be able to act as a ORM and OGM for ArangoDB
Features
By now the available features are:
- Creating a database connection pool from a defined
schema.json - Structures can implement different behaviors:
Record: The structure can be written into a ArangoDB collection as well as retrieved, from its_keyor other query arguments.New: The structure can be initialized from an other type (a form for example). It allows to maintain a privacy level in the model and to use different data formats.Update: The structure can be updated from an other type (a form for example). It allows to maintain a privacy level in the model and to use different data formats.Validate: The structure can perform simple validations before being created or saved into the database.Authenticate: The structure can define a authentication behaviour from asecret(a password for example) (seepassword_hashingsection)AuthorizeAction: The structure can define authorization behavior on a target record with custom Action type.
- Different operations can return a
ServiceErrorerror that can easily be transformed into a Http Error (can be used for the actix framework)
Cargo features
Actix Http Error
If you use this crate with the actix framework, you may want the aragog errors to be usable as http errors.
To do so cou can add to your cargo.toml the following feature: actix_http_error.
= { = "0.4.3", = ["actix_http_error"] }
Password hashing
You may want aragog to provide a more complete Authenticate trait allowing to hash and verify passwords.
To do so cou can add to your cargo.toml the following feature: password_hashing.
= { = "0.4.3", = ["password_hashing"] }
It will add two functions in the Authenticate trait:
;
;
hash_passwordwill return a Argon2 encrypted password hash you can safely store to your databaseverify_passwordwill check if the providedpasswordmatches the Argon2 encrypted hash you stored.
The Argon2 encryption is based on the argonautica crate.
That crate requires the clang lib, so if you deploy on docker you will need to install it or define a custom image.
Schema and collections
In order for everything yo work you need to specify a schema.json file. The path of the schema must be set in SCHEMA_PATH environment variable or by default the pool will look for it in src/config/db/schema.json.
There is an example
schema.jsonfile in /examples/simple_food_order_app
The json must look like this:
When initializing the DatabaseConnectionPool every collection name will be searched in the database and if not found the collection will be automatically created.
You don't need to create the collections yourself
Indexes
The array of Index in indexes must have that exact format:
name: the index name,fields: an array of the fields concerned on that compound index,settings: this json bloc must be the serialized version of an IndexSettings variant from arangors driver.
Database Record
The global architecture is simple, every Model you define that can be synced with the database must implement Record and derive from serde::Serialize, serde::Deserialize and Clone.
If you want any of the other behaviors you can implement the associated trait
The final Model structure will be an Exact representation of the content of a ArangoDB document, so without its _key, _id and _rev.
Your project should contain some models folder with every struct representation of your database documents.
The real representation of a complete document is DatabaseRecord<T> where T is your model structure.
Example:
use ;
use ;
use tokio;
async
Querying
You can retrieve a document from the database as simply as it gets, from the unique ArangoDB _key or from multiple conditions.
The example below show different ways to retrieve records, look at each function documentation for more exhaustive explanations.
Example
let record = create.await.unwrap;
// Find with the primary key
let user_record = find.await.unwrap;
// Find a user with multiple conditions
let mut query = new.and;
let user_record = find_where.await.unwrap;
// Find all users with multiple conditions
let mut query = new.and;
let user_records = get_where.await.unwrap;
The querying system hierarchy works this way:
new.and.or
Each comparison is a QueryItem built via QueryItemBuilder:
// for a simple field comparison
field.some_comparison;
// for field veing arrays (see ArangoDB operators)
all.some_comparison;
any.some_comparison;
none.some_comparison;
All the currently implemented comparison methods are listed under QueryItemBuilder documentation page.
TODO
- Query system:
- Simple and modular query system
- Macros for lighter queries
- Advanced query system supporting:
- Arithmetic Operators
- Array variant querying (
ANY,NONE,ALL) - ArangoDB functions (
LENGTH,ABS, etc.)
- ORM and OGM
- Pundit like authorizations (authorize actions on model)
- Relations
- Handle graph vertices and edges
- Handle SQL-like relations (foreign keys)
- Handle key-value pair system (redis like)
- Middle and long term:
- Handle revisions/concurrency correctly
- Code Generation
- Avoid string literals as collection names
- Handle Migrations
- Define possible
asyncvalidations for database advance state check
Arango db setup
Installation (See official documentation [Here] arango_doc)
- Download Link
- Run it with
/usr/local/sbin/arangodThe default installation contains one database_systemand a user namedroot - Create a user and database for the project with the
arangoshshell
arangosh> db._createDatabase("DB_NAME");
arangosh> var users = require("@arangodb/users");
arangosh> users.save("DB_USER", "DB_PASSWORD");
arangosh> users.grantDatabase("DB_USER", "DB_NAME");
It is a good practice to create a test db and a development db.
- you can connect to the new created db with
$> arangosh --server.username $DB_USER --server.database $DB_NAME
License
aragog is provided under the MIT license. See LICENSE.
An simple lightweight ODM for ArangoDB based on arangors.