ldkit 0.6.3 → 0.6.5

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 (53) hide show
  1. package/esm/library/decoder.js +2 -1
  2. package/esm/library/encoder.js +7 -5
  3. package/esm/library/rdf.js +89 -11
  4. package/esm/library/resource/query_builder.js +35 -34
  5. package/esm/library/schema/data_types.js +2 -1
  6. package/esm/library/schema/utils.js +1 -1
  7. package/esm/library/sparql/escape.js +22 -0
  8. package/esm/library/sparql/mod.js +3 -0
  9. package/esm/library/sparql/sparql_query_builders.js +35 -0
  10. package/esm/library/sparql/sparql_shared_builders.js +40 -0
  11. package/esm/library/sparql/sparql_tag.js +47 -0
  12. package/esm/library/sparql/sparql_update_builders.js +15 -0
  13. package/esm/library/sparql/stringify.js +50 -0
  14. package/esm/sparql.js +1 -1
  15. package/package.json +1 -4
  16. package/script/library/decoder.js +11 -7
  17. package/script/library/encoder.js +13 -8
  18. package/script/library/rdf.js +90 -10
  19. package/script/library/resource/query_builder.js +46 -42
  20. package/script/library/schema/data_types.js +39 -35
  21. package/script/library/schema/utils.js +6 -3
  22. package/script/library/sparql/escape.js +26 -0
  23. package/script/library/sparql/mod.js +14 -0
  24. package/script/library/sparql/sparql_query_builders.js +38 -0
  25. package/script/library/sparql/sparql_shared_builders.js +47 -0
  26. package/script/library/sparql/sparql_tag.js +54 -0
  27. package/script/library/sparql/sparql_update_builders.js +18 -0
  28. package/script/library/sparql/stringify.js +61 -0
  29. package/script/sparql.js +1 -1
  30. package/types/library/namespaces/dbo.d.ts +7 -7
  31. package/types/library/namespaces/dc.d.ts +2 -2
  32. package/types/library/namespaces/dcterms.d.ts +3 -3
  33. package/types/library/namespaces/gr.d.ts +1 -1
  34. package/types/library/namespaces/rdf.d.ts +1 -1
  35. package/types/library/namespaces/rdfs.d.ts +1 -1
  36. package/types/library/namespaces/schema.d.ts +4 -4
  37. package/types/library/namespaces/sioc.d.ts +2 -2
  38. package/types/library/namespaces/xsd.d.ts +5 -5
  39. package/types/library/rdf.d.ts +32 -13
  40. package/types/library/resource/types.d.ts +2 -2
  41. package/types/library/schema/interface.d.ts +2 -2
  42. package/types/library/schema/mod.d.ts +1 -1
  43. package/types/library/sparql/escape.d.ts +1 -0
  44. package/types/library/sparql/mod.d.ts +3 -0
  45. package/types/library/sparql/sparql_query_builders.d.ts +1178 -0
  46. package/types/library/sparql/sparql_shared_builders.d.ts +13 -0
  47. package/types/library/sparql/sparql_tag.d.ts +3 -0
  48. package/types/library/sparql/sparql_update_builders.d.ts +131 -0
  49. package/types/library/sparql/stringify.d.ts +6 -0
  50. package/types/sparql.d.ts +1 -1
  51. package/esm/library/sparql.js +0 -2
  52. package/script/library/sparql.js +0 -13
  53. package/types/library/sparql.d.ts +0 -2
@@ -1,5 +1,6 @@
1
1
  import { fromRdf } from "./rdf.js";
