Skip to main content

Crate larust_repository

Crate larust_repository 

Source
Expand description

A storage-agnostic CRUD contract - the “any backend, including a non-SQL one” half of Larust’s persistence story. #[derive(Model)]/ QueryBuilder (in larust-orm) remain the SQL-family story - SQLite, MySQL, and (as of this session) Postgres, all via sqlx::Any. Adding Postgres was real, structurally significant work, not a footnote: Any does not rewrite ? placeholders to Postgres’s $1, $2, ... syntax (confirmed by reading sqlx’s own source), so every SQL string and QueryBuilder’s own dynamic condition-rendering needed real backend-aware placeholder handling - see query_builder.rs’s own doc comment for the full account, including the empirically-confirmed finding that Postgres, unlike SQLite/MySQL, has no bool/TEXT decode gap through Any. This crate and Repository<T> are unaffected by any of that - Repository<T> exists for everything sqlx::Any structurally cannot reach at all, not for another SQL dialect: a document store like Firestore, DynamoDB, or MongoDB has no SQL text, no columns, and no shared wire protocol with a SQL database, so that abstraction has to live above sqlx entirely, as a plain trait an app implements by hand. SQL Server is the concrete, now-shipped example: sqlx has no driver for it at all (confirmed - no vendored sqlx-mssql/tiberius integration exists anywhere), so it can never become a Backend variant the way Postgres did - the larust-mssql crate implements Repository<T> by hand against tiberius instead, with a real worked example (larust-mssql/tests/widget_repository.rs) verified end to end against a real local SQL Server server, proving this crate’s whole premise concretely, not just for a hypothetical Firestore/DynamoDB. That verification pass caught a real SQL Server semantics gotcha along the way - see widget_repository.rs’s own create() for what SCOPE_IDENTITY() gets wrong through tiberius and why OUTPUT INSERTED.id is the correct fix.

This mirrors larust_auth::Authenticatable’s own existing shape (see that trait’s doc comment): a small, storage-agnostic interface that a SQL-family app satisfies with a two-line delegation into #[derive(Model)]’s generated methods, and that a non-SQL app implements directly against its own store. larust-orm’s AnyRepository<T> is the SQL-family delegation; a Firestore-backed app writes its own implementation the same way it already hand-writes Authenticatable::find_for_auth today.

Deliberately out of scope, on purpose, not as an oversight:

  • No relations. has_many/belongs_to/belongs_to_many-style loading is entirely QueryBuilder-coupled (WHERE IN (...) batch loaders, hand-written JOINs for many-to-many) and doesn’t generalize to a document store, which usually models relations as reference fields or subcollections looked up in backend-specific ways. A non-SQL app hand-writes its own relation-loading methods directly on its model type.
  • No migrations. larust_orm::migrate is inherently SQL-text oriented (.sql files, a bookkeeping table). A document store is schemaless - collections appear implicitly on first write - so there is nothing for a non-SQL app to migrate; it simply never calls xr migrate.
  • No pagination, sorting, or aggregate helpers. Those stay backend-specific methods an app adds to its own repository type, same as belongs_to_many’s attach/detach/sync methods already live outside QueryBuilder’s scope for the SQL-family case.

Filter is deliberately opaque to this trait rather than a shared query DSL: an SQL-family implementation might use a small enum of (column, value) conditions (or, as larust-orm’s own AnyRepository<T> does, a partially-built QueryBuilder<T>); a Firestore implementation might use a native query type. Callers of a specific Repository implementation are expected to know its concrete Filter type, the same way calling code today already commits to QueryBuilder’s own calling convention.

Traits§

Repository
Storage-agnostic CRUD contract. Implemented automatically for any #[derive(Model)] SQL-family struct via larust_orm::AnyRepository<T> (a thin wrapper over the existing QueryBuilder/Model machinery - see that type’s own doc comment), and by hand for a non-SQL backend such as Firestore, DynamoDB, or MongoDB.