Browse documentation
Database row models & serialization
Use Lokit's public typed rows to inspect, transform, or integrate the exact data exchanged with PostgreSQL.
These are typed transport models, not ORM entities
Lokit's database models are slotted dataclasses and typed aliases used by COPY staging, row factories, matching, and reconstruction. They do not attach to an application's SQLAlchemy declarative base. Alembic integration uses the ordered schema statements documented separately, while these models provide a stable typed Python boundary for custom loaders and adapters.
Public model groups
| Group | Public types |
|---|---|
| Load and match | LoadStats, MatchInput, MatchRow |
| Unit rows | UnitInsertRow, UnitFetchRow |
| Inline tags | TagInsertRow, TagFetchRow |
| Ordered parts | PartInsertRow, PartFetchRow |
| Comments | CommentInsertRow, CommentFetchRow |
| Aggregates | SerializedUnit, UnitWithChildren |
| JSON payloads | JsonScalar, JsonValue, JsonDict |
Serialize one common-model unit
serialize_unit() converts one Data value into its unit row and child rows. It assigns load and database UUIDs, derives adjacent source context, carries project and domain classification, serializes plurals and metadata, and preserves inline tag and comment payloads.
from lokit.database import SerializedUnit, serialize_unitfrom lokit.types import Data
unit = Data( source="Place order", target="Passer la commande", extensions={"component": "checkout"},)
rows: SerializedUnit = serialize_unit( "checkout.submit", unit, source_locale="en-US", target_locale="fr-FR", project="storefront", domain="checkout",)
print(rows.unit.source_text)print(rows.tags, rows.parts, rows.comments)Serialize a document lazily
iter_serialized_units() accepts BaseStructure or StreamingStructure and yields SerializedUnit values lazily. A multilingual Data.targets mapping produces one serialized unit per target locale in document order. The iterator closes an underlying closeable stream when iteration finishes or is closed early.
import lokitfrom lokit.database import iter_serialized_units
document = lokit.stream.tmx( "memory.tmx", target_language="fr",)
for rows in iter_serialized_units( document, project="storefront", domain="checkout",): print(rows.unit.unit_key, len(rows.tags), len(rows.comments))Reconstruct Data from fetched rows
UnitWithChildren groups a UnitFetchRow with its tag, part, and comment rows. deserialize_unit() turns that aggregate back into (unit_key, Data), restoring translation status, plurals, metadata, context, inline content, comments, project/domain values, and extension payloads.
from lokit.database import UnitWithChildren, deserialize_unit
def restore(rows: UnitWithChildren) -> None: unit_key, unit = deserialize_unit(rows) print(unit_key, unit.source, unit.target)Use MatchInput for checked batch requests
MatchInput is a TypedDict with required-at-runtime source, source_locale, and target_locale keys plus optional previous_source and next_source. MatchRow represents the internal database candidate before Lokit converts it into the public MatchResult returned by matching APIs.
Import from the public package
Import these types and helpers from lokit.database, not the legacy lokit.db implementation package. The public module uses explicit exports and remains the supported integration surface for type checkers and downstream adapters.