24 releases
| 0.2.0 | Oct 22, 2024 |
|---|---|
| 0.1.24 | Dec 29, 2023 |
| 0.1.20 | Oct 26, 2023 |
| 0.1.14 | Jul 9, 2023 |
| 0.1.0 | Jan 20, 2022 |
#1730 in Database interfaces
26 downloads per month
Used in 3 crates
39KB
896 lines
Catalytic: ScyllaDB and Cassandra Object-Relation Mapper
This library contains several crates with the following features:
- Automatic map tables to Rust
structs: catalytic_table_to_struct. See Usage. See the generated dir for the generated code. - Compile time checked queries: catalytic_macro (
catalytic_macro::query). See Usage. - Plugin derive macro to generate business logic: catalytic_macro (
catalytic_macro::mirror) - Automatic JSON mapping. When there is a column with type
text, you can implement theTransformertrait and map it to a type that implementsserde::Serializeandserde::Deserialize - All queries are executed as a prepared statement
- Support for Materialized Views (and mapping between the base table if the columns are the same)
Not all types are supported yet due to https://github.com/scylladb/scylla-rust-driver/issues/104
Query types
Depending on a query, a certain query type can be derived. These can be found here. These are the commonly used query types:
SelectMultiple: Can be used as an iterator to iterate over the rowsSelectUnique: Selects an optional unique row by full primary keySelectUniqueExpect: Same asSelectUnique, but fails if the row doesn't existSelectUniqueExpectwithCountas entity type: has the specialselect_countmethod for queries like "select count(*) from ..."
There are also structs for CRUD operations.
Usage
Automatic map tables to Rust
You can see an example on how to generate Rust structs in the example dir.
- In the build.rs file the
Transformertrait is implemented and used for json mapping - In the generated dir you can see the generated
structs
How to implement it yourself (step-by-step guide how to mimic the example crate):
- Add a build-dependency:
catalytic_table_to_struct = "0.1" - Create a build.rs file
- Optionally implement the
Tranformertrait (or useDefaultTransformer) - Call
catalytic_table_to_struct::generate - Build the project
An example of the structure of the generated code for table 'child' is:
- Child: a
structwith owned values. Can be converted to ChildRef, PrimaryKey and PrimaryKeyRef - ChildRef: a
structwhere the values are all borrowed. Thisstructcan perform insertions. Can be converted to Child, PrimaryKey and PrimaryKeyRef - PrimaryKey: a
structwhich holds the owned values of the primary key fields of Child. Can be converted to PrimaryKeyRef. - PrimaryKeyRef: a
structwhere the values are all borrowed. Thisstructcan perform updates, deletions and selects. Can be converted to PrimaryKey - UpdatableColumn: an
enumwhich holds the enum variants with owned values of columns that can be updated. Can be converted to UpdatableColumnRef. - UpdatableColumnRef: an
enumwhere the values are all borrowed. This is used by PrimaryKeyRef for updates. Can be converted to UpdatableColumn.
Macros
Crate catalytic_macro holds several macros that can be used to ensure compile/type checked queries.
Examples on how this can be used can be found in lib.rs, method qmd.
How to implement it yourself:
- Add dependency:
catalytic_macro = "0.1" - The
querymacro returns the correct query type for the given query - The
query_base_tablemacro transforms aselectquery to the materialized view table, to aselectquery of the base table mirrorandprimary_keycan be used for other derive macros
Used in production
https://www.beezleapp.com/ - Beezle Social allows users to connect which are nearby, discover local events now
Dependencies
~11–21MB
~273K SLoC