npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@graviola/sparql-schema

v1.4.0

Published

JsonSchema to SPARQL Query converter

Readme

@graviola/sparql-schema

A utility for converting JSON Schema to SPARQL queries and performing CRUD operations with RDF data.

Environment: Universal

Overview

This package provides tools for bridging the gap between JSON Schema and SPARQL, enabling seamless interaction with RDF data stores using familiar JSON Schema definitions. It converts JSON Schema structures to SPARQL queries, handles CRUD operations, and provides utilities for finding and manipulating RDF data.

Ecosystem Integration

Position in the Graviola Framework

The sparql-schema package is a core component of the Graviola framework's data layer when using Triple or Quad Stores and SPARQL endpoints. It serves as the bridge between JSON Schema definitions and SPARQL queries, enabling applications to interact with RDF data stores using familiar JSON Schema structures. This package is essential for applications that need to store and retrieve linked data from SPARQL endpoints.

Dependency Graph

flowchart TD
    A[graviola/sparql-schema] --> B[graviola/edb-core-utils]
    A --> C[graviola/jsonld-utils]
    A --> D[graviola/json-schema-utils]
    A --> E[graviola/edb-graph-traversal]
    G[graviola/sparql-db-impl] --> A
    H[graviola/sparql-store-provider] --> A

    style A fill:#f9f,stroke:#333,stroke-width:2px

Package Relationships

  • Dependencies:

    • @graviola/edb-core-utils: Provides utility functions for working with IRIs and other core operations
    • @graviola/jsonld-utils: Provides utilities for working with JSON-LD data
    • @graviola/json-schema-utils: Provides utilities for working with JSON Schema
    • @graviola/edb-graph-traversal: Provides utilities for traversing graph data structures
  • Peer Dependencies:

    • @rdfjs/data-model: RDF/JS data model implementation
    • @rdfjs/parser-jsonld: JSON-LD parser for RDF/JS
    • @tpluscode/sparql-builder: SPARQL query builder
    • json-schema: JSON Schema definitions
    • jsonld: JSON-LD processor
    • jsonld-context-parser: JSON-LD context parser
    • n3: N3.js RDF library (parse and serialize turtle and n-triples)
    • rdf-dataset-ext: RDF dataset extensions
    • string-to-stream: Convert strings to streams
  • Used By:

    • @graviola/sparql-db-impl: Implements database operations using sparql-schema
    • @graviola/sparql-store-provider: Provides a store interface using sparql-schema

Installation

bun add @graviola/sparql-schema
# or
npm install @graviola/sparql-schema
# or
yarn add @graviola/sparql-schema

Features

JSON Schema to SPARQL Conversion

  • jsonSchema2construct: Convert JSON Schema to SPARQL CONSTRUCT queries
  • jsonSchema2Select: Convert JSON Schema to SPARQL SELECT queries
  • jsonSchema2constructWithLimits: Convert JSON Schema to SPARQL CONSTRUCT queries with limits
  • prefixes2sparqlPrefixDeclaration: Convert prefix mappings to SPARQL PREFIX declarations

CRUD Operations

  • save: Save an entity to the RDF store
  • load: Load an entity from the RDF store
  • remove: Remove an entity from the RDF store
  • exists: Check if an entity exists in the RDF store
  • getClasses: Get all classes from the RDF store
  • moveToTrash: Move an entity to the trash
  • restoreFromTrash: Restore an entity from the trash

Entity Finding

  • findEntityByClass: Find entities by their class
  • searchEntityByLabel: Search for entities by their label
  • findEntityByAuthorityIRI: Find entities by their authority IRI

SPARQL Query Generation

  • makeSPARQLConstructQuery: Create a SPARQL CONSTRUCT query
  • makeSPARQLDeleteQuery: Create a SPARQL DELETE query
  • makeSPARQLWherePart: Create the WHERE part of a SPARQL query
  • makeSPARQLToTrashQuery: Create a query to move an entity to trash
  • makeSPARQLRestoreFromTrashQuery: Create a query to restore an entity from trash

Usage

Converting JSON Schema to SPARQL CONSTRUCT Query

import { jsonSchema2construct } from "@graviola/sparql-schema";
import { JSONSchema7 } from "json-schema";

