@rdfx/tinybase 0.0.45

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,45 @@
1
+ import type { DataFactory, DatasetCoreFactory, Quad, Stream } from "@rdfjs/types";
2
+ import { GraphIdentifier, type GraphStore } from "@rdfx/graph-store";
3
+ import { Either, Maybe } from "purify-ts";
4
+ import { type NoValuesSchema, type Store } from "tinybase/with-schemas";
5
+ import type { Logger } from "ts-log";
6
+ export declare class TinyBaseGraphStore implements GraphStore {
7
+ private readonly dataFactory;
8
+ private readonly datasetFactory;
9
+ private readonly logger;
10
+ readonly tinyBaseStore: TinyBaseGraphStore.TinyBaseStore;
11
+ constructor({ dataFactory, datasetFactory, logger, parseGraph, tinyBaseStore, }: {
12
+ dataFactory: DataFactory;
13
+ datasetFactory: DatasetCoreFactory;
14
+ logger: Logger;
15
+ parseGraph?: (identifier: GraphIdentifier, ntriples: string) => Either<Error, Quad[]>;
16
+ tinyBaseStore?: TinyBaseGraphStore.TinyBaseStore;
17
+ });
18
+ clear(): Promise<Either<Error, object>>;
19
+ delete(identifier: GraphIdentifier): Promise<Either<Error, object>>;
20
+ deleteSync(identifier: GraphIdentifier): Either<Error, object>;
21
+ get(identifier: GraphIdentifier): Promise<Either<Error, Maybe<Stream>>>;
22
+ getQuadsSync(identifier?: GraphIdentifier): Either<Error, Maybe<readonly Quad[]>>;
23
+ getStreamSync(identifier?: GraphIdentifier): Either<Error, Maybe<Stream>>;
24
+ head(identifier: GraphIdentifier): Promise<Either<Error, boolean>>;
25
+ headSync(identifier: GraphIdentifier): Either<Error, boolean>;
26
+ identifiers(): Promise<Either<Error, readonly GraphIdentifier[]>>;
27
+ identifiersSync(): Either<Error, readonly GraphIdentifier[]>;
28
+ isEmpty(): Promise<Either<Error, boolean>>;
29
+ isEmptySync(): Either<Error, boolean>;
30
+ post(quads: Stream): Promise<Either<Error, object>>;
31
+ put(quads: Stream): Promise<Either<Error, object>>;
32
+ private clearSync;
33
+ private readonly parseGraph;
34
+ }
35
+ export declare namespace TinyBaseGraphStore {
36
+ type TinyBaseStore = Store<[typeof tablesSchema, NoValuesSchema]>;
37
+ const tablesSchema: {
38
+ readonly graph: {
39
+ readonly ntriples: {
40
+ readonly type: "string";
41
+ };
42
+ };
43
+ };
44
+ }
45
+ //# sourceMappingURL=TinyBaseGraphStore.d.ts.map
@@ -0,0 +1,181 @@
1
+ import { GraphIdentifier } from "@rdfx/graph-store";
2
+ import { iterableToStream } from "@rdfx/stream";
3
+ import { NTriplesTerm } from "@rdfx/string";
4
+ import { Parser } from "n3";
5
+ import { Either, Left, Maybe } from "purify-ts";
6
+ import { createMergeableStore, } from "tinybase/with-schemas";
7
+ export class TinyBaseGraphStore {
8
+ dataFactory;
9
+ datasetFactory;
10
+ logger;
11
+ tinyBaseStore;
12
+ constructor({ dataFactory, datasetFactory, logger, parseGraph, tinyBaseStore, }) {
13
+ this.dataFactory = dataFactory;
14
+ this.datasetFactory = datasetFactory;
15
+ this.logger = logger;
16
+ this.parseGraph =
17
+ parseGraph ??
18
+ ((identifier, ntriples) => Either.encase(() => {
19
+ const parser = new Parser({ format: "application/n-triples" });
20
+ // Hack to put the quads in this named graph rather than rewriting them all after parsing.
21
+ parser.DEFAULTGRAPH = identifier;
22
+ return parser.parse(ntriples);
23
+ }));
24
+ this.tinyBaseStore =
25
+ tinyBaseStore ??
26
+ createMergeableStore().setTablesSchema(TinyBaseGraphStore.tablesSchema);
27
+ }
28
+ async clear() {
29
+ return this.clearSync();
30
+ }
31
+ async delete(identifier) {
32
+ return this.deleteSync(identifier);
33
+ }
34
+ deleteSync(identifier) {
35
+ return Either.encase(() => {
36
+ this.tinyBaseStore.delRow("graph", GraphIdentifier.stringify(identifier));
37
+ return {};
38
+ });
39
+ }
40
+ async get(identifier) {
41
+ return this.getStreamSync(identifier);
42
+ }
43
+ getQuadsSync(identifier) {
44
+ return Either.encase(() => {
45
+ if (identifier) {
46
+ const row = this.tinyBaseStore.getRow("graph", GraphIdentifier.stringify(identifier));
47
+ if (Object.values(row).length === 0) {
48
+ return Maybe.empty();
49
+ }
50
+ const parsedQuadsEither = this.parseGraph(identifier, row.ntriples);
51
+ if (parsedQuadsEither.isLeft()) {
52
+ this.logger.warn("error parsing row %s: %s", GraphIdentifier.stringify(identifier), parsedQuadsEither.extract().message);
53
+ return Maybe.empty();
54
+ }
55
+ return Maybe.of(parsedQuadsEither.extract());
56
+ }
57
+ else {
58
+ const startTimestampMs = performance.now();
59
+ let quads = [];
60
+ const rows = Object.entries(this.tinyBaseStore.getTable("graph"));
61
+ // this.logger.debug("parsing %d rows", rows.length);
62
+ for (const [rowId, row] of rows) {
63
+ // this.logger.debug("parsing row %s", rowId);
64
+ const identifier = GraphIdentifier.parse(this.dataFactory, rowId);
65
+ this.parseGraph(identifier, row.ntriples)
66
+ .ifLeft((error) => {
67
+ this.logger.warn("error parsing row %s: %s", rowId, error.message);
68
+ })
69
+ .ifRight((newQuads) => {
70
+ quads = quads.concat(newQuads);
71
+ });
72
+ }
73
+ const elapsedTimeMs = performance.now() - startTimestampMs;
74
+ this.logger.debug("parsed %d quads from %d rows in %.2fms", quads, rows.length, elapsedTimeMs);
75
+ return Maybe.of(quads);
76
+ }
77
+ });
78
+ }
79
+ getStreamSync(identifier) {
80
+ return this.getQuadsSync(identifier).map((quadsMaybe) => quadsMaybe.map((quads) => iterableToStream(quads)));
81
+ }
82
+ async head(identifier) {
83
+ return this.headSync(identifier);
84
+ }
85
+ headSync(identifier) {
86
+ return Either.encase(() => this.tinyBaseStore.hasRow("graph", GraphIdentifier.stringify(identifier)));
87
+ }
88
+ async identifiers() {
89
+ return this.identifiersSync();
90
+ }
91
+ identifiersSync() {
92
+ return Either.encase(() => this.tinyBaseStore
93
+ .getRowIds("graph")
94
+ .map((rowId) => GraphIdentifier.parse(this.dataFactory, rowId)));
95
+ }
96
+ async isEmpty() {
97
+ return this.isEmptySync();
98
+ }
99
+ isEmptySync() {
100
+ return Either.encase(() => this.tinyBaseStore.getRowCount("graph") === 0);
101
+ }
102
+ async post(quads) {
103
+ return new Promise((resolve) => {
104
+ const datasetsByGraphIdentifier = new Map();
105
+ quads
106
+ .on("data", (quad) => {
107
+ const identifier = GraphIdentifier.fromQuadGraph(quad.graph).unsafeCoerce();
108
+ const graphIdentifierString_ = GraphIdentifier.stringify(identifier);
109
+ let dataset = datasetsByGraphIdentifier.get(graphIdentifierString_);
110
+ if (!dataset) {
111
+ dataset = this.datasetFactory.dataset();
112
+ datasetsByGraphIdentifier.set(graphIdentifierString_, dataset);
113
+ this.getQuadsSync(identifier)
114
+ .unsafeCoerce()
115
+ .ifJust((existingDataset) => {
116
+ for (const quad of existingDataset) {
117
+ dataset.add(this.dataFactory.quad(quad.subject, quad.predicate, quad.object));
118
+ }
119
+ });
120
+ }
121
+ dataset.add(this.dataFactory.quad(quad.subject, quad.predicate, quad.object));
122
+ })
123
+ .on("end", () => {
124
+ for (const [identifier, dataset,] of datasetsByGraphIdentifier.entries()) {
125
+ const ntriples = [];
126
+ for (const quad of dataset) {
127
+ ntriples.push(NTriplesTerm.stringify(quad));
128
+ }
129
+ this.tinyBaseStore.setRow("graph", identifier, {
130
+ ntriples: ntriples.join("\n"),
131
+ });
132
+ }
133
+ resolve(Either.of({}));
134
+ })
135
+ .on("error", (error) => {
136
+ resolve(Left(error));
137
+ });
138
+ });
139
+ }
140
+ async put(quads) {
141
+ return new Promise((resolve) => {
142
+ const ntriplesByGraphIdentifier = new Map();
143
+ quads
144
+ .on("data", (quad) => {
145
+ const graphIdentifierString_ = GraphIdentifier.stringify(GraphIdentifier.fromQuadGraph(quad.graph).unsafeCoerce());
146
+ let ntriples = ntriplesByGraphIdentifier.get(graphIdentifierString_);
147
+ if (!ntriples) {
148
+ ntriples = [];
149
+ ntriplesByGraphIdentifier.set(graphIdentifierString_, ntriples);
150
+ }
151
+ ntriples.push(NTriplesTerm.stringify(this.dataFactory.quad(quad.subject, quad.predicate, quad.object)));
152
+ })
153
+ .on("end", () => {
154
+ for (const [identifier, ntriples,] of ntriplesByGraphIdentifier.entries()) {
155
+ this.tinyBaseStore.setRow("graph", identifier, {
156
+ ntriples: ntriples.join("\n"),
157
+ });
158
+ }
159
+ resolve(Either.of({}));
160
+ })
161
+ .on("error", (error) => {
162
+ resolve(Left(error));
163
+ });
164
+ });
165
+ }
166
+ clearSync() {
167
+ return Either.encase(() => {
168
+ this.tinyBaseStore.delTable("graph");
169
+ return {};
170
+ });
171
+ }
172
+ parseGraph;
173
+ }
174
+ (function (TinyBaseGraphStore) {
175
+ TinyBaseGraphStore.tablesSchema = {
176
+ graph: {
177
+ ntriples: { type: "string" },
178
+ },
179
+ };
180
+ })(TinyBaseGraphStore || (TinyBaseGraphStore = {}));
181
+ //# sourceMappingURL=TinyBaseGraphStore.js.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "dependencies": {
3
+ "@rdfx/graph-store": "0.0.45",
4
+ "@rdfx/stream": "0.0.45",
5
+ "@rdfx/string": "0.0.45",
6
+ "@rdfjs/types": "~2.0.1",
7
+ "@types/n3": "~1.26.0",
8
+ "n3": "~1.26.0",
9
+ "purify-ts": "~2.1.4",
10
+ "tinybase": "~9.4.0",
11
+ "ts-log": "~3.0.2"
12
+ },
13
+ "devDependencies": {
14
+ "@rdfx/collection": "0.0.45",
15
+ "@rdfx/data-factory": "0.0.45"
16
+ },
17
+ "files": [
18
+ "LICENSE",
19
+ "dist/*.d.ts",
20
+ "dist/*.js"
21
+ ],
22
+ "license": "Apache-2.0",
23
+ "main": "./dist/index.js",
24
+ "name": "@rdfx/tinybase",
25
+ "packageManager": "npm@11.11.0",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/minorg/rdfx.git"
29
+ },
30
+ "scripts": {
31
+ "build": "tsc -b",
32
+ "clean": "rimraf dist",
33
+ "depcheck": "depcheck --ignores=\"@tsconfig/*,vitest,vitest-fetch-mock\" .",
34
+ "dev": "tsc -w --preserveWatchOutput",
35
+ "dev:tests": "tsc -p __tests__ -w --preserveWatchOutput",
36
+ "test": "cd ../.. && vitest run packages/tinybase/__tests__"
37
+ },
38
+ "type": "module",
39
+ "types": "./dist/index.d.ts",
40
+ "version": "0.0.45"
41
+ }