ldkit 0.8.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.
Files changed (157) hide show
  1. package/README.md +5 -5
  2. package/esm/library/decoder.js +20 -30
  3. package/esm/library/encoder.js +33 -17
  4. package/esm/library/engine/mod.js +2 -2
  5. package/esm/library/engine/query_engine.js +57 -29
  6. package/esm/library/engine/query_engine_proxy.js +8 -8
  7. package/esm/library/engine/query_resolvers.js +72 -0
  8. package/esm/library/engine/types.js +1 -0
  9. package/esm/library/lens/lens.js +414 -25
  10. package/esm/library/lens/mod.js +1 -1
  11. package/esm/library/lens/query_builder.js +81 -50
  12. package/esm/library/lens/search_helper.js +142 -0
  13. package/esm/library/lens/update_helper.js +157 -0
  14. package/esm/library/namespace.js +36 -0
  15. package/esm/library/options.js +50 -0
  16. package/esm/library/rdf.js +3 -2
  17. package/esm/library/schema/data_types.js +3 -38
  18. package/esm/library/schema/search.js +1 -0
  19. package/esm/library/schema/utils.js +31 -6
  20. package/esm/library/sparql/mod.js +2 -1
  21. package/esm/library/sparql/sparql_expression_builders.js +18 -0
  22. package/esm/library/sparql/sparql_query_builders.js +65 -0
  23. package/esm/library/sparql/sparql_shared_builders.js +1 -0
  24. package/esm/library/sparql/sparql_tag.js +27 -1
  25. package/esm/library/sparql/sparql_update_builders.js +70 -0
  26. package/esm/library/sparql/stringify.js +3 -4
  27. package/esm/mod.js +4 -3
  28. package/esm/{library/namespaces → namespaces}/dbo.js +7 -2
  29. package/esm/{library/namespaces → namespaces}/dc.js +7 -2
  30. package/esm/{library/namespaces → namespaces}/dcterms.js +7 -2
  31. package/esm/{library/namespaces → namespaces}/foaf.js +7 -2
  32. package/esm/{library/namespaces → namespaces}/gr.js +7 -2
  33. package/esm/namespaces/ldkit.js +11 -0
  34. package/esm/namespaces/owl.js +91 -0
  35. package/{script/library → esm}/namespaces/rdf.js +7 -4
  36. package/{script/library → esm}/namespaces/rdfs.js +7 -4
  37. package/esm/{library/namespaces → namespaces}/schema.js +7 -2
  38. package/esm/{library/namespaces → namespaces}/sioc.js +7 -2
  39. package/{script/library → esm}/namespaces/skos.js +7 -4
  40. package/{script/library → esm}/namespaces/xsd.js +7 -4
  41. package/esm/namespaces.js +30 -1
  42. package/esm/rdf.js +14 -1
  43. package/esm/sparql.js +13 -0
  44. package/package.json +4 -2
  45. package/script/library/decoder.js +25 -38
  46. package/script/library/encoder.js +38 -24
  47. package/script/library/engine/mod.js +16 -5
  48. package/script/library/engine/query_engine.js +57 -29
  49. package/script/library/engine/query_engine_proxy.js +7 -7
  50. package/script/library/engine/query_resolvers.js +77 -0
  51. package/script/library/engine/types.js +2 -0
  52. package/script/library/lens/lens.js +415 -27
  53. package/script/library/lens/mod.js +15 -4
  54. package/script/library/lens/query_builder.js +82 -54
  55. package/script/library/lens/search_helper.js +146 -0
  56. package/script/library/lens/update_helper.js +161 -0
  57. package/script/library/namespace.js +40 -0
  58. package/script/library/options.js +56 -0
  59. package/script/library/rdf.js +27 -3
  60. package/script/library/schema/data_types.js +3 -41
  61. package/script/library/schema/search.js +2 -0
  62. package/script/library/schema/utils.js +33 -11
  63. package/script/library/sparql/mod.js +17 -3
  64. package/script/library/sparql/sparql_expression_builders.js +22 -0
  65. package/script/library/sparql/sparql_query_builders.js +65 -0
  66. package/script/library/sparql/sparql_shared_builders.js +3 -1
  67. package/script/library/sparql/sparql_tag.js +30 -7
  68. package/script/library/sparql/sparql_update_builders.js +70 -0
  69. package/script/library/sparql/stringify.js +4 -8
  70. package/script/mod.js +20 -9
  71. package/script/{library/namespaces → namespaces}/dbo.js +8 -2
  72. package/script/{library/namespaces → namespaces}/dc.js +8 -2
  73. package/script/{library/namespaces → namespaces}/dcterms.js +8 -2
  74. package/script/{library/namespaces → namespaces}/foaf.js +8 -2
  75. package/script/{library/namespaces → namespaces}/gr.js +8 -2
  76. package/script/namespaces/ldkit.js +14 -0
  77. package/script/namespaces/owl.js +94 -0
  78. package/{esm/library → script}/namespaces/rdf.js +10 -2
  79. package/{esm/library → script}/namespaces/rdfs.js +10 -2
  80. package/script/{library/namespaces → namespaces}/schema.js +8 -2
  81. package/script/{library/namespaces → namespaces}/sioc.js +8 -2
  82. package/{esm/library → script}/namespaces/skos.js +10 -2
  83. package/{esm/library → script}/namespaces/xsd.js +10 -2
  84. package/script/namespaces.js +44 -1
  85. package/script/rdf.js +20 -15
  86. package/script/sparql.js +13 -0
  87. package/types/library/decoder.d.ts +4 -3
  88. package/types/library/encoder.d.ts +5 -3
  89. package/types/library/engine/mod.d.ts +2 -2
  90. package/types/library/engine/query_engine.d.ts +55 -13
  91. package/types/library/engine/query_engine_proxy.d.ts +4 -3
  92. package/types/library/engine/query_resolvers.d.ts +10 -0
  93. package/types/library/engine/types.d.ts +23 -0
  94. package/types/library/lens/lens.d.ts +400 -23
  95. package/types/library/lens/mod.d.ts +1 -1
  96. package/types/library/lens/query_builder.d.ts +10 -10
  97. package/types/library/lens/search_helper.d.ts +21 -0
  98. package/types/library/lens/types.d.ts +5 -2
  99. package/types/library/lens/update_helper.d.ts +23 -0
  100. package/types/library/namespace.d.ts +41 -0
  101. package/types/library/options.d.ts +72 -0
  102. package/types/library/rdf.d.ts +10 -16
  103. package/types/library/schema/data_types.d.ts +44 -37
  104. package/types/library/schema/interface.d.ts +75 -20
  105. package/types/library/schema/mod.d.ts +4 -2
  106. package/types/library/schema/schema.d.ts +23 -16
  107. package/types/library/schema/search.d.ts +20 -0
  108. package/types/library/schema/utils.d.ts +3 -3
  109. package/types/library/sparql/mod.d.ts +2 -1
  110. package/types/library/sparql/sparql_expression_builders.d.ts +19 -0
  111. package/types/library/sparql/sparql_query_builders.d.ts +68 -3
  112. package/types/library/sparql/sparql_shared_builders.d.ts +1 -0
  113. package/types/library/sparql/sparql_tag.d.ts +29 -1
  114. package/types/library/sparql/sparql_update_builders.d.ts +68 -0
  115. package/types/mod.d.ts +5 -5
  116. package/types/namespaces/dbo.d.ts +10 -0
  117. package/types/namespaces/dc.d.ts +10 -0
  118. package/types/namespaces/dcterms.d.ts +10 -0
  119. package/types/namespaces/foaf.d.ts +10 -0
  120. package/types/namespaces/gr.d.ts +10 -0
  121. package/types/namespaces/ldkit.d.ts +10 -0
  122. package/types/namespaces/owl.d.ts +10 -0
  123. package/types/namespaces/rdf.d.ts +10 -0
  124. package/types/namespaces/rdfs.d.ts +10 -0
  125. package/types/namespaces/schema.d.ts +10 -0
  126. package/types/namespaces/sioc.d.ts +10 -0
  127. package/types/namespaces/skos.d.ts +10 -0
  128. package/types/namespaces/xsd.d.ts +10 -0
  129. package/types/namespaces.d.ts +30 -1
  130. package/types/rdf.d.ts +14 -1
  131. package/types/sparql.d.ts +13 -0
  132. package/esm/library/global.js +0 -25
  133. package/esm/library/lens/query_helper.js +0 -102
  134. package/esm/library/namespaces/ldkit.js +0 -6
  135. package/esm/library/namespaces/mod.js +0 -13
  136. package/esm/library/namespaces/namespace.js +0 -11
  137. package/script/library/global.js +0 -32
  138. package/script/library/lens/query_helper.js +0 -106
  139. package/script/library/namespaces/ldkit.js +0 -8
  140. package/script/library/namespaces/mod.js +0 -32
  141. package/script/library/namespaces/namespace.js +0 -15
  142. package/types/library/global.d.ts +0 -5
  143. package/types/library/lens/query_helper.d.ts +0 -19
  144. package/types/library/namespaces/dbo.d.ts +0 -3649
  145. package/types/library/namespaces/dc.d.ts +0 -21
  146. package/types/library/namespaces/dcterms.d.ts +0 -104
  147. package/types/library/namespaces/foaf.d.ts +0 -69
  148. package/types/library/namespaces/gr.d.ts +0 -175
  149. package/types/library/namespaces/ldkit.d.ts +0 -7
  150. package/types/library/namespaces/mod.d.ts +0 -13
  151. package/types/library/namespaces/namespace.d.ts +0 -15
  152. package/types/library/namespaces/rdf.d.ts +0 -28
  153. package/types/library/namespaces/rdfs.d.ts +0 -21
  154. package/types/library/namespaces/schema.d.ts +0 -2697
  155. package/types/library/namespaces/sioc.d.ts +0 -105
  156. package/types/library/namespaces/skos.d.ts +0 -38
  157. package/types/library/namespaces/xsd.d.ts +0 -56
