Expand description
§bodega
bodega is a small, relatively simple object store implementation with lots of
“character.” Honestly, though, don’t use this. I have it set up for the way I
like to write my store layer in rust services, and it probably won’t fit your
use case and I have no intention of supporting things beyond my own needs.
It’s loosely based on the controller setup in this
series
by Jeremy Chone, and seeks to use more of the sea_query built-in stuff like
the Iden derivation, while also providing generation of CRUD methods on
controllers and various trait derivations.
This will only work with postgres, and is intended to complement a store layer
using sqlx and sea_query, among other things.
Again, you probably don’t want to use this.
§Example
use bodega::{
Select, Insert, Update, Filter, CursoredFilter, DbBmc, IdType, uuid_id
};
use chrono::{DateTime, Utc};
use derive_builder::Builder;
use sea_query::Expr;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[uuid_id]
pub struct BookId(Uuid);
#[derive(Debug, Clone, PartialEq, Eq, sqlx::FromRow, Select)]
#[sea_query::enum_def]
pub struct Book {
#[select(cursor)]
id: BookId,
title: String,
author: String,
pages: i64,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Insert)]
#[insert(iden_enum = BookIden)]
pub struct BookCreate {
title: String,
author: String,
pages: i64,
}
#[derive(Debug, Clone, Update)]
#[update(iden_enum = BookIden)]
pub struct BookUpdate {
title: Option<String>,
author: Option<String>,
pages: Option<i64>,
updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Builder)]
pub struct BookFilters {
#[builder(default = 20)]
limit: usize,
#[builder(setter(strip_option), default)]
cursor: Option<BookId>,
#[builder(setter(strip_option), default)]
author: Option<String>,
}
impl CursoredFilter for BookFilters {
type Entity = Book;
fn cursor(&self) -> Option<<Self::Entity as bodega::Cursored>::CursorType> {
self.cursor
}
fn set_cursor(&mut self, cursor: <Self::Entity as bodega::Cursored>::CursorType) {
self.cursor = Some(cursor);
}
fn page_limit(&self) -> usize {
self.limit
}
}
impl Filter for BookFilters {
fn filter_query(&self, query: &mut sea_query::SelectStatement) {
if let Some(ref author) = self.author {
query.and_where(Expr::col(BookIden::Author).eq(author));
}
}
}
#[derive(Debug, Clone, DbBmc)]
#[db_bmc(
model = Book,
id_type = BookId,
methods(
create = BookCreate,
get,
list,
update = BookUpdate,
delete,
count,
)
)]
pub struct BookBmc;Structs§
- Custom
Option - A wrapper around option for use in produing expressions that can be Null.
- DbModel
Manager - Acts as an interface to a db connection pool that is Clone + Send + Sync.
- Paginated
- A wrapper around the entities returned from the database that also includes information required for requesting the next page of entries.
- Transaction
- We wrap a transaction in this type to prevent a caller from outside of this crate having direct access to the transaction, and therefore access to an executor that can manipulate the database without going through the exposed interfaces of this crate.
Enums§
Traits§
- AsExecutor
- Indicates that this type can provide an executor.
- Cursored
- Indicated that the given type can produce a cursor for use in pagination.
- Cursored
Filter - Indicates the given type can be used for filtering paginated entries for a model manager.
- DbBmc
- The core trait that defines a type that acts as a database model controller.
- Filter
- Indicates that this type can add filtering conditions to select statements.
- IdType
- Indicates that this type can be used as an ID for the purposes of model controllers.
- Insert
- Indicates that this type supports insertion into the db by defining the colums that should be inserted along with their values.
- Select
- Indicates that this type supports selection from the db by defining the columns that should be fetched.
- Update
- Indicates that this type supports updating a row in the db by defining (column, value) pairs.
Functions§
- count
- Counts all of the rows in a model manager’s table.
- create
- Insert a new row into the model manager’s table using the specified executor.
- delete
- Delete a row in the model manager’s table, using the specified executor and id.
- get
- Get a row from the model manager’s table using the specified id and executor.
- list
- List all rows from the model manager’s table using the specified executor.
- list_
paginated - Get a page of rows from the model manager’s table using the specified executor and filters.
- new_
db_ pool - update
- Update a row in the model manager’s table using the specified executor, id, and data.
Type Aliases§
Attribute Macros§
- store_
enum - Modifies an enum corresponding to a postgres enum to support various
sea_queryoperations. - uuid_id
- Modifies a newtype in the form of
Foo(Uuid)to have functionality that makes it compatible with a store layer.
Derive Macros§
- DbBmc
- Implement
DbBmcon a type, and optionally add basic CRUD implementations. - Insert
- Derives an implementation for
Inserton a struct with named fields, allowing that struct to be used to be used to create an entry in the store. - Json
Value - Adds conversions from a type to a
serde_json::Valuefor use withsea_query. - Select
- Derives an implementation for the
Selecttrait on a struct with named fields, allowing it to be constructed from a response from a query against the store. - Update
- Derives an implementation for
Updateon a struct with named fields, allowing that struct to be used to be used to update an entry in the store.