Query Language
Complete reference for the LinkedRecords query syntax
Overview
LinkedRecords provides a declarative query language for finding records and facts. Queries are expressed as patterns that match against the triplestore.
There are two main query methods:
Record.findAll()- Find records 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 records that are Documents[userId, '$isMemberOf', '$it']- Match records the user is a member of['$it', 'belongsTo', projectId]- Match records belonging to a project
Record.findAll()
The primary way to query for records. Returns records matching your patterns.
Basic Structure
Each named query group returns an array of records 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 a record ID directly to fetch a specific record:
The $it Placeholder
$it is a special placeholder representing "the record being queried."
As Subject (Finding Records by Their Facts)
The most common pattern - find records that have specific facts:
As Object (Finding Records Referenced by Facts)
Find records 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 record type:
Valid types: KeyValueAttribute, LongTextAttribute, BlobAttribute
"Attribute" is the former name of the Record concept. The runtime type
identifiers still use the legacy names for backwards compatibility. When
using TypeScript, you can pass the SDK classes KeyValueRecord,
LongTextRecord, or BlobRecord (exported by @linkedrecords/browser)
instead of the strings - the SDK serializes them to the legacy names for you.
$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 records 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 records.
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 records and facts you have access to:
Loading Values
findAll() returns record handles. Call getValue() to load the data:
Use findAndLoadAll() to pre-load record 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'] | Records of a specific type |
['$it', 'belongsTo', id] | Records belonging to something |
[userId, '$isMemberOf', '$it'] | Records the user is a member of |
[id, '$isAccountableFor', '$it'] | Records something is accountable for |
['$it', '$hasDataType', 'KeyValueAttribute'] | Only KeyValue records |
['$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 for records