// Define a JSON Schema
const schema: JSONSchema7 = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "number" },
    email: { type: "string" },
  },
  required: ["name"],
};

// Convert to SPARQL CONSTRUCT query parts
const { construct, whereOptionals, whereRequired } = jsonSchema2construct(
  "?person", // Subject variable
  schema, // JSON Schema
);

console.log(construct);
// Output:
// ?person a ?__type_0 .
// ?person :name ?name_1 .
// ?person :age ?age_2 .
// ?person :email ?email_3 .

console.log(whereOptionals);
// Output:
// OPTIONAL { ?person a ?__type_0 . }
// OPTIONAL { ?person :age ?age_2 . }
// OPTIONAL { ?person :email ?email_3 . }

Saving an Entity

import { save } from "@graviola/sparql-schema";
import { JSONSchema7 } from "json-schema";

// Define a JSON Schema
const schema: JSONSchema7 = {
  type: "object",
  properties: {
    "@id": { type: "string" },
    "@type": { type: "string" },
    name: { type: "string" },
    age: { type: "number" },
  },
  required: ["@id", "@type", "name"],
};

// Define an entity
const person = {
  "@id": "http://example.org/person/1",
  "@type": "http://example.org/ontology#Person",
  name: "John Doe",
  age: 30,
};

// Save the entity
const sparqlEndpoint = "http://localhost:3030/dataset/sparql";
const sparqlUpdateEndpoint = "http://localhost:3030/dataset/update";

const result = await save({
  entity: person,
  schema,
  sparqlEndpoint,
  sparqlUpdateEndpoint,
});

console.log(result);
// Output: { success: true }

Finding Entities by Class

import { findEntityByClass } from "@graviola/sparql-schema";

// Find all Person entities
const sparqlEndpoint = "http://localhost:3030/dataset/sparql";
const persons = await findEntityByClass({
  classIRI: "http://example.org/ontology#Person",
  sparqlEndpoint,
  limit: 10,
  offset: 0,
});

console.log(persons);
// Output: [{ '@id': 'http://example.org/person/1', '@type': 'http://example.org/ontology#Person', name: 'John Doe', ... }, ...]

Searching Entities by Label

import { searchEntityByLabel } from "@graviola/sparql-schema";

// Search for entities with label containing "John"
const sparqlEndpoint = "http://localhost:3030/dataset/sparql";
const results = await searchEntityByLabel({
  searchText: "John",
  sparqlEndpoint,
  limit: 10,
  offset: 0,
});

console.log(results);
// Output: [{ '@id': 'http://example.org/person/1', '@type': 'http://example.org/ontology#Person', label: 'John Doe', ... }, ...]

API Reference

Schema to SPARQL Conversion

jsonSchema2construct(subjectURI, rootSchema, stopSymbols?, excludedProperties?, maxRecursion?)

Converts a JSON Schema to a SPARQL CONSTRUCT query.

  • Parameters:

    • subjectURI: The subject URI or variable
    • rootSchema: The JSON Schema to convert
    • stopSymbols?: Array of property names to stop recursion at
    • excludedProperties?: Array of property names to exclude
    • maxRecursion?: Maximum recursion depth (default: 4)
  • Returns: Object with construct, whereRequired, and whereOptionals strings

jsonSchema2Select(fields, where, limit?, offset?, orderBy?)

Creates a SPARQL SELECT query.

  • Parameters:

    • fields: Array of field names to select
    • where: WHERE clause of the query
    • limit?: Maximum number of results
    • offset?: Offset for pagination
    • orderBy?: Array of fields to order by
  • Returns: SPARQL SELECT query string

CRUD Operations

save(options)

Saves an entity to the RDF store.

  • Parameters:

    • options: Object with entity, schema, sparqlEndpoint, and sparqlUpdateEndpoint
  • Returns: Promise resolving to the save result

load(options)

Loads an entity from the RDF store.

  • Parameters:

    • options: Object with entityIRI, typeIRI, schema, and sparqlEndpoint
  • Returns: Promise resolving to the loaded entity

remove(options)

Removes an entity from the RDF store.

  • Parameters:

    • options: Object with entityIRI, typeIRI, schema, sparqlEndpoint, and sparqlUpdateEndpoint
  • Returns: Promise resolving to the remove result

License

This package is part of the Graviola project.