package/esm/rdf.js CHANGED
@@ -1 +1,14 @@
1
- export * from "./library/rdf.js";
1
+ /**
2
+ * RDF utilities
3
+ *
4
+ * This module contains a re-export of external RDF libraries that are used
5
+ * in LDkit and may be used in tandem with LDkit in Linked Data applications as well.
6
+ *
7
+ * Included packages:
8
+ * - [@rdfjs/types](https://github.com/rdfjs/types) [RDF/JS](https://rdf.js.org/) authoritative TypeScript typings
9
+ * - [n3](https://rdf.js.org/N3.js/) RDF parser and serializer
10
+ * - [rdf-data-factory](https://github.com/rubensworks/rdf-data-factory.js) A TypeScript/JavaScript implementation of the RDF/JS data factory
11
+ * - [rdf-literal](https://github.com/rubensworks/rdf-literal.js) Translates between RDF literals and JavaScript primitives
12
+ * @module
13
+ */
14
+ export { DataFactory, DefaultGraph, fromRdf, N3, toRdf, } from "./library/rdf.js";
package/esm/sparql.js CHANGED
@@ -1 +1,14 @@
1
+ /**
2
+ * SPARQL builders that provide a fluent interface for building SPARQL queries
3
+ *
4
+ * @example
5
+ * ```typescript
6
+ * import { SELECT } from "ldkit/sparql";
7
+ *
8
+ * const query = SELECT`?s ?p ?o`.WHERE`?s ?p ?o`.LIMIT(10).build();
9
+ * console.log(query); // SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 10;
10
+ * ```
11
+ *
12
+ * @module
13
+ */
1
14
  export * from "./library/sparql/mod.js";
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "./script/mod.js",
4
4
  "types": "./types/mod.d.ts",
