Creates a new instance of TermWrapper.
The IRI of a named node that is the original term being wrapped.
The dataset that contains the term being wrapped.
A collection of methods for creating terms.
Creates a new instance of TermWrapper.
The original term being wrapped.
The dataset that contains the term being wrapped.
A collection of methods for creating terms.
The well-known property containing a string that represents the type of this object.
The dataset that contains this term.
This accessor provides access to the underlying RDF graph that is the containing context of a node mapped to JavaScript by instances of this class.
RDF/JS, like many other RDF frameworks, keeps terms and datasets separate. This means that terms do not hold a reference to a dataset they reside in (or were found in). This, in turn, means that a dataset must always be available, separate from the term, if either changes to the underlying data or further traversal of the underlying data is called for. In an object-oriented context however, where property chaining is idiomatic (i.e. instance.property1.property2), there is no way to supply the dataset when dereferencing a link in the chain.
This property solves the problem by keeping a reference to the dataset.
Using the dataset to modify information related to this node in the underlying data:
class Book extends TermWrapper {
set author(value: string) {
const subject = this as Quad_Subject
const predicate = this.factory.namedNode("http://example.com/author")
const object = this.factory.literal(value)
const oldAuthors = this.factory.quad(subject, predicate)
const newAuthor = this.factory.quad(subject, predicate, object)
this.dataset.delete(oldAuthors)
this.dataset.add(newAuthor)
}
}
Note: The above example operates on a low level to explain this property. Library users are more likely to interact with OptionalAs, RequiredAs and LiteralFrom for a better experience.
Using the dataset to modify data related to this node in the underlying data:
class Container extends TermWrapper {
add(something: string) {
const subject = this as Quad_Subject
const predicate = this.factory.namedNode("http://example.com/contains")
const object = this.factory.literal(something)
const quad = this.factory.quad(subject, predicate, object)
this.dataset.add(quad)
}
}
The data factory this instance was instantiated with. A collection of methods that can be used to create terms by this or subsequent wrappers.
Using the factory to create a literal term from the current date and time:
class Calendar extends TermWrapper {
get currentDate(): Literal {
const date = new Date().toISOString()
const xsdDateTime = this.factory.namedNode("http://www.w3.org/2001/XMLSchema#dateTime")
return this.factory.literal(date, xsdDateTime)
}
}
Using the factory to create a quad:
class Container extends TermWrapper {
add(something: string) {
const subject = this as Quad_Subject
const predicate = this.factory.namedNode("http://example.com/contains")
const object = this.factory.literal(something)
const quad = this.factory.quad(subject, predicate, object)
this.dataset.add(quad)
}
}
TermWrapperis one of the two central constructs of this library. It is the base class of all models that represent a mapping from RDF to JavaScript. It is an RDF/JS term (or node) that also has a reference to both the dataset (or graph) that is the context of (i.e. contains) the term and to a factory that can be used to create additional terms.Remarks
This class contains all members of all types derived from Term. This is so instances of this class can be used as instances of any term type. See relevant example.
Example: Basic usage
The basic pattern of working with this class is to simply extend it and add accessors and mutators (both optional) that expose data from the underlying RDF:
Assume the following RDF data:
We can work with this data in JavaScript and TypeScript as follows:
Example: Using instances of TermWrapper as instances of RDF/JS Term
Since this class implements all members of all term types (named nodes, literals, blank nodes etc.), it can be cast to an RDF/JS Term:
Example: Using instances of TermWrapper to create quads
Instances of this class can be used anywhere an RDF/JS Term can be used, which includes creating quads:
Example: Using instances of TermWrapper to match graph patterns
Instances of this class can be used anywhere an RDF/JS Term can be used, which includes matching quads in a dataset: