@rdfx/graph-store 0.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ import type { DefaultGraph, NamedNode, Quad_Graph } from "@rdfjs/types";
2
+ import { Either } from "purify-ts";
3
+ export type GraphIdentifier = DefaultGraph | NamedNode;
4
+ export declare namespace GraphIdentifier {
5
+ function fromQuadGraph(quadGraph: Quad_Graph): Either<Error, GraphIdentifier>;
6
+ function stringify(graphIdentifier: GraphIdentifier): string;
7
+ }
8
+ //# sourceMappingURL=GraphIdentifier.d.ts.map
@@ -0,0 +1,19 @@
1
+ import { Either, Left } from "purify-ts";
2
+ export var GraphIdentifier;
3
+ (function (GraphIdentifier) {
4
+ function fromQuadGraph(quadGraph) {
5
+ switch (quadGraph.termType) {
6
+ case "DefaultGraph":
7
+ case "NamedNode":
8
+ return Either.of(quadGraph);
9
+ default:
10
+ return Left(new RangeError(quadGraph.termType));
11
+ }
12
+ }
13
+ GraphIdentifier.fromQuadGraph = fromQuadGraph;
14
+ function stringify(graphIdentifier) {
15
+ return graphIdentifier.value;
16
+ }
17
+ GraphIdentifier.stringify = stringify;
18
+ })(GraphIdentifier || (GraphIdentifier = {}));
19
+ //# sourceMappingURL=GraphIdentifier.js.map
@@ -0,0 +1,41 @@
1
+ import type { Stream } from "@rdfjs/types";
2
+ import type { Either, Maybe } from "purify-ts";
3
+ import type { GraphIdentifier } from "./GraphIdentifier.js";
4
+ /**
5
+ * Graph store abstraction, modeled on the SPARQL 1.1 Graph Store HTTP Protocol (https://www.w3.org/TR/sparql11-http-rdf-update/).
6
+ */
7
+ export interface GraphStore<ClearOptionsT extends object = object, ClearReturnT extends object = object, DeleteOptionsT extends object = object, DeleteReturnT extends object = object, GetOptionsT extends object = object, HeadOptionsT extends object = object, PostOptionsT extends object = object, PostReturnT extends object = object, PutOptionsT extends object = object, PutReturnT extends object = object> {
8
+ /**
9
+ * Clear the store.
10
+ */
11
+ clear(options?: ClearOptionsT): Promise<Either<Error, ClearReturnT>>;
12
+ /**
13
+ * Delete a graph.
14
+ */
15
+ delete(identifier: GraphIdentifier, options?: DeleteOptionsT): Promise<Either<Error, DeleteReturnT>>;
16
+ /**
17
+ * Get the triples of the named graph.
18
+ */
19
+ get(identifier: GraphIdentifier, options?: GetOptionsT): Promise<Either<Error, Maybe<Stream>>>;
20
+ /**
21
+ * Does the store have the named graph?
22
+ */
23
+ head(identifier: GraphIdentifier, options?: HeadOptionsT): Promise<Either<Error, boolean>>;
24
+ /**
25
+ * Get a list of the store's graph identifiers.
26
+ */
27
+ identifiers(): Promise<Either<Error, readonly GraphIdentifier[]>>;
28
+ /**
29
+ * Test if the entire store is empty.
30
+ */
31
+ isEmpty(): Promise<Either<Error, boolean>>;
32
+ /**
33
+ * Add quads without clearing quads' graphs first.
34
+ */
35
+ post(quads: Stream, options?: PostOptionsT): Promise<Either<Error, PostReturnT>>;
36
+ /**
37
+ * Add quads, clearing the quads' graphs first.
38
+ */
39
+ put(quads: Stream, options?: PutOptionsT): Promise<Either<Error, PutReturnT>>;
40
+ }
41
+ //# sourceMappingURL=GraphStore.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=GraphStore.js.map
@@ -0,0 +1,22 @@
1
+ import type { Stream } from "@rdfjs/types";
2
+ import type { Either, Maybe } from "purify-ts";
3
+ import type { Logger } from "ts-log";
4
+ import type { GraphIdentifier } from "./GraphIdentifier.js";
5
+ import type { GraphStore } from "./GraphStore.js";
6
+ /**
7
+ * Wrap a GraphStore implementation and log its operations.
8
+ */
9
+ export declare class LoggingGraphStore<ClearOptionsT extends object, ClearReturnT extends object, DeleteOptionsT extends object, DeleteReturnT extends object, GetOptionsT extends object, HeadOptionsT extends object, PostOptionsT extends object, PostReturnT extends object, PutOptionsT extends object, PutReturnT extends object> implements GraphStore<ClearOptionsT, ClearReturnT, DeleteOptionsT, DeleteReturnT, GetOptionsT, HeadOptionsT, PostOptionsT, PostReturnT, PutOptionsT, PutReturnT> {
10
+ private readonly delegate;
11
+ private readonly logger;
12
+ constructor(delegate: GraphStore<ClearOptionsT, ClearReturnT, DeleteOptionsT, DeleteReturnT, GetOptionsT, HeadOptionsT, PostOptionsT, PostReturnT, PutOptionsT, PutReturnT>, logger: Logger);
13
+ clear(options?: ClearOptionsT): Promise<Either<Error, ClearReturnT>>;
14
+ delete(identifier: GraphIdentifier, options?: DeleteOptionsT): Promise<Either<Error, DeleteReturnT>>;
15
+ get(identifier: GraphIdentifier, options?: GetOptionsT): Promise<Either<Error, Maybe<Stream>>>;
16
+ head(identifier: GraphIdentifier, options?: HeadOptionsT): Promise<Either<Error, boolean>>;
17
+ identifiers(): Promise<Either<Error, readonly GraphIdentifier[]>>;
18
+ isEmpty(): Promise<Either<Error, boolean>>;
19
+ post(quads: Stream, options?: PostOptionsT): Promise<Either<Error, PostReturnT>>;
20
+ put(quads: Stream, options?: PutOptionsT): Promise<Either<Error, PutReturnT>>;
21
+ }
22
+ //# sourceMappingURL=LoggingGraphStore.d.ts.map
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Wrap a GraphStore implementation and log its operations.
3
+ */
4
+ export class LoggingGraphStore {
5
+ delegate;
6
+ logger;
7
+ constructor(delegate, logger) {
8
+ this.delegate = delegate;
9
+ this.logger = logger;
10
+ }
11
+ async clear(options) {
12
+ this.logger.debug("clear(options=%s)", options);
13
+ return this.delegate.clear(options).then((either) => either
14
+ .ifLeft((error) => {
15
+ this.logger.error("clear(options=%s) -> ERROR: %s", options, error.message);
16
+ })
17
+ .ifRight((return_) => {
18
+ this.logger.debug("clear(options=%s) -> SUCCESS: %s", options, return_);
19
+ }));
20
+ }
21
+ async delete(identifier, options) {
22
+ this.logger.debug("delete(identifier=%s, options=%s)", identifier, options);
23
+ return this.delegate.delete(identifier, options).then((either) => either
24
+ .ifLeft((error) => {
25
+ this.logger.error("delete(identifier=%s, options=%s) -> ERROR: %s", identifier, options, error.message);
26
+ })
27
+ .ifRight((return_) => {
28
+ this.logger.debug("delete(identifier=%s, options=%s) -> SUCCESS: %s", identifier, options, return_);
29
+ }));
30
+ }
31
+ async get(identifier, options) {
32
+ this.logger.debug("get(identifier=%s, options=%s)", identifier, options);
33
+ return this.delegate.get(identifier, options).then((either) => either
34
+ .ifLeft((error) => {
35
+ this.logger.error("get(identifier=%s, options=%s) -> ERROR: %s", identifier, options, error.message);
36
+ })
37
+ .ifRight((maybeStream) => {
38
+ this.logger.debug("get(identifier=%s, options=%s) -> SUCCESS: %s", identifier, options, maybeStream.isJust() ? "<stream>" : "<nothing>");
39
+ }));
40
+ }
41
+ async head(identifier, options) {
42
+ this.logger.debug("head(identifier=%s, options=%s)", identifier, options);
43
+ return this.delegate.head(identifier, options).then((either) => either
44
+ .ifLeft((error) => {
45
+ this.logger.error("head(identifier=%s, options=%s) -> ERROR: %s", identifier, options, error.message);
46
+ })
47
+ .ifRight((return_) => {
48
+ this.logger.debug("head(identifier=%s, options=%s) -> SUCCESS: %s", identifier, options, return_);
49
+ }));
50
+ }
51
+ async identifiers() {
52
+ this.logger.debug("identifiers()");
53
+ return this.delegate.identifiers().then((either) => either
54
+ .ifLeft((error) => {
55
+ this.logger.error("identifiers() -> ERROR: %s", error.message);
56
+ })
57
+ .ifRight((return_) => {
58
+ this.logger.debug("identifiers() -> SUCCESS: %s", return_);
59
+ }));
60
+ }
61
+ async isEmpty() {
62
+ this.logger.debug("isEmpty()");
63
+ return this.delegate.isEmpty().then((either) => either
64
+ .ifLeft((error) => {
65
+ this.logger.error("isEmpty() -> ERROR: %s", error.message);
66
+ })
67
+ .ifRight((return_) => {
68
+ this.logger.debug("isEmpty() -> SUCCESS: %s", return_);
69
+ }));
70
+ }
71
+ async post(quads, options) {
72
+ this.logger.debug("post(quads, options=%s)", options);
73
+ return this.delegate.post(quads, options).then((either) => either
74
+ .ifLeft((error) => {
75
+ this.logger.error("post(quads, options=%s) -> ERROR: %s", options, error.message);
76
+ })
77
+ .ifRight((return_) => {
78
+ this.logger.debug("post(quads, options=%s) -> SUCCESS: %s", options, return_);
79
+ }));
80
+ }
81
+ async put(quads, options) {
82
+ this.logger.debug("put(quads, options=%s)", options);
83
+ return this.delegate.put(quads, options).then((either) => either
84
+ .ifLeft((error) => {
85
+ this.logger.error("put(quads, options=%s) -> ERROR: %s", options, error.message);
86
+ })
87
+ .ifRight((return_) => {
88
+ this.logger.debug("put(quads, options=%s) -> SUCCESS: %s", options, return_);
89
+ }));
90
+ }
91
+ }
92
+ //# sourceMappingURL=LoggingGraphStore.js.map
@@ -0,0 +1,20 @@
1
+ import type { DatasetCore, Stream } from "@rdfjs/types";
2
+ import { Either, Maybe } from "purify-ts";
3
+ import { GraphIdentifier } from "./GraphIdentifier.js";
4
+ import type { GraphStore } from "./GraphStore.js";
5
+ /**
6
+ * A GraphStore implementation backed by an RDF/JS Dataset.
7
+ */
8
+ export declare class RdfjsDatasetGraphStore implements GraphStore {
9
+ private readonly dataset;
10
+ constructor(dataset: DatasetCore);
11
+ clear(): Promise<Either<Error, object>>;
12
+ delete(identifier: GraphIdentifier): Promise<Either<Error, object>>;
13
+ head(identifier: GraphIdentifier): Promise<Either<Error, boolean>>;
14
+ identifiers(): Promise<Either<Error, readonly GraphIdentifier[]>>;
15
+ isEmpty(): Promise<Either<Error, boolean>>;
16
+ get(identifier: GraphIdentifier): Promise<Either<Error, Maybe<Stream>>>;
17
+ post(quads: Stream): Promise<Either<Error, object>>;
18
+ put(quads: Stream): Promise<Either<Error, object>>;
19
+ }
20
+ //# sourceMappingURL=RdfjsDatasetGraphStore.d.ts.map
@@ -0,0 +1,92 @@
1
+ import { Either, EitherAsync, Maybe } from "purify-ts";
2
+ import { Readable } from "readable-stream";
3
+ import { GraphIdentifier } from "./GraphIdentifier.js";
4
+ /**
5
+ * A GraphStore implementation backed by an RDF/JS Dataset.
6
+ */
7
+ export class RdfjsDatasetGraphStore {
8
+ dataset;
9
+ constructor(dataset) {
10
+ this.dataset = dataset;
11
+ }
12
+ async clear() {
13
+ return EitherAsync(async () => {
14
+ for (const quad of this.dataset) {
15
+ this.dataset.delete(quad);
16
+ }
17
+ return {};
18
+ });
19
+ }
20
+ async delete(identifier) {
21
+ return EitherAsync(async () => {
22
+ for (const quad of this.dataset.match(null, null, null, identifier)) {
23
+ this.dataset.delete(quad);
24
+ }
25
+ return {};
26
+ });
27
+ }
28
+ async head(identifier) {
29
+ return EitherAsync(async () => {
30
+ for (const _ of this.dataset.match(null, null, null, identifier)) {
31
+ return true;
32
+ }
33
+ return false;
34
+ });
35
+ }
36
+ async identifiers() {
37
+ return EitherAsync(async () => {
38
+ const identifiers = new Map();
39
+ for (const quad of this.dataset.match()) {
40
+ switch (quad.graph.termType) {
41
+ case "DefaultGraph":
42
+ identifiers.set("", quad.graph);
43
+ break;
44
+ case "NamedNode":
45
+ identifiers.set(quad.graph.value, quad.graph);
46
+ break;
47
+ }
48
+ }
49
+ return [...identifiers.values()];
50
+ });
51
+ }
52
+ async isEmpty() {
53
+ return Either.of(this.dataset.size === 0);
54
+ }
55
+ async get(identifier) {
56
+ for (const _ of this.dataset.match(null, null, null, identifier)) {
57
+ return Either.of(Maybe.of(Readable.from(this.dataset.match(null, null, null, identifier))));
58
+ }
59
+ return Either.of(Maybe.empty());
60
+ }
61
+ async post(quads) {
62
+ return new Promise((resolve) => {
63
+ quads.on("data", (quad) => {
64
+ this.dataset.add(quad);
65
+ });
66
+ quads.on("end", () => {
67
+ resolve(Either.of({}));
68
+ });
69
+ quads.on("error", (error) => resolve(Either.of(error)));
70
+ });
71
+ }
72
+ async put(quads) {
73
+ const clearedGraphIdentifiers = new Set();
74
+ return new Promise((resolve) => {
75
+ quads.on("data", (quad) => {
76
+ const graphIdentifierString = GraphIdentifier.stringify(GraphIdentifier.fromQuadGraph(quad.graph).unsafeCoerce());
77
+ if (!clearedGraphIdentifiers.has(graphIdentifierString)) {
78
+ for (const deleteQuad of this.dataset.match(null, null, null, quad.graph)) {
79
+ this.dataset.delete(deleteQuad);
80
+ }
81
+ clearedGraphIdentifiers.add(graphIdentifierString);
82
+ }
83
+ this.dataset.add(quad);
84
+ });
85
+ quads.on("end", () => {
86
+ resolve(Either.of({}));
87
+ });
88
+ quads.on("error", (error) => resolve(Either.of(error)));
89
+ });
90
+ }
91
+ }
92
+ //# sourceMappingURL=RdfjsDatasetGraphStore.js.map
@@ -0,0 +1,32 @@
1
+ import type { GraphStore } from "./GraphStore.js";
2
+ /**
3
+ * GraphStore specialization that supports retrieving versions of named graphs.
4
+ *
5
+ * Writes to the store return the new version of the store. Versions are intentionally opaque.
6
+ */
7
+ export type VersionedGraphStore<VersionT, ClearOptionsT extends object = object, ClearReturnT extends {
8
+ readonly version: VersionT;
9
+ } = {
10
+ readonly version: VersionT;
11
+ }, DeleteOptionsT extends object = object, DeleteReturnT extends {
12
+ readonly version: VersionT;
13
+ } = {
14
+ readonly version: VersionT;
15
+ }, GetOptionsT extends {
16
+ readonly version?: VersionT;
17
+ } = {
18
+ readonly version?: VersionT;
19
+ }, HeadOptionsT extends {
20
+ readonly version?: VersionT;
21
+ } = {
22
+ readonly version?: VersionT;
23
+ }, PostOptionsT extends object = object, PostReturnT extends {
24
+ readonly version: VersionT;
25
+ } = {
26
+ readonly version: VersionT;
27
+ }, PutOptionsT extends object = object, PutReturnT extends {
28
+ readonly version: VersionT;
29
+ } = {
30
+ readonly version: VersionT;
31
+ }> = GraphStore<ClearOptionsT, ClearReturnT, DeleteOptionsT, DeleteReturnT, GetOptionsT, HeadOptionsT, PostOptionsT, PostReturnT, PutOptionsT, PutReturnT>;
32
+ //# sourceMappingURL=VersionedGraphStore.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=VersionedGraphStore.js.map
@@ -0,0 +1,6 @@
1
+ export * from "./GraphIdentifier.js";
2
+ export * from "./GraphStore.js";
3
+ export * from "./LoggingGraphStore.js";
4
+ export * from "./RdfjsDatasetGraphStore.js";
5
+ export * from "./VersionedGraphStore.js";
6
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./GraphIdentifier.js";
2
+ export * from "./GraphStore.js";
3
+ export * from "./LoggingGraphStore.js";
4
+ export * from "./RdfjsDatasetGraphStore.js";
5
+ export * from "./VersionedGraphStore.js";
6
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "dependencies": {
3
+ "@rdfjs/types": "~2.0.1",
4
+ "@types/readable-stream": "~4.0.23",
5
+ "purify-ts": "~2.1.4",
6
+ "readable-stream": "^4.7.0",
7
+ "ts-log": "~3.0.2"
8
+ },
9
+ "devDependencies": {
10
+ "@rdfx/data-factory": "0.0.28",
11
+ "@rdfjs/dataset": "~2.0.2",
12
+ "get-stream": "~9.0.1",
13
+ "into-stream": "~9.1.0"
14
+ },
15
+ "files": [
16
+ "LICENSE",
17
+ "dist/*.d.ts",
18
+ "dist/*.js"
19
+ ],
20
+ "license": "Apache-2.0",
21
+ "main": "./dist/index.js",
22
+ "name": "@rdfx/graph-store",
23
+ "packageManager": "npm@11.11.0",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/minorg/rdfx.git"
27
+ },
28
+ "scripts": {
29
+ "build": "tsc -b",
30
+ "clean": "rimraf dist",
31
+ "depcheck": "depcheck .",
32
+ "dev": "tsc -w --preserveWatchOutput",
33
+ "dev:tests": "tsc -p __tests__ -w --preserveWatchOutput",
34
+ "test": "cd ../.. && vitest run /Users/minor/projects/rdfx/packages/graph-store/__tests__"
35
+ },
36
+ "type": "module",
37
+ "types": "./dist/index.d.ts",
38
+ "version": "0.0.28"
39
+ }