5
5
  "name": "ldkit",
6
- "version": "0.8.0",
6
+ "version": "2.0.0",
7
7
  "description": "LDkit, a Linked Data query toolkit for TypeScript developers",
8
8
  "homepage": "https://ldkit.io",
9
9
  "author": "Karel Klima <karelklima@gmail.com> (https://karelklima.com)",
@@ -65,9 +65,11 @@
65
65
  },
66
66
  "dependencies": {
67
67
  "@comunica/types": "2.6.8",
68
+ "@rdfjs/types": "*",
69
+ "@types/n3": "*",
68
70
  "asynciterator": "3.8.0",
71
+ "n3": "1.17.2",
69
72
  "rdf-data-factory": "1.1.1",
70
- "rdf-js": "4.0.2",
71
73
  "rdf-literal": "1.3.1"
72
74
  }
73
75
  }
@@ -1,14 +1,15 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.decode = void 0;
7
4
  const rdf_js_1 = require("./rdf.js");
8
- const ldkit_js_1 = __importDefault(require("./namespaces/ldkit.js"));
9
- const rdf_js_2 = __importDefault(require("./namespaces/rdf.js"));
5
+ const ldkit_js_1 = require("../namespaces/ldkit.js");
6
+ const rdf_js_2 = require("../namespaces/rdf.js");
7
+ const decode = (graph, schema, options) => {
8
+ return Decoder.decode(graph, schema, options);
9
+ };
10
+ exports.decode = decode;
10
11
  class Decoder {
11
- constructor(graph, schema, context) {
12
+ constructor(graph, schema, options) {
12
13
  Object.defineProperty(this, "graph", {
13
14
  enumerable: true,
14
15
  configurable: true,
@@ -21,7 +22,7 @@ class Decoder {
21
22
  writable: true,
22
23
  value: void 0
23
24
  });
24
- Object.defineProperty(this, "context", {
25
+ Object.defineProperty(this, "options", {
25
26
  enumerable: true,
26
27
  configurable: true,
27
28
  writable: true,
@@ -35,10 +36,10 @@ class Decoder {
35
36
  });
36
37
  this.graph = graph;
37
38
  this.schema = schema;
38
- this.context = context;
39
+ this.options = options;
39
40
  }
40
- static decode(graph, schema, context) {
41
- return new Decoder(graph, schema, context).decode();
41
+ static decode(graph, schema, options) {
42
+ return new Decoder(graph, schema, options).decode();
42
43
  }
43
44
  getCachedNode(nodeIri, schema) {
44
45
  if (!this.cache.has(schema)) {
@@ -55,17 +56,17 @@ class Decoder {
55
56
  decode() {
56
57
  const output = [];
57
58
  for (const [iri, properties] of this.graph) {
58
- if (properties.has(rdf_js_2.default.type)) {
59
- const types = properties.get(rdf_js_2.default.type);
59
+ if (properties.has(rdf_js_2.rdf.type)) {
60
+ const types = properties.get(rdf_js_2.rdf.type);
60
61
  for (const type of types) {
61
- if (type.termType === "NamedNode" && type.value === ldkit_js_1.default.Resource) {
62
+ if (type.termType === "NamedNode" && type.value === ldkit_js_1.ldkit.Resource) {
62
63
  output.push(this.decodeNode(iri, this.schema));
63
64
  }
64
65
  }
65
66
  }
66
67
  }
67
68
  if (this.graph.size > 0 && output.length < 1) {
68
- throw new Error(`Unable to decode graph - no resources with type <${ldkit_js_1.default.Resource}> found`);
69
+ throw new Error(`Unable to decode graph - no resources with type <${ldkit_js_1.ldkit.Resource}> found`);
69
70
  }
70
71
  return output;
71
72
  }
@@ -81,7 +82,6 @@ class Decoder {
81
82
  if (!node) {
82
83
  throw new Error(`Error decoding graph, <${nodeIri}> node not found.`);
83
84
  }
84
- output.$type = this.decodeNodeType(node);
85
85
  Object.keys(schema).forEach((key) => {
86
86
  if (key === "@type") {
87
87
  return;
@@ -94,20 +94,11 @@ class Decoder {
94
94
  this.setCachedNode(nodeIri, schema, output);
95
95
  return output;
96
96
  }
97
- decodeNodeType(node) {
98
- const typeTerms = node.get(rdf_js_2.default.type);
99
- if (!typeTerms) {
100
- return [];
101
- }
102
- return typeTerms.reduce((acc, term) => {
103
- if (term.value !== ldkit_js_1.default.Resource) {
104
- acc.push(term.value);
105
- }
106
- return acc;
107
- }, []);
108
- }
109
97
  decodeNodeProperty(nodeIri, node, propertyKey, property) {
110
- const terms = node.get(property["@id"]);
98
+ const allTerms = node.get(property["@id"]);
99
+ const terms = property["@id"] !== rdf_js_2.rdf.type
100
+ ? allTerms
101
+ : allTerms?.filter((term) => term.value !== ldkit_js_1.ldkit.Resource);
111
102
  if (!terms) {
112
103
  if (!property["@optional"]) {
113
104
  // No data, required property
@@ -115,7 +106,7 @@ class Decoder {
115
106
  }
116
107
  else {
117
108
  // No data, optional property
118
- return property["@array"] ? [] : undefined;
109
+ return property["@array"] ? [] : null;
119
110
  }
120
111
  }
121
112
  if (property["@multilang"]) {
@@ -147,13 +138,13 @@ class Decoder {
147
138
  }
148
139
  }
149
140
  if (property["@array"]) {
150
- if (property["@context"]) {
141
+ if (property["@schema"]) {
151
142
  // Collection of resources specified by sub schema
152
143
  return terms.map((term) => {
153
144
  if (term.termType !== "NamedNode" && term.termType !== "BlankNode") {
154
145
  throw new Error(`Property "${propertyKey}" data type mismatch - expected a node, but received ${term.termType} instead on resource <${nodeIri}>`);
155
146
  }
156
- return this.decodeNode(term.value, property["@context"]);
147
+ return this.decodeNode(term.value, property["@schema"]);
157
148
  });
158
149
  }
159
150
  else {
@@ -167,15 +158,15 @@ class Decoder {
167
158
  }
168
159
  }
169
160
  // Single return value expected from this point on
170
- if (property["@context"]) {
161
+ if (property["@schema"]) {
171
162
  for (const term of terms) {
172
163
  if (term.termType === "NamedNode" || term.termType === "BlankNode") {
173
- return this.decodeNode(term.value, property["@context"]);
164
+ return this.decodeNode(term.value, property["@schema"]);
174
165
  }
175
166
  }
176
167
  throw new Error(`Property "${propertyKey}" data type mismatch - expected a named node for context on resource <${nodeIri}>`);
177
168
  }
178
- const preferredLanguage = this.context.language;
169
+ const preferredLanguage = this.options.language;
179
170
  if (preferredLanguage) {
180
171
  // Try to find a term that corresponds to the preferred language
181
172
  for (const term of terms) {
@@ -205,7 +196,3 @@ class Decoder {
205
196
  }
206
197
  }
207
198
  }
208
- const decode = (graph, schema, context) => {
209
- return Decoder.decode(graph, schema, context);
210
- };
211
- exports.decode = decode;
@@ -1,19 +1,32 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.encode = void 0;
3
+ exports.encodeValue = exports.encode = void 0;
7
4
  const rdf_js_1 = require("./rdf.js");
8
- const xsd_js_1 = __importDefault(require("./namespaces/xsd.js"));
9
- const rdf_js_2 = __importDefault(require("./namespaces/rdf.js"));
10
- const encode = (node, schema, context, variableInitCounter = 0) => {
11
- return Encoder.encode(node, schema, context, variableInitCounter);
5
+ const xsd_js_1 = require("../namespaces/xsd.js");
6
+ const rdf_js_2 = require("../namespaces/rdf.js");
7
+ const ldkit_js_1 = require("../namespaces/ldkit.js");
8
+ const encode = (node, schema, options, includeType = true, variableInitCounter = 0) => {
9
+ return Encoder.encode(node, schema, options, includeType, variableInitCounter);
12
10
  };
13
11
  exports.encode = encode;
12
+ const encodeValue = (value, datatype, df) => {
13
+ if (datatype === ldkit_js_1.ldkit.IRI) {
14
+ return df.namedNode(value);
15
+ }
16
+ return (0, rdf_js_1.toRdf)(value, {
17
+ datatype: df.namedNode(datatype),
18
+ });
19
+ };
20
+ exports.encodeValue = encodeValue;
14
21
  class Encoder {
15
- constructor(context, variableInitCounter) {
16
- Object.defineProperty(this, "context", {
22
+ constructor(options, includeType, variableInitCounter) {
23
+ Object.defineProperty(this, "options", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: void 0
28
+ });
29
+ Object.defineProperty(this, "includeType", {
17
30
  enumerable: true,
18
31
  configurable: true,
19
32
  writable: true,
@@ -39,11 +52,12 @@ class Encoder {
39
52
  writable: true,
40
53
  value: []
41
54
  });
42
- this.context = context;
55
+ this.options = options;
56
+ this.includeType = includeType;
43
57
  this.variableCounter = variableInitCounter;
44
58
  }
45
- static encode(node, schema, context, variableInitCounter) {
46
- return new Encoder(context, variableInitCounter).encode(node, schema);
59
+ static encode(node, schema, options, includeType, variableInitCounter) {
60
+ return new Encoder(options, includeType, variableInitCounter).encode(node, schema);
47
61
  }
48
62
  encode(node, schema) {
49
63
  const nodeId = this.getNodeId(node);
@@ -65,7 +79,9 @@ class Encoder {
65
79
  return node.$type ? [node.$type] : [];
66
80
  }
67
81
  encodeNode(node, schema, nodeId) {
68
- this.encodeNodeType(node, schema["@type"], nodeId);
82
+ if (this.includeType) {
83
+ this.encodeNodeType(node, schema["@type"], nodeId);
84
+ }
69
85
  Object.keys(schema).forEach((key) => {
70
86
  if (key === "@type") {
71
87
  return;
@@ -76,7 +92,7 @@ class Encoder {
76
92
  encodeNodeType(node, requiredTypes, nodeId) {
77
93
  const finalTypes = new Set([...this.getNodeTypes(node), ...requiredTypes]);
78
94
  finalTypes.forEach((type) => {
79
- this.push(nodeId, this.df.namedNode(rdf_js_2.default.type), this.df.namedNode(type));
95
+ this.push(nodeId, this.df.namedNode(rdf_js_2.rdf.type), this.df.namedNode(type));
80
96
  });
81
97
  }
82
98
  encodeNodeProperty(value, property, nodeId) {
@@ -103,22 +119,20 @@ class Encoder {
103
119
  }
104
120
  const values = Array.isArray(value) ? value : [value];
105
121
  values.forEach((val) => {
106
- if (property["@context"]) {
122
+ if (property["@schema"]) {
107
123
  const subNodeId = this.getNodeId(val);
108
- this.encodeNode(val, property["@context"], subNodeId);
124
+ this.encodeNode(val, property["@schema"], subNodeId);
109
125
  this.push(nodeId, propertyId, subNodeId);
110
126
  return;
111
127
  }
112
- const propertyType = property["@type"] ? property["@type"] : xsd_js_1.default.string;
113
- if (typeof val === "string" && this.context.language) {
114
- if (propertyType === xsd_js_1.default.string || propertyType === rdf_js_2.default.langString) {
115
- this.push(nodeId, propertyId, this.df.literal(val, this.context.language));
128
+ const propertyType = property["@type"] ? property["@type"] : xsd_js_1.xsd.string;
129
+ if (typeof val === "string" && this.options.language) {
130
+ if (propertyType === xsd_js_1.xsd.string || propertyType === rdf_js_2.rdf.langString) {
131
+ this.push(nodeId, propertyId, this.df.literal(val, this.options.language));
116
132
  return;
117
133
  }
118
134
  }
119
- const rdfValue = (0, rdf_js_1.toRdf)(val, {
120
- datatype: this.df.namedNode(propertyType),
121
- });
135
+ const rdfValue = (0, exports.encodeValue)(val, propertyType, this.df);
122
136
  this.push(nodeId, propertyId, rdfValue);
123
137
  });
124
138
  }
@@ -1,7 +1,18 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.QueryEngineProxy = exports.QueryEngine = void 0;
4
- var query_engine_js_1 = require("./query_engine.js");
5
- Object.defineProperty(exports, "QueryEngine", { enumerable: true, get: function () { return query_engine_js_1.QueryEngine; } });
6
- var query_engine_proxy_js_1 = require("./query_engine_proxy.js");
7
- Object.defineProperty(exports, "QueryEngineProxy", { enumerable: true, get: function () { return query_engine_proxy_js_1.QueryEngineProxy; } });
17
+ __exportStar(require("./query_engine.js"), exports);
18
+ __exportStar(require("./types.js"), exports);
@@ -1,8 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QueryEngine = void 0;
4
- const rdf_js_1 = require("../rdf.js");
5
- const asynciterator_js_1 = require("../asynciterator.js");
4
+ const query_resolvers_js_1 = require("./query_resolvers.js");
5
+ /**
6
+ * A query engine that can query a SPARQL endpoint.
7
+ *
8
+ * Implements {@link IQueryEngine} interface.
9
+ *
10
+ * This engine is used by default if no other engine is configured.
11
+ * See {@link Options} for more details.
12
+ *
13
+ * If you need to query other data sources, or multiple SPARQL endpoints,
14
+ * you can use [Comunica](https://comunica.dev) instead, extend this engine,
15
+ * or implement your own.
16
+ */
6
17
  class QueryEngine {
7
18
  getSparqlEndpoint(context) {
8
19
  if (!context) {
@@ -33,10 +44,10 @@ class QueryEngine {
33
44
  getFetch(context) {
34
45
  return context && context.fetch ? context.fetch : fetch;
35
46
  }
36
- async query(query, responseType, context) {
47
+ query(query, responseType, context) {
37
48
  const endpoint = this.getSparqlEndpoint(context);
38
49
  const fetchFn = this.getFetch(context);
39
- const response = await fetchFn(endpoint, {
50
+ return fetchFn(endpoint, {
40
51
  method: "POST",
41
52
  headers: {
42
53
  "accept": responseType,
@@ -46,36 +57,53 @@ class QueryEngine {
46
57
  query,
47
58
  }),
48
59
  });
49
- const json = await response.json();
50
- return json;
51
60
  }
52
- async queryBindings(query, context) {
53
- const json = await this.query(query, "application/sparql-results+json", context);
54
- if (!Array.isArray(json.results?.bindings)) {
55
- throw new Error("Bindings SPARQL query result not found");
61
+ async queryAndResolve(type, query, context) {
62
+ const responseType = (0, query_resolvers_js_1.getResponseTypes)(type).join(", ");
63
+ const response = await this.query(query, responseType, context);
64
+ if (!response.ok) {
65
+ await response.body?.cancel();
66
+ throw new Error(`Invalid query response status '${response.status} ${response.statusText}'`);
56
67
  }
57
- const bindingsFactory = new rdf_js_1.BindingsFactory();
58
- const bindingsIterator = new asynciterator_js_1.ArrayIterator(json.results.bindings);
59
- // TODO: review the unknown type cast
60
- return new asynciterator_js_1.MappingIterator(bindingsIterator, (i) => bindingsFactory.fromJson(i));
68
+ return (0, query_resolvers_js_1.resolve)(type, response);
61
69
  }
62
- async queryBoolean(query, context) {
63
- const json = await this.query(query, "application/sparql-results+json", context);
64
- if ("boolean" in json) {
65
- return Boolean(json.boolean);
66
- }
67
- throw new Error("Boolean SPARQL query result not found");
70
+ /**
71
+ * Executes a SPARQL SELECT query and returns a stream of bindings.
72
+ *
73
+ * @param query SPARQL query string
74
+ * @param context Engine context
75
+ * @returns Stream of bindings
76
+ */
77
+ queryBindings(query, context) {
78
+ return this.queryAndResolve("bindings", query, context);
68
79
  }
69
- async queryQuads(query, context) {
70
- const json = await this.query(query, "application/rdf+json", context);
71
- if (!(json?.constructor === Object)) {
72
- throw new Error("Quads SPARQL query result not found");
73
- }
74
- const quadFactory = new rdf_js_1.QuadFactory();
75
- const treeIterator = new asynciterator_js_1.TreeIterator(json);
76
- // TODO: review the unknown type cast
77
- return new asynciterator_js_1.MappingIterator(treeIterator, (i) => quadFactory.fromJson(i));
80
+ /**
81
+ * Executes a SPARQL ASK query and returns a boolean result.
82
+ *
83
+ * @param query SPARQL query string
84
+ * @param context Engine context
85
+ * @returns Boolean result
86
+ */
87
+ queryBoolean(query, context) {
88
+ return this.queryAndResolve("boolean", query, context);
89
+ }
90
+ /**
91
+ * Executes a SPARQL CONSTRUCT query and returns a stream of quads.
92
+ *
93
+ * @param query SPARQL query string
94
+ * @param context Engine context
95
+ * @returns Stream of quads
96
+ */
97
+ queryQuads(query, context) {
98
+ return this.queryAndResolve("quads", query, context);
78
99
  }
100
+ /**
101
+ * Executes a SPARQL UPDATE query and returns nothing.
102
+ *
103
+ * @param query SPARQL query string
104
+ * @param context Engine context
105
+ * @returns Nothing
106
+ */
79
107
  async queryVoid(query, context) {
80
108
  await this.query(query, "application/sparql-results+json", context);
81
109
  }
@@ -2,23 +2,22 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QueryEngineProxy = void 0;
4
4
  const rdf_js_1 = require("../rdf.js");
5
- const global_js_1 = require("../global.js");
6
5
  class QueryEngineProxy {
7
- constructor(context, engine) {
8
- Object.defineProperty(this, "context", {
6
+ constructor(engine, context) {
7
+ Object.defineProperty(this, "engine", {
9
8
  enumerable: true,
10
9
  configurable: true,
11
10
  writable: true,
12
11
  value: void 0
13
12
  });
14
- Object.defineProperty(this, "engine", {
13
+ Object.defineProperty(this, "context", {
15
14
  enumerable: true,
16
15
  configurable: true,
17
16
  writable: true,
18
17
  value: void 0
19
18
  });
20
- this.context = (0, global_js_1.resolveContext)(context);
21
- this.engine = (0, global_js_1.resolveEngine)(engine);
19
+ this.engine = engine;
20
+ this.context = context;
22
21
  }
23
22
  queryBoolean(query) {
24
23
  return this.engine.queryBoolean(query, this.context);
@@ -30,7 +29,8 @@ class QueryEngineProxy {
30
29
  async queryGraph(query) {
31
30
  const quadStream = await this.engine.queryQuads(query, this.context);
32
31
  const quads = await (quadStream.toArray());
33
- return (0, rdf_js_1.quadsToGraph)(quads);
32
+ const store = new rdf_js_1.N3.Store(quads);
33
+ return (0, rdf_js_1.quadsToGraph)(store);
34
34
  }
35
35
  queryVoid(query) {
36
36
  return this.engine.queryVoid(query, this.context);
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolve = exports.getResponseTypes = void 0;
4
+ const rdf_js_1 = require("../rdf.js");
5
+ const asynciterator_js_1 = require("../asynciterator.js");
6
+ class QueryResolver {
7
+ }
8
+ class BooleanJsonResolver extends QueryResolver {
9
+ async resolve(response) {
10
+ const json = await response.json();
11
+ if ("boolean" in json) {
12
+ return Boolean(json.boolean);
13
+ }
14
+ throw new Error("Boolean SPARQL query result not found");
15
+ }
16
+ }
17
+ class BindingsJsonResolver extends QueryResolver {
18
+ async resolve(response) {
19
+ const json = await response.json();
20
+ if (!Array.isArray(json.results?.bindings)) {
21
+ throw new Error("Bindings SPARQL query result not found");
22
+ }
23
+ const bindingsFactory = new rdf_js_1.BindingsFactory();
24
+ const bindingsIterator = new asynciterator_js_1.ArrayIterator(json.results.bindings);
25
+ // TODO: review the unknown type cast
26
+ return new asynciterator_js_1.MappingIterator(bindingsIterator, (i) => bindingsFactory.fromJson(i));
27
+ }
28
+ }
29
+ class QuadsJsonResolver extends QueryResolver {
30
+ async resolve(response) {
31
+ const json = await response.json();
32
+ if (!(json?.constructor === Object)) {
33
+ throw new Error("Quads SPARQL query result not found");
34
+ }
35
+ const quadFactory = new rdf_js_1.QuadFactory();
36
+ const treeIterator = new asynciterator_js_1.TreeIterator(json);
37
+ // TODO: review the unknown type cast
38
+ return new asynciterator_js_1.MappingIterator(treeIterator, (i) => quadFactory.fromJson(i));
39
+ }
40
+ }
41
+ class QuadsTurtleResolver extends QueryResolver {
42
+ async resolve(response) {
43
+ const text = await response.text();
44
+ const quads = new rdf_js_1.N3.Parser({ format: "turtle" }).parse(text);
45
+ return new asynciterator_js_1.ArrayIterator(quads);
46
+ }
47
+ }
48
+ const resolvers = {
49
+ "boolean": {
50
+ "application/sparql-results+json": new BooleanJsonResolver(),
51
+ },
52
+ "bindings": {
53
+ "application/sparql-results+json": new BindingsJsonResolver(),
54
+ },
55
+ "quads": {
56
+ "application/rdf+json": new QuadsJsonResolver(),
57
+ "text/turtle": new QuadsTurtleResolver(),
58
+ },
59
+ };
60
+ const getResponseTypes = (resolverType) => Object.keys(resolvers[resolverType]);
61
+ exports.getResponseTypes = getResponseTypes;
62
+ const resolve = (resolverType, response) => {
63
+ const contentType = response.headers.get("Content-type");
64
+ if (!contentType) {
65
+ throw new Error(`Content-type header was not found in response`);
66
+ }
67
+ const separatorPosition = contentType.indexOf(";");
68
+ const mime = separatorPosition > 0
69
+ ? contentType.substring(0, separatorPosition)
70
+ : contentType;
71
+ const resolver = resolvers[resolverType][mime];
72
+ if (!resolver) {
73
+ throw new Error(`No resolver exists for response type '${mime}'`);
74
+ }
75
+ return resolver.resolve(response);
76
+ };
77
+ exports.resolve = resolve;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });