Skip to content

Grouped reads

Group a fragment’s nodes by keys, with numbers about each group, in one statement:

from drivine import avg, count, hop, key, path_key, refs
q = refs(Review)
top = await gom.groups.load(
Review,
keys={"page": key(q.page)}, # or path_key(Page, [hop("REVIEWS")]): the node
aggregates={"votes": count(q.helpful.eq(True)), "rating": avg(q.rating)},
where=q.published.is_not_null(),
having=[("votes", "gte", 3)],
order_by=[("votes", "desc")],
limit=10,
)
# [{"page": "/pricing", "votes": 12, "rating": 4.5}, ...]
await gom.groups.count_by(Review, "page") # {"/pricing": 14, ...}

Aggregates are count, sum_, avg, min_ and max_, each with an optional filter. Ties in an order are broken by the keys, so a page of groups is the same every time. A path key’s groups are ordered by its node’s id, so a path key to a class with no NodeId field is refused, before anything is run, on every store. On the SQL stores a key is ordered as Cypher orders it (a text, then a boolean, then a number, no value last), a date by its moment, and min_ and max_ take a value of any kind, a text or a date as well as a number, read back as the type its field declares. A text stored as "" is kept by min_ and max_, as Cypher keeps it, on Oracle too.

  • Models are Pydantic. A loaded fragment is validated, so a bad value fails at the load.
  • The annotation says what a relationship holds: a fragment, a relationship fragment or a view, one or a list. A single relationship typed without None is required, so only roots that have it load; Person | None is optional. A list is required with Relationship(..., required=True).
  • Properties are stored in camel case, as TypeScript and JVM models store them: start_date as startDate. GraphProperty("name") names one, and property_names="as_written" keeps a class’s fields as they are written.
  • A subclass is loaded as itself. A fragment’s labels are inherited, and a node loads as the most specific subclass whose labels it carries.
  • Recursion: Relationship("HAS_LOCATION", max_depth=3) on a list of the view’s own class expands three levels; depth={"sub_locations": 5} overrides it for one load.
  • More markers: GraphPath([hop(...), ...]) for the nodes at the end of several hops, Count and Aggregate computed in the query, SortedBy, ReadOnly, Transient, NodeStamp, NodeLabels, PropertyBag, JsonPacked, NullAsDefault and EmptyWhenAbsent, as in TypeScript’s Drivine.
  • A path in an order: one hop of a GraphPath may name an order, a property of the node it reaches by its stored name (sentAt for a field sent_at), lowest first unless ascending is False: hop("HAS_MESSAGE", Direction.OUTGOING, "Message", "sentAt"). A list then holds its targets in the order of the first node of that hop that leads to each, and a single field holds the target the first such node leads to. A node of the ordered hop without the property comes last, whichever way the path is read. Targets whose first nodes hold equal values come in no set order. A text of digits such as '0100' is ordered as text, and an empty text as a text, the lowest one. On Neo4j and the SQL stores a date is ordered by its moment, an offset’s included, not by its text. Where the property holds values of several kinds, Neo4j, Memgraph and the SQL stores order them by kind as Cypher’s ORDER BY does: lowest first a map, a list, a temporal value, a text, a boolean, then a number, and the other way highest first. On Neo4j and Memgraph the path is sorted by the engine’s own ORDER BY. FalkorDB orders them as its own ORDER BY does. A group key’s path names no order: path_key refuses one.
  • A set field is stored as a list of its items, sorted where they order (as the set iterates where they don’t), and a JsonPacked one packed so: a set is written alike in every process, whatever order it iterates in. patch writes one so too, and edges.relate writes a set property so. One that 0.1.0 wrote, in the order it iterated in, loads and update saves it, but until a save writes it again a where that compares the whole set, which it writes sorted, doesn’t find it on the graph engines (on Postgres and Oracle a whole-set where on a set that isn’t JsonPacked doesn’t find it either way), and on Neo4j a save given from_ of a JsonPacked one is refused.
  • A flat PropertyBag(flat=True) stores each entry under its bare key and loads every property no field or other bag is stored under. A save refuses, before it writes anything, an entry whose key a field (the id among them) is stored under, one under another bag’s prefix, or one beginning __drivine., Drivine’s own, such as the stamp.

The statements are TypeScript’s, character for character: the tests check them against those TypeScript’s Drivine writes for the same models.