Vantage Table
Type-safe table abstractions with ActiveEntity support for database-agnostic CRUD operations.
Quick Start
use *;
use Table;
// Create table with any compatible data source
let table = new
.
.
.;
// Load and modify entities
let mut user = table.get_entity.await?
.unwrap_or_else;
// Direct field modification
user.active = true;
user.email = "updated@example.com".to_string;
// Automatic persistence
user.save.await?;
Features
- ActiveEntity Pattern: Load entities, modify fields directly, and save changes automatically
- Database Agnostic: Works with CSV, PostgreSQL, MySQL, SQLite, SurrealDB, MongoDB, and custom data sources
- Type Safety: Full compile-time validation of entity structure and field access
- Flexible Queries: Integration with vantage-expressions for complex query building
- Relationships: Same-persistence traversal via
with_one/with_many. Cross-persistence references live in vantage-vista-factory'sVistaCatalog
Core Operations
Data Source
A Table is parameterized by its data source — the persistence that stores and retrieves records.
You pass it when constructing the table, and it determines what condition syntax you use, how
queries execute, and what types flow through:
// PostgreSQL — SQL expressions, connection pool
let pg_db = connect.await?;
let users = new
.with_id_column
.;
// MongoDB — BSON documents, collection-based
let mongo_db = connect.await?;
let users = new
.with_id_column
.;
// CSV — file-based, read-only
let csv = new;
let users = new
.;
The same entity struct works with any persistence. Model crates typically provide a constructor per persistence:
Columns
Define table structure with typed columns for precise querying:
let users_table = new
.
.
.;
// Add conditions to filter records
let active_users = users_table
.with_condition;
// Work with filtered dataset
for mut user in active_users.list_entities.await?
Conditions
Conditions can be added in two ways.
Using columns and operators — the same call syntax on every persistence. Each driver ships its
own operation trait (PostgresOperation, SqliteOperation, MongoOperation, SurrealOperation,
...) that provides .eq(), .gt(), .lt(), .gte(), .lte(), .ne(), .in_() on its columns
and builds that driver's condition type:
// Works on PostgreSQL, SQLite, MongoDB, SurrealDB — any persistence
products.add_condition;
products.add_condition;
Using persistence-specific syntax — for queries that need native features beyond what the generic operators express:
// PostgreSQL — vendor-specific SQL function
let mut products = new;
products.add_condition;
// MongoDB — native BSON operators
let mut products = new;
products.add_condition;
Either way, the effect is the same: the table now addresses a subset of your records. Every
subsequent operation honours the conditions — list(), get_count(), get_sum(), delete_all(),
relationship traversal, and any other operation on the table. A conditioned table isn't a query
result — it's a narrowed view of your data set.
Data Modeling
Entity-based modeling is a way to abstract persistence behind a unified interface. A single model file defines how an entity is stored — columns, relationships, business rules — for each persistence it needs to support. The rest of your application works with the model and doesn't know or care where the data lives:
// models/user.rs
Table<PostgresDB, User> retains full type information — you have access to PostgreSQL-specific
expressions, typed columns, and relationship traversal. A trait on the typed table can expose domain
methods that hide persistence details completely. The caller doesn't know table names, column
layouts, or SQL syntax — and may not even be aware whether data comes from PostgreSQL or has been
migrated to a REST API:
When you need to pass a table into generic code that doesn't care about the persistence type (CLI
rendering, admin panels, API handlers), turn it into a Vista with the driver's
vista_factory().from_table() to erase the types — see the Type erasure
section below.
Rest of your business logic doesn't build raw SQL or reference column names. If the storage changes, only the model file changes. Rest of the code is unchanged or has minimal changes:
// Today — discount computed via PostgreSQL subquery
let discount = users.discount_expr.get.await?;
// Tomorrow — persistence switched to RestAPI, same caller code
let discount = users.discount_expr.get.await?; // unchanged
Relationships
Vantage has unique relationship traversal. References are defined through expressions, traversing them is almost zero-cost:
let active_subscription = current_user.ref_active_subscription;
let orders = current_user.ref_orders;
You can decide when to execute queries and how:
if active_subscription.get_some_value.await?.is_some
A typed Table<T, E> is single-backend by construction. Relationships that cross persistence
boundaries (e.g. PostgreSQL users → billing API subscriptions) are no longer defined on the table
(with_foreign() was removed). They live one layer up, in vantage-vista-factory's VistaCatalog,
which builds models by name and follows a Relation from a parent row into another datasource.
Type erasure (Vista)
AnyTable has been removed. Type erasure now goes through Vista from vantage-vista: each driver
has a vista_factory() whose from_table() turns a typed table into a Vista. A Vista exposes the
same value-level interface for every persistence, with ciborium::Value values and String ids.
This is useful for generic code — CLI tools, API handlers, admin panels — that doesn't need to know
which database is behind it:
// Wrap any typed table
let vista = db.vista_factory.from_table?;
// Same interface regardless of persistence
let records = vista.list_values.await?;
let count = vista.get_count.await?;
vista.insert_value.await?;
vista.delete.await?;
A CLI tool that works with multiple persistence sources at runtime:
// Each driver's `vista_factory()` needs that driver's `vista` feature.
let vista: Vista = match source ;
// All commands work identically — list, count, insert, delete
let records = vista.list_values.await?;
render_records;
Most application code works directly with Table<T, E> instead — this gives access to typed
entities, persistence-specific conditions, and relationship traversal. Vista is for the layer
above, where persistence choice is a runtime decision. See the vantage-vista README for the rest of
its interface.
Loading Records
There are multiple ways to load records depending on your needs:
get_entity(id)— Load ActiveEntity by ID (returnsNoneif not found)get_value(id)— Load raw record data by ID (returnsNoneif not found)list()— Load all raw entities asIndexMap<Id, Entity>list_entities()— Load all entities asVec<ActiveEntity>list_values()— Load all raw record data
// Load raw entities (no save functionality)
let users: = table.list.await?;
for in users
// Load raw record data for inspection
if let Some = table.get_value.await?
Get-or-Create Pattern
let mut user = table.get_entity.await?
.unwrap_or_else;
Table Metadata
Various ways to interact with table metadata and statistics:
// Get record count
let count: i64 = table.get_count.await?;
// Get count as AssociatedExpression for use in other queries
let count_expr = table.get_expr_count;
let result = count_expr.get.await?;
// Aggregated values (also get_expr_sum / get_expr_min)
let max_age = table.get_expr_max;
let max_value = max_age.get.await?;
// Plain expressions, without a data source attached
let count_query = table.get_count_query;
// Select query builder, pre-filled with the table's columns and conditions
let select = table
.select
.with_condition
.with_limit;
let results = table.data_source.execute_select.await?;
Diverse Data Persistence Support
Vantage Table works with a wide range of data sources through modular adapter crates:
Available Adapters
- vantage-sql: PostgreSQL, MySQL, SQLite via vendor-aware SQL generation
- vantage-mongodb: MongoDB with native BSON conditions (no SQL)
- vantage-surrealdb: SurrealDB with native SurrealQL
- vantage-csv: CSV file persistence with type-safe column parsing
- vantage-api-client: REST API persistence with pagination
- [
MockTableSource]: In-memory testing and development mock
Create Your Own Persistence Adapter
A persistence adapter plugs into the Vantage framework by implementing traits at two levels. Once
implemented, Table<T, E> automatically bridges these into the full ReadableValueSet,
WritableValueSet, ReadableDataSet<E>, and WritableDataSet<E> trait families — you do not need
to implement those yourself.
Required Traits
| Trait | Crate | Purpose |
|---|---|---|
TableSource |
vantage-table | The core trait. Defines associated types (Column, AnyType, Value, Id, Condition) and all CRUD + aggregation methods. Also provides related_in_condition for same-persistence relationship traversal. |
ColumnLike<AnyType> |
vantage-table | Column metadata for your persistence's column type (name, alias, flags, get_type). You can use the built-in Column<T> or create a custom column type. |
Optional Traits
| Trait | Crate | Purpose |
|---|---|---|
ExprDataSource |
vantage-expressions | Execute expressions and create deferred closures. Required by SQL persistence for subquery-based relationship traversal. Not needed by document-oriented persistence like MongoDB. |
TableExprSource |
vantage-table | Expression-returning aggregations (get_table_count_expr, get_table_max_expr, ...) behind Table::get_expr_count / get_expr_max, for use in subqueries or deferred execution. |
TableQuerySource |
vantage-table | SELECT query builder support for persistence that can compose structured queries. |
What You Get for Free
Once TableSource is implemented, Table<T, E> provides:
ReadableValueSet/WritableValueSet— raw record CRUD (delegates to your TableSource)ReadableDataSet<E>/WritableDataSet<E>— typed entity CRUD with automatic Record-Entity conversionActiveEntitySet<E>— change-tracked entity wrappers with.save()- Relationship traversal —
with_one(),with_many()andget_ref_as()on tables, andget_ref()on active entities
Type erasure is not part of this list any more. To hand tables to driver-agnostic code, also give
your driver a vista factory: a struct implementing vantage-vista's VistaFactory trait plus an
inherent from_table() that returns Result<Vista>, exposed as vista_factory() on your data
source.
Supporting Crates
- vantage-types: Define your persistence type system with
vantage_type_system!macro - vantage-expressions: Build query primitives supporting nesting and cross-database operations
- vantage-core: Unified error handling across the ecosystem
Simple Alternative: Direct DataSet Implementation
For persistence that doesn't need table/column abstractions (e.g. in-memory stores), you can implement the vantage-dataset traits directly:
ReadableValueSet+WritableValueSet+InsertableValueSet(raw record layer)ReadableDataSet<E>+WritableDataSet<E>+InsertableDataSet<E>(typed entity layer)
See ImTable in vantage-dataset for a reference implementation of this approach.
Integration
Part of the Vantage framework:
- vantage-types: Type system, entity definitions, and
TerminalRendertrait - vantage-expressions: Query building and database abstraction
- vantage-dataset: CRUD operation traits
- vantage-vista: Type-erased
Vistahandle built from a typed table by each driver - vantage-vista-factory:
VistaCatalogfor name-based models and cross-persistence references - vantage-core: Error handling and utilities
- vantage-csv: CSV file data source
- vantage-sql: SQL persistence (PostgreSQL, MySQL, SQLite)
- vantage-mongodb: MongoDB persistence
- vantage-surrealdb: SurrealDB persistence
- vantage-cli-util: Terminal table rendering utilities