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