@ldo/dataset
v0.0.1-alpha.24
Published
An RDFJS dataset implementation
Downloads
1,634
Readme
Dataset
An implementation of the full RDF/JS Dataset API
Installation
npm i @ldo/dataset
Simple Example
import { createDataset } from "o-dataset-pack";
import { quad, namedNode } from "@rdfjs/data-model";
const dataset = createDataset();
dataset.add(
quad(
namedNode("http://example.org/cartoons#Zuko"),
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
namedNode("http://example.org/cartoons#Firebender")
)
);
/*
Prints:
<http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> .
*/
console.log(dataset.toString());
Advanced Example
The extended dataset implements all the additional methods of the RDFJS dataset spec.
Usage:
import { createDataset } from "@ldo/dataset";
import { quad, namedNode, literal } from "@rdfjs/data-model";
// Required for advanced features:
import { dataset as initializeDatasetCore } from "@rdfjs/dataset";
import { ExtendedDatasetFactory } from "o-dataset-pack";
import { Dataset, Quad, DatasetCoreFactory, DatasetCore } from "rdf-js";
/**
* Create a dataset with default settings
*/
const defaultDataset = createDataset();
/**
* Create a dataset with default settings and initialized values
*/
const initializedQuads = [
quad(
namedNode("http://example.org/cartoons#Tom"),
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
namedNode("http://example.org/cartoons#Cat")
),
quad(
namedNode("http://example.org/cartoons#Tom"),
namedNode("http://example.org/cartoons#name"),
literal("Tom")
),
];
const defaultDataset2 = createDataset(initializedQuads);
/**
* (Advanced Feature) Create a dataset by injecting a chosen datasetCore and datasetCoreFactory
*/
const datasetFactory: DatasetCoreFactory = {
dataset: (quads?: Dataset<Quad> | Quad[]): DatasetCore => {
return initializeDatasetCore(
Array.isArray(quads) ? quads : quads?.toArray()
);
},
};
const extendedDatasetFactory = new ExtendedDatasetFactory(datasetFactory);
const customDataset = extendedDatasetFactory.dataset(initializedQuads);
/**
* Do all the methods of the RDFJS Dataset interface. For a full list of methods, go to
* https://rdf.js.org/dataset-spec/#data-interfaces
*/
defaultDataset.add(
quad(
namedNode("http://example.org/cartoons#Miuki"),
namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
namedNode("http://example.org/cartoons#Cat")
)
);
const combinedDataset = defaultDataset.union(defaultDataset2);
const differenceDataset = combinedDataset.difference(customDataset);
// Prints true because "defaultDataset2" and "customDataset" have equal values
// combinedDataset = defaultDataset ∪ defaultDataset2
// differenceDatasset = defaultDataset \ customDataset
// Therefore differenceDataset == defaultDataset
console.log(differenceDataset.equals(defaultDataset));
Sponsorship
This project was made possible by a grant from NGI Zero Entrust via nlnet. Learn more on the NLnet project page.
Liscense
MIT