Browse documentation
DocsDatabase

Database row models & serialization

Use Lokit's public typed rows to inspect, transform, or integrate the exact data exchanged with PostgreSQL.

01

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.

02

Public model groups

GroupPublic types
Load and matchLoadStats, MatchInput, MatchRow
Unit rowsUnitInsertRow, UnitFetchRow
Inline tagsTagInsertRow, TagFetchRow
Ordered partsPartInsertRow, PartFetchRow
CommentsCommentInsertRow, CommentFetchRow
AggregatesSerializedUnit, UnitWithChildren
JSON payloadsJsonScalar, JsonValue, JsonDict
03

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.

serialize_unit.py
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)
04

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.

custom_loader.py
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))
05

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.

deserialize_unit.py
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)
06

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.

07

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.