Query Language
Complete reference for the LinkedRecords query syntax
Overview
LinkedRecords provides a declarative query language for finding attributes and facts. Queries are expressed as patterns that match against the triplestore.
There are two main query methods:
Attribute.findAll()- Find attributes matching fact patternsFact.findAll()- Find facts directly
The Triple Pattern
Every query is built from triple patterns - conditions that match facts in the triplestore:
For example:
['$it', 'isA', 'Document']- Match attributes that are Documents[userId, '$isMemberOf', '$it']- Match attributes the user is a member of['$it', 'belongsTo', projectId]- Match attributes belonging to a project
Attribute.findAll()
The primary way to query for attributes. Returns attributes matching your patterns.
Basic Structure
Each named query group returns an array of attributes that match all patterns in that group (AND logic).
Multiple Query Groups
You can run multiple independent queries in one call:
Direct ID Lookup
Pass an attribute ID directly to fetch a specific attribute:
The $it Placeholder
$it is a special placeholder representing "the attribute being queried."
As Subject (Finding Attributes by Their Facts)
The most common pattern - find attributes that have specific facts:
As Object (Finding Attributes Referenced by Facts)
Find attributes that appear as objects in facts:
Combined Subject and Object Patterns
Use $it in multiple positions to express complex relationships:
Query Modifiers
$hasDataType
Filter by attribute type:
Valid types: KeyValueAttribute, LongTextAttribute, BlobAttribute
$latest(predicate)
Match the most recent fact for a predicate. Essential for state tracking where you add new facts rather than deleting old ones:
$latest() uses the fact creation timestamp to determine which fact is most
recent. This lets you track state history by adding new facts instead of
deleting old ones.
$not(value)
Negation - match attributes that do NOT have a specific fact value:
Combining $latest and $not
The most powerful pattern for state filtering:
Transitive Predicates
Add * to a predicate to make it transitive - queries follow chains of relationships:
Fact.findAll()
Query facts directly instead of attributes.
Basic Structure
Query by Subject
Query by Predicate
Query by Object
Combined Queries
Multiple Query Sets (OR Logic)
Pass an array to combine queries with OR logic:
Authorization Filtering
All queries automatically filter results based on the current user's permissions. You only see attributes and facts you have access to:
Loading Values
findAll() returns attribute handles. Call getValue() to load the data:
Use findAndLoadAll() to pre-load values in a single request:
Real-Time Subscriptions
Subscribe to query results for live updates:
Complete Example
Here's a comprehensive example showing various query features:
Quick Reference
| Pattern | Meaning |
|---|---|
['$it', 'isA', 'Type'] | Attributes of a specific type |
['$it', 'belongsTo', id] | Attributes belonging to something |
[userId, '$isMemberOf', '$it'] | Attributes the user is a member of |
[id, '$isAccountableFor', '$it'] | Attributes something is accountable for |
['$it', '$hasDataType', 'KeyValueAttribute'] | Only KeyValue attributes |
['$it', '$latest(pred)', value] | Latest fact value matches |
['$it', 'pred', '$not(value)'] | Fact value does NOT match |
['$it', '$latest(pred)', '$not(value)'] | Latest value does NOT match |
['$it', 'isA*', 'Type'] | Transitive type matching |
Related
- Facts and Triples - Understanding the triplestore
- Query Patterns - Advanced patterns and use cases