Filter
Filter operations reduce the result set based on property values. TypeGraph provides whereNode()
for filtering nodes and whereEdge() for filtering edges during traversals.
whereNode()
Section titled “whereNode()”Filter nodes based on their properties:
const engineers = await store .query() .from("Person", "p") .whereNode("p", (p) => p.role.eq("Engineer")) .select((ctx) => ctx.p) .execute();Parameters
Section titled “Parameters”.whereNode(alias, predicateFunction)| Parameter | Type | Description |
|---|---|---|
alias | string | The node alias to filter (must exist in query) |
predicateFunction | (accessor) => Predicate | Function that returns a predicate |
The predicate function receives a typed accessor for the node’s properties.
whereEdge()
Section titled “whereEdge()”Filter based on edge properties during traversals:
const highPaying = await store .query() .from("Person", "p") .traverse("worksAt", "e") .whereEdge("e", (e) => e.salary.gte(100000)) .to("Company", "c") .select((ctx) => ({ person: ctx.p.name, company: ctx.c.name, salary: ctx.e.salary, })) .execute();Parameters
Section titled “Parameters”.whereEdge(alias, predicateFunction)| Parameter | Type | Description |
|---|---|---|
alias | string | The edge alias to filter (must exist in query) |
predicateFunction | (accessor) => Predicate | Function that returns a predicate |
Combining Predicates
Section titled “Combining Predicates”Both conditions must be true:
.whereNode("p", (p) => p.status.eq("active").and(p.role.eq("admin")))Either condition can be true:
.whereNode("p", (p) => p.role.eq("admin").or(p.role.eq("moderator")))Negate a condition:
.whereNode("p", (p) => p.status.eq("deleted").not())Complex Combinations
Section titled “Complex Combinations”Build complex logic with parenthetical grouping:
.whereNode("p", (p) => p.status .eq("active") .and(p.role.eq("admin").or(p.role.eq("moderator"))))This evaluates as: status = 'active' AND (role = 'admin' OR role = 'moderator')
Multiple Filters
Section titled “Multiple Filters”Chain multiple whereNode() calls for AND logic:
const activeManagers = await store .query() .from("Person", "p") .whereNode("p", (p) => p.status.eq("active")) .whereNode("p", (p) => p.role.eq("Manager")) .select((ctx) => ctx.p) .execute();This is equivalent to:
.whereNode("p", (p) => p.status.eq("active").and(p.role.eq("Manager")))Filtering After Traversal
Section titled “Filtering After Traversal”Filter nodes at any point in the query:
const techCompanyEngineers = await store .query() .from("Person", "p") .whereNode("p", (p) => p.role.eq("Engineer")) .traverse("worksAt", "e") .to("Company", "c") .whereNode("c", (c) => c.industry.eq("Technology")) .select((ctx) => ({ person: ctx.p.name, company: ctx.c.name, })) .execute();Common Predicates
Section titled “Common Predicates”Here are the most commonly used predicates. For complete reference, see Predicates.
Equality
Section titled “Equality”p.name.eq("Alice") // equalsp.name.neq("Bob") // not equalsComparison
Section titled “Comparison”p.age.gt(21) // greater thanp.age.gte(21) // greater than or equalp.age.lt(65) // less thanp.age.lte(65) // less than or equalp.age.between(18, 65) // inclusive rangeString Matching
Section titled “String Matching”p.name.contains("ali") // substring matchp.name.startsWith("A") // prefix matchp.name.endsWith("ice") // suffix matchp.email.like("%@example.com") // SQL LIKE patternp.name.ilike("alice") // case-insensitive LIKENull Checks
Section titled “Null Checks”p.deletedAt.isNull() // is null/undefinedp.email.isNotNull() // is not nullList Membership
Section titled “List Membership”p.status.in(["active", "pending"])p.status.notIn(["archived", "deleted"])Array Operations
Section titled “Array Operations”p.tags.contains("typescript")p.tags.containsAll(["typescript", "nodejs"])p.tags.containsAny(["typescript", "rust", "go"])p.tags.isEmpty()p.tags.isNotEmpty()Predicate Types by Field
Section titled “Predicate Types by Field”The available predicates depend on the field type:
| Field Type | Key Predicates |
|---|---|
| String | eq, contains, startsWith, like, ilike |
| Number | eq, gt, gte, lt, lte, between |
| Date | eq, gt, gte, lt, lte, between |
| Array | contains, containsAll, containsAny, isEmpty |
| Object | get(), hasKey, pathEquals |
| Embedding | similarTo() |
See Predicates for complete documentation.
Count and Existence Helpers
Section titled “Count and Existence Helpers”Count Results
Section titled “Count Results”const count: number = await store .query() .from("Person", "p") .whereNode("p", (p) => p.status.eq("active")) .count();Check Existence
Section titled “Check Existence”const exists: boolean = await store .query() .from("Person", "p") .whereNode("p", (p) => p.email.eq("alice@example.com")) .exists();Get First Result
Section titled “Get First Result”const alice = await store .query() .from("Person", "p") .whereNode("p", (p) => p.email.eq("alice@example.com")) .select((ctx) => ctx.p) .first();
if (alice) { console.log(alice.name);}Next Steps
Section titled “Next Steps”- Predicates - Complete predicate reference
- Traverse - Navigate relationships
- Advanced - Subqueries with
exists()andinSubquery()