@oxilite/common 0.1.0 → 0.2.2

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.
package/README.md CHANGED
@@ -1,5 +1,52 @@
1
+ <p align="center">
2
+ <a href="https://oxilitedb.com"><img src="https://raw.githubusercontent.com/Volland/oxilite/main/site/assets/logo.png" alt="oxilite" width="120"></a>
3
+ </p>
4
+
1
5
  # @oxilite/common
2
6
 
3
- RDF/JS terms (`DataFactory`, `NamedNode`, `Literal`, `Quad`…), JSON conversions and result types shared by [`@oxilite/node`](https://www.npmjs.com/package/@oxilite/node) and [`@oxilite/d1`](https://www.npmjs.com/package/@oxilite/d1). You normally get it through one of those packages.
7
+ [![npm](https://img.shields.io/npm/v/@oxilite/common.svg)](https://www.npmjs.com/package/@oxilite/common) [![license](https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue.svg)](https://github.com/Volland/oxilite#license)
8
+
9
+ **RDF/JS terms and the shared TypeScript types of oxilite**, used by [`@oxilite/node`](https://www.npmjs.com/package/@oxilite/node) and [`@oxilite/d1`](https://www.npmjs.com/package/@oxilite/d1).
10
+
11
+ **[Website](https://oxilitedb.com)** · [npm](https://www.npmjs.com/package/@oxilite/common) · **[Guide and architecture](https://github.com/Volland/oxilite#readme)** · [Changelog and issues](https://github.com/Volland/oxilite/issues)
12
+
13
+ You rarely install it directly: both packages re-export everything here.
14
+
15
+ ```ts
16
+ import { namedNode, literal, quad, DataFactory, type Term } from "@oxilite/node"; // or "@oxilite/d1"
17
+
18
+ const q = quad(namedNode("http://example.com/ada"), namedNode("http://example.com/name"), literal("Ada", "en"));
19
+ ```
20
+
21
+ ## What is inside
22
+
23
+ - **RDF/JS terms:** `NamedNode`, `BlankNode`, `Literal` (with language and direction), `DefaultGraph`, `Variable` and `Quad` (also usable as an RDF 1.2 triple term), plus `DataFactory` and its shortcuts `namedNode`, `blankNode`, `literal`, `defaultGraph`, `variable`, `quad`, `triple`. They follow the [RDF/JS data model](https://rdf.js.org/data-model-spec/), so they mix with other RDF/JS libraries.
24
+ - **SPARQL types:** `QueryOptions` (with oxilite's `reasoning` and `include_inferred`), `LoadOptions`, `DumpOptions`, `QueryResult`.
25
+ - **Cypher types:** `CypherValue`, `CypherNode`, `CypherRelationship`, `CypherPath`, `CypherTemporal`, `CypherResult`, `CypherStats` and `CypherOptions` (`base`, `prefixes`, `names`, `multiValue`, `reasoning`, `shapes`…).
26
+ - **JSON-LD and credential types:** `JsonLdOptions`, `CredentialOptions`, `KeyStrategy`, `GraphStrategy`, `StoredDocument`, `DocumentFilter`, `Drift`, `PresentationKeys` and the `JsonLdError` class (with its JSON-LD error `code`).
27
+ - **JSON helpers:** `toJson` / `fromJson` convert terms to and from the JSON form the oxilite core exchanges.
28
+
29
+ ## The oxilite family
30
+
31
+ oxilite is an Oxigraph-compatible RDF database and SPARQL 1.1 engine that stores its data in SQLite, so it runs anywhere SQLite runs: in-process, on a system or vendor `libsqlite3`, on Cloudflare D1 and in Durable Objects. The same data can be queried with SPARQL and openCypher, reasoned over with RDFS / OWL, and validated with SHACL and ShEx. Read the overview on **[oxilitedb.com](https://oxilitedb.com)** and the full guide in the [main README](https://github.com/Volland/oxilite#readme).
32
+
33
+ | Package | What it is for |
34
+ |---|---|
35
+ | [`oxilite`](https://crates.io/crates/oxilite) | The store: a drop-in for `oxigraph::store::Store`, plus `AsyncStore` for D1 |
36
+ | [`oxilite-core`](https://crates.io/crates/oxilite-core) | The sans-IO core: term encoding, schema, SPARQL → SQL compiler and planner |
37
+ | [`oxilite-rusqlite`](https://crates.io/crates/oxilite-rusqlite) | In-process backend with a bundled SQLite (the default) |
38
+ | [`oxilite-dylib`](https://crates.io/crates/oxilite-dylib) | Backend that loads your own `libsqlite3` at runtime |
39
+ | [`oxilite-d1`](https://crates.io/crates/oxilite-d1) | Cloudflare D1 backend for Rust Workers |
40
+ | [`oxilite-cypher`](https://crates.io/crates/oxilite-cypher) | openCypher over the same data, OWL- and SHACL-aware |
41
+ | [`oxilite-jsonld`](https://crates.io/crates/oxilite-jsonld) | JSON-LD documents stored verbatim, one named graph each |
42
+ | [`oxilite-vc`](https://crates.io/crates/oxilite-vc) | Verifiable Credentials: stored under their id, indexed, queryable |
43
+ | [`oxilite-reason`](https://crates.io/crates/oxilite-reason) | OWL 2 RL materialization with `reasonable` |
44
+ | [`oxilite-validate`](https://crates.io/crates/oxilite-validate) | SHACL and ShEx validation with rudof |
45
+ | [`oxilite-cli`](https://crates.io/crates/oxilite-cli) | The `oxilite` command and a SPARQL endpoint like `oxigraph serve` |
46
+ | [`@oxilite/node`](https://www.npmjs.com/package/@oxilite/node) | Node.js bindings, API of Oxigraph's JS package |
47
+ | [`@oxilite/d1`](https://www.npmjs.com/package/@oxilite/d1) | Cloudflare D1 and Durable Objects from TypeScript (WebAssembly core) |
48
+ | [`@oxilite/common`](https://www.npmjs.com/package/@oxilite/common) | RDF/JS terms and shared TypeScript types |
49
+
50
+ ## License
4
51
 
5
- See the [oxilite repository](https://github.com/Volland/oxilite) for documentation.
52
+ Dual-licensed under [MIT](https://github.com/Volland/oxilite/blob/main/LICENSE-MIT) or [Apache-2.0](https://github.com/Volland/oxilite/blob/main/LICENSE-APACHE), at your option, like Oxigraph.
package/dist/index.d.ts CHANGED
@@ -147,4 +147,178 @@ export interface DumpOptions {
147
147
  /** Content of `load()`: a string, bytes, or a list of them (like Oxigraph's JS API). */
148
148
  export type LoadData = string | Uint8Array | (string | Uint8Array)[];
149
149
  export declare function loadDataToString(data: LoadData): string;
150
+ /** A node of the property-graph view of the dataset. */
151
+ export interface CypherNode {
152
+ type: "node";
153
+ /** The node's IRI (or `_:label` for a blank node). */
154
+ id: string;
155
+ labels: string[];
156
+ properties: Record<string, CypherValue>;
157
+ }
158
+ /** A relationship: an RDF triple, identified by its reifier when it has one. */
159
+ export interface CypherRelationship {
160
+ type: "relationship";
161
+ id: string;
162
+ relType: string;
163
+ start: string;
164
+ end: string;
165
+ properties: Record<string, CypherValue>;
166
+ }
167
+ export interface CypherPath {
168
+ type: "path";
169
+ nodes: CypherNode[];
170
+ relationships: CypherRelationship[];
171
+ }
172
+ /** A temporal value in its ISO 8601 form (`value`). */
173
+ export interface CypherTemporal {
174
+ type: "date" | "datetime" | "localdatetime" | "time" | "localtime" | "duration";
175
+ value: string;
176
+ }
177
+ export type CypherValue = null | boolean | number | string | CypherValue[] | CypherNode | CypherRelationship | CypherPath | CypherTemporal | {
178
+ [key: string]: CypherValue;
179
+ };
180
+ /** What a statement changed. */
181
+ export interface CypherStats {
182
+ nodesCreated: number;
183
+ nodesDeleted: number;
184
+ relationshipsCreated: number;
185
+ relationshipsDeleted: number;
186
+ propertiesSet: number;
187
+ labelsAdded: number;
188
+ labelsRemoved: number;
189
+ }
190
+ export interface CypherResult {
191
+ columns: string[];
192
+ rows: CypherValue[][];
193
+ /** Rows as objects keyed by column. */
194
+ records: Record<string, CypherValue>[];
195
+ stats: CypherStats;
196
+ }
197
+ /** Options of the Cypher frontend (see `oxilite_cypher::json`). */
198
+ export interface CypherOptions {
199
+ /** Namespace of labels, relationship types and keys without a prefix (default `urn:oxilite:pg:`). */
200
+ base?: string;
201
+ /** Prefixes usable in names: `` :`schema:Person` ``. */
202
+ prefixes?: Record<string, string>;
203
+ /** Explicit name → IRI mappings. */
204
+ names?: Record<string, string>;
205
+ /** How a property with several RDF values reads: a list (default), its first value, or an error. */
206
+ multiValue?: "list" | "first" | "error";
207
+ /** Maximum hops of an unbounded variable-length relationship that binds a variable. */
208
+ varLengthCap?: number;
209
+ /** Maximum depth of an unbounded shortest-path search. */
210
+ shortestPathCap?: number;
211
+ /** Check writes against the SHACL shapes of the dataset (default true). */
212
+ shapes?: boolean;
213
+ /** Give created nodes an `rdf:type rdfs:Resource` triple (default true). */
214
+ nodeMarker?: boolean;
215
+ /** Entailment for matching: `"rdfs"` or `"owl-ql"` make labels follow class hierarchies. */
216
+ reasoning?: "none" | "rdfs" | "owl-ql";
217
+ useDefaultGraphAsUnion?: boolean;
218
+ }
219
+ /** Output of a Cypher job (`kind: "cypher"`). */
220
+ export interface CypherOutput {
221
+ kind: "cypher";
222
+ columns: string[];
223
+ rows: CypherValue[][];
224
+ stats: CypherStats;
225
+ }
226
+ export declare function cypherResult(out: CypherOutput): CypherResult;
227
+ /** Where a document's key comes from. */
228
+ export type KeyStrategy = "id" | "contentHash" | "explicit" | {
229
+ pointer: string;
230
+ };
231
+ /** Which graph a document's triples go to: the key itself (default), an IRI template with `{key}`, one fixed graph, or the default graph. */
232
+ export type GraphStrategy = "key" | "default" | {
233
+ template: string;
234
+ } | {
235
+ fixed: string;
236
+ };
237
+ /** Options of a JSON-LD document handle. */
238
+ export interface JsonLdOptions {
239
+ /** Default `"id"`: the top-level `@id` / `id`. */
240
+ key?: KeyStrategy;
241
+ /** When the key strategy finds nothing: fail (default for documents) or use `urn:oxilite:doc:sha256:<hex>` (default for credentials). */
242
+ onMissingKey?: "reject" | "contentHash";
243
+ /** Default `"key"`: one named graph per document, named after its key. */
244
+ graph?: GraphStrategy;
245
+ /** Base IRI for relative IRIs in documents. */
246
+ baseIri?: string;
247
+ rdfDirection?: "i18n-datatype" | "compound-literal";
248
+ processingMode?: "json-ld-1.0" | "json-ld-1.1";
249
+ /** Contexts available in memory: IRI → context document. */
250
+ contexts?: Record<string, object | string>;
251
+ /** Fetch unknown remote contexts over HTTP (Node only; default false: no network access). */
252
+ network?: boolean;
253
+ /** Persist fetched contexts in the store, so later loads work offline. */
254
+ cacheFetched?: boolean;
255
+ /** Metadata indexes created with the tables (all default true). */
256
+ indexes?: {
257
+ issuer?: boolean;
258
+ subject?: boolean;
259
+ validUntil?: boolean;
260
+ };
261
+ }
262
+ /** Options of a credentials handle. */
263
+ export interface CredentialOptions extends JsonLdOptions {
264
+ /** Also store each credential embedded in a presentation on its own (default true). */
265
+ embedCredentials?: boolean;
266
+ }
267
+ /** A stored document (or credential). */
268
+ export interface StoredDocument {
269
+ key: string;
270
+ /** The graph its default-graph triples were written to. */
271
+ graph: Term;
272
+ /** The JSON exactly as it was stored. */
273
+ json: string;
274
+ /** Hex SHA-256 of `json`. */
275
+ sha256: string;
276
+ /** `jsonld`, or `vc1` / `vc2` / `vp1` / `vp2` for credentials and presentations. */
277
+ profile: string;
278
+ issuer: string | null;
279
+ subject: string | null;
280
+ types: string[];
281
+ validFrom: Date | null;
282
+ validUntil: Date | null;
283
+ /** Keys of the credentials a presentation embeds. */
284
+ refs: string[];
285
+ storedAt: Date;
286
+ }
287
+ /** Metadata filter of `find`. */
288
+ export interface DocumentFilter {
289
+ issuer?: string;
290
+ subject?: string;
291
+ /** One value of the `type` array. */
292
+ type?: string;
293
+ /** Valid at this instant (validFrom ≤ t < validUntil; open ends allowed). */
294
+ validAt?: Date | number;
295
+ profile?: string;
296
+ /** Keyset paging: keys after this one. */
297
+ after?: string;
298
+ limit?: number;
299
+ }
300
+ /** A document whose graphs differ from a fresh conversion of its JSON. */
301
+ export interface Drift {
302
+ key: string;
303
+ missing: number;
304
+ extra: number;
305
+ }
306
+ /** Keys written by `putPresentation`. */
307
+ export interface PresentationKeys {
308
+ key: string;
309
+ credentials: string[];
310
+ }
311
+ /** A JSON-LD or credential error; `code` is the JSON-LD error code (e.g. `invalid local context`) or `missing-key`, `invalid-graph-name`, `graph-owned`, `document-too-large`, `invalid`, `json`, `store`. */
312
+ export declare class JsonLdError extends Error {
313
+ readonly code: string;
314
+ constructor(code: string, message: string);
315
+ }
316
+ /** Rethrows an error of the core's JSON-LD operations as a `JsonLdError`. */
317
+ export declare function jsonLdError(e: unknown): unknown;
318
+ /** A stored document from the core's JSON (epoch seconds become `Date`s). */
319
+ export declare function storedDocument(j: Record<string, unknown> | null): StoredDocument | null;
320
+ /** A filter in the core's JSON form. */
321
+ export declare function filterJson(f: DocumentFilter): Record<string, unknown>;
322
+ /** A document argument: JSON text is stored verbatim, objects are serialized. */
323
+ export declare function documentText(doc: string | object): string;
150
324
  export {};
package/dist/index.js CHANGED
@@ -209,3 +209,63 @@ export function loadDataToString(data) {
209
209
  const one = (d) => (typeof d === "string" ? d : new TextDecoder().decode(d));
210
210
  return Array.isArray(data) ? data.map(one).join("\n") : one(data);
211
211
  }
212
+ export function cypherResult(out) {
213
+ return {
214
+ columns: out.columns,
215
+ rows: out.rows,
216
+ records: out.rows.map((row) => Object.fromEntries(out.columns.map((c, i) => [c, row[i] ?? null]))),
217
+ stats: out.stats,
218
+ };
219
+ }
220
+ /** A JSON-LD or credential error; `code` is the JSON-LD error code (e.g. `invalid local context`) or `missing-key`, `invalid-graph-name`, `graph-owned`, `document-too-large`, `invalid`, `json`, `store`. */
221
+ export class JsonLdError extends Error {
222
+ code;
223
+ constructor(code, message) {
224
+ super(message);
225
+ this.name = "JsonLdError";
226
+ this.code = code;
227
+ }
228
+ }
229
+ /** Rethrows an error of the core's JSON-LD operations as a `JsonLdError`. */
230
+ export function jsonLdError(e) {
231
+ const message = e instanceof Error ? e.message : String(e);
232
+ const at = message.indexOf("oxilite-jsonld:");
233
+ if (at < 0)
234
+ return e;
235
+ try {
236
+ const { code, message: m } = JSON.parse(message.slice(at + "oxilite-jsonld:".length));
237
+ return new JsonLdError(code, m);
238
+ }
239
+ catch {
240
+ return e;
241
+ }
242
+ }
243
+ /** A stored document from the core's JSON (epoch seconds become `Date`s). */
244
+ export function storedDocument(j) {
245
+ if (!j)
246
+ return null;
247
+ const date = (v) => (typeof v === "number" ? new Date(v * 1000) : null);
248
+ return {
249
+ key: j.key,
250
+ graph: fromJson(j.graph),
251
+ json: j.json,
252
+ sha256: j.sha256,
253
+ profile: j.profile,
254
+ issuer: j.issuer ?? null,
255
+ subject: j.subject ?? null,
256
+ types: j.types ?? [],
257
+ validFrom: date(j.validFrom),
258
+ validUntil: date(j.validUntil),
259
+ refs: j.refs ?? [],
260
+ storedAt: date(j.storedAt),
261
+ };
262
+ }
263
+ /** A filter in the core's JSON form. */
264
+ export function filterJson(f) {
265
+ const t = f.validAt;
266
+ return { ...f, validAt: t === undefined ? undefined : (t instanceof Date ? t.getTime() : t) / 1000 };
267
+ }
268
+ /** A document argument: JSON text is stored verbatim, objects are serialized. */
269
+ export function documentText(doc) {
270
+ return typeof doc === "string" ? doc : JSON.stringify(doc);
271
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxilite/common",
3
- "version": "0.1.0",
3
+ "version": "0.2.2",
4
4
  "description": "RDF/JS terms and result conversion shared by the oxilite JavaScript packages",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "type": "module",
@@ -17,15 +17,15 @@
17
17
  "url": "git+https://github.com/Volland/oxilite.git",
18
18
  "directory": "packages/common"
19
19
  },
20
- "homepage": "https://github.com/Volland/oxilite#readme",
20
+ "homepage": "https://oxilitedb.com",
21
21
  "bugs": "https://github.com/Volland/oxilite/issues",
22
22
  "author": "Volodymyr Pavlyshyn",
23
23
  "keywords": [
24
24
  "rdf",
25
+ "rdfjs",
25
26
  "sparql",
26
- "sqlite",
27
- "oxigraph",
28
- "rdfjs"
27
+ "cypher",
28
+ "oxilite"
29
29
  ],
30
30
  "publishConfig": {
31
31
  "access": "public"