2
- import { ldkit, rdf } from "./namespaces/mod.js";
2
+ import ldkit from "./namespaces/ldkit.js";
3
+ import rdf from "./namespaces/rdf.js";
3
4
  class Decoder {
4
5
  constructor(graph, schema, context) {
5
6
  Object.defineProperty(this, "graph", {
@@ -1,5 +1,6 @@
1
1
  import { DataFactory, toRdf } from "./rdf.js";
2
- import { rdf, xsd } from "./namespaces/mod.js";
2
+ import xsd from "./namespaces/xsd.js";
3
+ import rdf from "./namespaces/rdf.js";
3
4
  export const encode = (node, schema, context, variableInitCounter = 0) => {
4
5
  return Encoder.encode(node, schema, context, variableInitCounter);
5
6
  };
@@ -82,10 +83,11 @@ class Encoder {
82
83
  return;
83
84
  }
84
85
  if (property["@multilang"]) {
85
- Object.keys(value).forEach((language) => {
86
- const languageValue = Array.isArray(value[language])
87
- ? value[language]
88
- : [value[language]];
86
+ const multiValue = value;
87
+ Object.keys(multiValue).forEach((language) => {
88
+ const languageValue = Array.isArray(multiValue[language])
89
+ ? multiValue[language]
90
+ : [multiValue[language]];
89
91
  languageValue.forEach((singleValue) => {
90
92
  this.push(nodeId, propertyId, this.df.literal(singleValue, language.length > 0 ? language : undefined));
91
93
  });
@@ -1,7 +1,6 @@
1
1
  export { fromRdf, toRdf } from "rdf-literal";
2
- import { DataFactory } from "rdf-data-factory";
3
- export { DataFactory };
4
- import { BindingsFactory as ComunicaBindingsFactory } from "@comunica/bindings-factory";
2
+ import { DataFactory, DefaultGraph, } from "rdf-data-factory";
3
+ export { DataFactory, DefaultGraph };
5
4
  export const quadsToGraph = (quads) => {
6
5
  const graph = new Map();
7
6
  for (const quad of quads) {
@@ -39,10 +38,89 @@ export class TermFactory {
39
38
  return this.dataFactory.literal(jsonTerm.value);
40
39
  }
41
40
  }
42
- export class BindingsFactory extends ComunicaBindingsFactory {
43
- constructor(dataFactory = new DataFactory(), termFactory = new TermFactory()) {
44
- super(dataFactory);
45
- Object.defineProperty(this, "localDataFactory", {
41
+ export class ReadOnlyBindings {
42
+ constructor(bindings, dataFactory = new DataFactory()) {
43
+ Object.defineProperty(this, "type", {
44
+ enumerable: true,
45
+ configurable: true,
46
+ writable: true,
47
+ value: "bindings"
48
+ });
49
+ Object.defineProperty(this, "dataFactory", {
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true,
53
+ value: void 0
54
+ });
55
+ Object.defineProperty(this, "entries", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: void 0
60
+ });
61
+ Object.defineProperty(this, "variables", {
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true,
65
+ value: void 0
66
+ });
67
+ this.entries = bindings;
68
+ this.dataFactory = dataFactory;
69
+ this.variables = new Map();
70
+ for (const variable of bindings.keys()) {
71
+ this.variables.set(variable.value, variable);
72
+ }
73
+ }
74
+ has(key) {
75
+ const stringKey = typeof key === "string" ? key : key.value;
76
+ const variableKey = this.variables.get(stringKey);
77
+ return this.entries.has(variableKey);
78
+ }
79
+ get(key) {
80
+ const stringKey = typeof key === "string" ? key : key.value;
81
+ const variableKey = this.variables.get(stringKey);
82
+ return this.entries.get(variableKey);
83
+ }
84
+ set(_key, _value) {
85
+ throw new Error("Method not implemented.");
86
+ }
87
+ delete(_key) {
88
+ throw new Error("Method not implemented.");
89
+ }
90
+ keys() {
91
+ return this.entries.keys();
92
+ }
93
+ values() {
94
+ return this.entries.values();
95
+ }
96
+ forEach(fn) {
97
+ return this.entries.forEach(fn);
98
+ }
99
+ get size() {
100
+ return this.entries.size;
101
+ }
102
+ [Symbol.iterator]() {
103
+ return this.entries.entries();
104
+ }
105
+ equals(_other) {
106
+ throw new Error("Method not implemented.");
107
+ }
108
+ filter(_fn) {
109
+ throw new Error("Method not implemented.");
110
+ }
111
+ map(_fn) {
112
+ throw new Error("Method not implemented.");
113
+ }
114
+ merge(_other) {
115
+ throw new Error("Method not implemented.");
116
+ }
117
+ mergeWith(_merger, _other) {
118
+ throw new Error("Method not implemented.");
119
+ }
120
+ }
121
+ export class BindingsFactory {
122
+ constructor(dataFactory = new DataFactory(), termFactory = new TermFactory(dataFactory)) {
123
+ Object.defineProperty(this, "dataFactory", {
46
124
  enumerable: true,
47
125
  configurable: true,
48
126
  writable: true,
@@ -54,21 +132,21 @@ export class BindingsFactory extends ComunicaBindingsFactory {
54
132
  writable: true,
55
133
  value: void 0
56
134
  });
57
- this.localDataFactory = dataFactory;
135
+ this.dataFactory = dataFactory;
58
136
  this.termFactory = termFactory;
59
137
  }
60
138
  fromJson(jsonBindings) {
61
139
  const bindingsEntries = Object.entries(jsonBindings).map(([varName, jsonTerm]) => {
62
140
  return [
63
- this.localDataFactory.variable(varName),
141
+ this.dataFactory.variable(varName),
64
142
  this.termFactory.fromJson(jsonTerm),
65
143
  ];
66
144
  });
67
- return this.bindings(bindingsEntries);
145
+ return new ReadOnlyBindings(new Map(bindingsEntries), this.dataFactory);
68
146
  }
69
147
  }
70
148
  export class QuadFactory {
71
- constructor(dataFactory = new DataFactory(), termFactory = new TermFactory()) {
149
+ constructor(dataFactory = new DataFactory(), termFactory = new TermFactory(dataFactory)) {
72
150
  Object.defineProperty(this, "dataFactory", {
73
151
  enumerable: true,
74
152
  configurable: true,
@@ -1,7 +1,8 @@
1
1
  import { getSchemaProperties } from "../schema/mod.js";
2
- import { $, CONSTRUCT, DELETE, INSERT, SELECT } from "../sparql.js";
2
+ import { CONSTRUCT, DELETE, INSERT, SELECT, sparql as $, } from "../sparql/mod.js";
3
3
  import { DataFactory } from "../rdf.js";
4
- import { ldkit, rdf } from "../namespaces/mod.js";
4
+ import ldkit from "../namespaces/ldkit.js";
5
+ import rdf from "../namespaces/rdf.js";
5
6
  import { encode } from "../encoder.js";
6
7
  import { QueryHelper } from "./query_helper.js";
7
8
  export class QueryBuilder {
@@ -35,11 +36,11 @@ export class QueryBuilder {
35
36
  configurable: true,
36
37
  writable: true,
37
38
  value: (iris) => {
38
- return DELETE `
39
- ?s ?p ?o
40
- `.WHERE `
41
- ?s ?p ?o .
42
- VALUES ?s { ${iris.map(this.df.namedNode)} }
39
+ return DELETE `
40
+ ?s ?p ?o
41
+ `.WHERE `
42
+ ?s ?p ?o .
43
+ VALUES ?s { ${iris.map(this.df.namedNode)} }
43
44
  `.build();
44
45
  }
45
46
  });
@@ -95,36 +96,36 @@ export class QueryBuilder {
95
96
  return SELECT `(count(?iri) as ?count)`.WHERE `${quads}`.build();
96
97
  }
97
98
  getQuery(where, limit = 1000) {
98
- const selectSubQuery = SELECT `
99
- ${this.df.variable("iri")}
100
- `.WHERE `
101
- ${this.getShape(false, true)}
102
- ${where}
103
- `.LIMIT(limit);
104
- const query = CONSTRUCT `
105
- ${this.getResourceSignature()}
106
- ${this.getTypesSignature()}
107
- ${this.getShape(true, false, true)}
108
- `.WHERE `
109
- ${this.getTypesSignature()}
110
- ${this.getShape(true, true, true)}
111
- {
112
- ${selectSubQuery}
113
- }
99
+ const selectSubQuery = SELECT `
100
+ ${this.df.variable("iri")}
101
+ `.WHERE `
102
+ ${this.getShape(false, true)}
103
+ ${where}
104
+ `.LIMIT(limit).build();
105
+ const query = CONSTRUCT `
106
+ ${this.getResourceSignature()}
107
+ ${this.getTypesSignature()}
108
+ ${this.getShape(true, false, true)}
109
+ `.WHERE `
110
+ ${this.getTypesSignature()}
111
+ ${this.getShape(true, true, true)}
112
+ {
113
+ ${selectSubQuery}
114
+ }
114
115
  `.build();
115
116
  return query;
116
117
  }
117
118
  getByIrisQuery(iris) {
118
- const query = CONSTRUCT `
119
- ${this.getResourceSignature()}
120
- ${this.getTypesSignature()}
121
- ${this.getShape(true, false, true)}
122
- `.WHERE `
123
- ${this.getTypesSignature()}
124
- ${this.getShape(true, true, true)}
125
- VALUES ?iri {
126
- ${iris.map(this.df.namedNode)}
127
- }
119
+ const query = CONSTRUCT `
120
+ ${this.getResourceSignature()}
121
+ ${this.getTypesSignature()}
122
+ ${this.getShape(true, false, true)}
123
+ `.WHERE `
124
+ ${this.getTypesSignature()}
125
+ ${this.getShape(true, true, true)}
126
+ VALUES ?iri {
127
+ ${iris.map(this.df.namedNode)}
128
+ }
128
129
  `.build();
129
130
  return query;
130
131
  }
@@ -136,7 +137,7 @@ export class QueryBuilder {
136
137
  return INSERT.DATA `${quads}`.build();
137
138
  }
138
139
  deleteDataQuery(quads) {
139
- return $ `DELETE DATA { ${quads} }`.toString();
140
+ return DELETE.DATA `${quads}`.build();
140
141
  }
141
142
  updateQuery(entities) {
142
143
  const deleteQuads = [];
@@ -1,4 +1,5 @@
1
- import { rdf, xsd } from "../namespaces/mod.js";
1
+ import xsd from "../namespaces/xsd.js";
2
+ import rdf from "../namespaces/rdf.js";
2
3
  const SupportedDataTypesPrototype = {
3
4
  [xsd.dateTime]: new Date(),
4
5
  [xsd.date]: new Date(),
@@ -1,4 +1,4 @@
1
- import { xsd } from "../namespaces/mod.js";
1
+ import xsd from "../namespaces/xsd.js";
2
2
  export const expandSchema = (schemaPrototype) => {
3
3
  const expandArray = (stringOrStrings) => {
4
4
  return Array.isArray(stringOrStrings) ? stringOrStrings : [stringOrStrings];
@@ -0,0 +1,22 @@
1
+ const escapeCharRegEx = /["\\\n\r]/;
2
+ const escapeCharRegExAll = /["\\\n\r]/g;
3
+ const replacer = (char) => {
4
+ switch (char) {
5
+ case `"`:
6
+ return `\\"`;
7
+ case `\\`:
8
+ return `\\\\`;
9
+ case `\n`:
10
+ return `\\n`;
11
+ case `\r`:
12
+ return `\\r`;
13
+ default:
14
+ return char; // this should never happen
15
+ }
16
+ };
17
+ export const escape = (value) => {
18
+ if (escapeCharRegEx.test(value)) {
19
+ return value.replace(escapeCharRegExAll, replacer);
20
+ }
21
+ return value;
22
+ };
@@ -0,0 +1,3 @@
1
+ export { sparql } from "./sparql_tag.js";
2
+ export { ASK, CONSTRUCT, DESCRIBE, SELECT } from "./sparql_query_builders.js";
3
+ export { DELETE, INSERT, WITH } from "./sparql_update_builders.js";
@@ -0,0 +1,35 @@
1
+ import { build, createNumericBuilder, createTemplateBuilder, } from "./sparql_shared_builders.js";
2
+ const OFFSET = createNumericBuilder((value) => `OFFSET ${value}\n`, { build });
3
+ const LIMIT = createNumericBuilder((value) => `LIMIT ${value}\n`, { build, OFFSET });
4
+ const ORDER_BY = createTemplateBuilder((value) => `ORDER BY ${value}\n`, { build, LIMIT });
5
+ const HAVING = createTemplateBuilder((value) => `HAVING(${value})\n`, { build, LIMIT });
6
+ const GROUP_BY = createTemplateBuilder((value) => `GROUP BY ${value}\n`, { build, HAVING, ORDER_BY, LIMIT });
7
+ const WHERE = createTemplateBuilder((value) => `WHERE {\n${value}\n}\n`, { build, GROUP_BY, ORDER_BY, LIMIT });
8
+ const FROM_NAMED = createTemplateBuilder((value) => `FROM NAMED ${value}\n`, { WHERE });
9
+ const FROM = createTemplateBuilder((value) => `FROM ${value}\n`, { FROM_NAMED, WHERE });
10
+ const _SELECT = createTemplateBuilder((value) => `SELECT ${value}\n`, { FROM, FROM_NAMED, WHERE });
11
+ const DISTINCT = createTemplateBuilder((value) => `SELECT DISTINCT ${value}\n`, { FROM, WHERE });
12
+ const REDUCED = createTemplateBuilder((value) => `SELECT REDUCED ${value}\n`, {
13
+ FROM,
14
+ WHERE,
15
+ });
16
+ export const SELECT = Object.assign(_SELECT, {
17
+ DISTINCT,
18
+ REDUCED,
19
+ ALL: _SELECT `*`,
20
+ });
21
+ const _CONSTRUCT = createTemplateBuilder((value) => `CONSTRUCT {\n${value}\n}\n`, { WHERE });
22
+ const _CONSTRUCT_WHERE = createTemplateBuilder((value) => `CONSTRUCT WHERE {\n${value}\n}\n`, { build, GROUP_BY, ORDER_BY, LIMIT });
23
+ export const CONSTRUCT = Object.assign(_CONSTRUCT, {
24
+ WHERE: _CONSTRUCT_WHERE,
25
+ });
26
+ const _ASK = createTemplateBuilder((value) => `ASK {\n${value}\n}\n`, { build });
27
+ const _ASK_FROM = createTemplateBuilder((value) => `ASK\nFROM ${value}\n`, { FROM_NAMED, WHERE });
28
+ const _ASK_FROM_NAMED = createTemplateBuilder((value) => `ASK\nFROM NAMED ${value}\n`, { FROM_NAMED, WHERE });
29
+ const _ASK_WHERE = createTemplateBuilder((value) => `ASK\nWHERE {\n${value}\n}\n`, { build, GROUP_BY, ORDER_BY, LIMIT });
30
+ export const ASK = Object.assign(_ASK, {
31
+ FROM: _ASK_FROM,
32
+ FROM_NAMED: _ASK_FROM_NAMED,
33
+ WHERE: _ASK_WHERE,
34
+ });
35
+ export const DESCRIBE = createTemplateBuilder((value) => `DESCRIBE ${value}\n`, { build, FROM, FROM_NAMED, WHERE });
@@ -0,0 +1,40 @@
1
+ import { DataFactory } from "../rdf.js";
2
+ import { sparql } from "./sparql_tag.js";
3
+ const createContext = (self, current, builders) => {
4
+ const previousQuery = self?.$partialQuery || "";
5
+ const context = { $partialQuery: previousQuery + current };
6
+ return Object.keys(builders).reduce((acc, key) => {
7
+ acc[key] = builders[key].bind(context);
8
+ return acc;
9
+ }, {});
10
+ };
11
+ export const createTemplateBuilder = (wrap, builders) => {
12
+ return function (strings, ...values) {
13
+ const self = this;
14
+ const inner = sparql(strings, ...values);
15
+ const current = wrap(inner);
16
+ return createContext(self, current, builders);
17
+ };
18
+ };
19
+ export const createNumericBuilder = (wrap, builders) => {
20
+ return function (number) {
21
+ const self = this;
22
+ const current = wrap(number);
23
+ return createContext(self, current, builders);
24
+ };
25
+ };
26
+ const df = new DataFactory();
27
+ export const createNamedNodeBuilder = (wrap, builders) => {
28
+ return function (stringOrNamedNode) {
29
+ const self = this;
30
+ const namedNode = typeof stringOrNamedNode === "string"
31
+ ? df.namedNode(stringOrNamedNode)
32
+ : stringOrNamedNode;
33
+ const inner = sparql `${namedNode}`;
34
+ const current = wrap(inner);
35
+ return createContext(self, current, builders);
36
+ };
37
+ };
38
+ export const build = function () {
39
+ return this?.$partialQuery || "";
40
+ };
@@ -0,0 +1,47 @@
1
+ import { DataFactory } from "../rdf.js";
2
+ import xsd from "../namespaces/xsd.js";
3
+ import { stringify } from "./stringify.js";
4
+ export const sparql = (strings, ...values) => {
5
+ let counter = 0;
6
+ let result = "";
7
+ for (const value of values) {
8
+ result += strings[counter++];
9
+ result += valueToString(value);
10
+ }
11
+ result += strings[counter];
12
+ return result;
13
+ };
14
+ const isIterable = (obj) => {
15
+ return Symbol.iterator in Object(obj);
16
+ };
17
+ const df = new DataFactory();
18
+ const valueToString = (value) => {
19
+ if (typeof value === "undefined" || value === null) {
20
+ return "";
21
+ }
22
+ if (typeof value === "string") {
23
+ return value;
24
+ }
25
+ if (typeof value === "number") {
26
+ const numberDataType = Number.isInteger(value) ? xsd.integer : xsd.decimal;
27
+ return stringify(df.literal(value.toString(), df.namedNode(numberDataType)));
28
+ }
29
+ if (typeof value === "boolean") {
30
+ return stringify(df.literal(value.toString(), df.namedNode(xsd.boolean)));
31
+ }
32
+ if (value instanceof Date) {
33
+ return stringify(df.literal(value.toISOString(), df.namedNode(xsd.dateTime)));
34
+ }
35
+ if (isIterable(value)) {
36
+ const [first, ...rest] = value;
37
+ let result = valueToString(first);
38
+ for (const part of rest) {
39
+ result += `\n${valueToString(part)}`;
40
+ }
41
+ return result;
42
+ }
43
+ if (value.termType) {
44
+ return stringify(value);
45
+ }
46
+ throw new Error("Not supported input type detected.");
47
+ };
@@ -0,0 +1,15 @@
1
+ import { build, createNamedNodeBuilder, createTemplateBuilder, } from "./sparql_shared_builders.js";
2
+ const WHERE = createTemplateBuilder((value) => `WHERE {\n${value}\n}\n`, { build });
3
+ const USING_NAMED = createNamedNodeBuilder((value) => `USING NAMED ${value}\n`, { WHERE });
4
+ const USING = createNamedNodeBuilder((value) => `USING ${value}\n`, { USING_NAMED, WHERE });
5
+ const _INSERT = createTemplateBuilder((value) => `INSERT {\n${value}\n}\n`, { USING, USING_NAMED, WHERE });
6
+ const _INSERT_DATA = createTemplateBuilder((value) => `INSERT DATA {\n${value}\n}\n`, { build });
7
+ export const INSERT = Object.assign(_INSERT, {
8
+ DATA: _INSERT_DATA,
9
+ });
10
+ const _DELETE = createTemplateBuilder((value) => `DELETE {\n${value}\n}\n`, { INSERT: _INSERT, USING, USING_NAMED, WHERE });
11
+ const _DELETE_DATA = createTemplateBuilder((value) => `DELETE DATA {\n${value}\n}\n`, { build });
12
+ export const DELETE = Object.assign(_DELETE, {
13
+ DATA: _DELETE_DATA,
14
+ });
15
+ export const WITH = createNamedNodeBuilder((value) => `WITH ${value}\n`, { build, INSERT: _INSERT, DELETE: _DELETE });
@@ -0,0 +1,50 @@
1
+ import { DefaultGraph } from "../rdf.js";
2
+ import xsd from "../namespaces/xsd.js";
3
+ import { escape } from "./escape.js";
4
+ export const blankNode = (term) => {
5
+ return `_:${term.value}`;
6
+ };
7
+ export const namedNode = (term) => {
8
+ return `<${term.value}>`;
9
+ };
10
+ export const variable = (term) => {
11
+ return `?${term.value}`;
12
+ };
13
+ export const literal = (term) => {
14
+ const datatype = term.datatype.value;
15
+ if (datatype === xsd.integer || datatype === xsd.boolean ||
16
+ datatype === xsd.decimal) {
17
+ return term.value;
18
+ }
19
+ const value = `"${escape(term.value)}"`;
20
+ if (term.language) {
21
+ return `${value}@${term.language}`;
22
+ }
23
+ if (datatype !== xsd.string) {
24
+ return `${value}^^${namedNode(term.datatype)}`;
25
+ }
26
+ return value;
27
+ };
28
+ const quad = (term) => {
29
+ const triple = `${stringify(term.subject)} ${stringify(term.predicate)} ${stringify(term.object)} .`;
30
+ if (term.graph.equals(DefaultGraph.INSTANCE)) {
31
+ return triple;
32
+ }
33
+ return `GRAPH ${stringify(term.graph)} { ${triple} }`;
34
+ };
35
+ export const stringify = (term) => {
36
+ switch (term.termType) {
37
+ case "BlankNode":
38
+ return blankNode(term);
39
+ case "NamedNode":
40
+ return namedNode(term);
41
+ case "Variable":
42
+ return variable(term);
43
+ case "Literal":
44
+ return literal(term);
45
+ case "Quad":
46
+ return quad(term);
47
+ default:
48
+ throw new Error("Unknown RDF type found.");
49
+ }
50
+ };
package/esm/sparql.js CHANGED
@@ -1 +1 @@
1
- export * from "./library/sparql.js";
1
+ 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.6.3",
6
+ "version": "0.6.5",
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)",
@@ -44,10 +44,7 @@
44
44
  }
45
45
  },
46
46
  "dependencies": {
47
- "@comunica/bindings-factory": "2.2.0",
48
47
  "@comunica/types": "2.4.0",
49
- "@tpluscode/rdf-string": "0.2.26",
50
- "@tpluscode/sparql-builder": "0.3.23",
51
48
  "asynciterator": "3.7.0",
52
49
  "rdf-data-factory": "1.1.1",
53
50
  "rdf-js": "4.0.2",
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.decode = void 0;
4
7
  const rdf_js_1 = require("./rdf.js");
5
- const mod_js_1 = require("./namespaces/mod.js");
8
+ const ldkit_js_1 = __importDefault(require("./namespaces/ldkit.js"));
9
+ const rdf_js_2 = __importDefault(require("./namespaces/rdf.js"));
6
10
  class Decoder {
7
11
  constructor(graph, schema, context) {
8
12
  Object.defineProperty(this, "graph", {
@@ -51,17 +55,17 @@ class Decoder {
51
55
  decode() {
52
56
  const output = [];
53
57
  for (const [iri, properties] of this.graph) {
54
- if (properties.has(mod_js_1.rdf.type)) {
55
- const types = properties.get(mod_js_1.rdf.type);
58
+ if (properties.has(rdf_js_2.default.type)) {
59
+ const types = properties.get(rdf_js_2.default.type);
56
60
  for (const type of types) {
57
- if (type.termType === "NamedNode" && type.value === mod_js_1.ldkit.Resource) {
61
+ if (type.termType === "NamedNode" && type.value === ldkit_js_1.default.Resource) {
58
62
  output.push(this.decodeNode(iri, this.schema));
59
63
  }
60
64
  }
61
65
  }
62
66
  }
63
67
  if (this.graph.size > 0 && output.length < 1) {
64
- throw new Error(`Unable to decode graph - no resources with type <${mod_js_1.ldkit.Resource}> found`);
68
+ throw new Error(`Unable to decode graph - no resources with type <${ldkit_js_1.default.Resource}> found`);
65
69
  }
66
70
  return output;
67
71
  }
@@ -91,12 +95,12 @@ class Decoder {
91
95
  return output;
92
96
  }
93
97
  decodeNodeType(node) {
94
- const typeTerms = node.get(mod_js_1.rdf.type);
98
+ const typeTerms = node.get(rdf_js_2.default.type);
95
99
  if (!typeTerms) {
96
100
  return [];
97
101
  }
98
102
  return typeTerms.reduce((acc, term) => {
99
- if (term.value !== mod_js_1.ldkit.Resource) {
103
+ if (term.value !== ldkit_js_1.default.Resource) {
100
104
  acc.push(term.value);
101
105
  }
102
106
  return acc;
@@ -1,8 +1,12 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.encode = void 0;
4
7
  const rdf_js_1 = require("./rdf.js");
5
- const mod_js_1 = require("./namespaces/mod.js");
8
+ const xsd_js_1 = __importDefault(require("./namespaces/xsd.js"));
9
+ const rdf_js_2 = __importDefault(require("./namespaces/rdf.js"));
6
10
  const encode = (node, schema, context, variableInitCounter = 0) => {
7
11
  return Encoder.encode(node, schema, context, variableInitCounter);
8
12
  };
@@ -72,7 +76,7 @@ class Encoder {
72
76
  encodeNodeType(node, requiredTypes, nodeId) {
73
77
  const finalTypes = new Set([...this.getNodeTypes(node), ...requiredTypes]);
74
78
  finalTypes.forEach((type) => {
75
- this.push(nodeId, this.df.namedNode(mod_js_1.rdf.type), this.df.namedNode(type));
79
+ this.push(nodeId, this.df.namedNode(rdf_js_2.default.type), this.df.namedNode(type));
76
80
  });
77
81
  }
78
82
  encodeNodeProperty(value, property, nodeId) {
@@ -86,10 +90,11 @@ class Encoder {
86
90
  return;
87
91
  }
88
92
  if (property["@multilang"]) {
89
- Object.keys(value).forEach((language) => {
90
- const languageValue = Array.isArray(value[language])
91
- ? value[language]
92
- : [value[language]];
93
+ const multiValue = value;
94
+ Object.keys(multiValue).forEach((language) => {
95
+ const languageValue = Array.isArray(multiValue[language])
96
+ ? multiValue[language]
97
+ : [multiValue[language]];
93
98
  languageValue.forEach((singleValue) => {
94
99
  this.push(nodeId, propertyId, this.df.literal(singleValue, language.length > 0 ? language : undefined));
95
100
  });
@@ -104,9 +109,9 @@ class Encoder {
104
109
  this.push(nodeId, propertyId, subNodeId);
105
110
  return;
106
111
  }
107
- const propertyType = property["@type"] ? property["@type"] : mod_js_1.xsd.string;
112
+ const propertyType = property["@type"] ? property["@type"] : xsd_js_1.default.string;
108
113
  if (typeof val === "string" && this.context.language) {
109
- if (propertyType === mod_js_1.xsd.string || propertyType === mod_js_1.rdf.langString) {
114
+ if (propertyType === xsd_js_1.default.string || propertyType === rdf_js_2.default.langString) {
110
115
  this.push(nodeId, propertyId, this.df.literal(val, this.context.language));
111
116
  return;
112
117
  }