httk.data.db.entry_provider¶
Serve stored dataclasses through the httk-core entry-provider contract.
StoreEntryProvider bridges the SQL storage layer to the neutral
EntryProvider contract: it serves the rows of one or more
storable classes in a SqlStore as described,
JSON-able entry-type records, so a serving module (such as httk-optimade)
can expose a database as an OPTIMADE API without either side depending on the
other.
For each served class the provider either passes through a supplied
EntryTypeDefinition (validated to describe every served
property) or auto-generates one from the class’s resolved
TableSchema: the OPTIMADE core id/type
properties plus one from_simple()
definition per servable stored field, each named with a registered
database-specific prefix (default "_httk_") and merged in via
extended() — the same construction route
the other httk entry providers use.
The schema-to-OPTIMADE type mapping is:
str/int/bool/floatfields —string/integer/boolean/float;rational fields (
fractions.Fraction,FracScalar,SurdScalar) —float, served as the nearest float (stored values themselves remain exact; only the served value is approximate);datetime.datetimefields —timestamp, served as ISO-8601 text;fixed-shape (
Shape(r, c)) and variable-rows (Shape(0, c))FracVectorfields —list of list of float(the fixed shape also declares its dimension sizes);list/tuplefields of scalars or of the codec types above —list ofthe mapped element type.
Not every stored field can be served as a property: bytes fields (and
fields encoded by a custom, non-built-in value codec) have no OPTIMADE value
representation and are skipped, while reference fields and child fields of
storable elements surface through StoreEntryProvider.relationships()
instead — when their target class is itself served, each record declares its
related entries as a flat tuple of RelatedEntry values,
carrying the role/description metadata of an optional
Related field marker (Related(serve=False) suppresses
the field as a relationship). Class-level
RelationshipLink declarations contribute further
relationships: each stored row of the declaring class — a served class, or an
unserved join class passed via link_classes — expresses one FROM→TO
relationship between the entries its link endpoints resolve to, carrying the
link’s metadata.
Classes¶
Serves the stored rows of storable classes as httk-core entry types. |
Functions¶
|
The served |
|
Auto-generate the |
Module Contents¶
- httk.data.db.entry_provider.served_specs(schema: httk.data.db.schema.TableSchema, prefix: str) list[tuple[str, httk.data.db.schema.FieldSpec, str]][source]¶
The served
(property name, field spec, fulltype)triples of a storable class.One triple per servable stored field of
schema(see the module docstring for which fields are servable and how their types map), each named{prefix}{field}.
- httk.data.db.entry_provider.auto_definition(entry_type: str, schema: httk.data.db.schema.TableSchema, prefix: str) httk.core.EntryTypeDefinition[source]¶
Auto-generate the
EntryTypeDefinitionof a storable class.The definition carries the OPTIMADE core
id/typeproperties plus onefrom_simple()definition per triple ofserved_specs(), merged in viaextended()(soprefixmust be a registered definition prefix).
- class httk.data.db.entry_provider.StoreEntryProvider(store: httk.data.db.store.SqlStore, classes: collections.abc.Mapping[str, type], *, definitions: collections.abc.Mapping[str, httk.core.EntryTypeDefinition] | None = None, prefix: str = '_httk_', id_of: collections.abc.Callable[[str, int, Any], str] | None = None, link_classes: collections.abc.Iterable[type] = ())[source]¶
Bases:
httk.core.EntryProviderServes the stored rows of storable classes as httk-core entry types.
classesmaps each served entry-type name to its storable dataclass; the classes’ tables are read throughstore.definitionsoptionally supplies theEntryTypeDefinitionof an entry type (validated: it must describe every property the provider serves for it); entry types without a supplied definition get one auto-generated from the class’s schema, with every schema-derived property name carryingprefix(which must be registered, seeregister_definition_prefix()).id_ofmaps(entry_type, sid, instance)to the served entry id; the default is"<entry_type>-<sid>".link_classesnames storable join-object classes that are not served as entries themselves but whoseRelationshipLinkdeclarations contribute relationships between served entries; every link endpoint (on a served class or a link class) must resolve to a served entry type, and a link class without link declarations is rejected.See the module docstring for which stored fields are served as properties (and how their types map), which are skipped, and which surface through
relationships()instead.- entry_types() collections.abc.Mapping[str, httk.core.EntryTypeDefinition][source]¶
Return the served entry types keyed by name.
Each value is an
EntryTypeDefinitiondescribing the entry type and its properties. The subset a provider actually serves is named byproperty_keys(); a definition may describe more properties than are served.
- property_keys(entry_type: str) collections.abc.Mapping[str, str][source]¶
Return the served-property-name to record-key map for
entry_type.The mapping MUST include entries for at least
idandtype. Every key names a property described byentry_types(); every value names the key under which that property’s value is found in a record fromrecords().
- records(entry_type: str) collections.abc.Iterator[collections.abc.Mapping[str, Any]][source]¶
Yield the records for
entry_typeas plain JSON-able mappings.Each record is a mapping keyed by the record keys named in
property_keys(); values are JSON-able (strings, numbers, booleans,None, or nested lists/dicts of the same).
- relationships(entry_type: str) collections.abc.Mapping[str, tuple[httk.core.RelatedEntry, Ellipsis]][source]¶
Return the related entries for each record of
entry_type.The result maps an entry id to a flat tuple of
RelatedEntryvalues, e.g.{"struct-1": (RelatedEntry("references", "ref-1"), RelatedEntry("references", "ref-2", description="Cites the method"))}. Grouping the related entries by related entry type is the serving layer’s concern (JSON:API groups them at render time). This is the neutral source of an OPTIMADE relationships block: a consumer turns each related entry into a resource identifier under its entry type (carrying thedescription/rolemetadata when present), and aninclude=<type>request then embeds those related resources. The default implementation returns an empty mapping (no relationships); a provider overrides it to declare them. Ids referring to records this provider (or a sibling provider serving the related type) does not supply are simply not resolvable by the consumer.