Skip to content

Queries

A query is built from a class’s references, which mirror its fields:

from drivine import any_of, not_, refs
q = refs(PersonCareer)
leads = await gom.load_all(
PersonCareer,
where=q.person.bio.contains("Lead"),
order_by=q.person.name.asc(),
limit=20,
)
await gom.load_all(PersonCareer, where=[ # a list: all must hold
q.employment_history.target.name.eq("Acme"), # across a relationship
any_of(q.person.name.eq("Ada"), q.person.name.eq("Grace")),
not_(q.person.bio.is_null()),
])
# one relationship that meets both, rather than each met by any
q.employment_history.any(lambda it: [it.role.eq("Engineer"), it.target.name.eq("Acme")])
q.employment_history.none(lambda it: it.role.eq("Lead"))
  • Operators: eq, neq, gt, gte, lt, lte, in_, not_in, contains, starts_with, ends_with, matches, contains_ignore_case, eq_ignore_case, has_item, is_null, is_not_null. On a node: instance_of(Type), has_any_label(...), and property(name) or predicate(name, operator, value) for a property named at run time by its stored name.
  • Fields go by their Python names, q.start_date, and Drivine writes the stored names. A field the class doesn’t have fails as the query is written, naming it and the class. q.x == 1 raises: write q.x.eq(1).
  • Order and pages: order_by takes terms, asc() or desc(), then nulls_first() or nulls_last(); limit and skip apply in the database. An order on a list relationship’s property sorts that list in each object.
  • Keyset pages: seek=[q.released.after(last.released), q.uuid.after(last.uuid)], one value for each term of order_by, ending with a unique key.
  • An order by an expression: plus, minus, times, div, negate, exp, floor, coalesce, case_of with above, below, at_least, at_most, equal_to, has_value and the rest, all computed in the database.
  • count, delete, delete_all and patch take conditions too.
  • eq_ignore_case and contains_ignore_case lowercase the property, which no index reads. On a CaseInsensitive field whose index ignores case, they compare with a lowercased copy instead, which the index serves: see below.