Returns the total number of entities corresponding to the data schema. Optionally, you can specify search criteria and a maximum number of results to count. The maximum caps the number of distinct matching entities.
Search criteria and maximum number of results to count
total number of entities corresponding to the data schema
import { createLens } from "ldkit";
import { schema } from "ldkit/namespaces";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema above
const Persons = createLens(PersonSchema);
// Count all persons
const count = await Persons.count(); // number
// Count all persons with name that starts with "Ada"
const adaCount = await Persons.count({
where: {
name: { $strStarts: "Ada" },
},
});
// Count matching persons identified by an IRI; other criteria still apply
const adaByIriCount = await Persons.count({
where: {
$id: "http://example.org/Ada_Lovelace",
name: { $strStarts: "Ada" },
},
});
// Count at most 100 distinct matching persons
const limitedCount = await Persons.count({ max: 100 });
Deletes one or more entities from the data store.
This method accepts IRIs of the entities to delete and attemps to delete all triples from the database that corresponds to the data schema. Other triples that are not covered by the data schema will not be deleted.
If you need to have more control of what triples to delete, use deleteData instead.
Identities or IRIs of the entities to delete
Nothing
import { createLens } from "ldkit";
import { schema } from "ldkit/namespaces";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema above
const Persons = createLens(PersonSchema);
// Delete a person
await Persons.delete("http://example.org/Alan_Turing");
Deletes raw RDF quads from the data store.
This method is useful when you need to delete data that is not covered by the data schema.
Quads to delete from the data store
Nothing
import { createLens } from "ldkit";
import { schema } from "ldkit/namespaces";
import { DataFactory } from "ldkit/rdf";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema and context above
const Persons = createLens(PersonSchema);
// Create a custom quad to insert
const df = new DataFactory();
const quad = df.quad(
df.namedNode("http://example.org/Alan_Turing"),
df.namedNode("http://schema.org/name"),
df.literal("Alan Turing"),
);
// Delete the quad
await Persons.deleteData(quad);
Find entities that match the given search criteria.
The search criteria is a JSON object that may contain properties from the data schema. In addition you can specify how many results to return and how many to skip for pagination purposes.
Search criteria and pagination options
entities that match the given search criteria
import { createLens } from "ldkit";
import { schema } from "ldkit/namespaces";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema above
const Persons = createLens(PersonSchema);
// Find 100 persons with name that starts with "Ada"
const persons = await Persons.find({
where: {
name: { $strStarts: "Ada" },
},
take: 100,
});
Find a single entity that matches the given IRI.
IRI of the entity to find
Entity if found, null otherwise
import { createLens } from "ldkit";
import { schema } from "ldkit/namespaces";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema above
const Persons = createLens(PersonSchema);
// Get a particular person identified by IRI
const ada = await Persons.findByIri("http://dbpedia.org/resource/Ada_Lovelace");
console.log(ada?.name); // string "Ada Lovelace"
Find entities that match the given IRIs.
IRIs of the entities to find
Array of found entities, empty array if there are no matches
import { createLens } from "ldkit";
import { schema } from "ldkit/namespaces";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema above
const Persons = createLens(PersonSchema);
// Get specific persons identified by IRIs
const matches = await Persons.findByIris([
"http://dbpedia.org/resource/Ada_Lovelace",
"http://dbpedia.org/resource/Alan_Turing"
]);
console.log(matches[0].name); // string "Ada Lovelace"
console.log(matches[1].name); // string "Alan Turing"
Find one entity that matches the given search criteria.
The search criteria is a JSON object that may contain properties from the data schema.
Optionalwhere: SchemaSearchInterface<T>
Search criteria
the first entity that matches the given search criteria, or null
import { createLens } from "ldkit";
import { schema } from "ldkit/namespaces";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema above
const Persons = createLens(PersonSchema);
// Find one person with name that starts with "Ada"
const person = await Persons.findOne({
name: { $strStarts: "Ada" },
});
Inserts one or more entities to the data store.
Entities to insert
Nothing
import { createLens } from "ldkit";
import { schema } from "ldkit/namespaces";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema above
const Persons = createLens(PersonSchema);
// Insert a new person
await Persons.insert({
$id: "http://example.org/Alan_Turing",
name: "Alan Turing",
});
Inserts raw RDF quads to the data store.
This method is useful when you need to insert data that is not covered by the data schema.
Quads to insert to the data store
Nothing
import { createLens } from "ldkit";
import { schema } from "ldkit/namespaces";
import { DataFactory } from "ldkit/rdf";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema above
const Persons = createLens(PersonSchema);
// Create a custom quad to insert
const df = new DataFactory();
const quad = df.quad(
df.namedNode("http://example.org/Alan_Turing"),
df.namedNode("http://schema.org/name"),
df.literal("Alan Turing"),
);
// Insert the quad
await Persons.insertData(quad);
Find entities with a custom SPARQL query.
The query must be a CONSTRUCT query, and the root nodes must be of type ldkit:Resource.
So that the decoder can decode the results, the query must also return all properties
according to the data schema.
CONSTRUCT SPARQL query
Found entities
import { createLens } from "ldkit";
import { ldkit, schema } from "ldkit/namespaces";
import { CONSTRUCT } from "ldkit/sparql";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema above
const Persons = createLens(PersonSchema);
// Query to find all persons named "Doe"
const query = CONSTRUCT`?s a <${ldkit.Resource}>; <${schema.name}> ?name`
.WHERE`?s <${schema.name}> ?name; <${schema.familyName}> "Doe"`.build();
// Find all persons that match the custom query
const doePersons = await Persons.query(query);
Updates one or more entities in the data store.
Partial entities to update
Nothing
import { createLens } from "ldkit";
import { schema } from "ldkit/namespaces";
// Create a schema
const PersonSchema = {
"@type": schema.Person,
name: schema.name,
} as const;
// Create a resource using the data schema above
const Persons = createLens(PersonSchema);
// Update Alan Turing's name
await Persons.update({
$id: "http://example.org/Alan_Turing",
name: "Not Alan Turing",
});
Lens provides an interface to Linked Data based on the data schema.
For the best developer experience, use the createLens function to create the instance.