Rust ORM for ScyllaDB and Apache Cassandra
Charybdis is a ORM layer on top of ScyllaDB Rust Driver focused on easy of use and performance
Usage considerations:
- Provide and expressive API for CRUD & Complex Statement operations on model as a whole
- Provide easy way to work with subset of model fields by using automatically
generated
partial_<model>!macro - Provide easy way to run complex queries by using automatically generated
find_<model>!macro - Automatic migration tool analyzes the project files and runs migrations according to differences between the model definition and database
Performance consideration:
- It uses prepared statements (shard/token aware) -> bind values
- It expects
CachingSessionas a session arg for operations - Queries are macro generated str constants (no concatenation at runtime)
- By using
find_<model>!macro we can run complex queries that are generated at compile time as&'static str - Although it has expressive API it's thin layer on top of scylla_rust_driver, and it does not introduce any significant overhead
Table of Contents
- Charybdis Models
- Automatic migration with
charybdis-migrate - Basic Operations
- Configuration Options
- Batch Operations
- Callbacks
- Collection
- Ignored fields
- Roadmap
Charybdis Models
Before getting started, ensure that the scylla dependency is included in your Cargo.toml file. The
version of scylla should match the one used by the charybdis crate.
[]
= "1.2.0"
= "1.0.2"
Define Tables
use charybdis_model;
use ;
Define UDT
use charybdis_udt_model;
use Text;
🚨 UDT fields must be in the same order as they are in the database.
Note that in order for migration to correctly detect changes on each migration, type_name has to
match struct name. So if we have struct ReorderData we have to use
#[charybdis_udt_model(type_name = reorderdata)] - without underscores.
Define Materialized Views
use charybdis_view_model;
use ;
Resulting auto-generated migration query will be:
CREATE MATERIALIZED VIEW IF NOT EXISTS users_by_email
AS SELECT created_at, updated_at, username, email, id
FROM users
WHERE email IS NOT NULL AND id IS NOT NULL
PRIMARY KEY (email, id)
Automatic migration
-
charybdis-migrateenables automatic migration to database without need to write migrations by hand. It iterates over project files and generates migrations based on differences between model definitions and database. It supports following operations:- Create new tables
- Create new columns
- Drop columns
- Change field types (drop and recreate column
--drop-and-replaceflag) - Create secondary indexes
- Drop secondary indexes
- Create UDTs
- Create materialized views
- Table options
- ⚠️ If table exists, table options will result in alter table query that without
CLUSTERING ORDERandCOMPACT STORAGEoptions.
- ⚠️ If table exists, table options will result in alter table query that without
Model dropping is not added. If you removed model, you need to drop table manually.
-
Running migration
)-
⚠️ Always run migrations from desired directories ('src' or 'test'), to avoid scanning 'target' or other large directories.
-
⚠️ If you are working with existing datasets, before running migration you need to make sure that your model definitions structure matches the database in respect to table names, column names, column types,partition keys,clustering keys and secondary indexes so you don't alter structure accidentally. If structure is matched, it will not run any migrations. As mentioned above, in case there is no model definition for table, it will not drop it. In future, we will add
modelizecommand that will generatesrc/modelsfiles from existing data source. -
⚠️ Make sure that nested collections are 'Frozen' as per ScyllaDB requirement, so when using
--drop-and-replaceflag, it will drop and recreate columns.
-
-
Programmatically running migrations
Within testing or development environment, we can trigger migrations programmatically:
use MigrationBuilder; let migration = new .keyspace .drop_and_replace .build .await; migration.run.await; -
Global secondary indexes
If we have model:
resulting query will be:
CREATE INDEX ON users (username); -
Local secondary Indexes
Indexes that are scoped to the partition key
resulting query will be:
CREATE INDEX ON menus((location), dish_type);
Basic Operations:
For each operation you need to bring respective trait into scope. They are defined
in charybdis::operations module.
Insert
-
use ; async
Find
-
Find by primary key
let user = User ; let user = user.find_by_primary_key.execute.await?; -
Find by partition key
let users = User .find_by_partition_key.execute.await; -
Find by primary key associated
let users = find_by_primary_key_value.execute.await; -
Available find functions
use CachingSession; use CharybdisError; use charybdis_model; use CharybdisModelStream; use ; -
Custom filtering:
Lets use our
Postmodel as an example:We get automatically generated
find_post!macro that follows conventionfind_<struct_name>!. It can be used to create custom queries.Following will return stream of
Postmodels, and query will be constructed at compile time as&'static str.// automatically generated macro rule let posts = find_post! .execute .await?;We can also use
find_first_post!macro to get single result:let post = find_first_post! .execute .await?;If we just need the
Statementand not the result, we can usefind_post_query!macro:let query = find_post_query!;
Update
-
let user = from_json; user.username = "scylla".to_string; user.email = "some@email.com"; user.update.execute.await; -
Collection:
- Let's use our
Usermodel as an example: push_to_<field_name>andpull_from_<field_name>methods are generated for each collection field.let user: User; user.push_tags.execute.await; user.pull_tags.execute.await; user.push_post_ids.execute.await; user.pull_post_ids.execute.await;
- Let's use our
-
Counter
- Let's define post_counter model:
- We can use
increment_<field_name>anddecrement_<field_name>methods to update counter fields.let post_counter: PostCounter; post_counter.increment_likes.execute.await; post_counter.decrement_likes.execute.await; post_counter.increment_comments.execute.await; post_counter.decrement_comments.execute.await;
- Let's define post_counter model:
Delete
-
let user = from_json; user.delete.execute.await; -
Macro generated delete helpers
Lets use our
Postmodel as an example:We have macro generated functions for up to 3 fields from primary key.
delete_by_date.execute.await?; delete_by_date_and_category_id.execute.await?; delete_by_date_and_category_id_and_title.execute.await?; -
Custom delete queries
We can use
delete_post!macro to create custom delete queries.delete_post!.execute.await?
Configuration
Every operation returns CharybdisQuery that can be configured before execution with method
chaining.
let user: User = find_by_id
.consistency
.timeout
.execute
.await?;
let result: QueryResult = user.update.consistency.execute.await?;
Supported configuration options:
consistencyserial_consistencytimestamptimeoutpage_sizetimestamp
Batch
CharybdisModelBatch operations are used to perform multiple operations in a single batch.
-
Batch Operations
let users: ; let batch = batch; // inserts batch.append_inserts; // or updates batch.append_updates; // or deletes batch.append_deletes; batch.execute.await?; -
Chunked Batch Operations
Chunked batch operations are used to operate on large amount of data in chunks.
let users: ; let chunk_size = 100; batch.chunked_inserts.await?; batch.chunked_updates.await?; batch.chunked_deletes.await?; -
Batch Configuration
Batch operations can be configured before execution with method chaining.
let batch = batch .consistency .retry_policy .chunked_inserts .await?;We could also use method chaining to append operations to batch:
let batch = batch .consistency .retry_policy .append_update .append_update .execute .await?; -
Statements Batch
We can use batch statements to perform collection operations in batch:
let batch = batch; let users: ; for user in users batch.execute.await;
Partial Models
Overview
Partial models allow you to work with a subset of fields from a complete model, making operations more efficient and focused. Each partial model implements the same ORM traits as the original model but only includes the fields you specify.
Usage
Use the auto-generated partial_<model>! macro to create a struct with the same structure as the original model, but
only with the fields you need:
// auto-generated macro - available in crate::models::<original_model>
partial_user!;
This creates a new UpdateUsernameUser struct that is equivalent to the User model, but only with id and
username fields.
let mut update_user_username = UpdateUsernameUser ;
update_user_username.update.execute.await?;
Design Pattern Benefits
- Separation of Concerns: Each partial model handles a specific responsibility (e.g., for image operations)
UpdateCoverImageUserfor updating a user's cover image. - Type Safety: Type system enforces which fields are required for each operation
- Performance: Only reads/writes necessary fields from the database
- Maintainability: Clearer intention in code about what's being modified
Requirements
- The original model must include
#[derive(Default)] - Partial model definitions must include all primary key fields
- The macro should be used in the same file as the original model to access the same imports
Inheritance of Attributes
Partial models inherit:
- All derives defined after the
#[charybdis_model]macro - All field attributes from the original model (e.g.,
#[serde(rename = "rootId")]) - All ORM capabilities of the original model
As Native
In case we need to run operations on native model, we can use as_native method:
let native_user: User = update_user_username.as_native.find_by_primary_key.execute.await?;
// action that requires native model
authorize_user;
Naming Convention
For clarity, follow the pattern: Purpose + Original Struct Name.
Examples:
UpdateAddressUser- For updating a user's address- For updating a user's cover image
UpdateCoverImageUser - For authentication/authorization operations on a post
AuthPost
Callbacks
Callbacks are convenient way to run additional logic on model before or after certain operations. E.g.
- we can use
before_insertto set default values and/or validate model before insert. - we can use
after_updateto update other data sources, e.g. elastic search.
Implementation:
- Let's say we define custom extension that will be used to
update elastic document on every post update:
- Now we can implement Callback that will utilize this extension:
-
Possible Callbacks:
before_insertbefore_updatebefore_deleteafter_insertafter_updateafter_delete
-
Triggering Callbacks
In order to trigger callback we use<operation>_cb. method:insert_cb,update_cb,delete_cbaccording traits. This enables us to have clear distinction betweeninsertand insert with callbacks (insert_cb). Just as on main operation, we can configure callback operation query before execution.use ; post.insert_cb.execute.await; post.update_cb.execute.await; post.delete_cb.consistency.execute.await;
Collections
For each collection field, we get following:
PUSH_<field_name>_QUERYstatic strPUSH_<field_name>_IF_EXISTS_QUERYstatic str'PULL_<field_name>_QUERYstatic strPULL_<field_name>_IF_EXISTS_QUERYstatic strpush_<field_name>methodpush_<field_name>_if_existsmethodpull_<field_name>methodpull_<field_name>_if_existsmethod
-
Model:
-
Generated Collection Queries:
Generated query will expect value as first bind value and primary key fields as next bind values.
Now we could use this constant within Batch operations.
let batch = batch; let users: ; for user in users batch.execute.await; -
Generated Collection Methods:
push_to_<field_name>andpull_from_<field_name>methods are generated for each collection field.let user: new; user.push_tags.execute.await; user.push_tags_if_exists.execute.await; user.pull_tags.execute.await; user.pull_tags_if_exists.execute.await; user.push_post_ids.execute.await; user.push_post_ids_if_exists.execute.await; user.pull_post_ids.execute.await; user.pull_post_ids_if_exists.execute.await; user.push_books_by_genre.execute.await; user.push_books_by_genre_if_exists.execute.await; user.pull_books_by_genre.execute.await; user.pull_books_by_genre_if_exists.execute.await;
Ignored fields
We can ignore fields by using #[charybdis(ignore)] attribute:
So field organization will be ignored in all operations and
default value will be used when deserializing from other data sources.
It can be used to hold data that is not persisted in database.
Custom Fields
Any rust type can be used directly in table or UDT definition.
User must choose a ScyllaDB backing type (such as "TinyInt" or "Text")
and implement SerializeValue and DeserializeValue traits:
See custom_field.rs integration test for examples using int and text encoding.