@ldo/jsonld-dataset-proxy 0.0.1-1.0.0-alpha.1.0
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/LICENSE.txt +21 -0
- package/README.md +830 -0
- package/dist/ContextUtil.d.ts +17 -0
- package/dist/ContextUtil.js +108 -0
- package/dist/ContextUtil.js.map +1 -0
- package/dist/JsonldDatasetProxyBuilder.d.ts +16 -0
- package/dist/JsonldDatasetProxyBuilder.js +41 -0
- package/dist/JsonldDatasetProxyBuilder.js.map +1 -0
- package/dist/LdoJsonldContext.d.ts +8 -0
- package/dist/LdoJsonldContext.js +3 -0
- package/dist/LdoJsonldContext.js.map +1 -0
- package/dist/ProxyContext.d.ts +31 -0
- package/dist/ProxyContext.js +63 -0
- package/dist/ProxyContext.js.map +1 -0
- package/dist/arrayProxy/ArrayProxy.d.ts +12 -0
- package/dist/arrayProxy/ArrayProxy.js +3 -0
- package/dist/arrayProxy/ArrayProxy.js.map +1 -0
- package/dist/arrayProxy/arrayMethods.d.ts +17 -0
- package/dist/arrayProxy/arrayMethods.js +161 -0
- package/dist/arrayProxy/arrayMethods.js.map +1 -0
- package/dist/arrayProxy/createArrayHandler.d.ts +9 -0
- package/dist/arrayProxy/createArrayHandler.js +140 -0
- package/dist/arrayProxy/createArrayHandler.js.map +1 -0
- package/dist/arrayProxy/isArrayProxy.d.ts +2 -0
- package/dist/arrayProxy/isArrayProxy.js +17 -0
- package/dist/arrayProxy/isArrayProxy.js.map +1 -0
- package/dist/arrayProxy/modifyArray.d.ts +12 -0
- package/dist/arrayProxy/modifyArray.js +73 -0
- package/dist/arrayProxy/modifyArray.js.map +1 -0
- package/dist/graphOf.d.ts +4 -0
- package/dist/graphOf.js +25 -0
- package/dist/graphOf.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/dist/jsonldDatasetProxy.d.ts +5 -0
- package/dist/jsonldDatasetProxy.js +19 -0
- package/dist/jsonldDatasetProxy.js.map +1 -0
- package/dist/language/languageMapProxy.d.ts +4 -0
- package/dist/language/languageMapProxy.js +55 -0
- package/dist/language/languageMapProxy.js.map +1 -0
- package/dist/language/languageSet.d.ts +23 -0
- package/dist/language/languageSet.js +75 -0
- package/dist/language/languageSet.js.map +1 -0
- package/dist/language/languageTypes.d.ts +2 -0
- package/dist/language/languageTypes.js +3 -0
- package/dist/language/languageTypes.js.map +1 -0
- package/dist/language/languageUtils.d.ts +14 -0
- package/dist/language/languageUtils.js +73 -0
- package/dist/language/languageUtils.js.map +1 -0
- package/dist/language/languagesOf.d.ts +13 -0
- package/dist/language/languagesOf.js +17 -0
- package/dist/language/languagesOf.js.map +1 -0
- package/dist/setLanguagePreferences.d.ts +3 -0
- package/dist/setLanguagePreferences.js +9 -0
- package/dist/setLanguagePreferences.js.map +1 -0
- package/dist/setProxy/ObjectSetProxy.d.ts +17 -0
- package/dist/setProxy/ObjectSetProxy.js +31 -0
- package/dist/setProxy/ObjectSetProxy.js.map +1 -0
- package/dist/setProxy/SetProxy.d.ts +29 -0
- package/dist/setProxy/SetProxy.js +72 -0
- package/dist/setProxy/SetProxy.js.map +1 -0
- package/dist/setProxy/SubjectSetProxy.d.ts +15 -0
- package/dist/setProxy/SubjectSetProxy.js +38 -0
- package/dist/setProxy/SubjectSetProxy.js.map +1 -0
- package/dist/setProxy/WildcardObjectSetProxy.d.ts +20 -0
- package/dist/setProxy/WildcardObjectSetProxy.js +49 -0
- package/dist/setProxy/WildcardObjectSetProxy.js.map +1 -0
- package/dist/setProxy/WildcardSubjectSetProxy.d.ts +19 -0
- package/dist/setProxy/WildcardSubjectSetProxy.js +31 -0
- package/dist/setProxy/WildcardSubjectSetProxy.js.map +1 -0
- package/dist/setProxy/createNewSetProxy.d.ts +5 -0
- package/dist/setProxy/createNewSetProxy.js +27 -0
- package/dist/setProxy/createNewSetProxy.js.map +1 -0
- package/dist/setProxy/isSetProxy.d.ts +2 -0
- package/dist/setProxy/isSetProxy.js +13 -0
- package/dist/setProxy/isSetProxy.js.map +1 -0
- package/dist/setProxy/ldSet/BasicLdSet.d.ts +36 -0
- package/dist/setProxy/ldSet/BasicLdSet.js +193 -0
- package/dist/setProxy/ldSet/BasicLdSet.js.map +1 -0
- package/dist/setProxy/ldSet/LdSet.d.ts +30 -0
- package/dist/setProxy/ldSet/LdSet.js +3 -0
- package/dist/setProxy/ldSet/LdSet.js.map +1 -0
- package/dist/setProxy/set.d.ts +2 -0
- package/dist/setProxy/set.js +9 -0
- package/dist/setProxy/set.js.map +1 -0
- package/dist/subjectProxy/SubjectProxy.d.ts +14 -0
- package/dist/subjectProxy/SubjectProxy.js +3 -0
- package/dist/subjectProxy/SubjectProxy.js.map +1 -0
- package/dist/subjectProxy/createSubjectHandler.d.ts +6 -0
- package/dist/subjectProxy/createSubjectHandler.js +75 -0
- package/dist/subjectProxy/createSubjectHandler.js.map +1 -0
- package/dist/subjectProxy/deleteFromDataset.d.ts +3 -0
- package/dist/subjectProxy/deleteFromDataset.js +25 -0
- package/dist/subjectProxy/deleteFromDataset.js.map +1 -0
- package/dist/subjectProxy/getValueForKey.d.ts +5 -0
- package/dist/subjectProxy/getValueForKey.js +41 -0
- package/dist/subjectProxy/getValueForKey.js.map +1 -0
- package/dist/subjectProxy/isSubjectProxy.d.ts +4 -0
- package/dist/subjectProxy/isSubjectProxy.js +25 -0
- package/dist/subjectProxy/isSubjectProxy.js.map +1 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +11 -0
- package/dist/types.js.map +1 -0
- package/dist/util/NodeSet.d.ts +9 -0
- package/dist/util/NodeSet.js +35 -0
- package/dist/util/NodeSet.js.map +1 -0
- package/dist/util/RawObject.d.ts +9 -0
- package/dist/util/RawObject.js +3 -0
- package/dist/util/RawObject.js.map +1 -0
- package/dist/util/addObjectToDataset.d.ts +8 -0
- package/dist/util/addObjectToDataset.js +83 -0
- package/dist/util/addObjectToDataset.js.map +1 -0
- package/dist/util/createInteractOptions.d.ts +6 -0
- package/dist/util/createInteractOptions.js +37 -0
- package/dist/util/createInteractOptions.js.map +1 -0
- package/dist/util/getNodeFromRaw.d.ts +6 -0
- package/dist/util/getNodeFromRaw.js +47 -0
- package/dist/util/getNodeFromRaw.js.map +1 -0
- package/dist/util/isProxy.d.ts +6 -0
- package/dist/util/isProxy.js +17 -0
- package/dist/util/isProxy.js.map +1 -0
- package/dist/util/nodeToJsonldRepresentation.d.ts +7 -0
- package/dist/util/nodeToJsonldRepresentation.js +75 -0
- package/dist/util/nodeToJsonldRepresentation.js.map +1 -0
- package/dist/write.d.ts +3 -0
- package/dist/write.js +9 -0
- package/dist/write.js.map +1 -0
- package/package.json +53 -0
- package/src/ContextUtil.ts +173 -0
- package/src/JsonldDatasetProxyBuilder.ts +107 -0
- package/src/LdoJsonldContext.ts +14 -0
- package/src/ProxyContext.ts +118 -0
- package/src/graphOf.ts +52 -0
- package/src/index.ts +39 -0
- package/src/jsonldDatasetProxy.ts +28 -0
- package/src/language/languageMapProxy.ts +75 -0
- package/src/language/languageSet.ts +129 -0
- package/src/language/languageTypes.ts +3 -0
- package/src/language/languageUtils.ts +159 -0
- package/src/language/languagesOf.ts +65 -0
- package/src/setLanguagePreferences.ts +14 -0
- package/src/setProxy/ObjectSetProxy.ts +66 -0
- package/src/setProxy/SetProxy.ts +141 -0
- package/src/setProxy/SubjectSetProxy.ts +85 -0
- package/src/setProxy/WildcardObjectSetProxy.ts +89 -0
- package/src/setProxy/WildcardSubjectSetProxy.ts +58 -0
- package/src/setProxy/createNewSetProxy.ts +47 -0
- package/src/setProxy/isSetProxy.ts +7 -0
- package/src/setProxy/ldSet/BasicLdSet.ts +266 -0
- package/src/setProxy/ldSet/LdSet.ts +189 -0
- package/src/setProxy/set.ts +14 -0
- package/src/subjectProxy/SubjectProxy.ts +20 -0
- package/src/subjectProxy/createSubjectHandler.ts +115 -0
- package/src/subjectProxy/deleteFromDataset.ts +33 -0
- package/src/subjectProxy/getValueForKey.ts +62 -0
- package/src/subjectProxy/isSubjectProxy.ts +30 -0
- package/src/types.ts +11 -0
- package/src/util/NodeSet.ts +35 -0
- package/src/util/RawObject.ts +21 -0
- package/src/util/addObjectToDataset.ts +147 -0
- package/src/util/createInteractOptions.ts +55 -0
- package/src/util/getNodeFromRaw.ts +54 -0
- package/src/util/isProxy.ts +21 -0
- package/src/util/nodeToJsonldRepresentation.ts +78 -0
- package/src/write.ts +12 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { SubjectProxyTarget } from "./createSubjectHandler";
|
|
2
|
+
import { namedNode } from "@rdfjs/data-model";
|
|
3
|
+
import { nodeToJsonldRepresentation } from "../util/nodeToJsonldRepresentation";
|
|
4
|
+
import type { SubjectProxy } from "./SubjectProxy";
|
|
5
|
+
import type { SetProxy } from "../setProxy/SetProxy";
|
|
6
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
7
|
+
import { filterQuadsByLanguageOrdering } from "../language/languageUtils";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Given a subject target and a key return the correct value
|
|
11
|
+
*/
|
|
12
|
+
export function getValueForKey(
|
|
13
|
+
target: SubjectProxyTarget,
|
|
14
|
+
key: string | symbol,
|
|
15
|
+
proxyContext: ProxyContext,
|
|
16
|
+
): SubjectProxy | SetProxy | string | number | boolean | undefined {
|
|
17
|
+
const { contextUtil, dataset } = proxyContext;
|
|
18
|
+
if (key === "@id") {
|
|
19
|
+
if (target["@id"].termType === "BlankNode") {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
// Purposly don't provide a typeName because we don't want to use the nested
|
|
23
|
+
// context
|
|
24
|
+
return contextUtil.iriToKey(target["@id"].value, []);
|
|
25
|
+
}
|
|
26
|
+
if (key === "toString" || key === Symbol.toStringTag) {
|
|
27
|
+
// TODO: this toString method right now returns [object Object],
|
|
28
|
+
// which is correct, but it could be more descriptive, especially
|
|
29
|
+
// because console.log doesn't return anyting helpful due to the proxy.
|
|
30
|
+
return Reflect.get(target, "toString");
|
|
31
|
+
}
|
|
32
|
+
if (typeof key === "symbol") {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const subject = target["@id"];
|
|
36
|
+
const rdfType = proxyContext.getRdfType(subject);
|
|
37
|
+
const predicate = namedNode(contextUtil.keyToIri(key, rdfType));
|
|
38
|
+
if (contextUtil.isSet(key, rdfType)) {
|
|
39
|
+
const arrayProxy = proxyContext.createSetProxy(
|
|
40
|
+
[subject, predicate, null, null],
|
|
41
|
+
false,
|
|
42
|
+
contextUtil.isLangString(key, rdfType),
|
|
43
|
+
);
|
|
44
|
+
return arrayProxy;
|
|
45
|
+
}
|
|
46
|
+
let objectDataset = dataset.match(subject, predicate);
|
|
47
|
+
if (contextUtil.isLangString(key, rdfType)) {
|
|
48
|
+
objectDataset = filterQuadsByLanguageOrdering(
|
|
49
|
+
objectDataset,
|
|
50
|
+
proxyContext.languageOrdering,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (objectDataset.size === 0) {
|
|
54
|
+
return undefined;
|
|
55
|
+
} else {
|
|
56
|
+
const thing = nodeToJsonldRepresentation(
|
|
57
|
+
objectDataset.toArray()[0].object,
|
|
58
|
+
proxyContext,
|
|
59
|
+
);
|
|
60
|
+
return thing;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ObjectLike } from "../types";
|
|
2
|
+
import {
|
|
3
|
+
_getUnderlyingDataset,
|
|
4
|
+
_getUnderlyingNode,
|
|
5
|
+
_proxyContext,
|
|
6
|
+
_writeGraphs,
|
|
7
|
+
} from "../types";
|
|
8
|
+
import type { SubjectProxy } from "./SubjectProxy";
|
|
9
|
+
|
|
10
|
+
export function isSubjectProxy(
|
|
11
|
+
someObject?: unknown,
|
|
12
|
+
): someObject is SubjectProxy {
|
|
13
|
+
if (!someObject) return false;
|
|
14
|
+
if (typeof someObject !== "object") return false;
|
|
15
|
+
const potentialSubjectProxy = someObject as SubjectProxy;
|
|
16
|
+
return !(
|
|
17
|
+
typeof potentialSubjectProxy[_writeGraphs] !== "object" ||
|
|
18
|
+
typeof potentialSubjectProxy[_getUnderlyingDataset] !== "object" ||
|
|
19
|
+
typeof potentialSubjectProxy[_getUnderlyingNode] !== "object" ||
|
|
20
|
+
typeof potentialSubjectProxy[_proxyContext] !== "object"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getSubjectProxyFromObject(object: ObjectLike): SubjectProxy {
|
|
25
|
+
const potentialSubjectProxy = object as SubjectProxy;
|
|
26
|
+
if (!isSubjectProxy(potentialSubjectProxy)) {
|
|
27
|
+
throw new Error(`${object} is not a Jsonld Dataset Proxy Subject`);
|
|
28
|
+
}
|
|
29
|
+
return potentialSubjectProxy;
|
|
30
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const _getUnderlyingNode = Symbol("_getUnderlyingNode");
|
|
2
|
+
export const _getUnderlyingMatch = Symbol("_getUnderlyingMatch");
|
|
3
|
+
export const _isSubjectOriented = Symbol("_isSubjectOriented");
|
|
4
|
+
export const _isLangString = Symbol("_isLangString");
|
|
5
|
+
export const _getUnderlyingDataset = Symbol("_getUnderlyingDataset");
|
|
6
|
+
export const _proxyContext = Symbol("_proxyContext");
|
|
7
|
+
export const _writeGraphs = Symbol("_writeGraphs");
|
|
8
|
+
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
export type ObjectLike = Record<string | number | symbol, any>;
|
|
11
|
+
export type LiteralLike = string | number | boolean;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { BlankNode, DefaultGraph, Literal, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import type { ObjectNode } from "@ldo/rdf-utils";
|
|
3
|
+
|
|
4
|
+
export function nodeToString(
|
|
5
|
+
node: NamedNode | BlankNode | DefaultGraph | Literal | null | undefined,
|
|
6
|
+
): string {
|
|
7
|
+
if (node == null) {
|
|
8
|
+
return "null";
|
|
9
|
+
}
|
|
10
|
+
switch (node.termType) {
|
|
11
|
+
case "NamedNode":
|
|
12
|
+
return `namedNode(${node.value})`;
|
|
13
|
+
case "BlankNode":
|
|
14
|
+
return `blankNode(${node.value})`;
|
|
15
|
+
case "Literal":
|
|
16
|
+
return `literal(${node.value},${node.datatype.value})`;
|
|
17
|
+
case "DefaultGraph":
|
|
18
|
+
return "defaultGraph()";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class NodeSet {
|
|
23
|
+
private set: Set<string> = new Set();
|
|
24
|
+
private map: Record<string, ObjectNode> = {};
|
|
25
|
+
|
|
26
|
+
add(node: ObjectNode) {
|
|
27
|
+
const key = nodeToString(node);
|
|
28
|
+
this.set.add(key);
|
|
29
|
+
this.map[key] = node;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
has(node: ObjectNode): boolean {
|
|
33
|
+
return this.set.has(nodeToString(node));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import { _getUnderlyingNode } from "../types";
|
|
3
|
+
import type { LdSet } from "../setProxy/ldSet/LdSet";
|
|
4
|
+
import type { SubjectProxy } from "../subjectProxy/SubjectProxy";
|
|
5
|
+
|
|
6
|
+
export type RawObject =
|
|
7
|
+
| ({
|
|
8
|
+
"@id"?: string | NamedNode | BlankNode;
|
|
9
|
+
} & {
|
|
10
|
+
[key: string | symbol | number]: RawValue | LdSet<RawValue>;
|
|
11
|
+
})
|
|
12
|
+
| SubjectProxy;
|
|
13
|
+
|
|
14
|
+
export type RawValue =
|
|
15
|
+
| string
|
|
16
|
+
| boolean
|
|
17
|
+
| number
|
|
18
|
+
| RawObject
|
|
19
|
+
| NamedNode
|
|
20
|
+
| BlankNode
|
|
21
|
+
| undefined;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import { literal, namedNode, quad } from "@rdfjs/data-model";
|
|
3
|
+
import { _getUnderlyingNode } from "../types";
|
|
4
|
+
import type { SubjectProxy } from "../subjectProxy/SubjectProxy";
|
|
5
|
+
import { getNodeFromRawObject, getNodeFromRawValue } from "./getNodeFromRaw";
|
|
6
|
+
import type { RawObject, RawValue } from "./RawObject";
|
|
7
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
8
|
+
import { isSubjectProxy } from "../subjectProxy/isSubjectProxy";
|
|
9
|
+
import { NodeSet } from "./NodeSet";
|
|
10
|
+
import {
|
|
11
|
+
getLanguageKeyForWriteOperation,
|
|
12
|
+
languageDeleteMatch,
|
|
13
|
+
languageKeyToLiteralLanguage,
|
|
14
|
+
} from "../language/languageUtils";
|
|
15
|
+
import { BasicLdSet } from "../setProxy/ldSet/BasicLdSet";
|
|
16
|
+
|
|
17
|
+
export function addRawValueToDatasetRecursive(
|
|
18
|
+
subject: NamedNode | BlankNode,
|
|
19
|
+
key: string,
|
|
20
|
+
value: RawValue,
|
|
21
|
+
visitedObjects: NodeSet,
|
|
22
|
+
shouldDeleteOldTriples: boolean,
|
|
23
|
+
proxyContext: ProxyContext,
|
|
24
|
+
): void {
|
|
25
|
+
const { dataset, contextUtil } = proxyContext;
|
|
26
|
+
const rdfType = proxyContext.getRdfType(subject);
|
|
27
|
+
const predicate = namedNode(contextUtil.keyToIri(key, rdfType));
|
|
28
|
+
// Get the Object Node
|
|
29
|
+
const object = getNodeFromRawValue(
|
|
30
|
+
value,
|
|
31
|
+
proxyContext,
|
|
32
|
+
contextUtil.getDataType(key, rdfType),
|
|
33
|
+
);
|
|
34
|
+
if (object == undefined) {
|
|
35
|
+
dataset.deleteMatches(subject, predicate);
|
|
36
|
+
} else if (object.termType === "Literal") {
|
|
37
|
+
let languageAppliedObject = object;
|
|
38
|
+
// Handle language use case
|
|
39
|
+
if (contextUtil.isLangString(key, rdfType)) {
|
|
40
|
+
const languageKey = getLanguageKeyForWriteOperation(
|
|
41
|
+
proxyContext.languageOrdering,
|
|
42
|
+
);
|
|
43
|
+
if (!languageKey) return;
|
|
44
|
+
languageAppliedObject = literal(
|
|
45
|
+
object.value,
|
|
46
|
+
languageKeyToLiteralLanguage(languageKey),
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
proxyContext.writeGraphs.forEach((graph) => {
|
|
50
|
+
proxyContext.dataset.add(
|
|
51
|
+
quad(subject, predicate, languageAppliedObject, graph),
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
} else {
|
|
55
|
+
// Delete any triples if the id is the same
|
|
56
|
+
// if (!visitedObjects.has(object) && !isSubjectProxy(value)) {
|
|
57
|
+
// console.log("deleting 2", object.value);
|
|
58
|
+
// dataset.deleteMatches(object, undefined, undefined);
|
|
59
|
+
// }
|
|
60
|
+
proxyContext.writeGraphs.forEach((graph) => {
|
|
61
|
+
dataset.add(quad(subject, predicate, object, graph));
|
|
62
|
+
});
|
|
63
|
+
if (!isSubjectProxy(value)) {
|
|
64
|
+
const updateData: RawObject = (
|
|
65
|
+
typeof value === "object"
|
|
66
|
+
? { ...value, "@id": object }
|
|
67
|
+
: { "@id": object }
|
|
68
|
+
) as RawObject;
|
|
69
|
+
addRawObjectToDatasetRecursive(
|
|
70
|
+
updateData,
|
|
71
|
+
visitedObjects,
|
|
72
|
+
shouldDeleteOldTriples,
|
|
73
|
+
proxyContext,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function addRawObjectToDatasetRecursive(
|
|
80
|
+
item: RawObject,
|
|
81
|
+
visitedObjects: NodeSet,
|
|
82
|
+
shouldDeleteOldTriples: boolean,
|
|
83
|
+
proxyContext: ProxyContext,
|
|
84
|
+
): SubjectProxy {
|
|
85
|
+
const { dataset } = proxyContext;
|
|
86
|
+
const subject = getNodeFromRawObject(item, proxyContext.contextUtil);
|
|
87
|
+
const rdfType = proxyContext.getRdfType(subject);
|
|
88
|
+
if (visitedObjects.has(subject)) {
|
|
89
|
+
return proxyContext.createSubjectProxy(subject);
|
|
90
|
+
}
|
|
91
|
+
visitedObjects.add(subject);
|
|
92
|
+
Object.entries(item).forEach(([key, value]) => {
|
|
93
|
+
if (key === "@id") {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const predicate = namedNode(
|
|
97
|
+
proxyContext.contextUtil.keyToIri(key, rdfType),
|
|
98
|
+
);
|
|
99
|
+
if (shouldDeleteOldTriples) {
|
|
100
|
+
if (proxyContext.contextUtil.isLangString(key, rdfType)) {
|
|
101
|
+
const languageKey = getLanguageKeyForWriteOperation(
|
|
102
|
+
proxyContext.languageOrdering,
|
|
103
|
+
);
|
|
104
|
+
if (languageKey) {
|
|
105
|
+
languageDeleteMatch(dataset, subject, predicate, languageKey);
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
dataset.deleteMatches(subject, predicate);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (value instanceof BasicLdSet) {
|
|
112
|
+
value.forEach((valueItem) => {
|
|
113
|
+
addRawValueToDatasetRecursive(
|
|
114
|
+
subject,
|
|
115
|
+
key,
|
|
116
|
+
valueItem,
|
|
117
|
+
visitedObjects,
|
|
118
|
+
true,
|
|
119
|
+
proxyContext,
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
} else {
|
|
123
|
+
addRawValueToDatasetRecursive(
|
|
124
|
+
subject,
|
|
125
|
+
key,
|
|
126
|
+
value as RawValue,
|
|
127
|
+
visitedObjects,
|
|
128
|
+
true,
|
|
129
|
+
proxyContext,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
return proxyContext.createSubjectProxy(subject);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function addObjectToDataset(
|
|
137
|
+
item: RawObject,
|
|
138
|
+
shouldDeleteOldTriples: boolean,
|
|
139
|
+
proxyContext: ProxyContext,
|
|
140
|
+
): SubjectProxy {
|
|
141
|
+
return addRawObjectToDatasetRecursive(
|
|
142
|
+
item,
|
|
143
|
+
new NodeSet(),
|
|
144
|
+
shouldDeleteOldTriples,
|
|
145
|
+
proxyContext,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { getSubjectProxyFromObject } from "../subjectProxy/isSubjectProxy";
|
|
2
|
+
import type { ObjectLike } from "../types";
|
|
3
|
+
import { _getUnderlyingNode, _proxyContext } from "../types";
|
|
4
|
+
import { getProxyFromObject } from "./isProxy";
|
|
5
|
+
|
|
6
|
+
export interface InteractOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Given a dataset proxy, this will set the action on that dataset proxy
|
|
9
|
+
* @param objects Any number of dataset proxies
|
|
10
|
+
* @returns An end function. Call this if to reset the interaction
|
|
11
|
+
*/
|
|
12
|
+
using(...objects: ObjectLike[]): () => void;
|
|
13
|
+
/**
|
|
14
|
+
* Given a dataset proxy this will copy the dataset proxy and set the action
|
|
15
|
+
* on the copy
|
|
16
|
+
* @param objects Any number of dataset proxies
|
|
17
|
+
* @returns cloned dataset proxies
|
|
18
|
+
*/
|
|
19
|
+
usingCopy<T extends ObjectLike>(...objects: T[]): T[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createInteractOptions(
|
|
23
|
+
paramKey: string,
|
|
24
|
+
parameter: unknown,
|
|
25
|
+
): InteractOptions {
|
|
26
|
+
return {
|
|
27
|
+
using(...objects: ObjectLike[]): () => void {
|
|
28
|
+
const onEndFunctions: (() => void)[] = [];
|
|
29
|
+
objects.forEach((object) => {
|
|
30
|
+
const proxy = getProxyFromObject(object);
|
|
31
|
+
const oldProxyContext = proxy[_proxyContext];
|
|
32
|
+
proxy[_proxyContext] = proxy[_proxyContext].duplicate({
|
|
33
|
+
[paramKey]: parameter,
|
|
34
|
+
});
|
|
35
|
+
onEndFunctions.push(() => {
|
|
36
|
+
proxy[_proxyContext] = oldProxyContext;
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
return function endWrite() {
|
|
40
|
+
onEndFunctions.forEach((func) => func());
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
usingCopy<T extends ObjectLike>(...objects: T[]): T[] {
|
|
44
|
+
return objects.map((object) => {
|
|
45
|
+
const proxy = getSubjectProxyFromObject(object);
|
|
46
|
+
const newProxyContext = proxy[_proxyContext].duplicate({
|
|
47
|
+
[paramKey]: parameter,
|
|
48
|
+
});
|
|
49
|
+
return newProxyContext.createSubjectProxy(
|
|
50
|
+
proxy[_getUnderlyingNode],
|
|
51
|
+
) as unknown as T;
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { BlankNode, Literal, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import { namedNode, literal, blankNode } from "@rdfjs/data-model";
|
|
3
|
+
import type { ContextUtil } from "../ContextUtil";
|
|
4
|
+
import { _getUnderlyingNode } from "../types";
|
|
5
|
+
import type { RawObject, RawValue } from "./RawObject";
|
|
6
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
7
|
+
|
|
8
|
+
export function getNodeFromRawObject(
|
|
9
|
+
item: RawObject,
|
|
10
|
+
contextUtil: ContextUtil,
|
|
11
|
+
): NamedNode | BlankNode {
|
|
12
|
+
if (item[_getUnderlyingNode]) {
|
|
13
|
+
return item[_getUnderlyingNode] as NamedNode | BlankNode;
|
|
14
|
+
} else if (!item["@id"]) {
|
|
15
|
+
return blankNode();
|
|
16
|
+
} else if (typeof item["@id"] === "string") {
|
|
17
|
+
// Purposly do not include typeName because we don't want to reference
|
|
18
|
+
// nested context
|
|
19
|
+
return namedNode(contextUtil.keyToIri(item["@id"], []));
|
|
20
|
+
} else {
|
|
21
|
+
return item["@id"];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getNodeFromRawValue(
|
|
26
|
+
value: RawValue,
|
|
27
|
+
proxyContext: ProxyContext,
|
|
28
|
+
// To get this run proxyContext.contextUtil.getDataType(key, proxyContext.getRdfType(subjectNode))
|
|
29
|
+
datatype?: string,
|
|
30
|
+
): BlankNode | NamedNode | Literal | undefined {
|
|
31
|
+
// Get the Object Node
|
|
32
|
+
if (value == undefined) {
|
|
33
|
+
return undefined;
|
|
34
|
+
} else if (
|
|
35
|
+
typeof value === "string" ||
|
|
36
|
+
typeof value === "boolean" ||
|
|
37
|
+
typeof value === "number"
|
|
38
|
+
) {
|
|
39
|
+
if (!datatype) {
|
|
40
|
+
return undefined;
|
|
41
|
+
} else if (datatype === "@id") {
|
|
42
|
+
return namedNode(value.toString());
|
|
43
|
+
} else {
|
|
44
|
+
return literal(value.toString(), datatype);
|
|
45
|
+
}
|
|
46
|
+
} else if (
|
|
47
|
+
typeof value.termType === "string" &&
|
|
48
|
+
(value.termType === "NamedNode" || value.termType === "BlankNode")
|
|
49
|
+
) {
|
|
50
|
+
return value as NamedNode | BlankNode;
|
|
51
|
+
} else {
|
|
52
|
+
return getNodeFromRawObject(value as RawObject, proxyContext.contextUtil);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { isSetProxy } from "../setProxy/isSetProxy";
|
|
2
|
+
import type { LdSet } from "../setProxy/ldSet/LdSet";
|
|
3
|
+
import type { SetProxy } from "../setProxy/SetProxy";
|
|
4
|
+
import { isSubjectProxy } from "../subjectProxy/isSubjectProxy";
|
|
5
|
+
import type { SubjectProxy } from "../subjectProxy/SubjectProxy";
|
|
6
|
+
import type { ObjectLike } from "../types";
|
|
7
|
+
|
|
8
|
+
export function isProxy(
|
|
9
|
+
someObject?: unknown,
|
|
10
|
+
): someObject is SetProxy | SubjectProxy {
|
|
11
|
+
return isSubjectProxy(someObject) || isSetProxy(someObject);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function getProxyFromObject(
|
|
15
|
+
object: ObjectLike | LdSet<ObjectLike>,
|
|
16
|
+
): SubjectProxy | SetProxy {
|
|
17
|
+
if (!isProxy(object)) {
|
|
18
|
+
throw new Error(`${object} is not a Jsonld Dataset Proxy`);
|
|
19
|
+
}
|
|
20
|
+
return object;
|
|
21
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { Literal, Quad_Object } from "@rdfjs/types";
|
|
2
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
3
|
+
import type { SubjectProxy } from "../subjectProxy/SubjectProxy";
|
|
4
|
+
import type { LiteralLike } from "../types";
|
|
5
|
+
|
|
6
|
+
export type ObjectJsonRepresentation = LiteralLike | SubjectProxy;
|
|
7
|
+
|
|
8
|
+
export function literalToJsonldRepresentation(literal: Literal) {
|
|
9
|
+
switch (literal.datatype.value) {
|
|
10
|
+
case "http://www.w3.org/2001/XMLSchema#string":
|
|
11
|
+
case "http://www.w3.org/2001/XMLSchema#ENTITIES":
|
|
12
|
+
case "http://www.w3.org/2001/XMLSchema#ENTITY":
|
|
13
|
+
case "http://www.w3.org/2001/XMLSchema#ID":
|
|
14
|
+
case "http://www.w3.org/2001/XMLSchema#IDREF":
|
|
15
|
+
case "http://www.w3.org/2001/XMLSchema#IDREFS":
|
|
16
|
+
case "http://www.w3.org/2001/XMLSchema#language":
|
|
17
|
+
case "http://www.w3.org/2001/XMLSchema#Name":
|
|
18
|
+
case "http://www.w3.org/2001/XMLSchema#NCName":
|
|
19
|
+
case "http://www.w3.org/2001/XMLSchema#NMTOKEN":
|
|
20
|
+
case "http://www.w3.org/2001/XMLSchema#NMTOKENS":
|
|
21
|
+
case "http://www.w3.org/2001/XMLSchema#normalizedString":
|
|
22
|
+
case "http://www.w3.org/2001/XMLSchema#QName":
|
|
23
|
+
case "http://www.w3.org/2001/XMLSchema#token":
|
|
24
|
+
return literal.value;
|
|
25
|
+
case "http://www.w3.org/2001/XMLSchema#date":
|
|
26
|
+
case "http://www.w3.org/2001/XMLSchema#dateTime":
|
|
27
|
+
case "http://www.w3.org/2001/XMLSchema#duration":
|
|
28
|
+
case "http://www.w3.org/2001/XMLSchema#gDay":
|
|
29
|
+
case "http://www.w3.org/2001/XMLSchema#gMonth":
|
|
30
|
+
case "http://www.w3.org/2001/XMLSchema#gMonthDay":
|
|
31
|
+
case "http://www.w3.org/2001/XMLSchema#gYear":
|
|
32
|
+
case "http://www.w3.org/2001/XMLSchema#gYearMonth":
|
|
33
|
+
case "http://www.w3.org/2001/XMLSchema#time":
|
|
34
|
+
return literal.value;
|
|
35
|
+
case "http://www.w3.org/2001/XMLSchema#integer":
|
|
36
|
+
case "http://www.w3.org/2001/XMLSchema#byte":
|
|
37
|
+
case "http://www.w3.org/2001/XMLSchema#decimal":
|
|
38
|
+
case "http://www.w3.org/2001/XMLSchema#double":
|
|
39
|
+
case "http://www.w3.org/2001/XMLSchema#int":
|
|
40
|
+
case "http://www.w3.org/2001/XMLSchema#long":
|
|
41
|
+
case "http://www.w3.org/2001/XMLSchema#negativeInteger":
|
|
42
|
+
case "http://www.w3.org/2001/XMLSchema#nonNegativeInteger":
|
|
43
|
+
case "http://www.w3.org/2001/XMLSchema#nonPositiveInteger":
|
|
44
|
+
case "http://www.w3.org/2001/XMLSchema#positiveInteger":
|
|
45
|
+
case "http://www.w3.org/2001/XMLSchema#short":
|
|
46
|
+
case "http://www.w3.org/2001/XMLSchema#unsignedLong":
|
|
47
|
+
case "http://www.w3.org/2001/XMLSchema#unsignedInt":
|
|
48
|
+
case "http://www.w3.org/2001/XMLSchema#unsignedShort":
|
|
49
|
+
case "http://www.w3.org/2001/XMLSchema#unsignedByte":
|
|
50
|
+
return parseFloat(literal.value);
|
|
51
|
+
case "http://www.w3.org/2001/XMLSchema#boolean":
|
|
52
|
+
return literal.value === "true";
|
|
53
|
+
case "http://www.w3.org/2001/XMLSchema#hexBinary":
|
|
54
|
+
return literal.value;
|
|
55
|
+
case "http://www.w3.org/2001/XMLSchema#anyURI":
|
|
56
|
+
return literal.value;
|
|
57
|
+
case "http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML":
|
|
58
|
+
case "http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral":
|
|
59
|
+
case "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral":
|
|
60
|
+
case "http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON":
|
|
61
|
+
return literal.value;
|
|
62
|
+
default:
|
|
63
|
+
return literal.value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function nodeToJsonldRepresentation(
|
|
68
|
+
node: Quad_Object,
|
|
69
|
+
proxyContext: ProxyContext,
|
|
70
|
+
): ObjectJsonRepresentation {
|
|
71
|
+
if (node.termType === "Literal") {
|
|
72
|
+
return literalToJsonldRepresentation(node);
|
|
73
|
+
} else if (node.termType === "NamedNode" || node.termType === "BlankNode") {
|
|
74
|
+
return proxyContext.createSubjectProxy(node);
|
|
75
|
+
} else {
|
|
76
|
+
throw new Error("Can only convert NamedNodes or Literals or BlankNodes");
|
|
77
|
+
}
|
|
78
|
+
}
|
package/src/write.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GraphNode } from "@ldo/rdf-utils";
|
|
2
|
+
import type { InteractOptions } from "./util/createInteractOptions";
|
|
3
|
+
import { createInteractOptions } from "./util/createInteractOptions";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Set the graphs that should be written to
|
|
7
|
+
* @param graphs The graphs that should be written to
|
|
8
|
+
* @returns a write builder
|
|
9
|
+
*/
|
|
10
|
+
export function write(...graphs: GraphNode[]): InteractOptions {
|
|
11
|
+
return createInteractOptions("writeGraphs", graphs);
|
|
12
|
+
}
|