ldkit 1.1.0 → 2.0.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/README.md +5 -5
- package/esm/library/decoder.js +20 -30
- package/esm/library/encoder.js +33 -17
- package/esm/library/engine/mod.js +2 -2
- package/esm/library/engine/query_engine.js +40 -0
- package/esm/library/engine/query_engine_proxy.js +8 -8
- package/esm/library/engine/types.js +1 -0
- package/esm/library/lens/lens.js +414 -25
- package/esm/library/lens/mod.js +1 -1
- package/esm/library/lens/query_builder.js +81 -50
- package/esm/library/lens/search_helper.js +142 -0
- package/esm/library/lens/update_helper.js +157 -0
- package/esm/library/namespace.js +36 -0
- package/esm/library/options.js +50 -0
- package/esm/library/schema/data_types.js +3 -38
- package/esm/library/schema/search.js +1 -0
- package/esm/library/schema/utils.js +31 -6
- package/esm/library/sparql/mod.js +2 -1
- package/esm/library/sparql/sparql_expression_builders.js +18 -0
- package/esm/library/sparql/sparql_query_builders.js +65 -0
- package/esm/library/sparql/sparql_shared_builders.js +1 -0
- package/esm/library/sparql/sparql_tag.js +27 -1
- package/esm/library/sparql/sparql_update_builders.js +70 -0
- package/esm/library/sparql/stringify.js +3 -4
- package/esm/mod.js +4 -4
- package/esm/{library/namespaces → namespaces}/dbo.js +7 -2
- package/esm/{library/namespaces → namespaces}/dc.js +7 -2
- package/esm/{library/namespaces → namespaces}/dcterms.js +7 -2
- package/esm/{library/namespaces → namespaces}/foaf.js +7 -2
- package/esm/{library/namespaces → namespaces}/gr.js +7 -2
- package/esm/namespaces/ldkit.js +11 -0
- package/esm/namespaces/owl.js +91 -0
- package/{script/library → esm}/namespaces/rdf.js +7 -4
- package/{script/library → esm}/namespaces/rdfs.js +7 -4
- package/esm/{library/namespaces → namespaces}/schema.js +7 -2
- package/esm/{library/namespaces → namespaces}/sioc.js +7 -2
- package/{script/library → esm}/namespaces/skos.js +7 -4
- package/{script/library → esm}/namespaces/xsd.js +7 -4
- package/esm/namespaces.js +30 -1
- package/esm/rdf.js +14 -1
- package/esm/sparql.js +13 -0
- package/package.json +2 -2
- package/script/library/decoder.js +25 -38
- package/script/library/encoder.js +38 -24
- package/script/library/engine/mod.js +16 -5
- package/script/library/engine/query_engine.js +40 -0
- package/script/library/engine/query_engine_proxy.js +7 -7
- package/script/library/engine/types.js +2 -0
- package/script/library/lens/lens.js +415 -27
- package/script/library/lens/mod.js +15 -4
- package/script/library/lens/query_builder.js +82 -54
- package/script/library/lens/search_helper.js +146 -0
- package/script/library/lens/update_helper.js +161 -0
- package/script/library/namespace.js +40 -0
- package/script/library/options.js +56 -0
- package/script/library/schema/data_types.js +3 -41
- package/script/library/schema/search.js +2 -0
- package/script/library/schema/utils.js +33 -11
- package/script/library/sparql/mod.js +17 -3
- package/script/library/sparql/sparql_expression_builders.js +22 -0
- package/script/library/sparql/sparql_query_builders.js +65 -0
- package/script/library/sparql/sparql_shared_builders.js +3 -1
- package/script/library/sparql/sparql_tag.js +30 -7
- package/script/library/sparql/sparql_update_builders.js +70 -0
- package/script/library/sparql/stringify.js +4 -8
- package/script/mod.js +20 -11
- package/script/{library/namespaces → namespaces}/dbo.js +8 -2
- package/script/{library/namespaces → namespaces}/dc.js +8 -2
- package/script/{library/namespaces → namespaces}/dcterms.js +8 -2
- package/script/{library/namespaces → namespaces}/foaf.js +8 -2
- package/script/{library/namespaces → namespaces}/gr.js +8 -2
- package/script/namespaces/ldkit.js +14 -0
- package/script/namespaces/owl.js +94 -0
- package/{esm/library → script}/namespaces/rdf.js +10 -2
- package/{esm/library → script}/namespaces/rdfs.js +10 -2
- package/script/{library/namespaces → namespaces}/schema.js +8 -2
- package/script/{library/namespaces → namespaces}/sioc.js +8 -2
- package/{esm/library → script}/namespaces/skos.js +10 -2
- package/{esm/library → script}/namespaces/xsd.js +10 -2
- package/script/namespaces.js +44 -1
- package/script/rdf.js +20 -15
- package/script/sparql.js +13 -0
- package/types/library/decoder.d.ts +4 -3
- package/types/library/encoder.d.ts +5 -3
- package/types/library/engine/mod.d.ts +2 -2
- package/types/library/engine/query_engine.d.ts +50 -9
- package/types/library/engine/query_engine_proxy.d.ts +4 -3
- package/types/library/engine/types.d.ts +23 -0
- package/types/library/lens/lens.d.ts +400 -23
- package/types/library/lens/mod.d.ts +1 -1
- package/types/library/lens/query_builder.d.ts +10 -10
- package/types/library/lens/search_helper.d.ts +21 -0
- package/types/library/lens/types.d.ts +5 -2
- package/types/library/lens/update_helper.d.ts +23 -0
- package/types/library/namespace.d.ts +41 -0
- package/types/library/options.d.ts +72 -0
- package/types/library/rdf.d.ts +8 -15
- package/types/library/schema/data_types.d.ts +44 -37
- package/types/library/schema/interface.d.ts +75 -20
- package/types/library/schema/mod.d.ts +4 -2
- package/types/library/schema/schema.d.ts +23 -16
- package/types/library/schema/search.d.ts +20 -0
- package/types/library/schema/utils.d.ts +3 -3
- package/types/library/sparql/mod.d.ts +2 -1
- package/types/library/sparql/sparql_expression_builders.d.ts +19 -0
- package/types/library/sparql/sparql_query_builders.d.ts +68 -3
- package/types/library/sparql/sparql_shared_builders.d.ts +1 -0
- package/types/library/sparql/sparql_tag.d.ts +29 -1
- package/types/library/sparql/sparql_update_builders.d.ts +68 -0
- package/types/mod.d.ts +5 -6
- package/types/namespaces/dbo.d.ts +10 -0
- package/types/namespaces/dc.d.ts +10 -0
- package/types/namespaces/dcterms.d.ts +10 -0
- package/types/namespaces/foaf.d.ts +10 -0
- package/types/namespaces/gr.d.ts +10 -0
- package/types/namespaces/ldkit.d.ts +10 -0
- package/types/namespaces/owl.d.ts +10 -0
- package/types/namespaces/rdf.d.ts +10 -0
- package/types/namespaces/rdfs.d.ts +10 -0
- package/types/namespaces/schema.d.ts +10 -0
- package/types/namespaces/sioc.d.ts +10 -0
- package/types/namespaces/skos.d.ts +10 -0
- package/types/namespaces/xsd.d.ts +10 -0
- package/types/namespaces.d.ts +30 -1
- package/types/rdf.d.ts +14 -1
- package/types/sparql.d.ts +13 -0
- package/esm/library/global.js +0 -25
- package/esm/library/lens/query_helper.js +0 -102
- package/esm/library/namespaces/ldkit.js +0 -6
- package/esm/library/namespaces/mod.js +0 -13
- package/esm/library/namespaces/namespace.js +0 -11
- package/script/library/global.js +0 -32
- package/script/library/lens/query_helper.js +0 -106
- package/script/library/namespaces/ldkit.js +0 -8
- package/script/library/namespaces/mod.js +0 -32
- package/script/library/namespaces/namespace.js +0 -15
- package/types/library/global.d.ts +0 -5
- package/types/library/lens/query_helper.d.ts +0 -19
- package/types/library/namespaces/dbo.d.ts +0 -3649
- package/types/library/namespaces/dc.d.ts +0 -21
- package/types/library/namespaces/dcterms.d.ts +0 -104
- package/types/library/namespaces/foaf.d.ts +0 -69
- package/types/library/namespaces/gr.d.ts +0 -175
- package/types/library/namespaces/ldkit.d.ts +0 -7
- package/types/library/namespaces/mod.d.ts +0 -13
- package/types/library/namespaces/namespace.d.ts +0 -15
- package/types/library/namespaces/rdf.d.ts +0 -28
- package/types/library/namespaces/rdfs.d.ts +0 -21
- package/types/library/namespaces/schema.d.ts +0 -2697
- package/types/library/namespaces/sioc.d.ts +0 -105
- package/types/library/namespaces/skos.d.ts +0 -38
- package/types/library/namespaces/xsd.d.ts +0 -56
|
@@ -1,25 +1,28 @@
|
|
|
1
|
-
import { getSchemaProperties } from "../schema/mod.js";
|
|
1
|
+
import { getSchemaProperties, } from "../schema/mod.js";
|
|
2
2
|
import { CONSTRUCT, DELETE, INSERT, SELECT, sparql as $, } from "../sparql/mod.js";
|
|
3
3
|
import { DataFactory } from "../rdf.js";
|
|
4
|
-
import ldkit from "
|
|
5
|
-
import rdf from "
|
|
4
|
+
import { ldkit } from "../../namespaces/ldkit.js";
|
|
5
|
+
import { rdf } from "../../namespaces/rdf.js";
|
|
6
6
|
import { encode } from "../encoder.js";
|
|
7
|
-
import {
|
|
7
|
+
import { UpdateHelper } from "./update_helper.js";
|
|
8
|
+
import { SearchHelper } from "./search_helper.js";
|
|
9
|
+
var Flags;
|
|
10
|
+
(function (Flags) {
|
|
11
|
+
Flags[Flags["None"] = 0] = "None";
|
|
12
|
+
Flags[Flags["ExcludeOptional"] = 1] = "ExcludeOptional";
|
|
13
|
+
Flags[Flags["UnwrapOptional"] = 2] = "UnwrapOptional";
|
|
14
|
+
Flags[Flags["IncludeTypes"] = 4] = "IncludeTypes";
|
|
15
|
+
Flags[Flags["IgnoreInverse"] = 8] = "IgnoreInverse";
|
|
16
|
+
})(Flags || (Flags = {}));
|
|
8
17
|
export class QueryBuilder {
|
|
9
|
-
constructor(schema,
|
|
18
|
+
constructor(schema, options) {
|
|
10
19
|
Object.defineProperty(this, "schema", {
|
|
11
20
|
enumerable: true,
|
|
12
21
|
configurable: true,
|
|
13
22
|
writable: true,
|
|
14
23
|
value: void 0
|
|
15
24
|
});
|
|
16
|
-
Object.defineProperty(this, "
|
|
17
|
-
enumerable: true,
|
|
18
|
-
configurable: true,
|
|
19
|
-
writable: true,
|
|
20
|
-
value: void 0
|
|
21
|
-
});
|
|
22
|
-
Object.defineProperty(this, "context", {
|
|
25
|
+
Object.defineProperty(this, "options", {
|
|
23
26
|
enumerable: true,
|
|
24
27
|
configurable: true,
|
|
25
28
|
writable: true,
|
|
@@ -45,24 +48,32 @@ export class QueryBuilder {
|
|
|
45
48
|
}
|
|
46
49
|
});
|
|
47
50
|
this.schema = schema;
|
|
48
|
-
this.
|
|
49
|
-
this.context = context;
|
|
51
|
+
this.options = options;
|
|
50
52
|
this.df = new DataFactory();
|
|
51
53
|
}
|
|
52
54
|
getResourceSignature() {
|
|
53
55
|
return this.df.quad(this.df.variable("iri"), this.df.namedNode(rdf.type), this.df.namedNode(ldkit.Resource));
|
|
54
56
|
}
|
|
55
|
-
getTypesSignature() {
|
|
56
|
-
return this.df.quad(this.df.variable("iri"), this.df.namedNode(rdf.type), this.df.variable("iri_type"));
|
|
57
|
-
}
|
|
58
57
|
entitiesToQuads(entities) {
|
|
59
|
-
const quadArrays = entities.map((entity) => encode(entity, this.schema, this.
|
|
58
|
+
const quadArrays = entities.map((entity) => encode(entity, this.schema, this.options));
|
|
60
59
|
return [].concat(...quadArrays);
|
|
61
60
|
}
|
|
62
|
-
getShape(
|
|
61
|
+
getShape(flags, searchSchema) {
|
|
62
|
+
const includeOptional = (flags & Flags.ExcludeOptional) === 0;
|
|
63
|
+
const wrapOptional = (flags & Flags.UnwrapOptional) === 0;
|
|
64
|
+
const omitRootTypes = (flags & Flags.IncludeTypes) === 0;
|
|
65
|
+
const ignoreInverse = (flags & Flags.IgnoreInverse) === Flags.IgnoreInverse;
|
|
63
66
|
const mainVar = "iri";
|
|
64
67
|
const conditions = [];
|
|
65
|
-
const
|
|
68
|
+
const populateSearchConditions = (property, varName, search) => {
|
|
69
|
+
if (search === undefined) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const helper = new SearchHelper(property, varName, search);
|
|
73
|
+
helper.process();
|
|
74
|
+
conditions.push(helper.sparqlValues);
|
|
75
|
+
};
|
|
76
|
+
const populateConditionsRecursive = (s, varPrefix, search) => {
|
|
66
77
|
const rdfType = s["@type"];
|
|
67
78
|
const properties = getSchemaProperties(s);
|
|
68
79
|
if (varPrefix !== "iri" || !omitRootTypes) {
|
|
@@ -73,59 +84,84 @@ export class QueryBuilder {
|
|
|
73
84
|
Object.keys(properties).forEach((prop, index) => {
|
|
74
85
|
const property = properties[prop];
|
|
75
86
|
const isOptional = property["@optional"];
|
|
76
|
-
|
|
87
|
+
const propertySchema = search?.[prop];
|
|
88
|
+
if (!includeOptional && isOptional && propertySchema === undefined) {
|
|
77
89
|
return;
|
|
78
90
|
}
|
|
79
91
|
if (wrapOptional && isOptional) {
|
|
80
92
|
conditions.push($ `\nOPTIONAL {`);
|
|
81
93
|
}
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
|
|
94
|
+
const isInverse = property["@inverse"];
|
|
95
|
+
if (ignoreInverse || !isInverse) {
|
|
96
|
+
conditions.push(this.df.quad(this.df.variable(varPrefix), this.df.namedNode(property["@id"]), this.df.variable(`${varPrefix}_${index}`)));
|
|
97
|
+
populateSearchConditions(property, `${varPrefix}_${index}`, propertySchema);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
conditions.push(this.df.quad(this.df.variable(`${varPrefix}_${index}`), this.df.namedNode(property["@id"]), this.df.variable(varPrefix)));
|
|
101
|
+
}
|
|
102
|
+
if (typeof property["@schema"] === "object") {
|
|
103
|
+
populateConditionsRecursive(property["@schema"], `${varPrefix}_${index}`, propertySchema);
|
|
85
104
|
}
|
|
86
105
|
if (wrapOptional && isOptional) {
|
|
87
106
|
conditions.push($ `\n}\n`);
|
|
88
107
|
}
|
|
89
108
|
});
|
|
90
109
|
};
|
|
91
|
-
populateConditionsRecursive(this.schema, mainVar);
|
|
110
|
+
populateConditionsRecursive(this.schema, mainVar, searchSchema);
|
|
92
111
|
return conditions;
|
|
93
112
|
}
|
|
94
113
|
countQuery() {
|
|
95
|
-
const quads = this.getShape();
|
|
114
|
+
const quads = this.getShape(Flags.ExcludeOptional | Flags.IncludeTypes);
|
|
96
115
|
return SELECT `(count(?iri) as ?count)`.WHERE `${quads}`.build();
|
|
97
116
|
}
|
|
98
|
-
getQuery(where, limit
|
|
99
|
-
const selectSubQuery = SELECT `
|
|
117
|
+
getQuery(where, limit, offset) {
|
|
118
|
+
const selectSubQuery = SELECT.DISTINCT `
|
|
100
119
|
${this.df.variable("iri")}
|
|
101
120
|
`.WHERE `
|
|
102
|
-
${this.getShape(
|
|
121
|
+
${this.getShape(Flags.ExcludeOptional | Flags.IncludeTypes)}
|
|
103
122
|
${where}
|
|
104
|
-
`.LIMIT(limit).build();
|
|
123
|
+
`.LIMIT(limit).OFFSET(offset).build();
|
|
124
|
+
const query = CONSTRUCT `
|
|
125
|
+
${this.getResourceSignature()}
|
|
126
|
+
${this.getShape(Flags.UnwrapOptional | Flags.IgnoreInverse)}
|
|
127
|
+
`.WHERE `
|
|
128
|
+
{
|
|
129
|
+
${selectSubQuery}
|
|
130
|
+
}
|
|
131
|
+
${this.getShape(Flags.None)}
|
|
132
|
+
`.build();
|
|
133
|
+
return query;
|
|
134
|
+
}
|
|
135
|
+
getSearchQuery(where, limit, offset) {
|
|
136
|
+
if (where.$id) {
|
|
137
|
+
const ids = Array.isArray(where.$id) ? where.$id : [where.$id];
|
|
138
|
+
return this.getByIrisQuery(ids, where);
|
|
139
|
+
}
|
|
140
|
+
const selectSubQuery = SELECT.DISTINCT `
|
|
141
|
+
${this.df.variable("iri")}
|
|
142
|
+
`.WHERE `
|
|
143
|
+
${this.getShape(Flags.ExcludeOptional | Flags.IncludeTypes, where)}
|
|
144
|
+
`.LIMIT(limit).OFFSET(offset).build();
|
|
105
145
|
const query = CONSTRUCT `
|
|
106
146
|
${this.getResourceSignature()}
|
|
107
|
-
${this.
|
|
108
|
-
${this.getShape(true, false, true)}
|
|
147
|
+
${this.getShape(Flags.UnwrapOptional | Flags.IgnoreInverse)}
|
|
109
148
|
`.WHERE `
|
|
110
|
-
${this.getTypesSignature()}
|
|
111
|
-
${this.getShape(true, true, true)}
|
|
112
149
|
{
|
|
113
150
|
${selectSubQuery}
|
|
114
151
|
}
|
|
152
|
+
${this.getShape(Flags.None, where)}
|
|
115
153
|
`.build();
|
|
116
154
|
return query;
|
|
117
155
|
}
|
|
118
|
-
getByIrisQuery(iris) {
|
|
156
|
+
getByIrisQuery(iris, where) {
|
|
119
157
|
const query = CONSTRUCT `
|
|
120
158
|
${this.getResourceSignature()}
|
|
121
|
-
${this.
|
|
122
|
-
${this.getShape(true, false, true)}
|
|
159
|
+
${this.getShape(Flags.UnwrapOptional | Flags.IgnoreInverse)}
|
|
123
160
|
`.WHERE `
|
|
124
|
-
${this.getTypesSignature()}
|
|
125
|
-
${this.getShape(true, true, true)}
|
|
126
161
|
VALUES ?iri {
|
|
127
162
|
${iris.map(this.df.namedNode)}
|
|
128
163
|
}
|
|
164
|
+
${this.getShape(Flags.IncludeTypes, where)}
|
|
129
165
|
`.build();
|
|
130
166
|
return query;
|
|
131
167
|
}
|
|
@@ -140,16 +176,11 @@ export class QueryBuilder {
|
|
|
140
176
|
return DELETE.DATA `${quads}`.build();
|
|
141
177
|
}
|
|
142
178
|
updateQuery(entities) {
|
|
143
|
-
const
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
insertQuads.push(...helper.getInsertQuads());
|
|
150
|
-
whereQuads.push(...helper.getWhereQuads());
|
|
151
|
-
});
|
|
152
|
-
return DELETE `${deleteQuads}`.INSERT `${insertQuads}`
|
|
153
|
-
.WHERE `${deleteQuads}`.build();
|
|
179
|
+
const helper = new UpdateHelper(this.schema, this.options);
|
|
180
|
+
for (const entity of entities) {
|
|
181
|
+
helper.process(entity);
|
|
182
|
+
}
|
|
183
|
+
return DELETE `${helper.deleteQuads}`.INSERT `${helper.insertQuads}`
|
|
184
|
+
.WHERE `${helper.whereQuads}`.build();
|
|
154
185
|
}
|
|
155
186
|
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { DataFactory } from "../rdf.js";
|
|
2
|
+
import { sparql as $ } from "../sparql/mod.js";
|
|
3
|
+
import { encodeValue } from "../encoder.js";
|
|
4
|
+
import { xsd } from "../../namespaces/xsd.js";
|
|
5
|
+
export class SearchHelper {
|
|
6
|
+
constructor(property, varName, searchSchema) {
|
|
7
|
+
Object.defineProperty(this, "property", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true,
|
|
11
|
+
value: void 0
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(this, "propertyType", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: void 0
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(this, "varName", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: void 0
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, "searchSchema", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: void 0
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(this, "df", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: new DataFactory({
|
|
36
|
+
blankNodePrefix: "b",
|
|
37
|
+
})
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(this, "sparqlValues", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: []
|
|
44
|
+
});
|
|
45
|
+
this.property = property;
|
|
46
|
+
this.propertyType = property["@type"] ? property["@type"] : xsd.string;
|
|
47
|
+
this.varName = varName;
|
|
48
|
+
this.searchSchema = this.isPlainObject(searchSchema)
|
|
49
|
+
? searchSchema
|
|
50
|
+
: { $equals: searchSchema };
|
|
51
|
+
}
|
|
52
|
+
process() {
|
|
53
|
+
this.processOperators();
|
|
54
|
+
this.processStringFunctions();
|
|
55
|
+
this.processRegex();
|
|
56
|
+
this.processLangMatches();
|
|
57
|
+
this.processArrayFunctions();
|
|
58
|
+
this.processFilter();
|
|
59
|
+
}
|
|
60
|
+
processOperators() {
|
|
61
|
+
const map = {
|
|
62
|
+
$equals: "=",
|
|
63
|
+
$not: "!=",
|
|
64
|
+
$gt: ">",
|
|
65
|
+
$gte: ">=",
|
|
66
|
+
$lt: "<",
|
|
67
|
+
$lte: "<=",
|
|
68
|
+
};
|
|
69
|
+
for (const [key, operator] of Object.entries(map)) {
|
|
70
|
+
const value = this.searchSchema[key];
|
|
71
|
+
if (value === undefined) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
this.addFilter($ `${this.df.variable(this.varName)} ${operator} ${this.encode(value)}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
processStringFunctions() {
|
|
78
|
+
const map = {
|
|
79
|
+
$contains: "CONTAINS",
|
|
80
|
+
$strStarts: "STRSTARTS",
|
|
81
|
+
$strEnds: "STRENDS",
|
|
82
|
+
};
|
|
83
|
+
for (const [key, func] of Object.entries(map)) {
|
|
84
|
+
const value = this.searchSchema[key];
|
|
85
|
+
if (value === undefined) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
this.addFilter($ `${func}(${this.df.variable(this.varName)}, ${this.encode(value)})`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
processRegex() {
|
|
92
|
+
const value = this.searchSchema.$regex;
|
|
93
|
+
if (value === undefined) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
this.addFilter($ `REGEX(${this.df.variable(this.varName)}, "${value}")`);
|
|
97
|
+
}
|
|
98
|
+
processLangMatches() {
|
|
99
|
+
const value = this.searchSchema.$langMatches;
|
|
100
|
+
if (value === undefined) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
this.addFilter($ `LANGMATCHES(LANG(${this.df.variable(this.varName)}), "${value}")`);
|
|
104
|
+
}
|
|
105
|
+
processArrayFunctions() {
|
|
106
|
+
const map = {
|
|
107
|
+
$in: "IN",
|
|
108
|
+
$notIn: "NOT IN",
|
|
109
|
+
};
|
|
110
|
+
for (const [key, func] of Object.entries(map)) {
|
|
111
|
+
const value = this.searchSchema[key];
|
|
112
|
+
if (value === undefined) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const values = value.map((v) => $ `${this.encode(v)}`);
|
|
116
|
+
this.addFilter($ `${this.df.variable(this.varName)} ${func} (${values.join(", ")})`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
processFilter() {
|
|
120
|
+
const value = this.searchSchema.$filter;
|
|
121
|
+
if (value === undefined) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const stringified = $ `${value}`;
|
|
125
|
+
this.addFilter(stringified.replace("?value", `?${this.varName}`));
|
|
126
|
+
}
|
|
127
|
+
addFilter(filter) {
|
|
128
|
+
this.sparqlValues.push($ `FILTER (${filter}) .`);
|
|
129
|
+
}
|
|
130
|
+
encode(value) {
|
|
131
|
+
return encodeValue(value, this.propertyType, this.df);
|
|
132
|
+
}
|
|
133
|
+
isPlainObject(value) {
|
|
134
|
+
if (typeof value !== "object" || value === null) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
const prototype = Object.getPrototypeOf(value);
|
|
138
|
+
return (prototype === null || prototype === Object.prototype ||
|
|
139
|
+
Object.getPrototypeOf(prototype) === null) &&
|
|
140
|
+
!(Symbol.toStringTag in value) && !(Symbol.iterator in value);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { OPTIONAL } from "../sparql/mod.js";
|
|
2
|
+
import { getSchemaProperties, } from "../schema/mod.js";
|
|
3
|
+
import { encode } from "../encoder.js";
|
|
4
|
+
export class UpdateHelper {
|
|
5
|
+
constructor(schema, options, variableInitCounter = 0) {
|
|
6
|
+
Object.defineProperty(this, "schema", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
writable: true,
|
|
10
|
+
value: void 0
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(this, "properties", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: void 0
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(this, "options", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: void 0
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(this, "variableCounter", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
configurable: true,
|
|
27
|
+
writable: true,
|
|
28
|
+
value: 0
|
|
29
|
+
});
|
|
30
|
+
Object.defineProperty(this, "deleteQuads", {
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true,
|
|
34
|
+
value: []
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(this, "insertQuads", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: []
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(this, "whereQuads", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
value: []
|
|
47
|
+
});
|
|
48
|
+
this.schema = schema;
|
|
49
|
+
this.properties = getSchemaProperties(schema);
|
|
50
|
+
this.options = options;
|
|
51
|
+
this.variableCounter = variableInitCounter;
|
|
52
|
+
}
|
|
53
|
+
process(entity) {
|
|
54
|
+
for (const name in this.properties) {
|
|
55
|
+
this.processProperty(entity, name, this.properties[name]);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
processProperty(entity, propertyName, property) {
|
|
59
|
+
this.variableCounter++;
|
|
60
|
+
const value = entity[propertyName];
|
|
61
|
+
if (value === undefined) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (property["@array"]) {
|
|
65
|
+
this.processArrayProperty(entity, propertyName, value);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
this.processSingleProperty(entity, propertyName, value, property);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
processSingleProperty(entity, propertyName, propertyValue, property) {
|
|
72
|
+
const deletePattern = {
|
|
73
|
+
$id: entity.$id,
|
|
74
|
+
[propertyName]: null,
|
|
75
|
+
};
|
|
76
|
+
const quadsToDelete = this.encode(deletePattern);
|
|
77
|
+
this.deleteQuads.push(...quadsToDelete);
|
|
78
|
+
if (property["@optional"]) {
|
|
79
|
+
this.whereQuads.push(OPTIONAL `${quadsToDelete}`);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.whereQuads.push(...quadsToDelete);
|
|
83
|
+
}
|
|
84
|
+
if (property["@optional"] && propertyValue === null) {
|
|
85
|
+
// The intention was to delete a value of an optional property, nothing to insert
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const insertPattern = {
|
|
89
|
+
$id: entity.$id,
|
|
90
|
+
[propertyName]: propertyValue,
|
|
91
|
+
};
|
|
92
|
+
const quadsToInsert = this.encode(insertPattern);
|
|
93
|
+
this.insertQuads.push(...quadsToInsert);
|
|
94
|
+
}
|
|
95
|
+
processArrayProperty(entity, propertyName, propertyValue) {
|
|
96
|
+
const config = this.parseArrayUpdateConfig(propertyValue);
|
|
97
|
+
if (config.$set) {
|
|
98
|
+
this.processArraySet(entity, propertyName, config.$set);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
this.processArrayAddRemove(entity, propertyName, config.$add, config.$remove);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
processArraySet(entity, propertyName, propertyValue) {
|
|
105
|
+
const deletePattern = {
|
|
106
|
+
$id: entity.$id,
|
|
107
|
+
[propertyName]: null,
|
|
108
|
+
};
|
|
109
|
+
const quadsToDelete = this.encode(deletePattern);
|
|
110
|
+
this.deleteQuads.push(...quadsToDelete);
|
|
111
|
+
this.whereQuads.push(...quadsToDelete);
|
|
112
|
+
const insertPattern = {
|
|
113
|
+
$id: entity.$id,
|
|
114
|
+
[propertyName]: propertyValue,
|
|
115
|
+
};
|
|
116
|
+
const quadsToInsert = this.encode(insertPattern);
|
|
117
|
+
this.insertQuads.push(...quadsToInsert);
|
|
118
|
+
}
|
|
119
|
+
processArrayAddRemove(entity, propertyName, $add, $remove) {
|
|
120
|
+
if ($remove) {
|
|
121
|
+
const deletePattern = {
|
|
122
|
+
$id: entity.$id,
|
|
123
|
+
[propertyName]: $remove,
|
|
124
|
+
};
|
|
125
|
+
const quadsToDelete = this.encode(deletePattern);
|
|
126
|
+
this.deleteQuads.push(...quadsToDelete);
|
|
127
|
+
}
|
|
128
|
+
if ($add) {
|
|
129
|
+
const insertPattern = {
|
|
130
|
+
$id: entity.$id,
|
|
131
|
+
[propertyName]: $add,
|
|
132
|
+
};
|
|
133
|
+
const quadsToInsert = this.encode(insertPattern);
|
|
134
|
+
this.insertQuads.push(...quadsToInsert);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
parseArrayUpdateConfig(config) {
|
|
138
|
+
if (Array.isArray(config)) {
|
|
139
|
+
return { $set: config, $add: undefined, $remove: undefined };
|
|
140
|
+
}
|
|
141
|
+
if (config == null || typeof config !== "object") {
|
|
142
|
+
throw new Error("Invalid array update query, expected an array, or an object with $add, $set, or $remove properties");
|
|
143
|
+
}
|
|
144
|
+
const { $add, $set, $remove } = config;
|
|
145
|
+
switch (true) {
|
|
146
|
+
case Array.isArray($set) && $add === undefined && $remove === undefined:
|
|
147
|
+
case $set === undefined && Array.isArray($add) && $remove === undefined:
|
|
148
|
+
case $set === undefined && $add === undefined && Array.isArray($remove):
|
|
149
|
+
case $set === undefined && Array.isArray($add) && Array.isArray($remove):
|
|
150
|
+
return { $add, $set, $remove };
|
|
151
|
+
}
|
|
152
|
+
throw new Error("Invalid array update query, expected an array, or an object with $add, $set, or $remove properties");
|
|
153
|
+
}
|
|
154
|
+
encode(entity) {
|
|
155
|
+
return encode(entity, this.schema, this.options, false, this.variableCounter);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a strongly typed container for Linked Data vocabulary to provide
|
|
3
|
+
* type safe access to all vocabulary terms as well as IDE autocompletion.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import { createNamespace } from "ldkit";
|
|
8
|
+
*
|
|
9
|
+
* const onto = createNamespace(
|
|
10
|
+
* {
|
|
11
|
+
* iri: "http://www.example.com/ontology#",
|
|
12
|
+
* prefix: "onto:",
|
|
13
|
+
* terms: [
|
|
14
|
+
* "object",
|
|
15
|
+
* "predicate",
|
|
16
|
+
* "subject",
|
|
17
|
+
* ],
|
|
18
|
+
* } as const,
|
|
19
|
+
* );
|
|
20
|
+
*
|
|
21
|
+
* console.log(onto.subject); // prints http://www.example.com/ontology#subject
|
|
22
|
+
* console.log(onto.unknown); // TypeScript error! This term does not exist
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @param namespaceSpec Specification of the namespace
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
export function createNamespace(namespaceSpec) {
|
|
29
|
+
return Object.assign(namespaceSpec.terms.reduce((acc, term) => {
|
|
30
|
+
acc[term] = `${namespaceSpec.iri}${term}`;
|
|
31
|
+
return acc;
|
|
32
|
+
}, {}), {
|
|
33
|
+
$prefix: namespaceSpec["prefix"],
|
|
34
|
+
$iri: namespaceSpec["iri"],
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { QueryEngine, } from "./engine/mod.js";
|
|
2
|
+
const defaultOptions = {
|
|
3
|
+
engine: new QueryEngine(),
|
|
4
|
+
take: 1000,
|
|
5
|
+
logQuery: () => { },
|
|
6
|
+
};
|
|
7
|
+
let globalOptions = {};
|
|
8
|
+
/**
|
|
9
|
+
* Sets global configuration {@link Options} for LDkit that will be used
|
|
10
|
+
* by default in all queries, unless overridden in {@link Lens}.
|
|
11
|
+
*
|
|
12
|
+
* LDkit-specific options are:
|
|
13
|
+
* - `engine` - a query engine to use for querying data sources
|
|
14
|
+
* - `language` - a preferred language for literals
|
|
15
|
+
* - `take` - a default number of results to take (limit of SELECT queries)
|
|
16
|
+
* - `logQuery` - a function that will be called for each SPARQL query
|
|
17
|
+
*
|
|
18
|
+
* Default values for these options are:
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const defaultOptions = {
|
|
21
|
+
* engine: new QueryEngine(),
|
|
22
|
+
* take: 1000,
|
|
23
|
+
* logQuery: () => {},
|
|
24
|
+
* };
|
|
25
|
+
* ```
|
|
26
|
+
* The default configuration uses built-in {@link QueryEngine}. Language is not set by default.
|
|
27
|
+
*
|
|
28
|
+
* @param options LDkit options and query engine context
|
|
29
|
+
*/
|
|
30
|
+
export function setGlobalOptions(options) {
|
|
31
|
+
globalOptions = options;
|
|
32
|
+
}
|
|
33
|
+
export function resolveOptions(options = {}) {
|
|
34
|
+
return {
|
|
35
|
+
...defaultOptions,
|
|
36
|
+
...globalOptions,
|
|
37
|
+
...options,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export function resolveQueryContext(options) {
|
|
41
|
+
const { engine: _engine, language: _language, take: _take, ...context } = options;
|
|
42
|
+
if (context.source !== undefined && context.sources === undefined) {
|
|
43
|
+
context.sources = [context.source];
|
|
44
|
+
delete context.source;
|
|
45
|
+
}
|
|
46
|
+
if (context.sources === undefined) {
|
|
47
|
+
throw new Error("No data source defined in Options");
|
|
48
|
+
}
|
|
49
|
+
return context;
|
|
50
|
+
}
|
|
@@ -1,38 +1,3 @@
|
|
|
1
|
-
import xsd from "
|
|
2
|
-
import rdf from "
|
|
3
|
-
|
|
4
|
-
[xsd.dateTime]: new Date(),
|
|
5
|
-
[xsd.date]: new Date(),
|
|
6
|
-
[xsd.gDay]: new Date(),
|
|
7
|
-
[xsd.gMonthDay]: new Date(),
|
|
8
|
-
[xsd.gYear]: new Date(),
|
|
9
|
-
[xsd.gYearMonth]: new Date(),
|
|
10
|
-
[xsd.boolean]: true,
|
|
11
|
-
[xsd.double]: 0.0,
|
|
12
|
-
[xsd.decimal]: 0.0,
|
|
13
|
-
[xsd.float]: 0.0,
|
|
14
|
-
[xsd.integer]: 0,
|
|
15
|
-
[xsd.long]: 0,
|
|
16
|
-
[xsd.int]: 0,
|
|
17
|
-
[xsd.byte]: 0,
|
|
18
|
-
[xsd.short]: 0,
|
|
19
|
-
[xsd.negativeInteger]: 0,
|
|
20
|
-
[xsd.nonNegativeInteger]: 0,
|
|
21
|
-
[xsd.nonPositiveInteger]: 0,
|
|
22
|
-
[xsd.positiveInteger]: 0,
|
|
23
|
-
[xsd.unsignedByte]: 0,
|
|
24
|
-
[xsd.unsignedInt]: 0,
|
|
25
|
-
[xsd.unsignedLong]: 0,
|
|
26
|
-
[xsd.unsignedShort]: 0,
|
|
27
|
-
[xsd.string]: "",
|
|
28
|
-
[xsd.normalizedString]: "",
|
|
29
|
-
[xsd.anyURI]: "",
|
|
30
|
-
[xsd.base64Binary]: "",
|
|
31
|
-
[xsd.language]: "",
|
|
32
|
-
[xsd.Name]: "",
|
|
33
|
-
[xsd.NCName]: "",
|
|
34
|
-
[xsd.NMTOKEN]: "",
|
|
35
|
-
[xsd.token]: "",
|
|
36
|
-
[xsd.hexBinary]: "",
|
|
37
|
-
[rdf.langString]: "",
|
|
38
|
-
};
|
|
1
|
+
import { xsd } from "../../namespaces/xsd.js";
|
|
2
|
+
import { rdf } from "../../namespaces/rdf.js";
|
|
3
|
+
import { ldkit } from "../../namespaces/ldkit.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|