ldkit 1.1.0 → 2.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.
Files changed (152) hide show
  1. package/README.md +15 -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 +40 -0
  6. package/esm/library/engine/query_engine_proxy.js +8 -8
  7. package/esm/library/engine/types.js +1 -0
  8. package/esm/library/lens/lens.js +416 -27
  9. package/esm/library/lens/mod.js +1 -1
  10. package/esm/library/lens/query_builder.js +86 -52
  11. package/esm/library/lens/search_helper.js +142 -0
  12. package/esm/library/lens/update_helper.js +157 -0
  13. package/esm/library/namespace.js +36 -0
  14. package/esm/library/options.js +50 -0
  15. package/esm/library/schema/data_types.js +3 -38
  16. package/esm/library/schema/search.js +1 -0
  17. package/esm/library/schema/utils.js +31 -6
  18. package/esm/library/sparql/mod.js +2 -1
  19. package/esm/library/sparql/sparql_expression_builders.js +18 -0
  20. package/esm/library/sparql/sparql_query_builders.js +65 -0
  21. package/esm/library/sparql/sparql_shared_builders.js +1 -0
  22. package/esm/library/sparql/sparql_tag.js +27 -1
  23. package/esm/library/sparql/sparql_update_builders.js +70 -0
  24. package/esm/library/sparql/stringify.js +3 -4
  25. package/esm/mod.js +4 -4
  26. package/esm/{library/namespaces → namespaces}/dbo.js +7 -2
  27. package/esm/{library/namespaces → namespaces}/dc.js +7 -2
  28. package/esm/{library/namespaces → namespaces}/dcterms.js +7 -2
  29. package/esm/{library/namespaces → namespaces}/foaf.js +7 -2
  30. package/esm/{library/namespaces → namespaces}/gr.js +7 -2
  31. package/esm/namespaces/ldkit.js +11 -0
  32. package/esm/namespaces/owl.js +91 -0
  33. package/{script/library → esm}/namespaces/rdf.js +7 -4
  34. package/{script/library → esm}/namespaces/rdfs.js +7 -4
  35. package/esm/{library/namespaces → namespaces}/schema.js +7 -2
  36. package/esm/{library/namespaces → namespaces}/sioc.js +7 -2
  37. package/{script/library → esm}/namespaces/skos.js +7 -4
  38. package/{script/library → esm}/namespaces/xsd.js +7 -4
  39. package/esm/namespaces.js +30 -1
  40. package/esm/rdf.js +14 -1
  41. package/esm/sparql.js +13 -0
  42. package/package.json +8 -8
  43. package/script/library/decoder.js +25 -38
  44. package/script/library/encoder.js +38 -24
  45. package/script/library/engine/mod.js +16 -5
  46. package/script/library/engine/query_engine.js +40 -0
  47. package/script/library/engine/query_engine_proxy.js +7 -7
  48. package/script/library/engine/types.js +2 -0
  49. package/script/library/lens/lens.js +417 -29
  50. package/script/library/lens/mod.js +15 -4
  51. package/script/library/lens/query_builder.js +87 -56
  52. package/script/library/lens/search_helper.js +146 -0
  53. package/script/library/lens/update_helper.js +161 -0
  54. package/script/library/namespace.js +40 -0
  55. package/script/library/options.js +56 -0
  56. package/script/library/schema/data_types.js +3 -41
  57. package/script/library/schema/search.js +2 -0
  58. package/script/library/schema/utils.js +33 -11
  59. package/script/library/sparql/mod.js +17 -3
  60. package/script/library/sparql/sparql_expression_builders.js +22 -0
  61. package/script/library/sparql/sparql_query_builders.js +65 -0
  62. package/script/library/sparql/sparql_shared_builders.js +3 -1
  63. package/script/library/sparql/sparql_tag.js +30 -7
  64. package/script/library/sparql/sparql_update_builders.js +70 -0
  65. package/script/library/sparql/stringify.js +4 -8
  66. package/script/mod.js +20 -11
  67. package/script/{library/namespaces → namespaces}/dbo.js +8 -2
  68. package/script/{library/namespaces → namespaces}/dc.js +8 -2
  69. package/script/{library/namespaces → namespaces}/dcterms.js +8 -2
  70. package/script/{library/namespaces → namespaces}/foaf.js +8 -2
  71. package/script/{library/namespaces → namespaces}/gr.js +8 -2
  72. package/script/namespaces/ldkit.js +14 -0
  73. package/script/namespaces/owl.js +94 -0
  74. package/{esm/library → script}/namespaces/rdf.js +10 -2
  75. package/{esm/library → script}/namespaces/rdfs.js +10 -2
  76. package/script/{library/namespaces → namespaces}/schema.js +8 -2
  77. package/script/{library/namespaces → namespaces}/sioc.js +8 -2
  78. package/{esm/library → script}/namespaces/skos.js +10 -2
  79. package/{esm/library → script}/namespaces/xsd.js +10 -2
  80. package/script/namespaces.js +44 -1
  81. package/script/rdf.js +20 -15
  82. package/script/sparql.js +13 -0
  83. package/types/library/decoder.d.ts +4 -3
  84. package/types/library/encoder.d.ts +5 -3
  85. package/types/library/engine/mod.d.ts +2 -2
  86. package/types/library/engine/query_engine.d.ts +50 -9
  87. package/types/library/engine/query_engine_proxy.d.ts +4 -3
  88. package/types/library/engine/types.d.ts +23 -0
  89. package/types/library/lens/lens.d.ts +401 -24
  90. package/types/library/lens/mod.d.ts +1 -1
  91. package/types/library/lens/query_builder.d.ts +11 -11
  92. package/types/library/lens/search_helper.d.ts +21 -0
  93. package/types/library/lens/types.d.ts +5 -2
  94. package/types/library/lens/update_helper.d.ts +23 -0
  95. package/types/library/namespace.d.ts +41 -0
  96. package/types/library/options.d.ts +70 -0
  97. package/types/library/rdf.d.ts +8 -15
  98. package/types/library/schema/data_types.d.ts +44 -37
  99. package/types/library/schema/interface.d.ts +75 -20
  100. package/types/library/schema/mod.d.ts +4 -2
  101. package/types/library/schema/schema.d.ts +23 -16
  102. package/types/library/schema/search.d.ts +20 -0
  103. package/types/library/schema/utils.d.ts +3 -3
  104. package/types/library/sparql/mod.d.ts +2 -1
  105. package/types/library/sparql/sparql_expression_builders.d.ts +19 -0
  106. package/types/library/sparql/sparql_query_builders.d.ts +68 -3
  107. package/types/library/sparql/sparql_shared_builders.d.ts +1 -0
  108. package/types/library/sparql/sparql_tag.d.ts +29 -1
  109. package/types/library/sparql/sparql_update_builders.d.ts +68 -0
  110. package/types/mod.d.ts +5 -6
  111. package/types/namespaces/dbo.d.ts +10 -0
  112. package/types/namespaces/dc.d.ts +10 -0
  113. package/types/namespaces/dcterms.d.ts +10 -0
  114. package/types/namespaces/foaf.d.ts +10 -0
  115. package/types/namespaces/gr.d.ts +10 -0
  116. package/types/namespaces/ldkit.d.ts +10 -0
  117. package/types/namespaces/owl.d.ts +10 -0
  118. package/types/namespaces/rdf.d.ts +10 -0
  119. package/types/namespaces/rdfs.d.ts +10 -0
  120. package/types/namespaces/schema.d.ts +10 -0
  121. package/types/namespaces/sioc.d.ts +10 -0
  122. package/types/namespaces/skos.d.ts +10 -0
  123. package/types/namespaces/xsd.d.ts +10 -0
  124. package/types/namespaces.d.ts +30 -1
  125. package/types/rdf.d.ts +14 -1
  126. package/types/sparql.d.ts +13 -0
  127. package/esm/library/global.js +0 -25
  128. package/esm/library/lens/query_helper.js +0 -102
  129. package/esm/library/namespaces/ldkit.js +0 -6
  130. package/esm/library/namespaces/mod.js +0 -13
  131. package/esm/library/namespaces/namespace.js +0 -11
  132. package/script/library/global.js +0 -32
  133. package/script/library/lens/query_helper.js +0 -106
  134. package/script/library/namespaces/ldkit.js +0 -8
  135. package/script/library/namespaces/mod.js +0 -32
  136. package/script/library/namespaces/namespace.js +0 -15
  137. package/types/library/global.d.ts +0 -5
  138. package/types/library/lens/query_helper.d.ts +0 -19
  139. package/types/library/namespaces/dbo.d.ts +0 -3649
  140. package/types/library/namespaces/dc.d.ts +0 -21
  141. package/types/library/namespaces/dcterms.d.ts +0 -104
  142. package/types/library/namespaces/foaf.d.ts +0 -69
  143. package/types/library/namespaces/gr.d.ts +0 -175
  144. package/types/library/namespaces/ldkit.d.ts +0 -7
  145. package/types/library/namespaces/mod.d.ts +0 -13
  146. package/types/library/namespaces/namespace.d.ts +0 -15
  147. package/types/library/namespaces/rdf.d.ts +0 -28
  148. package/types/library/namespaces/rdfs.d.ts +0 -21
  149. package/types/library/namespaces/schema.d.ts +0 -2697
  150. package/types/library/namespaces/sioc.d.ts +0 -105
  151. package/types/library/namespaces/skos.d.ts +0 -38
  152. package/types/library/namespaces/xsd.d.ts +0 -56
