A type-safe graph query builder and OGM for linked data — like Drizzle or Prisma, but for RDF and SPARQL.
Linked gives you a schema-parameterized query language and SHACL-driven Shape classes for graph data. Every query has one canonical, language-neutral form — DSL-JSON — that serializes losslessly, crosses any boundary, and rehydrates anywhere. Datasets execute these queries however they like; the built-in SPARQL dataset lowers them to a normalized IR and on to SPARQL.
- Schema-Parameterized Query DSL: TypeScript-embedded queries driven by your Shape definitions.
- Fully Inferred Result Types: The TypeScript return type of every query is automatically inferred from the selected paths — no manual type annotations needed. Select
p.nameand get{id: string; name: string}[]. Selectp.friends.nameand get nested result types. This works for all operations: select, create, update, and delete. - DSL-JSON, the standard wire format: Every query — select, create, update, delete — serializes to a compact, lossless JSON structure with
query.toJSON()and rehydrates withfromJSON(json). This is the canonical interchange format for Linked queries: send it over HTTP, cache it, queue it, or implement it in another language. See DSL-JSON. - Dynamic Query Building: Build queries programmatically with
SelectBuilder(andCreate/Update/DeleteBuilder), compose field selections withFieldSet— for CMS dashboards, dynamic forms, and API-driven query construction. - Shape Classes (SHACL): TypeScript classes that generate SHACL shape metadata.
- Full CRUD Operations: Query, create, update, and delete data using the same Shape-based API — including expression-based updates, conditional mutations, bulk operations, and automatic cleanup of owned (
contains) values on replace/remove/delete (see Owned properties). - Dataset Routing:
LinkedStorageroutes query objects (by their target shape) to your configured dataset(s) that implementIDataset. - Automatic Data Validation: SHACL shapes can be synced to your store for schema-level validation, and enforced at runtime by stores that support it.
npm install @_linked/coreCore works with any SPARQL 1.1 store. It checks what a store does by asking it, never by its product or version.
- Always: SPARQL 1.1 Query and Update. Core sends standard queries and updates only.
- The graph API (
NamedGraph,OntologyGraph,.from(),.into(), graph routing): named graphs. A graph query is one update whose conditions are part of the update (a create writes only while its graph has no record and holds no triples; a replace or patch only while the record exists), so concurrent writers are decided in the store's own transaction. A store without graph queries refuses them (unsupported-query-kind), and the ontology sync falls back to the default graph. - Polymorphic reads: RDFS class inference. A node stored as
ex:Projectmust also answer as every superclass (schema:CreativeWork,schema:Thing), with the hierarchy built from the storedrdfs:subClassOftriples of the default graph and of every graph the registry markscore:feedsHierarchy true(the package ontology graphssyncOntologieswrites, or the shared one; see Ontology and shape graphs). UnderontologyGraph: 'default'the ontology is in the default graph, so a store that infers from the default graph only is enough there. Class-scoped selects, counts, asks andEXISTS, bulk mutations (deleteAll,deleteWhere,update().forAll()/.where()), the owned-link class check, the owned walk's cycle hops and the single-owner SHACL constraint rely on it. Only classes are inferred:rdfs:subPropertyOf,rdfs:domainandrdfs:rangeare not needed. - The hierarchy where the instances are. A store infers from the ontology graphs it holds. The routed ontology sync writes a package's ontology into every dataset the shapes using its classes are pinned to (see Ontology and shape graphs), so a store holding instances also holds their hierarchy.
- No graph-target setting. A store takes no ontology or shape graph setting: where the syncs write is the process's one global setting (see Ontology and shape graphs), and every store's readers follow it.
- A store that infers over all graphs works too. The caveat: drafts and other graphs the
registry marks
feedsHierarchy falsethen feed the hierarchy as well, so a draft ontology's classes infer before it is published.
How the ontology sync measures the store (syncOntologies, @_linked/core/ontology/syncOntologies;
run by @_linked/server at boot): before it writes its first package graph (or the shared graph),
it probes the store once per dataset. It writes a probe rdfs:subClassOf into a temporary registered graph and a typed
instance into a second graph that does not feed the hierarchy, asks through the normal query path
whether the instance is found by its superclass, then removes the probe. A sync with nothing to
write runs no probe; unless an earlier sync left a default-graph hierarchy to remove (once), it
sends nothing but its reads. On every store with named graphs the ontology
data is written into package graphs only, and the hierarchy an earlier sync kept in the default
graph is removed, so readers of graphs.allOntologies see the same everywhere. The answer only
decides a warning. See ontologies.
| The store infers from | The sync does |
|---|---|
| the registered graphs | writes each package's ontology data into its own graph |
| the default graph only | the same; warns once that class-scoped reads miss subclass instances |
| nothing | the same; warns once that class-scoped reads miss subclass instances |
A store that infers only from the default graph therefore needs an inferring store (above) for
polymorphic reads: core never copies the hierarchy into the default graph. Under ontologyGraph: 'default' the probe tests the default graph instead (it writes a probe axiom and instance there,
asks, and removes them) and warns once when the store does not infer from it.
The probe runs only before a write: a sync with nothing to write sends no request beyond its reads, in every mode. Its answer never stops the sync.
Without inference core still reads and writes, but everything that matches by class sees only nodes stored with exactly that class. Selects, counts and asks of a superclass miss the instances of its subclasses; bulk mutations skip them; the owned-link check refuses a link to a subclass instance; the single-owner constraint does not see it.
@_linked/fuseki's image (ghcr.io/linked-fw/fuseki, its tdb2 datasets) is one store that meets
these requirements. Core does not depend on it: any store with the behaviour above works.
Where the syncs keep the registered packages' ontology data and shapes is one global setting of the process, two strings set by the host. It is not a store's setting: no store, store option or datasets config entry takes it.
| Setting | Env variable (read by @_linked/server) |
Values |
|---|---|---|
ontologyGraph |
LINKED_ONTOLOGY_GRAPH |
'package' (the default), 'default', or an absolute graph IRI |
shapeGraph |
LINKED_SHAPE_GRAPH |
the same |
import {setGraphTargets, getGraphTargets} from '@_linked/core';
setGraphTargets({ontologyGraph: 'default'}); // once, at boot, before the stores load and the syncs
getGraphTargets(); // {ontologyGraph: 'default', shapeGraph: 'package'}@_linked/server reads both variables at boot and calls setGraphTargets once, before it loads
the stores and before the syncs; an invalid value stops the boot. A host of its own does the same
(setGraphTargets(graphTargetOptionsFromEnv(process.env)), @_linked/core/utils/graphTargets).
Core itself never reads process.env for them. With nothing set both are 'package'. Each call
replaces both settings (one left out is 'package'). A value that is neither keyword must be an
absolute IRI (not a portable graph name such as graphs.default, and not the same IRI for both
settings); anything else throws, naming the setting and where it came from.
The frontend needs nothing when its queries go through the backend: a browser's
.from(graphs.allOntologies) or .from(graphs.allShapes) travels to the backend as the portable
name, and the backend's store expands it under the backend's setting. A store used directly in a
browser expands it under the browser's own setting ('package' unless set).
Every copy of core in a process must be this version or later: an older copy's stores still read targets of their own and ignore the global setting.
| Mode | Ontology sync (syncOntologies) |
Shape sync (syncShapeGraphs) |
|---|---|---|
'package' |
One OntologyGraph per package, {package IRI}/graph/ontology, replaced whole when the package's hash changes. |
One ShapesGraph per package, {package IRI}/graph/shapes, plus the app's generated shapes in {DATA_ROOT}/graph/shapes. |
| an IRI | One shared OntologyGraph at that IRI (feedsHierarchy true) holding every registered package's ontology data. When any package's hash changes it is rebuilt whole in one update. Its record lists packageHashes ("{package} {sha256}") and sources ({package}/{file}). |
One shared ShapesGraph at that IRI with the run's packages' shapes and the app's generated ones, rebuilt whole on any change; the same record fields. packages, when given, names every package the graph holds (the server leaves out packages without shapes.ttl). |
'default' |
The whole ontology (not only the hierarchy) in the default graph. When the combined hash changes, one update deletes every package-owned term and inserts every package's current data. | The default-graph shape sync (syncShapes): each shape replaced on its own hash, keeping the links other shapes hold to it. Every registered shape is synced (packages and orphanScope do not apply). |
The shared graph IRI may not be a package's own graph name (…/pkg/{slug}/graph/ontology or
/graph/shapes).
The readers follow the mode: on every store, graphs.allOntologies and graphs.allShapes read the
package graphs, the shared graph, or the default graph. .from() and readers built on them work the same in every
mode. An unchanged sync writes nothing in every mode (and so probes nothing).
'default' mode's package-owned terms. A package-owned term is every subject whose IRI starts
with a registered package's ontology namespace (ns('') of each ontology that ships data), and
every subject of a package's current Turtle. Each goes with all its outgoing triples and,
recursively, the blank nodes they lead to (OWL restrictions, lists; at most
DEFAULT_GRAPH_TERMS_DEPTH, 8, blank-node steps deep). Subjects outside that set are never
touched: instance data uses the app's IRIs, and a class of no package stays. The combined hash is
kept on https://linked.cm/pkg/core/ontology-sync/default-graph, replaced in the same update. The
sync refuses, before writing, a namespace that holds the store's data root or a registered shape,
a blank node no IRI subject links to, and a store without replaceDefaultGraphTerms
(SparqlDataset has it). When a package's data cannot be read, the shared and default modes skip
the rebuild with a warning, since a rebuild without it would drop its terms. What the sweep cannot
see stays: a subject outside every namespace that a package drops from its Turtle (say
schema:Person rdfs:subClassOf … written by a package that does not own schema:), and every
term of a package that is no longer registered. Remove those by hand. A namespace must end in / or
#.
A package's ontology Turtle is the source of its terms. Any triples about a package's terms
must live in that package's Turtle. Anything else written about them (by hand, by another tool) can
be overwritten by the next sync: in 'default' mode it is deleted, in the other modes the graph is
replaced whole.
Switching modes needs clearing first. Each kind of data lives only where its mode puts it, and
the syncs never copy or move it. A sync that finds another mode's data (package graphs while in
'default' or shared mode, a shared graph while in 'package' mode, the default-graph ontology or
default-graph shapes while in another mode) logs one warning naming it and leaves it. To switch, drop
the old graphs and records (OntologyGraph.delete(iri), ShapesGraph.delete(iri)), or remove the
default-graph terms, then set the new mode and sync. The 'package' and shared modes need a
store with graph queries; the ontology sync refuses one without them.
An app whose host reads package graphs must use 'package' for both. A host that reads them
(catalogs, its gateway's graph rules) reports another mode as an error.
Which datasets the syncs write to. syncShapeGraphs(ds) and syncOntologies(ds) write into the
one dataset they are given. The routed syncs (syncShapeGraphsRouted, syncOntologiesRouted,
@_linked/core/shapes/syncTargets; what @_linked/server's boot runs) follow the storage routing
instead, and graphSyncTargets() tells where each graph goes:
| Graph | Goes to |
|---|---|
| a package's shape graph | every dataset one of the package's shapes routes to, and the dataset(s) the shapes of sh:NodeShape / sh:PropertyShape route to |
| a package's ontology graph | every dataset one of the package's shapes routes to, every dataset a shape targeting one of its classes (a class in its namespace) routes to, and the dataset(s) the ontology term shapes (rdfs:Class, owl:Class, rdf:Property, …) route to; so an ontology-only package follows the shapes that use it, and the hierarchy is where the instances are |
| the app's shape graph | every dataset a shape it constrains routes to (the shapes that declare contains properties and their sub-shapes), and those of sh:NodeShape / sh:PropertyShape |
A sub-shape follows its parent's pin; shapes the routing does not pin go to the default dataset,
so an app without pins writes everything into the default dataset, exactly as before. Each package's
text is read or generated once and every target gets the same bytes and hash. In the shared and
'default' modes a dataset's graph holds the packages routed to it. A dataset that cannot be read
or written fails alone: the routed sync reports it (failed) and syncs the others.
When routing no longer sends a package's graph to a dataset (a pin moved), the routed sync drops
it only in the default dataset. In another dataset it leaves the graph and reports it
(leftovers, one warning): a store several apps share may hold another app's copy there. Drop
those by hand once nothing reads them. The routed sync reads only the datasets the router knows, so
a dataset nothing is pinned to any more is not looked at. In the 'default' ontology mode the sync
keeps one combined state per dataset and sweeps only the namespaces of the packages routed to it,
so the terms of a package routed away stay in that dataset's default graph (it warns once).
graphContent() (same module) answers what the 'package'-mode syncs would write for each
package (shape graph Turtle, ontology graph N-Triples, the hash each record gets, its sources) and
for the app's graph, writing nothing. A host that writes an app's graphs itself reads them through
@_linked/server's POST /admin/graph-content.
After cloning this repository, run:
npm install
npm run setupnpm run setup installs agent skills and syncs tooling configuration.
@_linked/rdf-mem-store: in-memory RDF store that implementsIDataset.@_linked/react: React bindings for Linked queries and shapes.
- DSL-JSON — the Linked query wire format — the canonical, standardized query structure.
- Intermediate Representation (IR) — the internal algebra the SPARQL dataset lowers to.
- SPARQL Algebra Layer
- Live queries —
query.live(), change sources, and what triggers a refetch.
Linked turns TypeScript classes into a type-safe query pipeline. Here is the full flow, traced through a single example:
Shape class → DSL query → IR (AST) → Target query language → Execute → Map results
Shape classes use decorators to generate SHACL metadata. These shapes define the data model, drive the DSL's type safety, and can be synced to a store for runtime data validation.
import {createNameSpace} from '@_linked/core/utils/NameSpace';
const ns = createNameSpace('https://example.org/');
// Example ontology references
const ex = {
Person: ns('Person'),
name: ns('name'),
knows: ns('knows'),
// ... rest of your ontology
};
@linkedShape
export class Person extends Shape {
static targetClass = ex.Person;
@literalProperty({path: ex.name, maxCount: 1})
get name(): string { return ''; }
@objectProperty({path: ex.knows, shape: Person})
get friends(): ShapeSet<Person> { return null; }
}The DSL uses these shape classes to provide compile-time checked queries. You cannot write a query that references a property not defined on the shape. The result type is fully inferred from the selected paths — no manual type annotations needed:
// TypeScript infers: Promise<{id: string; name: string}[]>
const result = await Person.select(p => p.name);
// TypeScript infers: Promise<{id: string; friends: {id: string; name: string}[]}[]>
const nested = await Person.select(p => p.friends.name);The DSL compiles to a backend-agnostic AST — the Intermediate Representation. This is the contract between the DSL and any store implementation.
{
"kind": "select",
"root": { "kind": "shape_scan", "shape": ".../Person", "alias": "a0" },
"projection": [
{ "alias": "a1", "expression": { "kind": "property_expr", "sourceAlias": "a0", "property": ".../name" } }
],
"resultMap": [{ "key": ".../name", "alias": "a1" }]
}The IR uses full SHACL-derived URIs for shapes and properties. A dataset (IDataset) that wants the IR obtains it by calling lower(query) and translates it into its native query language. (Datasets that forward or execute directly never touch the IR — see Datasets, target languages, and the IR.)
For SPARQL-backed stores, the IR is converted into a formal SPARQL algebra — a tree of typed nodes aligned with the SPARQL 1.1 specification.
SparqlSelectPlan {
projection: [?a0, ?a0_name]
algebra: LeftJoin(
BGP(?a0 rdf:type <Person>),
BGP(?a0 <name> ?a0_name) ← wrapped in OPTIONAL
)
}
Properties are wrapped in LeftJoin (OPTIONAL) so missing values don't eliminate result rows.
The algebra is a plain data structure — stores can inspect or optimize it before serialization (e.g., rewriting patterns, adding graph clauses, or pruning redundant joins).
The algebra tree is then serialized into a SPARQL query string with automatic PREFIX generation:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT DISTINCT ?a0 ?a0_name
WHERE {
?a0 rdf:type <.../Person> .
OPTIONAL {
?a0 <.../name> ?a0_name .
}
}The SPARQL endpoint returns JSON results, which are mapped back into typed result objects:
Endpoint returns: Mapped to:
┌──────────┬──────────┐ ┌──────────────────────────────┐
│ a0 │ a0_name │ │ { id: ".../p1", name: "Semmy" } │
│ .../p1 │ "Semmy" │ → │ { id: ".../p2", name: "Moa" } │
│ .../p2 │ "Moa" │ │ ... │
└──────────┴──────────┘ └──────────────────────────────┘
Values are automatically coerced: xsd:boolean → boolean, xsd:integer → number, xsd:dateTime → Date. Nested traversals are grouped and deduplicated into nested result objects.
SparqlStore handles this entire pipeline. Concrete stores only implement the transport:
import { SparqlStore } from '@_linked/core/sparql';
class MyStore extends SparqlStore {
protected async executeSparqlSelect(sparql: string) {
// Send SPARQL to your endpoint, return JSON results
}
protected async executeSparqlUpdate(sparql: string) {
// Send SPARQL UPDATE to your endpoint
}
}See the SPARQL Algebra Layer docs for the full type reference, conversion algorithm, and store implementation guide.
Linked packages expose shapes, utilities, and ontologies through a small package.ts file. This makes module exports discoverable across Linked modules and enables linked decorators.
Minimal package.ts
import {linkedPackage} from '@_linked/core/utils/Package';
export const {
linkedShape,
linkedUtil,
registerOntologies,
registerPackageExport,
registerPackageModule,
packageExports,
getPackageShape,
} = linkedPackage('my-package-name');Decorators and helpers
@linkedShape: registers a Shape class and generates SHACL shape metadata@linkedUtil: exposes utilities to other Linked modulesregisterOntologies(...): registers the package's ontologies and where their data is (see §Registering ontologies)registerPackageExport(...): manually export something into the Linked package treeregisterPackageModule(...): lower-level module registrationgetPackageShape(...): resolve a Shape class by name to avoid circular imports
An ontology is addressed by its package's npm name and its name within the package. The
name is unique in the package, lowercase letters and digits in words joined by - or _
(my-vocab), and need not equal the module's file name. It is not a prefix: prefixes are free
choices and not unique across packages. An ontology's suggested prefix is data,
vann:preferredNamespacePrefix (with vann:preferredNamespaceUri) on the owl:Ontology node of
its Turtle.
A package has one terms module per ontology and one module that registers them all,
src/ontologies/index.ts, which the package's entry imports (linked generate-ontology writes
both from package.json linkedOntologies and the data). A terms module holds only the terms,
each as an {id} literal with its full IRI, and imports nothing:
// src/ontologies/my-vocab.ts
export const ExampleClass = {id: 'https://example.org/ont/my-vocab/ExampleClass'};
export const exampleProperty = {id: 'https://example.org/ont/my-vocab/exampleProperty'};
// src/ontologies/index.ts
import * as myVocab from './my-vocab.js';
import {registerOntologies} from '../package.js';
registerOntologies(
[
{
name: 'my-vocab',
namespace: 'https://example.org/ont/my-vocab/',
terms: myVocab,
data: ['../data/my-vocab.ttl'], // optional, relative to the second argument
suggestedPrefix: 'myvocab', // optional, from the data's vann prefix
},
],
import.meta.url,
);registerOntologies(ontologies: OntologyDeclaration[], moduleUrl: string) throws on an invalid
or repeated name, an empty namespace or an invalid suggested prefix. It registers no prefix.
The data is read only when the ontology sync or the shape check asks (server only, with
loadOntologyFiles(moduleUrl, data)). A namespace function, where code needs one, is
createNameSpace(namespace) from @_linked/core/utils/NameSpace.
getRegisteredOntologies() (@_linked/core/utils/Package) lists every registration as
{packageName, name, namespace, suggestedPrefix?, files, load?, terms}, keyed by package and
name: registering the same package and name again replaces the entry. suggestedPrefixes() lists
{packageName, name, namespace, prefix} for each registration with a suggested prefix, for a tool
that builds an app's prefix map. registerInstalledOntologies(dirs)
(@_linked/core/ontology/registerInstalled) imports <pkg>/ontologies/index of each package
directory.
Prefixes are an app's choice: no package registers one. The app's map is the prefixes of its
linked.app.json, and the app loads it into core once at start (the cli does this on the server
and in the browser bundle):
import {Prefix, type PrefixMap} from '@_linked/core/utils/Prefix';
Prefix.load({schema: 'https://schema.org/', ex: 'https://example.org/ns/'});Prefix.load(map: PrefixMap)replaces the map in use (PrefixMapisRecord<prefix, namespace>). It throws on a prefix that is not a SPARQL/TurtlePN_PREFIXor an empty namespace. When two prefixes share a namespace, compaction uses the later one.Prefix.toMap()returns a copy of the map in use;Prefix.add,delete,clearchange it one entry at a time (tests).- An unknown prefix throws.
Prefix.toFull(value, map?)returns a full IRI as is (one containing://, or with the schemehttp,https,urn,mailto,did,fileordata:Prefix.isFullIri), expandsprefix:localwhen the prefix is in the map, and otherwise throwsUnknownPrefixError(.prefix,.value), naming both. There is no silent fallback to the string as an IRI: query DSL subjects (.for('ex:alice'),update().for,deleteids), property path steps ('ex:knows/ex:name') and every other expansion in core go through it. - Acting for another app: pass that app's map. Expansion and compaction take an optional
explicit map:
Prefix.toFull(value, map),Prefix.toPrefixed(iri, map),Prefix.toPrefixedIfPossible(iri, map),Prefix.getFullURI(prefix, map),Prefix.getPrefix(iri, map),Prefix.findMatch(iri, map),resolveUriOrThrow(value, map). - Output stays self-contained: SPARQL that core writes declares a
PREFIXfor every prefixed name it uses (from the map in use), andshapesToTurtlewrites its own prefix section. - A test declares the prefixes it assumes in its own setup (
Prefix.load(...)).
Linked uses Shape classes to generate SHACL metadata. Paths, target classes, and node kinds are expressed as NodeReferenceValue objects: {id: string}.
import {Shape} from '@_linked/core';
import {ShapeSet} from '@_linked/core/collections/ShapeSet';
import {literalProperty, objectProperty} from '@_linked/core/shapes/SHACL';
import {createNameSpace} from '@_linked/core/utils/NameSpace';
import {linkedShape} from './package';
const ns = createNameSpace('https://example.org/');
// Example ontology references
const ex = {
Person: ns('Person'),
name: ns('name'),
knows: ns('knows'),
// ... rest of your ontology
};
@linkedShape
export class Person extends Shape {
static targetClass = ex.Person;
@literalProperty({path: ex.name, required: true, maxCount: 1})
declare name: string;
@objectProperty({path: ex.knows, shape: Person})
declare knows: ShapeSet<Person>;
}A vocabulary package (@_linked/schema, rdfs, owl, xsd, dcmi, dcat, org, sioc, or
an app's own ontology package) exports one named term per class and property, generated from its
Turtle by linked generate-ontology. Use them like this:
-
Import the module as a namespace.
schema.Personreads like the Turtle'sschema:Person, and a bundler keeps only the terms a module uses:// With the imports of the §Shapes example; needs `@_linked/schema` and `@_linked/xsd` installed. import * as schema from '@_linked/schema/ontologies/schema'; import * as xsd from '@_linked/xsd/ontologies/xsd'; @linkedShape export class Event extends Shape { static targetClass = schema.Event; @literalProperty({path: schema.startDate, datatype: xsd.dateTime, maxCount: 1}) declare startDate: Date; }
There is no aggregated object (
import {schema} from …). A local name that is not an identifier follows one rule:schema:3DModelisschema._3DModel,schema:yieldisschema.yield_,dcterms:ISO639-2isdcterms.ISO639_2. A terms module exports only terms; a namespace function, where code needs one, iscreateNameSpace(namespace). -
Prefer the accessor over
{id}.schema.nameis checked by the compiler and found by "go to definition";{id: 'http://schema.org/name'}is a string nothing checks. -
Use
{id}(orcreateNameSpace) only for a namespace without a module, such as a vocabulary no package mirrors, as in the §Shapes example.
By default an object property is a plain reference: the two nodes exist independently, and removing the link leaves the referenced node untouched. Two optional flags let you model composition (ownership) instead, so the framework cleans owned nodes up automatically.
contains: true on an object property — the edge asserts exclusive ownership of the
value. Whenever that edge goes away, the owned node is deleted with it:
parent.delete()deletes the owned child (and its owned sub-tree). So do the bulk deletes,DeleteBuilder.from(Parent).all()and.where(...).update({ownedProp: newValue})replaces the owned node — the old one is deleted, not orphaned.update({ownedProp: {remove: [x]}})deletesxitself, not just the link.
Ownership belongs to the property, not to its predicate. A plain property of another shape on
the same predicate (say ex:avatar without contains) is a reference: replacing it or deleting
its subject never deletes the value, and it is not class-checked. A contains property needs a
shape; declaring one without it throws when the class is decorated.
@linkedShape
export class Person extends Shape {
static targetClass = ex.Person;
// Person exclusively owns its avatar image. Replacing or clearing `avatar`,
// or deleting the Person, deletes the old ImageObject too.
@objectProperty({path: ex.avatar, shape: ImageObject, maxCount: 1, contains: true})
declare avatar: ImageObject;
}Replace versus patch in place. A plain nested object on a contains property is a
replace: a new node is created and the old one is deleted. To change the owned node itself,
update it directly:
// Replace: creates a new ImageObject, deletes the old one.
await Person.update({avatar: {contentUrl: 'https://cdn.example.org/b.png'}}).for(personId);
// Patch in place: the same ImageObject keeps its id and its other fields.
await ImageObject.update({contentUrl: 'https://cdn.example.org/b.png'}).for(avatarId);
// Re-sending the current id is a no-op: the edge and the node are left as they are.
await Person.update({avatar: {id: avatarId}}).for(personId);The same holds for an array overwrite of a contains set: ids you send again are kept, ids you
leave out are deleted. {id: current, ...fields} is a nested create with a predefined id, not a
patch: it deletes all of that node's triples and writes the new ones.
Linking an existing node by {id} is allowed, e.g. upload an image first, then attach it with
{avatar: {id: uploadedId}}. The link transfers ownership: the linked node is deleted when it is
replaced, removed, or when the owner is deleted.
Because of that, core checks the class of every id linked into a contains property that
declares a shape. A link is refused, and nothing is written, when the store already types the
node and none of its types (stored or inferred) is the shape's targetClass or the property's
sh:class. A subclass instance carries its superclasses' types through the store's inference, so
it is accepted:
Cannot link <https://my.app/alice> into the contains property "avatar" of <…/shape/my-pkg/Person>:
it is typed <https://example.org/Person>, but "avatar" only owns instances of <https://schema.org/ImageObject>. …
Ids that do not exist yet, or have no rdf:type, are allowed, so upload-then-attach works in
either write order. The check runs in SparqlDataset (one SELECT per mutation that links owned
ids, none otherwise). Plain references are never checked.
Two limits: an id linked while it is untyped, and typed as something else later, is not caught.
Nor is a node typed by another request between the check's SELECT and the UPDATE that
follows it: the two are separate requests, not one transaction. Raw SPARQL updates bypass the
check altogether. SHACL validation of sh:class is the backstop for all three.
What a delete removes. Deleting an owner deletes everything it owns (see the recursion
limit below): the values of its contains properties (own and inherited), the values their shapes own, and so on. A
replace or set-remove does the same below the old value. The walk follows the shapes, not the
predicates: from the owner's shape it follows that shape's contains properties to their value
shapes, then those shapes' contains properties. When a value is typed with a registered
sub-shape of the declared value shape, the contains properties the sub-shape adds are followed
too, but only from nodes of that sub-shape's class (?img a ex:Photo . ?img ex:thumbnail ?t,
which the store's inference also matches on the nodes of a subclass of ex:Photo).
The class must tell those nodes apart: an added property is not followed through the parent
(core warns once) when the sub-shape has no targetClass, shares it with one of its
super-shapes, or shares it (or a subclass of it) with another shape that holds the same predicate
as a plain property (through a simple path). Another shape that merely shares the class, without that predicate,
changes nothing. A copy of a sub-shape that adds nothing of its own, with the same class (a
"profile") or with no targetClass at all, is skipped silently: its parent's entry covers it.
When a sub-shape redeclares an inherited contains property without contains, the inherited
ownership still applies.
Recursion has a limit. A shape that owns its own kind (a list cell owning the next cell through
rdf:rest, a comment owning its replies) is followed to any depth (rdf:rest*). Any other cycle
(A owns B owns A, or a sub-shape's property leading back into its own kind) is followed hop by
hop, each hop checked against the class that declares it, for at most MAX_OWNED_RECURSION (4)
re-entries per path; values deeper than that are left in place, and so is anything behind an
untyped node on such a cycle. rdf:nil, the end of every list, is never deleted.
The bulk deletes (.all(), .where(...)) remove the same subtree for every matched subject; they
do not delete incoming references to the subjects, which a delete by id does. Bulk mutations are
polymorphic, like select and count: Thing.deleteAll() or Thing.update(...).forAll() matches
?s a schema:Thing, so it changes the instances of every subclass of Thing too.
Non-owning references to an owned node, through other properties or from other subjects, are
allowed. They are not cleaned up: after the owner is deleted they dangle. The same holds when two
subjects own one node (an {id} linked into a second owner, which breaks the single-owner rule
below): deleting either owner deletes the node, and the other owner's edge dangles.
The single-owner constraint. An owned node has at most one owner. Shape sync persists this as
one generated SHACL NodeShape per contains property (simple paths only). It is written only by
the class that declares the property, at the property shape's IRI plus .singleOwner:
Thing.image gets one constraint at Thing, not one per subclass. It is marked core:generated true and has no sh:targetClass, so catalog readers can skip it, and it is never registered in
memory. It counts only instances of the declaring class (subclass instances included, through
the store's inference), so a plain reference through the same predicate from another class is
allowed. For Thing.image:
<…/shape/my-pkg/Thing/image.singleOwner> a sh:NodeShape ;
core:generated true ;
sh:targetObjectsOf schema:image ;
sh:property <…/Thing/image.singleOwner/owners> .
<…/Thing/image.singleOwner/owners> a sh:PropertyShape ;
rdfs:label "owners" ;
sh:path [ sh:inversePath schema:image ] ;
sh:qualifiedValueShape [ sh:class schema:Thing ] ;
sh:qualifiedMaxCount 1 .sh:class follows rdf:type/rdfs:subClassOf*, so with the ontology hierarchy synced an
ex:Project ⊑ schema:Thing subject counts as an owner. A declaring shape without targetClass
gets no constraint (sync logs it once).
The inverse path is stored as an untyped blank node, as is every complex sh:path shape sync
writes (inverse, alternative, sequence, */+/?): an IRI in sh:path is a predicate path to a
SHACL validator. Nothing marks them: PropertyShape.path and the path operator slots are
declared sh:BlankNodeOrIRI, see Blank-node values. Versions before 2.30 wrote IRI-named, typed path nodes (…/owners/inv, …/region/seq/0). The
next sync writes blank nodes and leaves those behind, unreferenced; re-create the dataset, or
delete them by IRI prefix (…/owners/inv, …/seq/…), to remove them.
Read a stored path's structure in the same query that finds it (?ps sh:path ?p . ?p sh:inversePath ?x): a blank node's label is not stable across queries.
Each step of a path is a prefixed name (ex:name) or an absolute IRI (https://…, {id: 'urn:x:name'}, or <urn:x:name> in the string syntax). A string step is written into a query as a
prefixed name when it is one SPARQL prefixed-name token, expanded against the app's prefix map (an
unknown prefix throws UnknownPrefixError); a token with an IRI scheme (urn:prop:a) and any other
step are written as <IRI>. A path read as data (fromWire,
ShapeCatalog.from, registerRuntimeShape) or given to a decorator is checked when it loads: a
step that is neither throws InvalidPropertyPathError, naming the step and its property shape.
The declaring shape owns its generated shapes through core:generatedShape, a contains edge:
<…/Thing> core:generatedShape <…/Thing/image.singleOwner>. Syncing a shape is still two
requests: deleting the shape deletes the generated shapes with it (their own triples, property
shapes and their blank path and qualified-shape nodes, never the predicate IRI), and the create writes the current ones nested in
the same request. A removed or renamed accessor therefore loses its constraint, a pruned shape
takes its constraints with it, and a NodeShape the shape does not own is never touched.
singleOwnerShapeIri, singleOwnerProperties and buildSingleOwnerConstraints are exported if
you need to recompute them.
Each constraint counts owners through its own predicate only. A node owned through two different
contains predicates (schema:image from one subject, ex:attachments from another) is not
reported. That needs overlapping value shapes, and the class check already refuses a wrongly
typed link.
Not enforced on write. Core does not check the single-owner rule, or
sh:classon non-owning references, when it writes. The default Fuseki setup does not validate SHACL on write either: a dataset exposes only query, update and graph-store services. Run a SHACL validator (Jena SHACL, pySHACL, …) against the persisted shapes to enforce them.
dependent: true on a shape is deprecated and has no effect: every node a contains property
reaches is deleted with its owner, whether or not its shape is dependent. It is still accepted,
and shape sync still persists it (core:dependent).
Every node core writes is IRI-named unless the property that holds it says otherwise. A
nested create is written as a blank node when its property's nodeKind allows blank nodes:
nodeKind of the property |
nested value without an id | with an id |
|---|---|---|
unset, shacl.IRI (the objectProperty default), shacl.IRIOrLiteral |
IRI (minted) | IRI |
shacl.BlankNodeOrIRI |
blank node | IRI |
shacl.BlankNode |
blank node | error |
shacl.BlankNodeOrLiteral |
blank node | error |
shacl.Literal |
error | error |
@linkedShape
export class Place extends Shape {
static targetClass = ex.Place;
@objectProperty({path: ex.geo, shape: GeoPoint, maxCount: 1, nodeKind: shacl.BlankNodeOrIRI})
declare geo: GeoPoint;
}
// <place1> ex:geo _:b0 . _:b0 ex:lat "52.1" .
await Place.create({geo: {lat: '52.1'}}).withId(place1);A blank node is written with its field triples only, without rdf:type: its shape is the one the
property declares. Nothing about it travels in DSL-JSON; the side that runs the mutation decides
from its own copy of the shape. Core uses this for SHACL paths: PropertyShape.path, the
PathNode slots and PathList.first / .rest are sh:BlankNodeOrIRI, so a predicate path (an
{id}) stays an IRI and every operator node and list cell becomes a blank node.
A blank value goes with what holds it. Deleting the subject (by id, .all(), .where()),
replacing or unsetting the property, and deleting or replacing an owned (contains) value at any
depth delete the blank value and the blank nodes below it, following only blank-capable properties.
Only blank nodes are deleted: an IRI the structure refers to (schema:image in
[ sh:inversePath schema:image ], an entity a list lists) is never touched, nor is a blank node
reached through one. contains is not needed for this.
Not supported yet, and refused with an error rather than written wrongly:
- an
update/upsert/updateWherewhose new value would be a blank node (its INSERT runs once per WHERE solution, so the node would be written several times). Create it with its parent, or give it an id undersh:BlankNodeOrIRI; - addressing a blank node by id: linking
{id: '_:b0'},update().for('_:b0'), deleting{id: '_:b0'}. Change it through the property that holds it.
A select returns a blank value as its store label (Fuseki: b0), valid only within that result.
Queries are expressed with the same Shape classes and compile to a query object that a store executes.
Use this section as a quick start. Detailed query variations are documented in Query examples below.
A few quick examples:
1) Select one field for all matching nodes
const names = await Person.select((p) => p.name);
/* names: {id: string; name: string}[] */2) Select all decorated fields of nested related nodes
const allFriends = await Person.select((p) => p.knows.selectAll());
/* allFriends: {
id?: string;
knows: {
id?: string;
...all decorated Person fields...
}[]
}[] */3) Apply a simple mutation
const updated = await Person.update({name: 'Alicia'}).for({id: 'https://my.app/node1'});
/* updated: {id: string} & UpdatePartial<Person> */LinkedStorage is the routing helper (not an interface). It routes query objects — by their target shape — to a dataset that implements IDataset.
import {LinkedStorage} from '@_linked/core';
import {InMemoryStore} from '@_linked/rdf-mem-store';
LinkedStorage.setDefaultDataset(new InMemoryStore());You can also route specific shapes to specific stores:
LinkedStorage.setDatasetForShapes(new InMemoryStore(), Person);For apps that want to keep their storage wiring in JSON rather than code, @_linked/core ships two helpers that read a linked.datasets.json-style file and instantiate the stores per alias.
// @_linked/core/utils/parseDatasetsConfig — pure parser, browser-safe
parseDatasetsConfig(raw: unknown, env?: Record<string, string | undefined>): DatasetsConfigValidates a config of the shape { datasets: { <alias>: { store: <npm-path>, config: { ... } } } }, resolves ${VAR} / ${VAR:-default} placeholders in string leaves against env, strips _* comment keys, and throws on malformed input. Returns a typed DatasetsConfig.
// @_linked/core/utils/loadStores — async, backend-only
loadStores<T>(config: DatasetsConfig): Promise<Record<string, T>>Dynamically imports each alias's store path and instantiates the resolved class with new StoreClass(entry.config). Returns alias → store. The graph targets are not part of a store's config: they are one global setting (see Ontology and shape graphs); an entry whose config still sets ontologyGraph / shapeGraph gets a warning. Convention: the last segment of the store path is the named export to use (with default as a fallback). Async + uses runtime dynamic import() — works in Node where module specifiers resolve at runtime. Frontends can't use this (webpack can't bundle import(variableString)); frontend code instead imports each store class statically and constructs per alias by hand.
Host loader. When globalThis[Symbol.for('@_linked/core:importModule')] is a function (specifier: string) => Promise<unknown>, loadStores imports each store through it instead of import(), passing the store specifier unchanged; it is read on every call. linked start (dev) sets it so a store comes from the same copy of its package as the app's own imports under Vite's SSR runner. Nothing else needs to set it. loadStores reads it through importPackageModule (@_linked/core/utils/importPackageModule), which other framework code that imports package modules by specifier uses too (registerInstalledOntologies); see docs/architecture/loading-package-modules.md.
A typical backend setup looks like:
import rawConfig from './linked.datasets.json' assert { type: 'json' };
import { parseDatasetsConfig } from '@_linked/core/utils/parseDatasetsConfig';
import { loadStores } from '@_linked/core/utils/loadStores';
import { LinkedStorage } from '@_linked/core/utils/LinkedStorage';
const config = parseDatasetsConfig(rawConfig, process.env);
const stores = await loadStores(config);
LinkedStorage.setDefaultDataset(stores.appData);
// or per-shape:
// LinkedStorage.setDatasetForShapes(stores.appData, [Person, BlogPost]);The file holds what changes per deployment (stores, endpoints, credentials). Graph routing is not
part of it: the app routes shapes in code, e.g.
LinkedStorage.setDatasetForShapes(stores.appData, [BlogPost], {graph: 'own'}) (see
docs/architecture/ontologies.md, Graph routing). An alias with a graphs key gets a warning
from loadStores and routes nothing.
Each store class must accept a single config-object argument (new StoreClass(config)). @_linked/fuseki's FusekiStore and @_linked/server's BackendAPIStore both follow this contract.
LinkedStorage routes quads; LinkedFileStorage routes files. Files are addressed by purpose — a short string naming what the files are for (uploads, appAssets, …) — so an app can send release bundles to a CDN bucket and user uploads to another store without any caller knowing which.
import {LinkedFileStorage, FileStorePurposes} from '@_linked/core';
import {S3FileStore} from '@_linked/s3';
LinkedFileStorage.setDefaultStore(new S3FileStore({/* … */})); // fallback for every purpose
LinkedFileStorage.setStore(FileStorePurposes.appAssets, new S3FileStore({/* cdn */}));
await LinkedFileStorage.getStore(FileStorePurposes.appAssets).saveFile('/app.js', bytes);
LinkedFileStorage.accessURLFor(FileStorePurposes.appAssets); // → https://cdn.example.comgetStore(purpose) resolves in one order, and there is no fourth case:
- configured — a store was passed to
setStore(purpose, store)→ that store; - registered but not configured → the default store, with a one-time
console.debugline saying the purpose has no dedicated store (the normal case for a single-store app; it throws instead if no default store is set); - not registered → throws, listing the known purposes. A typo like
'appAsset'must not silently write release bundles into the uploads store.
uploads is an ordinary purpose with no special case: setDefaultStore configures no purpose, so uploads reaches the default store through the fallback in step 2, and setStore('uploads', other) wins whether it is called before or after setDefaultStore.
| Method | Meaning |
|---|---|
registerPurpose(purpose, {description?}) |
Declare a purpose without configuring a store. |
setStore(purpose, store) |
Configure (and register) the store; calls store.init(). |
getStore(purpose) |
Resolve per the rule above. A live reference — call it per operation if config can change. |
hasStore(purpose) |
Whether a store was explicitly configured. Ignores the fallback, so it is false while getStore still works. Never use it as a guard before getStore. |
listPurposes() |
{purpose, description?, configured} for every declared purpose. |
accessURLFor(purpose) |
The accessURL of the resolved store. |
getDefaultStore() / setDefaultStore(store) |
The fallback store. getDefaultDataset/setDefaultDataset are @deprecated aliases that forward, kept until the next major. |
Export the constant and register it in the same module, so importing the constant is the registration and load order resolves itself:
// @_linked/captures/purposes.ts
import {LinkedFileStorage} from '@_linked/core';
export const CapturesPurpose = 'captures';
LinkedFileStorage.registerPurpose(CapturesPurpose, {description: '3D capture artifacts.'});A consumer that imports CapturesPurpose can call getStore(CapturesPurpose) immediately; an app that never splits it out simply gets the default store. Core ships only FileStorePurposes.uploads and FileStorePurposes.appAssets — a new purpose belongs in the package that owns the concept.
await store.saveFile('/app.js', bytes, {
mimeType: 'application/javascript',
cacheControl: 'public, max-age=31536000, immutable',
metadata: {release: '1.2.3'},
preservePath: true, // keep a generated release key exactly as supplied
preventDuplicates: true, // rename instead of overwriting when the path is taken
});
// Backwards compatible: a string third argument is read as the positional mimeType.
await store.saveFile('/photo.webp', bytes, 'image/webp', true);options.preventDuplicates wins over the positional fourth argument. Implementations should start with normalizeSaveFileOptions(options, preventDuplicates), which collapses both forms into one object.
Set preservePath: true only for generated object keys whose paths are already part of an external contract, such as
a release manifest. A store must either keep that key verbatim or reject it; it must not silently sanitise or rename
the key. Filesystem-backed stores must reject absolute and traversal paths before writing. Ordinary user uploads should
leave this option unset and retain the store's normal filename hygiene.
There is no core-wide
preventDuplicatesdefault. When a caller does not specify one it staysundefinedall the way to the store, and each store applies its own:LocalFileStoreadds a random suffix,S3FileStoreoverwrites. Pass it explicitly whenever the behaviour matters — never assume a default.
const stat = await store.statFile?.('/app.js'); // FileStat | null | undefined
if (!stat) {
// Store cannot report metadata, or the file is absent — skip verification.
} else if (stat.sha256) {
// Only a real content hash verifies. `etag` is not one on S3.
}statFile is optional by design: a store without it still publishes. A caller must treat a missing method (undefined) or a missing sha256 as "cannot verify" and continue, rather than failing the upload or falling back to etag.
SHACL shapes are ideal for data validation. Linked generates SHACL shapes from your TypeScript Shape classes, which you can sync to your store for schema-level validation. When your store enforces those shapes at runtime, you get both schema validation and runtime enforcement for extra safety.
The query DSL is schema-parameterized: you define your own SHACL shapes, and Linked exposes a type-safe, object-oriented query API for those shapes.
- Basic selection (literals, objects, dates, booleans)
- Target a specific subject by
{id}or instance - Multiple paths and mixed results
- Nested paths (deep selection)
- Sub-queries on object/set properties
- Filtering with
where(...)andequals(...) and(...)/or(...)combinations- Set filtering with
some(...)/every(...)(and implicitsome) - Outer
where(...)chaining - Counting:
.count()for how many instances match,.size()for a property's value count - Grouping:
.groupBy('key')withExpr.count(),.countDistinct(),.min(),.max(),having - A node's types with
$type(): as a field, a group key, a filter, a sort key - Custom result formats (object mapping)
- Computed values — derive new fields with arithmetic, string, date, and comparison methods
- Expression-based WHERE filters (
p.name.strlen().gt(5)) - Standalone expressions with
Expr— timestamps, conditionals, null coalescing - Type casting with
.as(Shape) - MINUS exclusion (by shape, property, condition, multi-property, nested path)
- Sorting, limiting, and
.one() - Query context variables
- Preloading (
preloadFor) for component-like queries - Create / Update / Delete mutations (including bulk and conditional)
- Expression-based updates (
p => ({age: p.age.plus(1)})) - Dynamic query building with
SelectBuilder(aliasQueryBuilder) - Composable field sets with
FieldSet - Mutation builders (
CreateBuilder,UpdateBuilder,DeleteBuilder) - DSL-JSON serialization (
toJSON/fromJSON) — the standard wire format - Query and FieldSet JSON serialization / deserialization
Result types are inferred from your Shape definitions and the selected paths. Examples below show abbreviated result shapes.
/* names: {id: string; name: string}[] */
const names = await Person.select((p) => p.name);
/* friends: {
id: string;
knows: { id: string }[]
}[] */
const friends = await Person.select((p) => p.knows);
const dates = await Person.select((p) => [p.birthDate, p.name]);
const flags = await Person.select((p) => p.isRealPerson);/* Result: {id: string; name: string} */
const one = await Person.select((p) => p.name).for({id: 'https://my.app/node1'});
const missing = await Person.select((p) => p.name).for({id: 'https://my.app/missing'}); // null/* Result: Array<{id: string; name: string; knows: Array<{id: string}>; bestFriend: {id: string; name: string}}> */
const mixed = await Person.select((p) => [p.name, p.knows, p.bestFriend.name]);
const deep = await Person.select((p) => p.knows.bestFriend.name);const detailed = await Person.select((p) =>
p.knows.select((f) => f.name),
);
const allPeople = await Person.selectAll();
const detailedAll = await Person.select((p) =>
p.knows.selectAll(),
);const filtered = await Person.select().where((p) => p.name.equals('Semmy'));
const byRef = await Person.select().where((p) =>
p.bestFriend.equals({id: 'https://my.app/node3'}),
);const andQuery = await Person.select((p) =>
p.knows.where((f) =>
f.name.equals('Moa').and(f.hobby.equals('Jogging')),
),
);
const orQuery = await Person.select((p) =>
p.knows.where((f) =>
f.name.equals('Jinx').or(f.hobby.equals('Jogging')),
),
);const implicitSome = await Person.select().where((p) =>
p.knows.name.equals('Moa'),
);
const explicitSome = await Person.select().where((p) =>
p.knows.some((f) => f.name.equals('Moa')),
);
const every = await Person.select().where((p) =>
p.knows.every((f) => f.name.equals('Moa').or(f.name.equals('Jinx'))),
);const outer = await Person.select((p) => p.knows).where((p) =>
p.name.equals('Semmy'),
);Two different questions, two different queries.
.count() answers how many instances match — one number for the whole match set. It respects
where and minus, and ignores limit/offset, so a paged table can ask for its total row count
beside the page it is showing:
/* Result: number */
const total = await Person.count();
const matching = await Person.select().where((p) => p.name.equals('Semmy')).count();
// Lowers to: SELECT (COUNT(DISTINCT ?a0) AS ?count) WHERE { ?a0 a Person ; name ?n . FILTER(…) }.size() answers how many values a property has, per row:
/* Result: Array<{id: string; knows: number}> */
const perPerson = await Person.select((p) => p.knows.size());A count resolves to a real number and rejects on failure — it never reports an unreachable store
as 0. .toCount() gives you the count query itself, for a router that forwards
(builder.toCount().toJSON()) rather than executes.
A count is always of distinct values, so two counted paths in one select do not inflate each
other ({friends: p.friends.size(), nicks: p.nickNames.size()}).
Select the keys and the aggregates, filter the members with where, then group by the key
fields, named as in the select's object:
import {Expr} from '@_linked/core';
/* Result: Array<{hobby: string; n: number}> */
const perHobby = await Person.select((p) => ({hobby: p.hobby, n: Expr.count()}))
.where((p) => p.name.isDefined())
.groupBy('hobby');
/* Result: Array<{friend: {id: string}; last: Date}> */
const latest = await Person.select((p) => ({friend: p.bestFriend, last: p.birthDate.max()}))
.groupBy('friend')
.having('last', (last) => last.gt(new Date('2000-01-01')))
.orderBy('last', 'DESC')
.limit(10);
// Lowers to: SELECT ?a0_bestFriend (MAX(?a0_birthDate) AS ?a2) WHERE { … }
// GROUP BY ?a0_bestFriend HAVING(MAX(?a0_birthDate) > "2000-01-01T00:00:00.000Z"^^xsd:dateTime)
// ORDER BY DESC(MAX(?a0_birthDate)) LIMIT 10- Aggregates:
Expr.count()(the members of the group),.countDistinct()on a single-valued path,.size()on a multi-valued one,.min()/.max()on literal paths. Each is typed: a count is anumber,min/maxthe value's type. - Keys:
groupBy('a', 'b')names one or more fields; every field that is not an aggregate must be a key (a type error and a runtime error otherwise). A key can be a nested path (p.bestFriend.hobby), a relation (its value is{id}), the subject itself ({person: p}), or a multi-valued path, where each value is its own group. Members without the key form a group whose key isnull. - The grouped builder has
having(field, condition)(several calls must all hold),orderBy(field | fields, direction),limit,offset,fromandexec. It has nowhere(filter the members before grouping), no.for/.one, and its rows have noid. groupByneeds the object form of select, and is refused after.for(id),.one(), anorderByon the members, or awherethat holds an aggregate (filter groups withhaving). A field is a key or an aggregate: arithmetic on aggregates (Expr.count().plus(1)) is one, an aggregate mixed with a member's own value is refused.orderByon a plain select sorts by paths and refuses an aggregate. The groupedorderByputs the group without a value for that field last, in both directions (see "Sorting, limiting, one"). Aggregates are refused as update values.- Outside a grouped select, an aggregate is per row, like
.size():Person.select((p) => ({first: p.nickNames.min()})).
A grouped select is still a select: a dataset receives it through readQuery and lowers it to
an IRSelectQuery with groupBy. It needs the 'groupBy' feature (min/max need theirs), and
a dataset that does not list it in supports is refused before it is asked. SparqlDataset
supports all of them.
$-prefixed members are facts about the node, not shape fields. No property of a shape can
shadow them, and they read the same on every shape. $type() is the node's rdf:type values:
multi-valued IRI nodes, each typed {id}.
/* Result: Array<{id: string; name: string; $type: {id: string}[]}> */
const things = await Thing.select((t) => [t.name, t.$type()]);
/* Result: Array<{kind: {id: string}; n: number}>: how many terms of each type a graph holds */
const perType = await Term.select((t) => ({kind: t.$type(), n: Expr.count()}))
.from(graph)
.groupBy('kind');
const hasClass = await Thing.select().where((t) => t.$type().equals({id: C})).exists();
const notOfClass = await Thing.select((t) => t.name).where((t) => t.$type().none((c) => c.equals({id: C})));
const byFriendType = await Person.select((p) => p.name).orderBy((p) => p.bestFriend.$type());- Where:
equals(x)holds when some type isx(as on any multi-valued relation:p.friends.equals({id})). It is an EXISTS of its own, so a projected$type()keeps every type andt.$type().equals(A).and(t.$type().equals(B))finds the nodes of both types;.not()is "no type isx".some(…)andnone(…)take a callback on the type node. A member's types can be tested inside a quantifier,p.friends.some((f) => f.$type().equals({id: C}).and(…)), or from the outside,p.friends.$type().equals({id: C})(some friend has typeC). Asome/every/nonecallback takes any quantifier on the member's own paths (p.friends.some((f) => f.friends.some(…))). - Group key: one group per type value. A node with two types is in two groups, so the counts
of a
groupByon$type()can add up to more than the number of nodes. - Sorting follows the rules in "Sorting, limiting, one": a node without a type (on a nested path, say) sorts last, and a node with several types sorts by its lowest type IRI ascending and its highest descending.
- Stored and inferred types:
$type()answers what the store answers forrdf:type. On a store that infers classes throughrdfs:subClassOf(an inferring Fuseki, such as the inferring test image), that is the stored types and every superclass; on a plain store, the stored ones only.InMemoryDatasetanswers$type()as a field with the stored types, and refuses it in a where, a group key or a sort key (as it refuses those on any path). - DSL-JSON: the path segment
$type("fields": ["$type"],"bestFriend.$type","where": {"$type": {"@id": "…"}}), wire version 1.6. A label select takes it too (select(['name', '$type'])). Reserved names:$-prefixed names andand/or/notthrow; the other reserved names warn (a query builder member such assize), so an existing shape keeps loading. A shape loaded as data is only reported.$graph()(a node's named graphs) is planned.
Triples reads answer triples rather than rows, for data no shape describes (a draft graph, an import's staging graph, a node of unknown type, the references to a term):
import {DraftGraph, TriplesBuilder} from '@_linked/core';
import {graphs} from '@_linked/core/ontologies/graphs';
import {rdf} from '@_linked/core/ontologies/rdf';
// Every triple of one graph, registered or not. Routed like DraftGraph's graph queries, so it
// reads where DraftGraph.replaceContent(graph, body) writes.
const draft = await DraftGraph.readContent(graph).limit(1001);
// One subject's triples (as a subject), under any type or none: in these graphs, or where a
// shapeless ask of it looks (the default graph, or every graph the store's routing knows).
const staged = await TriplesBuilder.forNode({id: instanceId}).from(importGraph).exec(dataset);
const types = staged.filter((t) => t.predicate.id === rdf.type.id).map((t) => t.object);
// The triples that point at a node (as their object), in the same graphs or the same fallback.
const refs = await TriplesBuilder.toNode({id: term}).from(graphs.allOntologies).exec(dataset);
// The triples of one predicate: it needs .from(), a predicate read names the graphs it reads.
// .withPredicate(p) also narrows a node read; .exists() asks whether the read finds any triple.
const used = await TriplesBuilder.withPredicate({id: property}).from(dataGraphs).exists(dataset);
// A list of terms is a terms read: which of them occur, in one query (a Set of IRIs).
const instantiated = await TriplesBuilder.toNode(classes).withPredicate(rdf.type).from(dataGraphs).whichExist(dataset);
const usedProperties = await TriplesBuilder.withPredicate(properties).from(dataGraphs).whichExist(dataset);Each answer is a Triple[]: {subject, predicate, object, graph?}. An IRI is {id}, a blank node
{id: '_:label'} (the label holds within that answer only), and a literal {value, datatype, language?} with its lexical form and its datatype as {id} (xsd:string for a plain literal,
rdf:langString for a tagged one), so a read writes back unchanged. graph is the named graph
the triple was read in, absent for the default graph. .limit(n) bounds a read; no order is
implied. .from() takes what select's .from() takes, portable names included. A read with
no node (a subject or an object) and no graph is refused, and so is a predicate read without
graphs. .exists() sends the read with limit: 1 (a SPARQL store stops at the first match).
toNode and withPredicate take a list of terms too ([{id}, …], one list per read): a terms
read. .whichExist() answers the Set of the listed IRIs that some triple of the read has in
that position, sent to IDataset.readQuery as a terms read (kind: 'triples_terms',
toTriplesTermsQuery(), parseTriplesTermsQuery() at a wire boundary) to a dataset that lists
'triplesTerms' and 'triplesPattern' in supports; SparqlDataset answers it with
SELECT DISTINCT ?t WHERE { VALUES ?t { … } FILTER EXISTS { … } }, one query per 400 terms, each
term stopping at its first match. It has no exec() (it answers terms, not triples);
.exists() asks whether any term occurs.
A triples read is plain data (toTriplesQuery(), parseTriplesQuery() at a wire boundary) sent to
IDataset.readQuery, only to a dataset that lists 'triples' in supports. SparqlDataset
answers it with one SELECT; a dataset that does not list it is refused. A read that fixes the predicate or the object (toNode,
withPredicate) needs the 'triplesPattern' feature in the dataset's supports: a dataset that
predates it is refused rather than sent fields it would drop. LinkedStorage sends a graph read
where its record class's graph queries go, and any other read (no shape, no routing key) to every
dataset it knows, joining the answers up to the limit, as it asks a shapeless ask; .exists()
stops at the first dataset that answers a triple. A router forwards it as a read.
A read with no shape has no routing key. LinkedStorage therefore sends a shapeless read that
names no dataset to every dataset it knows: a node read (TriplesBuilder.forNode({id}),
toNode({id})) or a predicate read is read in each and the answers joined, a shapeless ask (Shape.exists(iri) on the base class,
AskBuilder.forNode(iri)) asks each in turn until one answers true. The answer is right, but it
costs a query per dataset, so with more than one dataset configured it logs a notice
(console.warn, [linked] … sent it to all 2 datasets it knows …) once per call site, naming the
fix:
await Shape.exists(iri, dataset); // ask one dataset
await AskBuilder.forNode(iri).exec(dataset);
await TriplesBuilder.forNode({id}).exec(dataset); // read one dataset
await TriplesBuilder.forNode({id}).from(importGraph); // read only these graphsThere is no notice with one dataset, with an explicit dataset, for a shaped read (it routes by its
shape), or for a triples read with .from() graphs, which still reads every dataset, in those
graphs only. The call site is the first stack frame outside core. A builder awaited without
.exec() (await TriplesBuilder.forNode({id})) runs from a microtask with its caller off the
stack, so all such reads share one notice per kind of read.
/* Result: Array<{id: string; nameIsMoa: boolean; numFriends: number}> */
const custom = await Person.select((p) => ({
nameIsMoa: p.name.equals('Moa'),
numFriends: p.knows.size(),
}));You can compute derived values directly in your queries — string manipulation, arithmetic, date extraction, and more. Expression methods chain naturally left-to-right.
// String length as a computed field
const withLen = await Person.select((p) => ({
name: p.name,
nameLen: p.name.strlen(),
}));
// Arithmetic chaining (left-to-right, no hidden precedence)
const withAge = await Person.select((p) => ({
name: p.name,
ageInMonths: p.age.times(12),
agePlusTen: p.age.plus(10).times(2), // (age + 10) * 2
}));
// String manipulation
const upper = await Person.select((p) => ({
shout: p.name.ucase(),
greeting: p.name.concat(' says hello'),
}));
// Date extraction
const birthYear = await Person.select((p) => ({
year: p.birthDate.year(),
}));Expression methods by type:
| Type | Methods |
|---|---|
| Numeric | plus, minus, times, divide, abs, round, ceil, floor, power |
| String | concat, contains, startsWith, endsWith, substr, before, after, replace, ucase, lcase, strlen, encodeForUri, matches |
| Date | year, month, day, hours, minutes, seconds, timezone, tz |
| Boolean | and, or, not |
| Comparison | eq, neq, gt, gte, lt, lte |
| Null-handling | isDefined, isNotDefined, defaultTo |
| Type | str, iri, isIri, isLiteral, isBlank, isNumeric, lang, datatype |
| Hash | md5, sha256, sha512 |
Every single-valued literal on a query path has the methods of its type, in select, where and
update callbacks alike. A relation (an object property) has equals, oneOf, notOneOf,
str, isIri, isBlank, isDefined and isNotDefined: .where((k) => k.ofShape.isDefined()).
The root subject has equals, str, isIri and isBlank: .where((p) => p.isIri()).
Expressions can be used in where() clauses for computed filtering:
// Filter by string length
const longNames = await Person.select((p) => p.name)
.where((p) => p.name.strlen().gt(5));
// Filter by arithmetic
const young = await Person.select((p) => p.name)
.where((p) => p.age.plus(10).lt(100));
// Chain expressions with and/or
const filtered = await Person.select((p) => p.name)
.where((p) => p.name.strlen().gt(3).and(p.age.gt(18)));
// Expression WHERE on nested paths
const deep = await Person.select((p) => p.name)
.where((p) => p.bestFriend.name.strlen().gt(3));
// Expression WHERE on mutations
await Person.update({status: 'senior'}).where((p) => p.age.plus(10).gt(65));Some expressions don't belong to a specific property — like getting the current timestamp, picking the first non-null value, or conditional logic. Use the Expr module for these:
import {Expr} from '@_linked/core';
// Current timestamp
const withTimestamp = await Person.update({lastSeen: Expr.now()}).for(entity);
// Conditional expressions
const labeled = await Person.select((p) => ({
label: Expr.ifThen(p.age.gt(18), 'adult', 'minor'),
}));
// First non-null value
const display = await Person.select((p) => ({
display: Expr.firstDefined(p.name, p.nickNames, 'Unknown'),
}));Cast to a subtype when you know the concrete shape — for example, selecting dog-specific properties from a pets collection:
const guards = await Person.select((p) => p.pets.as(Dog).guardDogLevel);// Exclude by shape — all Persons that are NOT also Employees
const nonEmployees = await Person.select((p) => p.name).minus(Employee);
// Exclude by property existence — Persons that do NOT have a hobby
const noHobby = await Person.select((p) => p.name).minus((p) => p.hobby);
// Exclude by multiple properties — Persons missing BOTH hobby AND nickNames
const sparse = await Person.select((p) => p.name).minus((p) => [p.hobby, p.nickNames]);
// Exclude by nested path — Persons whose bestFriend does NOT have a name
const unnamed = await Person.select((p) => p.name).minus((p) => [p.bestFriend.name]);
// Exclude by condition — Persons whose hobby is NOT 'Chess'
const noChess = await Person.select((p) => p.name).minus((p) => p.hobby.equals('Chess'));
// Exclude by the subject's term type — Persons that are NOT blank nodes
const named = await Person.select((p) => p.name).minus((p) => p.isBlank());A node, the root subject or a relation's value, has isIri() and isBlank():
.where((p) => p.isIri()) keeps the named subjects, .where((p) => p.bestFriend.isBlank()) those
whose best friend is a blank node. On a paged select the filter applies before the page is cut,
so a page holds as many named subjects as asked.
const sorted = await Person.select((p) => p.name).orderBy((p) => p.name, 'ASC');
const limited = await Person.select((p) => p.name).limit(1);
const single = await Person.select((p) => p.name).one();
const page3 = await Person.select((p) => [p.name, p.nickNames]).orderBy((p) => p).limit(20).offset(40);limit(n) and offset(m) page over the selected instances (the root subjects), not over
the rows a store returns: a page holds n instances after the first m, and each has every
value of every selected path, as without paging. An instance is never split across two pages,
however many values its multi-valued paths have. .one() is the first instance, with all its
values; .for(id) reads one instance and needs no limit.
This holds whatever a path's declared maxCount: data can hold more values than its shape
allows (a maxCount: 1 label in two languages), and such an instance still counts once. Its
single-valued field is one of those values, the first in the select's order. An offset
without an orderBy pages in IRI order.
Sorting never drops an instance, and missing values sort last, ascending and descending,
for direct and nested paths alike. With Anna (best friend Bob), Carl (best friend Ada) and Dirk
(no best friend), Person.select((p) => [p.name, p.bestFriend.name]).orderBy((p) => p.bestFriend.name) returns Carl, Anna, Dirk; with 'DESC', Anna, Carl, Dirk. The same holds
for a direct path (orderBy((p) => p.hobby): the instances without a hobby come last), paged
or not, for .one() (the first instance that has the value, if any has it), and for a grouped
select's orderBy (the group without the key's value, or whose aggregate has no value, comes
last).
An orderBy on a multi-valued or nested path (p.nickNames, p.friends.name) gives each
instance one sort value: its first value in that direction, the lowest ascending and the
highest descending, of the values it has. Each orderBy key is taken this way on its own, and
on a page ties are broken by the instance's IRI, so consecutive pages neither repeat nor skip
an instance. Without an orderBy the order of the instances, and so which ones a page holds,
is the store's.
A sort key can be a computed expression (orderBy((p) => p.hobby.strlen())), sorted by the
same rules: an instance whose expression has no value (no hobby) comes last. A key holding an
aggregate (p.friends.size(), Expr.count()) is refused; group the select and sort the groups
instead. A sort path can filter a segment, orderBy((p) => p.friends.where((f) => f.name.equals('Anna')).hobby): each instance sorts by the hobbies of its friends named Anna, and
an instance without such a friend is kept and comes last. The filter may sit on any relation of
the path: with orderBy((p) => p.bestFriend.friends.where(…).hobby) an instance without a best
friend, or whose best friend has no friend the filter keeps, is kept and comes last. A projection
keeps those instances too (select((p) => p.bestFriend.friends.where(…).name)). A sort key reads
its path on its own:
a filter in the projection (select((p) => p.friends.where(…).name)) does not filter the
friends an orderBy((p) => p.friends.hobby) reads. An orderBy callback that returns anything
else (a string, undefined) is refused, never ignored.
A where that holds an aggregate (.where((p) => p.friends.size().equals(2))) is tested per
instance, paged or not, whatever the select reads next to it.
A grouped select (groupBy, below) pages over its groups: limit(10) is ten groups.
count() and exists() drop limit and offset.
Query context lets you inject request-scoped values (like the current user) into filters without threading them through every call.
setQueryContext('user', {id: 'https://my.app/user1'}, Person);
const ctx = await Person.select((p) => p.name).where((p) =>
p.bestFriend.equals(getQueryContext('user')),
);Preloading appends another query to the current query so the combined data is loaded in one round-trip. This is helpful when rendering a nested tree of components and loading all data at once.
const preloaded = await Person.select((p) => [
p.hobby,
p.bestFriend.preloadFor(ChildComponent),
]);/* Result: {id: string} & UpdatePartial<Person> */
const created = await Person.create({name: 'Alice'});Where UpdatePartial reflects the created properties.
Update will patch any property that you send as payload and leave the rest untouched. The data to update is required:
// Target a specific entity with .for(id)
/* Result: {id: string} & UpdatePartial<Person> */
const updated = await Person.update({name: 'Alicia'}).for({id: 'https://my.app/node1'});Returns:
{
id:"https://my.app/node1",
name:"Alicia"
}Expression-based updates:
Instead of static values, you can compute new values from existing ones. Pass a callback to reference the entity's current properties:
// Increment age by 1
await Person.update((p) => ({age: p.age.plus(1)})).for({id: 'https://my.app/node1'});
// Uppercase a name
await Person.update((p) => ({name: p.name.ucase()})).for({id: 'https://my.app/node1'});
// Reference related entity properties
await Person.update((p) => ({hobby: p.bestFriend.name.ucase()})).for({id: 'https://my.app/node1'});
// Mix literals and expressions
await Person.update((p) => ({name: 'Bob', age: p.age.plus(1)})).for({id: 'https://my.app/node1'});
// Use Expr module values directly in plain objects
await Person.update({lastSeen: Expr.now()}).for({id: 'https://my.app/node1'});The callback is type-safe — .plus() only appears on number properties, .ucase() only on strings, etc.
Conditional and bulk updates:
// Update all matching entities
const archived = await Person.update({status: 'archived'}).where(p => p.status.equals('inactive'));
// Update all instances of a shape
await Person.update({verified: true}).forAll();Updating multi-value properties When updating a property that holds multiple values (one that returns an array in the results), you can either overwrite all the values with a new explicit array of values, or delete from/add to the current values.
To overwrite all values:
// Overwrite the full set of "knows" values.
const overwriteFriends = await Person.update({
knows: [{id: 'https://my.app/person2'}],
}).for({id: 'https://my.app/person1'});The result will contain an object with updatedTo, to indicate that previous values were overwritten to this new set of values:
{
id: "https://my.app/person1",
knows: {
updatedTo: [{id:"https://my.app/person2"}],
}
}To make incremental changes to the current set of values you can provide an object with add and/or remove keys:
// Add one value and remove one value without replacing the whole set.
const addRemoveFriends = await Person.update({
knows: {
add: [{id: 'https://my.app/person2'}],
remove: [{id: 'https://my.app/person3'}],
},
}).for({id: 'https://my.app/person1'});This returns an object with the added and removed items
{
id: "https://my.app/person1",
knows: {
added?: [{id:"https://my.app/person2"},
removed?: [{id:"https://my.app/person3"}],
}
}// Delete a single node
const deleted = await Person.delete({id: 'https://my.app/node1'});
// Delete multiple nodes
const deleted = await Person.delete([{id: 'https://my.app/node1'}, {id: 'https://my.app/node2'}]);
// Delete all instances of a shape (with their blank-node values)
await Person.deleteAll();
// Conditional delete
await Person.deleteWhere(p => p.status.equals('inactive'));A delete by id removes each node's triples, its blank values, its owned (contains) subtree and
the links other nodes hold to it, in every graph that may hold one.
Shapeless delete. On the base class, Shape.delete(ids) deletes nodes whose types the caller
does not know: every triple whose subject is one of the ids, and every triple that links to one,
in every data graph of the dataset: the default graph and each named graph, routed or not (never by
way of graphs.union). Structure stays: the graph registry and the graphs it records as shapes or
ontology graphs (core:ShapesGraph, core:OntologyGraph), which only graph queries write. One
SPARQL update does it. There is no type guard and no cascade (no shape to read one from), and
.where(), .all() and .into() are refused on it.
await Shape.delete([{id: a}, {id: b}]); // LinkedStorage's one dataset
await Shape.delete([{id: a}]).exec(dataset); // a named datasetA dataset is sent one only when it lists 'shapelessDelete' in supports (SparqlDataset
does); otherwise the delete is refused before it is sent. It is never fanned out: without a
target, LinkedStorage sends it to the one dataset it knows, and refuses it when it knows several
(name the dataset). It reaches every data graph, so a receiver that runs it for a client decides
who may send it.
LinkedStorage.begin() opens a store transaction; pass it to exec(tx). No dataset is named: the
transaction binds to a store when shape routing first sends one of its queries there, and calls
that store's beginTransaction(). Every later query to that store carries the txId and is sent at
once (nothing is buffered); reads in the transaction see its own writes, and nothing of it shows
outside before the commit.
import {LinkedStorage} from '@_linked/core';
const tx = LinkedStorage.begin(); // nothing is sent yet
await AppConfig.delete(oldId).exec(tx); // routed to store X: X.beginTransaction() → t1
await AppConfig.create(cfg).exec(tx); // sent at once, with t1
const c = await AppConfig.select().exec(tx); // X answers with t1's own writes
await tx.commit(); // X.commit(t1)
// The convenient form: commit on return, rollback on a throw.
await LinkedStorage.transaction(async (tx) => {
await AppConfig.delete(oldId).exec(tx);
await AppConfig.create(cfg).exec(tx);
});exec(tx)resolves when the store has applied the query inside the transaction.- A failed (or refused) query fails the transaction: it is rolled back in every store it reached, and the error is passed on.
- After the end, a query, a second
commit()or arollback()after a commit throwsTransactionEndedError; arollback()of a rolled back transaction does nothing, so acatchthat rolls back never hides the error. tx.begin()is refused (no nesting). Core has no async context, so passtxon to the code that should run in it.- An
update().for(id)of a missing node writes nothing and resolves with its fields, as outside a transaction. - Graph queries and triples reads do not run in a transaction.
- Live queries hear of a transaction's writes when it commits.
Stores. A store declares 'transactions' in supports and implements beginTransaction(),
commit(txId) and rollback(txId); it reads the txId as query.txId (on DSL-JSON, from 1.5,
as txId, which fromJSON() restores). It holds the transaction and rolls it back after an idle
timeout of its own. A store without support (SparqlDataset, so Fuseki) is warned about once,
loudly ("store X does not support transactions; these writes are not atomic"), and the queries run
on it directly: core does not emulate transactions in SPARQL. A second store in one transaction
gets a transaction of its own, with a warning that the stores together are not atomic. A query
with a txId is refused by a store that does not declare 'transactions'.
InMemoryDataset (@_linked/core/memory/InMemoryDataset) keeps its data in memory and holds
transactions for real: a snapshot at begin, swapped in at commit (refused when the store was
written since), dropped at rollback, expired after 30 seconds idle (transactionTimeoutMs). It
answers a subset of queries (creates, updates and deletes by id, selects, counts and asks of one
shape with its own properties) and refuses the rest.
Shape classes can extend other shape classes. Subclasses inherit property shapes from their superclasses and may override them.
This example assumes Person from the Shapes section above.
import {literalProperty} from '@_linked/core/shapes/SHACL';
import {linkedShape} from './package';
@linkedShape
export class Employee extends Person {
static targetClass = ex.Employee;
// Override inherited "name" with stricter constraints (still maxCount: 1)
@literalProperty({path: ex.name, required: true, minLength: 2, maxCount: 1})
declare name: string;
@literalProperty({path: ex.employeeId, required: true, maxCount: 1})
declare employeeId: string;
}Override behavior:
getUniquePropertyShapes(SomeShape.shape)returns one property shape per label, with subclass overrides taking precedence.- Overrides are tighten-only, and a widening throws at registration:
minCountmay not go down,maxCountmay not go up,nodeKindmay not widen,datatypemay not change, a valueshapemust be the inherited one or a sub-shape of it,containsmay not be dropped (contains: false), and the path may not change. A new label whose path an inherited property already declares is refused too (redeclare it under the inherited label). - A narrower
classneeds the class hierarchy, so the shape check (checkShapeHierarchy, run bylinked build) reports an override whoseclassis not the inherited class or a subclass of it as anoverride-not-tightenederror. - A shape known only as data (registered from a store at runtime) is never refused: every case is
a warning there, and an
override-not-tightenedwarning in the shape check. - An override that omits
minCount,maxCount,nodeKind,datatype,class, valueshapeorcontainskeeps the inherited value. patternand the other value constraints are not compared.
The DSL (Person.select(...)) is ideal when you know shapes at compile time. For apps that need to build queries at runtime — CMS dashboards, configurable reports, API endpoints that accept field selections — use QueryBuilder and FieldSet.
SelectBuilder provides a fluent, chainable API for constructing select queries programmatically. It
accepts a Shape class or a shape IRI string. (QueryBuilder is a deprecated alias for SelectBuilder
and is used in the examples below; the mutation builders are CreateBuilder/UpdateBuilder/DeleteBuilder.)
import {QueryBuilder} from '@_linked/core'; // alias of SelectBuilder
// From a Shape class
const query = QueryBuilder.from(Person)
.select(p => [p.name, p.knows])
.where(p => p.name.equals('Semmy'))
.limit(10);
// From a shape IRI string (when the Shape class isn't available at compile time)
const query = QueryBuilder.from('https://schema.org/Person')
.select(['name', 'knows'])
.where(p => p.name.equals('Semmy'));
// SelectBuilder is PromiseLike — await it directly
const results = await query;
// Serialize to DSL-JSON, or lower to the IR without executing
const json = query.toJSON();
const ir = lower(query); // `lower` is a free import from '@_linked/core'Target specific entities:
// Single entity — result is unwrapped (not an array)
const person = await QueryBuilder.from(Person)
.for({id: 'https://my.app/person1'})
.select(p => p.name);
// Multiple entities
const people = await QueryBuilder.from(Person)
.forAll([{id: 'https://my.app/p1'}, {id: 'https://my.app/p2'}])
.select(p => p.name);Sorting, limiting, and single results:
const topFive = await QueryBuilder.from(Person)
.select(p => p.name)
.orderBy(p => p.name, 'ASC')
.limit(5);
const first = await QueryBuilder.from(Person)
.select(p => p.name)
.one();Select with a FieldSet:
const fields = FieldSet.for(Person, ['name', 'knows']);
const results = await QueryBuilder.from(Person).select(fields);FieldSet is an independent, reusable object that describes which fields to select from a shape. Create them, compose them, and feed them into queries.
Creating a FieldSet:
import {FieldSet} from '@_linked/core';
// From a Shape class with string field names
const fs = FieldSet.for(Person, ['name', 'knows']);
// From a Shape class with a type-safe callback
const fs = FieldSet.for(Person, p => [p.name, p.knows]);
// From a shape IRI string (when you only have the shape's IRI)
const fs = FieldSet.for('https://schema.org/Person', ['name', 'knows']);
// Select all decorated properties
const allFields = FieldSet.all(Person);
// Select all properties with depth (includes nested shapes)
const deep = FieldSet.all(Person, {depth: 2});Nested fields:
// Dot-separated paths for nested properties
const fs = FieldSet.for(Person, ['name', 'knows.name']);
// Object form for nested sub-selections
const fs = FieldSet.for(Person, [{knows: ['name', 'hobby']}]);Composing FieldSets:
const base = FieldSet.for(Person, ['name']);
// Add fields
const extended = base.add(['knows', 'birthDate']);
// Remove fields
const minimal = extended.remove(['birthDate']);
// Pick specific fields
const picked = extended.pick(['name', 'knows']);
// Merge multiple FieldSets
const merged = FieldSet.merge([fieldSet1, fieldSet2]);Inspecting a FieldSet:
const fs = FieldSet.for(Person, ['name', 'knows']);
fs.labels(); // ['name', 'knows']
fs.paths(); // [PropertyPath, PropertyPath]Use cases:
// Dynamically selected fields from a UI
const fields = FieldSet.for(Person, userSelectedFields);
const results = await QueryBuilder.from(Person).select(fields);
// API gateway: accept fields as query parameters
const fields = FieldSet.for(Person, req.query.fields.split(','));
const results = await QueryBuilder.from(Person).select(fields);
// Component composition: merge field sets from child components
const merged = FieldSet.merge([headerFields, sidebarFields, contentFields]);
const results = await QueryBuilder.from(Person).select(merged);
// Progressive loading: start minimal, add detail on demand
const summary = FieldSet.for(Person, ['name']);
const detail = summary.add(['email', 'knows', 'birthDate']);The mutation builders are the programmatic equivalent of Person.create(...), Person.update(...), and Person.delete(...). They accept Shape classes or shape IRI strings.
import {CreateBuilder, UpdateBuilder, DeleteBuilder} from '@_linked/core';
// Create — equivalent to Person.create({name: 'Alice'})
const created = await CreateBuilder.from(Person)
.set({name: 'Alice'})
.withId('https://my.app/alice');
// Update — equivalent to Person.update({name: 'Alicia'}).for({id: '...'})
const updated = await UpdateBuilder.from(Person)
.for({id: 'https://my.app/alice'})
.set({name: 'Alicia'});
// Delete by ID — equivalent to Person.delete({id: '...'})
const deleted = await DeleteBuilder.from(Person, {id: 'https://my.app/alice'});
// Delete all — equivalent to Person.deleteAll()
await DeleteBuilder.from(Person).all();
// Conditional update — equivalent to Person.update({...}).where(fn)
await UpdateBuilder.from(Person).set({verified: true}).forAll();
// All builders are PromiseLike — await them to run, toJSON() for the wire form,
// or lower() (a free function) for the IR
import {lower} from '@_linked/core';
const ir = lower(CreateBuilder.from(Person).set({name: 'Alice'}));The shape registry holds one definition per IRI. A host serving several projects (or branches)
that share shape IRIs with different properties resolves each query against a ShapeCatalog
instead:
import {ShapeCatalog, withShapeCatalog, fromJSON} from '@_linked/core';
const branch = ShapeCatalog.from(nodeShapeWires, {name: 'project-1#main'}); // immutable
const query = withShapeCatalog(branch, () =>
SelectBuilder.from('https://example.org/shape/Person').select(['name']),
);
await query; // lowers and compiles against `branch`, though the scope has closed
const received = fromJSON(json, {catalog: branch}); // the wire never carries a catalogA builder keeps the catalog it was made in (catalogOf(query)), through clones, exists(),
count(), lowering, compile and live queries. Inside a scope every registry read sees the
catalog's shapes plus core's built-ins (always from code, though syncShapes writes them
into every dataset); a class the catalog does not hold keeps resolving
globally. See runtime instances.
Every Linked query — select and every mutation — has one canonical, language-neutral form:
DSL-JSON. query.toJSON() serializes any query to a compact, lossless JSON object;
fromJSON(json) rehydrates it back into a live query you can run, inspect, or re-serialize. This is
the format to send over HTTP, cache, queue, persist in a query-editor UI, or implement on a non-JS
backend. See the full DSL-JSON specification.
import {fromJSON} from '@_linked/core';
// Any query → JSON (outbound)
const json = Person.select(p => [p.name, p.knows])
.where(p => p.name.equals('Semmy'))
.toJSON();
// JSON → live query → run it (inbound). `fromJSON` detects the kind (select/create/update/delete).
const results = await fromJSON(json).exec();Every envelope carries a wire version (v) and the target shape. A select:
{
"v": "1.0",
"shape": "https://schema.org/Person",
"fields": [{ "path": "name" }, { "path": "hobby" }],
"limit": 10
}A create:
{
"v": "1.0",
"op": "create",
"shape": "https://schema.org/Person",
"data": {
"shape": "https://schema.org/Person",
"fields": [
{ "prop": "name", "value": { "kind": "lit", "value": "Alice" } },
{ "prop": "hobby", "value": { "kind": "lit", "value": "Chess" } }
]
}
}An update targeting one node, and a delete by id:
{ "v": "1.0", "op": "update", "shape": "https://schema.org/Person", "mode": "for",
"targetId": "https://my.app/alice",
"data": { "shape": "https://schema.org/Person",
"fields": [{ "prop": "hobby", "value": { "kind": "lit", "value": "Go" } }] } }{ "v": "1.0", "op": "delete", "shape": "https://schema.org/Person", "mode": "ids",
"ids": ["https://my.app/alice"] }A query can also reference the current context (e.g. the signed-in user) without resolving it
yet — it travels as a {$ctx} marker and is resolved wherever the query is finally run:
{ "v": "1.0", "shape": "https://schema.org/Person",
"fields": [{ "path": "name" }], "subject": { "$ctx": "user" }, "singleResult": true }FieldSet serializes the same way (FieldSet.for(Person, ['name','knows']).toJSON() /
FieldSet.fromJSON(json)).
A live query keeps its result current: subscribe once, and whenever something changes that the query
can have read — a local mutation, a change reported by a dataset's feed, or one you publish yourself —
it fetches again and notifies its listeners. The store is framework-free and lives in core; UI
bindings such as @_linked/react sit on top of it. Full guide: documentation/live-queries.md.
const live = Team.select((t) => [t.name, t.members.size()]).for({id: teamId}).live();
const first = await live; // first data; the handle stays live
live.subscribe((state) => render(state.data)); // every later result
live.close();
Team.select().toCount().live((s) => setTotal(s.data)); // listener shorthand; counts and asks go live too
await Team.update({members: {add: [{id: personId}]}}).for({id: teamId});
// → every live query that reads this team's members refetches; unrelated ones do not.queryDependencies(query)andmutationEffects(mutation, result?)describe, in predicate IRIs, what a query reads and what a mutation writes; the store matches the two (by node id where it can, template-wide where it must, scoped by shape).subscribeQueryDispatch(listener)observes every query that runs through the dispatch,exec(target)included.- A dataset can report changes made elsewhere through the optional
IDataset.subscribeChanges(listener);publishChange(event)does the same from application code;invalidate(Shape | {id} | query)is the manual lever.
A framework module can be evaluated twice in one process (under linked start, once by Vite's SSR
module runner and once by Node) or twice in a browser. Two conventions make that second copy harmless:
-
Module state lives on
globalThis. A module-level value that more than one caller reads or writes (a registry, a configured singleton, a static that callers assign) is kept withsharedState, underSymbol.for('<pkg>:<name>'), so every copy reads and writes the same container:import {sharedState} from '@_linked/core'; const state = sharedState('@_linked/my-pkg', 'Mailer.state', () => ({provider: null as Provider | null})); export function setProvider(p: Provider) { state.provider = p; }
-
Errors are matched by brand, not
instanceof. A framework error class is branded once withbrandLinkedError(Cls, '<pkg>', '<ClassName>'), and code that catches an error from another package checks it withisLinkedError(err, Cls). That matches an instance thrown by any copy of the defining module, and a subclass instance against its base.export class StoreError extends Error {} brandLinkedError(StoreError, '@_linked/my-pkg', 'StoreError'); // elsewhere if (isLinkedError(err, StoreError)) { /* … */ }
-
Class checks on another package's values use
isLinkedInstance.value instanceof Shapeis false for a value another copy of core made.isLinkedInstance(value, Cls)also matches it, for any class branded withbrandLinkedClass(Cls, '<pkg>', '<ClassName>'), and falls back toinstanceoffor one that is not. Core brandsShape,ShapeSet,CoreSet, the query builders (QueryBuilder/SelectBuilder,CreateBuilder,UpdateBuilder,DeleteBuilder,AskBuilder,CountBuilder),PendingQueryContext,FieldSet,PropertyPath,ExpressionNode,ExistsConditionand the query-proxy classes. PassSub.prototypeto ask whetherSubextendsCls.import {isLinkedInstance, Shape} from '@_linked/core'; if (isLinkedInstance(input, Shape)) return input.id;
Core's own module state (query contexts and their listeners, LinkedFileStorage's stores,
LinkedErrorLogging's logger, Prefix, the default page limit, ontology registrations, package
publish roots, node-shape callbacks and catalog adapters) is kept with sharedState, so every copy
sees the same values.
The full convention, including what stays per copy and how React contexts are handled, is in shared state and error brands.
- Allow
preloadForto accept another query (not just a component). - Make and expose functions for auto syncing shapes to the graph.
A dataset (IDataset) receives the live query and decides what to do with it. LinkedStorage
routes each query to a dataset by its target shape. A dataset can:
- execute it directly against an in-memory graph,
- forward it as DSL-JSON (
query.toJSON()) to another service, or - lower it to an algebra for a query engine, via the free
lower(query)function.
Every read reaches a dataset through one method, readQuery(query), told apart by the query's
kind (readKindOf(query), @_linked/core/queries/readQuery): a select (SelectResult), a count
(a number), an ask (a boolean), a triples read (Triple[]) or a terms read (string[]); the
answer type follows the kind (ReadResult<Q>). Selects and counts are the baseline. A dataset
declares the other reads it answers in supports ('ask', 'triples', 'triplesTerms', next to
'groupBy', 'min', 'max', 'triplesPattern', …), and a read it does not declare is refused
before it is sent; an ask to a dataset without 'ask' is sent as a select LIMIT 1 instead.
Writes have their own methods (createQuery, updateQuery, deleteQuery, and graphQuery for
a whole named graph). A router sends each read on with resolveRead(dataset, query)
(@_linked/core/queries/queryDispatch), which makes those checks and checks the answer.
SPARQL is built in. This package ships a SparqlStore base class (@_linked/core/sparql) — extend
it for any SPARQL endpoint. Internally it calls lower(query) to obtain a normalized
Intermediate Representation (IR) and compiles that
to SPARQL.
The IR is an internal lowering target, not the contract — DSL-JSON is. It is a shape-resolved,
normalized algebra, which makes it a convenient thing to compile from: a backend for another target
language (SQL, a graph API, …) can consume the IR as-is, or just use it as a reference and lower
DSL-JSON to its own algebra directly. The IR is reached only through lower(), and the query
builders/serialization carry no dependency on it — so a client that never lowers (e.g. a frontend that
only builds and forwards queries) tree-shakes the entire IR + SPARQL pipeline out of its bundle. The IR
types are available from @_linked/core/queries/IntermediateRepresentation; see the
IR docs for the type reference and the dataset
implementer guide.
Store packages:
SparqlStorebase class — included in@_linked/core/sparql, extend it for any SPARQL endpoint@_linked/rdf-mem-store— in-memory RDF store
npm test runs the unit tests and the typecheck. The live Fuseki tests ([fuseki] blocks) run
only against a store the environment names, with no default:
export FUSEKI_BASE_URL=http://localhost:3321 # a free port, for a Fuseki of your own
export FUSEKI_ADMIN_PASSWORD=admin
REQUIRE_FUSEKI=1 npm test- Without
FUSEKI_BASE_URLandFUSEKI_ADMIN_PASSWORDno Fuseki is contacted: the live tests are reported as skipped, and a run underCIorREQUIRE_FUSEKI=1fails, naming the variables. - When the URL is unreachable, global setup starts the inferring image
(
ghcr.io/linked-fw/fuseki,LINKED_FUSEKI_IMAGEfor another) with Docker on its port, and stops it after the run. A reachable store must inferrdf:typethroughrdfs:subClassOf. - The tests create datasets named
linked-core-test-*, and setup and teardown delete those and nothing else. Two runs on one store would delete each other's: give each concurrent run its own store (port). - A store global setup starts runs under the compose project
linked-core-test-<port>(LINKED_CORE_TEST_COMPOSE_PROJECTfor another name). - Never point the variables at a store another project uses.
npm run test:fusekiruns only the live tests; CI runsnpm run test:ci, which sets them for the runner and refuses to run elsewhere.
See CHANGELOG.md.