Document
The Document class is the core building block of coodie. It maps a Pydantic v2
model to a Cassandra / ScyllaDB table. coodie provides both synchronous and
asynchronous variants.
Sync API (coodie.sync)
- class coodie.sync.document.Document
Bases:
BaseModelBase class for synchronous coodie documents.
- classmethod sync_table(dry_run: bool = False, drop_removed_indexes: bool = False) list[str]
Idempotently create or update the table in the database.
- Parameters:
dry_run – When
True, return the planned CQL statements without executing them.drop_removed_indexes – When
True, drop secondary indexes that exist in the database but are no longer defined in the model.
- Returns:
List of CQL statements that were (or would be) executed.
- classmethod table_name() str
Return the CQL table name for this model.
- classmethod drop_table() None
Drop the table for this model.
- classmethod truncate() None
Truncate (remove all rows from) the table for this model.
- save(ttl: int | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None, batch: BatchQuery | None = None) None
Insert (upsert) this document.
- save_json(ttl: int | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None) None
Insert (upsert) this document using
INSERT INTO … JSON.
- insert(ttl: int | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None, batch: BatchQuery | None = None) None
Insert IF NOT EXISTS (create-only).
- delete_columns(*column_names: str, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None, batch: BatchQuery | None = None, collection_elements: list[tuple[str, Any]] | None = None) None
Set one or more non-primary-key columns to null for this document.
Generates
DELETE col1, col2 FROM table WHERE pk = ?.When collection_elements is provided, each
(column, key_or_index)tuple generatesDELETE "col"[?] FROM table WHERE pk = ?which removes a single map entry or list element.Warning
This operation nullifies column values in-place and bypasses the schema-migration system. If you are removing or renaming a column, use coodie’s migration tools instead:
Document.sync_table()for development environments, orcoodie migrate(the CLI) for production deployments.
- delete(if_exists: bool = False, if_conditions: dict[str, Any] | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None, batch: BatchQuery | None = None) LWTResult | None
Delete this document by its primary key.
When if_exists is
Truethe generated CQL includesIF EXISTSand aLWTResultis returned.When if_conditions is supplied (e.g.
{"name": "old"}), the CQL includesIF name = ?and aLWTResultis returned. Operator suffixes likecol__ne,col__gt,col__inare supported.
- update(*, if_conditions: dict[str, Any] | None = None, if_exists: bool = False, ttl: int | None = None, **kwargs: Any) LWTResult | None
Partial update — sets only the given fields.
Supports collection operations via
parse_update_kwargs(e.g.add__,remove__prefixed keys).When if_conditions or if_exists is supplied the generated CQL includes a lightweight-transaction clause and a
LWTResultis returned.
- classmethod get(**kwargs: Any) Document
Return a single document; raise DocumentNotFound if missing.
- model_config = {'arbitrary_types_allowed': True, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_enum_values': True, 'validate_by_alias': True, 'validate_by_name': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class coodie.sync.document.CounterDocument
Bases:
DocumentBase class for synchronous counter-column documents.
Counter tables only support increment/decrement operations.
save()andinsert()are forbidden.- save(ttl: int | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None) None
Insert (upsert) this document.
- insert(ttl: int | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None) None
Insert IF NOT EXISTS (create-only).
- increment(**field_deltas: int) None
Increment counter columns by the given amounts.
Example:
page_view.increment(view_count=1, unique_visitors=1)
- decrement(**field_deltas: int) None
Decrement counter columns by the given amounts.
Example:
page_view.decrement(view_count=1)
- model_config = {'arbitrary_types_allowed': True, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_enum_values': True, 'validate_by_alias': True, 'validate_by_name': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class coodie.sync.document.MaterializedView
Bases:
DocumentBase class for synchronous materialized view documents (read-only).
Subclasses must define
Settingswith:__base_table__— the base table name (required).__view_columns__— columns to select (defaults to["*"]).__where_clause__— theWHEREclause (defaults to auto-generatedIS NOT NULLfor all primary-key/clustering columns).__clustering_order__— optional{column: "ASC"|"DESC"}dict.
The view name defaults to the model’s table name.
- classmethod sync_view() None
Create the materialized view in the database.
- classmethod drop_view() None
Drop the materialized view.
- save(**kwargs: Any) None
Insert (upsert) this document.
- insert(**kwargs: Any) None
Insert IF NOT EXISTS (create-only).
- delete(**kwargs: Any) LWTResult | None
Delete this document by its primary key.
When if_exists is
Truethe generated CQL includesIF EXISTSand aLWTResultis returned.When if_conditions is supplied (e.g.
{"name": "old"}), the CQL includesIF name = ?and aLWTResultis returned. Operator suffixes likecol__ne,col__gt,col__inare supported.
- update(**kwargs: Any) LWTResult | None
Partial update — sets only the given fields.
Supports collection operations via
parse_update_kwargs(e.g.add__,remove__prefixed keys).When if_conditions or if_exists is supplied the generated CQL includes a lightweight-transaction clause and a
LWTResultis returned.
- model_config = {'arbitrary_types_allowed': True, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_enum_values': True, 'validate_by_alias': True, 'validate_by_name': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Async API (coodie.aio)
- class coodie.aio.document.Document
Bases:
BaseModelBase class for asynchronous coodie documents.
- async classmethod sync_table(dry_run: bool = False, drop_removed_indexes: bool = False) list[str]
Idempotently create or update the table in the database.
- Parameters:
dry_run – When
True, return the planned CQL statements without executing them.drop_removed_indexes – When
True, drop secondary indexes that exist in the database but are no longer defined in the model.
- Returns:
List of CQL statements that were (or would be) executed.
- classmethod table_name() str
Return the CQL table name for this model.
- async classmethod drop_table() None
Drop the table for this model.
- async classmethod truncate() None
Truncate (remove all rows from) the table for this model.
- async save(ttl: int | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None, batch: AsyncBatchQuery | None = None) None
Insert (upsert) this document.
- async save_json(ttl: int | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None) None
Insert (upsert) this document using
INSERT INTO … JSON.
- async insert(ttl: int | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None, batch: AsyncBatchQuery | None = None) None
Insert IF NOT EXISTS (create-only).
- async delete_columns(*column_names: str, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None, batch: AsyncBatchQuery | None = None, collection_elements: list[tuple[str, Any]] | None = None) None
Set one or more non-primary-key columns to null for this document.
Generates
DELETE col1, col2 FROM table WHERE pk = ?.When collection_elements is provided, each
(column, key_or_index)tuple generatesDELETE "col"[?] FROM table WHERE pk = ?which removes a single map entry or list element.Warning
This operation nullifies column values in-place and bypasses the schema-migration system. If you are removing or renaming a column, use coodie’s migration tools instead:
Document.sync_table()for development environments, orcoodie migrate(the CLI) for production deployments.
- async delete(if_exists: bool = False, if_conditions: dict[str, Any] | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None, batch: AsyncBatchQuery | None = None) LWTResult | None
Delete this document by its primary key.
When if_exists is
Truethe generated CQL includesIF EXISTSand aLWTResultis returned.When if_conditions is supplied (e.g.
{"name": "old"}), the CQL includesIF name = ?and aLWTResultis returned. Operator suffixes likecol__ne,col__gt,col__inare supported.
- async update(*, if_conditions: dict[str, Any] | None = None, if_exists: bool = False, ttl: int | None = None, **kwargs: Any) LWTResult | None
Partial update — sets only the given fields.
Supports collection operations via
parse_update_kwargs(e.g.add__,remove__prefixed keys).When if_conditions or if_exists is supplied the generated CQL includes a lightweight-transaction clause and a
LWTResultis returned.
- async classmethod get(**kwargs: Any) Document
Return a single document; raise DocumentNotFound if missing.
- model_config = {'arbitrary_types_allowed': True, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_enum_values': True, 'validate_by_alias': True, 'validate_by_name': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class coodie.aio.document.CounterDocument
Bases:
DocumentBase class for asynchronous counter-column documents.
Counter tables only support increment/decrement operations.
save()andinsert()are forbidden.- async save(ttl: int | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None) None
Insert (upsert) this document.
- async insert(ttl: int | None = None, timestamp: int | None = None, consistency: str | None = None, timeout: float | None = None) None
Insert IF NOT EXISTS (create-only).
- async increment(**field_deltas: int) None
Increment counter columns by the given amounts.
Example:
await page_view.increment(view_count=1, unique_visitors=1)
- async decrement(**field_deltas: int) None
Decrement counter columns by the given amounts.
Example:
await page_view.decrement(view_count=1)
- model_config = {'arbitrary_types_allowed': True, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_enum_values': True, 'validate_by_alias': True, 'validate_by_name': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class coodie.aio.document.MaterializedView
Bases:
DocumentBase class for asynchronous materialized view documents (read-only).
Subclasses must define
Settingswith:__base_table__— the base table name (required).__view_columns__— columns to select (defaults to["*"]).__where_clause__— theWHEREclause (defaults to auto-generatedIS NOT NULLfor all primary-key/clustering columns).__clustering_order__— optional{column: "ASC"|"DESC"}dict.
The view name defaults to the model’s table name.
- async classmethod sync_view() None
Create the materialized view in the database.
- async classmethod drop_view() None
Drop the materialized view.
- async save(**kwargs: Any) None
Insert (upsert) this document.
- async insert(**kwargs: Any) None
Insert IF NOT EXISTS (create-only).
- async delete(**kwargs: Any) LWTResult | None
Delete this document by its primary key.
When if_exists is
Truethe generated CQL includesIF EXISTSand aLWTResultis returned.When if_conditions is supplied (e.g.
{"name": "old"}), the CQL includesIF name = ?and aLWTResultis returned. Operator suffixes likecol__ne,col__gt,col__inare supported.
- async update(**kwargs: Any) LWTResult | None
Partial update — sets only the given fields.
Supports collection operations via
parse_update_kwargs(e.g.add__,remove__prefixed keys).When if_conditions or if_exists is supplied the generated CQL includes a lightweight-transaction clause and a
LWTResultis returned.
- model_config = {'arbitrary_types_allowed': True, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_enum_values': True, 'validate_by_alias': True, 'validate_by_name': True}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].