package/README.md CHANGED
@@ -40,7 +40,7 @@ import * as ldkit from "https://deno.land/x/ldkit/mod.ts";
40
40
  ### Create data schema and set up RDF source
41
41
 
42
42
  ```ts
43
- import { type Context, createLens } from "ldkit";
43
+ import { createLens, type Options } from "ldkit";
44
44
  import { dbo, rdfs, xsd } from "ldkit/namespaces";
45
45
 
46
46
  // Create a schema
@@ -54,14 +54,14 @@ const PersonSchema = {
54
54
  },
55
55
  } as const;
56
56
 
57
- // Create a context for query engine
58
- const context: Context = {
57
+ // Create options for query engine
58
+ const options: Options = {
59
59
  sources: ["https://dbpedia.org/sparql"], // SPARQL endpoint
60
60
  language: "en", // Preferred language
61
61
  };
62
62
 
63
- // Create a resource using the data schema and context above
64
- const Persons = createLens(PersonSchema, context);
63
+ // Create a resource using the data schema and options above
64
+ const Persons = createLens(PersonSchema, options);
65
65
  ```
66
66
 
67
67
  ### List all available data
@@ -110,6 +110,16 @@ Persons.delete("http://dbpedia.org/resource/Alan_Turing");
110
110
 
111
111
  More complex examples can be found in [documentation](https://ldkit.io/docs).
112
112
 
113
+ ## Specification Compliance
114
+
115
+ LDkit complies with the following specifications:
116
+
117
+ - [RDF/JS: Data model specification](https://rdf.js.org/data-model-spec/)
118
+ - [RDF/JS: Query specification](https://rdf.js.org/query-spec/)
119
+ - [SPARQL 1.1 Query Language](https://www.w3.org/TR/sparql11-query/)
120
+ - [SPARQL 1.1 Update](https://www.w3.org/TR/2013/REC-sparql11-update-20130321/)
121
+ - [SPARQL 1.1 Protocol](https://www.w3.org/TR/2013/REC-sparql11-protocol-20130321/)
122
+
113
123
  ## License
114
124
 
115
125
  [MIT License](./LICENSE.md)
@@ -1,8 +1,11 @@
1
1
  import { fromRdf } from "./rdf.js";
2
- import ldkit from "./namespaces/ldkit.js";
3
- import rdf from "./namespaces/rdf.js";
2
+ import { ldkit } from "../namespaces/ldkit.js";
3
+ import { rdf } from "../namespaces/rdf.js";
4
+ export const decode = (graph, schema, options) => {
5
+ return Decoder.decode(graph, schema, options);
6
+ };
4
7
  class Decoder {
5
- constructor(graph, schema, context) {
8
+ constructor(graph, schema, options) {
6
9
  Object.defineProperty(this, "graph", {
7
10
  enumerable: true,
8
11
  configurable: true,
@@ -15,7 +18,7 @@ class Decoder {
15
18
  writable: true,
16
19
  value: void 0
17
20
  });
18
- Object.defineProperty(this, "context", {
21
+ Object.defineProperty(this, "options", {
19
22
  enumerable: true,
20
23
  configurable: true,
21
24
  writable: true,
@@ -29,10 +32,10 @@ class Decoder {
29
32
  });
30
33
  this.graph = graph;
31
34
  this.schema = schema;
32
- this.context = context;
35
+ this.options = options;
33
36
  }
34
- static decode(graph, schema, context) {
35
- return new Decoder(graph, schema, context).decode();
37
+ static decode(graph, schema, options) {
38
+ return new Decoder(graph, schema, options).decode();
36
39
  }
37
40
  getCachedNode(nodeIri, schema) {
38
41
  if (!this.cache.has(schema)) {
@@ -75,7 +78,6 @@ class Decoder {
75
78
  if (!node) {
76
79
  throw new Error(`Error decoding graph, <${nodeIri}> node not found.`);
77
80
  }
78
- output.$type = this.decodeNodeType(node);
79
81
  Object.keys(schema).forEach((key) => {
80
82
  if (key === "@type") {
81
83
  return;
@@ -88,20 +90,11 @@ class Decoder {
88
90
  this.setCachedNode(nodeIri, schema, output);
89
91
  return output;
90
92
  }
91
- decodeNodeType(node) {
92
- const typeTerms = node.get(rdf.type);
93
- if (!typeTerms) {
94
- return [];
95
- }
96
- return typeTerms.reduce((acc, term) => {
97
- if (term.value !== ldkit.Resource) {
98
- acc.push(term.value);
99
- }
100
- return acc;
101
- }, []);
102
- }
103
93
  decodeNodeProperty(nodeIri, node, propertyKey, property) {
104
- const terms = node.get(property["@id"]);
94
+ const allTerms = node.get(property["@id"]);
95
+ const terms = property["@id"] !== rdf.type
96
+ ? allTerms
97
+ : allTerms?.filter((term) => term.value !== ldkit.Resource);
105
98
  if (!terms) {
106
99
  if (!property["@optional"]) {
107
100
  // No data, required property
@@ -109,7 +102,7 @@ class Decoder {
109
102
  }
110
103
  else {
111
104
  // No data, optional property
112
- return property["@array"] ? [] : undefined;
105
+ return property["@array"] ? [] : null;
113
106
  }
114
107
  }
115
108
  if (property["@multilang"]) {
@@ -141,13 +134,13 @@ class Decoder {
141
134
  }
142
135
  }
143
136
  if (property["@array"]) {
144
- if (property["@context"]) {
137
+ if (property["@schema"]) {
145
138
  // Collection of resources specified by sub schema
146
139
  return terms.map((term) => {
147
140
  if (term.termType !== "NamedNode" && term.termType !== "BlankNode") {
148
141
  throw new Error(`Property "${propertyKey}" data type mismatch - expected a node, but received ${term.termType} instead on resource <${nodeIri}>`);
149
142
  }
150
- return this.decodeNode(term.value, property["@context"]);
143
+ return this.decodeNode(term.value, property["@schema"]);
151
144
  });
152
145
  }
153
146
  else {
@@ -161,15 +154,15 @@ class Decoder {
161
154
  }
162
155
  }
163
156
  // Single return value expected from this point on
164
- if (property["@context"]) {
157
+ if (property["@schema"]) {
165
158
  for (const term of terms) {
166
159
  if (term.termType === "NamedNode" || term.termType === "BlankNode") {
167
- return this.decodeNode(term.value, property["@context"]);
160
+ return this.decodeNode(term.value, property["@schema"]);
168
161
  }
169
162
  }
170
163
  throw new Error(`Property "${propertyKey}" data type mismatch - expected a named node for context on resource <${nodeIri}>`);
171
164
  }
172
- const preferredLanguage = this.context.language;
165
+ const preferredLanguage = this.options.language;
173
166
  if (preferredLanguage) {
174
167
  // Try to find a term that corresponds to the preferred language
175
168
  for (const term of terms) {
@@ -199,6 +192,3 @@ class Decoder {
199
192
  }
200
193
  }
201
194
  }
202
- export const decode = (graph, schema, context) => {
203
- return Decoder.decode(graph, schema, context);
204
- };
@@ -1,12 +1,27 @@
1
1
  import { DataFactory, toRdf } from "./rdf.js";
2
- import xsd from "./namespaces/xsd.js";
3
- import rdf from "./namespaces/rdf.js";
4
- export const encode = (node, schema, context, variableInitCounter = 0) => {
5
- return Encoder.encode(node, schema, context, variableInitCounter);
2
+ import { xsd } from "../namespaces/xsd.js";
3
+ import { rdf } from "../namespaces/rdf.js";
4
+ import { ldkit } from "../namespaces/ldkit.js";
5
+ export const encode = (node, schema, options, includeType = true, variableInitCounter = 0) => {
6
+ return Encoder.encode(node, schema, options, includeType, variableInitCounter);
7
+ };
8
+ export const encodeValue = (value, datatype, df) => {
9
+ if (datatype === ldkit.IRI) {
10
+ return df.namedNode(value);
11
+ }
12
+ return toRdf(value, {
13
+ datatype: df.namedNode(datatype),
14
+ });
6
15
  };
7
16
  class Encoder {
8
- constructor(context, variableInitCounter) {
9
- Object.defineProperty(this, "context", {
17
+ constructor(options, includeType, variableInitCounter) {
18
+ Object.defineProperty(this, "options", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: void 0
23
+ });
24
+ Object.defineProperty(this, "includeType", {
10
25
  enumerable: true,
11
26
  configurable: true,
12
27
  writable: true,
@@ -32,11 +47,12 @@ class Encoder {
32
47
  writable: true,
33
48
  value: []
34
49
  });
35
- this.context = context;
50
+ this.options = options;
51
+ this.includeType = includeType;
36
52
  this.variableCounter = variableInitCounter;
37
53
  }
38
- static encode(node, schema, context, variableInitCounter) {
39
- return new Encoder(context, variableInitCounter).encode(node, schema);
54
+ static encode(node, schema, options, includeType, variableInitCounter) {
55
+ return new Encoder(options, includeType, variableInitCounter).encode(node, schema);
40
56
  }
41
57
  encode(node, schema) {
42
58
  const nodeId = this.getNodeId(node);
@@ -58,7 +74,9 @@ class Encoder {
58
74
  return node.$type ? [node.$type] : [];
59
75
  }
60
76
  encodeNode(node, schema, nodeId) {
61
- this.encodeNodeType(node, schema["@type"], nodeId);
77
+ if (this.includeType) {
78
+ this.encodeNodeType(node, schema["@type"], nodeId);
79
+ }
62
80
  Object.keys(schema).forEach((key) => {
63
81
  if (key === "@type") {
64
82
  return;
@@ -96,22 +114,20 @@ class Encoder {
96
114
  }
97
115
  const values = Array.isArray(value) ? value : [value];
98
116
  values.forEach((val) => {
99
- if (property["@context"]) {
117
+ if (property["@schema"]) {
100
118
  const subNodeId = this.getNodeId(val);
101
- this.encodeNode(val, property["@context"], subNodeId);
119
+ this.encodeNode(val, property["@schema"], subNodeId);
102
120
  this.push(nodeId, propertyId, subNodeId);
103
121
  return;
104
122
  }
105
123
  const propertyType = property["@type"] ? property["@type"] : xsd.string;
106
- if (typeof val === "string" && this.context.language) {
124
+ if (typeof val === "string" && this.options.language) {
107
125
  if (propertyType === xsd.string || propertyType === rdf.langString) {
108
- this.push(nodeId, propertyId, this.df.literal(val, this.context.language));
126
+ this.push(nodeId, propertyId, this.df.literal(val, this.options.language));
109
127
  return;
110
128
  }
111
129
  }
112
- const rdfValue = toRdf(val, {
113
- datatype: this.df.namedNode(propertyType),
114
- });
130
+ const rdfValue = encodeValue(val, propertyType, this.df);
115
131
  this.push(nodeId, propertyId, rdfValue);
116
132
  });
117
133
  }
@@ -1,2 +1,2 @@
1
- export { QueryEngine } from "./query_engine.js";
2
- export { QueryEngineProxy } from "./query_engine_proxy.js";
1
+ export * from "./query_engine.js";
2
+ export * from "./types.js";
@@ -1,4 +1,16 @@
1
1
  import { getResponseTypes, resolve, } from "./query_resolvers.js";
2
+ /**
3
+ * A query engine that can query a SPARQL endpoint.
4
+ *
5
+ * Implements {@link IQueryEngine} interface.
6
+ *
7
+ * This engine is used by default if no other engine is configured.
8
+ * See {@link Options} for more details.
9
+ *
10
+ * If you need to query other data sources, or multiple SPARQL endpoints,
11
+ * you can use [Comunica](https://comunica.dev) instead, extend this engine,
12
+ * or implement your own.
13
+ */
2
14
  export class QueryEngine {
3
15
  getSparqlEndpoint(context) {
4
16
  if (!context) {
@@ -52,15 +64,43 @@ export class QueryEngine {
52
64
  }
53
65
  return resolve(type, response);
54
66
  }
67
+ /**
68
+ * Executes a SPARQL SELECT query and returns a stream of bindings.
69
+ *
70
+ * @param query SPARQL query string
71
+ * @param context Engine context
72
+ * @returns Stream of bindings
73
+ */
55
74
  queryBindings(query, context) {
56
75
  return this.queryAndResolve("bindings", query, context);
57
76
  }
77
+ /**
78
+ * Executes a SPARQL ASK query and returns a boolean result.
79
+ *
80
+ * @param query SPARQL query string
81
+ * @param context Engine context
82
+ * @returns Boolean result
83
+ */
58
84
  queryBoolean(query, context) {
59
85
  return this.queryAndResolve("boolean", query, context);
60
86
  }
87
+ /**
88
+ * Executes a SPARQL CONSTRUCT query and returns a stream of quads.
89
+ *
90
+ * @param query SPARQL query string
91
+ * @param context Engine context
92
+ * @returns Stream of quads
93
+ */
61
94
  queryQuads(query, context) {
62
95
  return this.queryAndResolve("quads", query, context);
63
96
  }
97
+ /**
98
+ * Executes a SPARQL UPDATE query and returns nothing.
99
+ *
100
+ * @param query SPARQL query string
101
+ * @param context Engine context
102
+ * @returns Nothing
103
+ */
64
104
  async queryVoid(query, context) {
65
105
  await this.query(query, "application/sparql-results+json", context);
66
106
  }
@@ -1,21 +1,20 @@
1
- import { quadsToGraph } from "../rdf.js";
2
- import { resolveContext, resolveEngine } from "../global.js";
1
+ import { N3, quadsToGraph } from "../rdf.js";
3
2
  export class QueryEngineProxy {
4
- constructor(context, engine) {
5
- Object.defineProperty(this, "context", {
3
+ constructor(engine, context) {
4
+ Object.defineProperty(this, "engine", {
6
5
  enumerable: true,
7
6
  configurable: true,
8
7
  writable: true,
9
8
  value: void 0
10
9
  });
11
- Object.defineProperty(this, "engine", {
10
+ Object.defineProperty(this, "context", {
12
11
  enumerable: true,
13
12
  configurable: true,
14
13
  writable: true,
15
14
  value: void 0
16
15
  });
17
- this.context = resolveContext(context);
18
- this.engine = resolveEngine(engine);
16
+ this.engine = engine;
17
+ this.context = context;
19
18
  }
20
19
  queryBoolean(query) {
21
20
  return this.engine.queryBoolean(query, this.context);
@@ -27,7 +26,8 @@ export class QueryEngineProxy {
27
26
  async queryGraph(query) {
28
27
  const quadStream = await this.engine.queryQuads(query, this.context);
29
28
  const quads = await (quadStream.toArray());
30
- return quadsToGraph(quads);
29
+ const store = new N3.Store(quads);
30
+ return quadsToGraph(store);
31
31
  }
32
32
  queryVoid(query) {
33
33
  return this.engine.queryVoid(query, this.context);
@@ -0,0 +1 @@
1
+ export {};