ldkit 0.6.4 → 0.7.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 (60) hide show
  1. package/README.md +15 -15
  2. package/esm/library/encoder.js +5 -4
  3. package/esm/library/global.js +2 -2
  4. package/esm/library/{resource/resource.js → lens/lens.js} +17 -2
  5. package/esm/library/lens/mod.js +1 -0
  6. package/esm/library/{resource → lens}/query_builder.js +33 -33
  7. package/esm/library/{resource → lens}/query_helper.js +0 -0
  8. package/esm/library/{resource → lens}/types.js +0 -0
  9. package/esm/library/rdf.js +2 -2
  10. package/esm/library/sparql/escape.js +22 -0
  11. package/esm/library/sparql/mod.js +3 -0
  12. package/esm/library/sparql/sparql_query_builders.js +73 -0
  13. package/esm/library/sparql/sparql_shared_builders.js +46 -0
  14. package/esm/library/sparql/sparql_tag.js +47 -0
  15. package/esm/library/sparql/sparql_update_builders.js +34 -0
  16. package/esm/library/sparql/stringify.js +50 -0
  17. package/esm/mod.js +1 -1
  18. package/esm/sparql.js +1 -1
  19. package/package.json +1 -3
  20. package/script/library/encoder.js +5 -4
  21. package/script/library/global.js +2 -2
  22. package/script/library/{resource/resource.js → lens/lens.js} +20 -4
  23. package/script/library/lens/mod.js +6 -0
  24. package/script/library/{resource → lens}/query_builder.js +38 -38
  25. package/script/library/{resource → lens}/query_helper.js +0 -0
  26. package/script/library/{resource → lens}/types.js +0 -0
  27. package/script/library/rdf.js +2 -1
  28. package/script/library/sparql/escape.js +26 -0
  29. package/script/library/sparql/mod.js +14 -0
  30. package/script/library/sparql/sparql_query_builders.js +77 -0
  31. package/script/library/sparql/sparql_shared_builders.js +52 -0
  32. package/script/library/sparql/sparql_tag.js +54 -0
  33. package/script/library/sparql/sparql_update_builders.js +38 -0
  34. package/script/library/sparql/stringify.js +61 -0
  35. package/script/mod.js +3 -2
  36. package/script/sparql.js +1 -1
  37. package/types/library/{resource/resource.d.ts → lens/lens.d.ts} +17 -2
  38. package/types/library/lens/mod.d.ts +1 -0
  39. package/types/library/{resource → lens}/query_builder.d.ts +0 -0
  40. package/types/library/{resource → lens}/query_helper.d.ts +0 -0
  41. package/types/library/lens/types.d.ts +5 -0
  42. package/types/library/rdf.d.ts +2 -2
  43. package/types/library/schema/interface.d.ts +2 -2
  44. package/types/library/schema/mod.d.ts +1 -1
  45. package/types/library/sparql/escape.d.ts +1 -0
  46. package/types/library/sparql/mod.d.ts +3 -0
  47. package/types/library/sparql/sparql_query_builders.d.ts +39 -0
  48. package/types/library/sparql/sparql_shared_builders.d.ts +15 -0
  49. package/types/library/sparql/sparql_tag.d.ts +3 -0
  50. package/types/library/sparql/sparql_update_builders.d.ts +22 -0
  51. package/types/library/sparql/stringify.d.ts +6 -0
  52. package/types/mod.d.ts +1 -1
  53. package/types/sparql.d.ts +1 -1
  54. package/esm/library/resource/mod.js +0 -1
  55. package/esm/library/sparql.js +0 -2
  56. package/script/library/resource/mod.js +0 -6
  57. package/script/library/sparql.js +0 -13
  58. package/types/library/resource/mod.d.ts +0 -1
  59. package/types/library/resource/types.d.ts +0 -5
  60. package/types/library/sparql.d.ts +0 -2
package/README.md CHANGED
@@ -37,18 +37,12 @@ For Deno environment, you can import LDkit like this:
37
37
  import * as ldkit from "https://deno.land/x/ldkit/mod.ts";
38
38
  ```
39
39
 
40
- ### Set up RDF source and create data schema
40
+ ### Create data schema and set up RDF source
41
41
 
42
42
  ```ts
43
- import { type Context, createResource } from "ldkit";
43
+ import { type Context, createLens } from "ldkit";
44
44
  import { dbo, rdfs, xsd } from "ldkit/namespaces";
45
45
 
46
- // Create a context for query engine
47
- const context: Context = {
48
- sources: ["https://dbpedia.org/sparql"], // SPARQL endpoint
49
- language: "en", // Preferred language
50
- };
51
-
52
46
  // Create a schema
53
47
  const PersonSchema = {
54
48
  "@type": dbo.Person,
@@ -60,22 +54,28 @@ const PersonSchema = {
60
54
  },
61
55
  } as const;
62
56
 
57
+ // Create a context for query engine
58
+ const context: Context = {
59
+ sources: ["https://dbpedia.org/sparql"], // SPARQL endpoint
60
+ language: "en", // Preferred language
61
+ };
62
+
63
63
  // Create a resource using the data schema and context above
64
- const Person = createResource(PersonSchema, context);
64
+ const Persons = createLens(PersonSchema, context);
65
65
  ```
66
66
 
67
67
  ### List all available data
68
68
 
69
69
  ```ts
70
70
  // List all persons
71
- const persons = await Person.find();
71
+ const persons = await Persons.find();
72
72
  for (const person of persons) {
73
73
  console.log(person.name); // string
74
74
  console.log(person.birthDate); // Date
75
75
  }
76
76
 
77
77
  // Get total count of all persons
78
- const count = await Person.count();
78
+ const count = await Persons.count();
79
79
  console.log(count); // number
80
80
  ```
81
81
 
@@ -83,7 +83,7 @@ console.log(count); // number
83
83
 
84
84
  ```ts
85
85
  // Get a particular person identified by IRI
86
- const ada = await Person.findByIri("http://dbpedia.org/resource/Ada_Lovelace");
86
+ const ada = await Persons.findByIri("http://dbpedia.org/resource/Ada_Lovelace");
87
87
  console.log(ada?.name); // string "Ada Lovelace"
88
88
  console.log(ada?.birthDate); // Date object of 1815-12-10
89
89
  ```
@@ -92,20 +92,20 @@ console.log(ada?.birthDate); // Date object of 1815-12-10
92
92
 
93
93
  ```ts
94
94
  // Insert a new person
95
- Person.insert({
95
+ Persons.insert({
96
96
  $id: "http://dbpedia.org/resource/Alan_Turing",
97
97
  name: "Alan Turing",
98
98
  birthDate: new Date("1912-06-23"),
99
99
  });
100
100
 
101
101
  // Modify a person's name
102
- Person.update({
102
+ Persons.update({
103
103
  $id: "http://dbpedia.org/resource/Alan_Turing",
104
104
  name: "Not Alan Turing",
105
105
  });
106
106
 
107
107
  // Delete a person
108
- Person.delete("http://dbpedia.org/resource/Alan_Turing");
108
+ Persons.delete("http://dbpedia.org/resource/Alan_Turing");
109
109
  ```
110
110
 
111
111
  More complex examples can be found in [documentation](https://ldkit.io/docs).
@@ -83,10 +83,11 @@ class Encoder {
83
83
  return;
84
84
  }
85
85
  if (property["@multilang"]) {
86
- Object.keys(value).forEach((language) => {
87
- const languageValue = Array.isArray(value[language])
88
- ? value[language]
89
- : [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]];
90
91
  languageValue.forEach((singleValue) => {
91
92
  this.push(nodeId, propertyId, this.df.literal(singleValue, language.length > 0 ? language : undefined));
92
93
  });
@@ -6,7 +6,7 @@ export const setDefaultContext = (context) => {
6
6
  };
7
7
  export const resolveContext = (context) => {
8
8
  if (!context && !defaultContext) {
9
- throw new Error("No context found. Please create a default context or pass one to createResource function");
9
+ throw new Error("No context found. Please create a default context or pass one to createLens function");
10
10
  }
11
11
  return context || defaultContext;
12
12
  };
@@ -18,7 +18,7 @@ export const resolveEngine = (engine) => {
18
18
  setDefaultEngine(new QueryEngine());
19
19
  // TODO: consider the consequences here, maybe log a warning?
20
20
  /*throw new Error(
21
- "No engine found. Please create a default engine or pass one to createResource function",
21
+ "No engine found. Please create a default engine or pass one to createLens function",
22
22
  );*/
23
23
  }
24
24
  return engine || defaultEngine;
@@ -3,8 +3,23 @@ import { expandSchema, } from "../schema/mod.js";
3
3
  import { decode } from "../decoder.js";
4
4
  import { QueryBuilder } from "./query_builder.js";
5
5
  import { QueryEngineProxy } from "../engine/query_engine_proxy.js";
6
- export const createResource = (spec, context, engine) => new Resource(spec, context, engine);
7
- export class Resource {
6
+ /**
7
+ * Lens lets you query and update RDF data via data schema using TypeScript native data types.
8
+ *
9
+ * https://ldkit.io/docs/components/lens
10
+ *
11
+ * @param schema data schema which extends `SchemaPrototype`
12
+ * @param context optional `Context` - contains LDkit and query engine configuration
13
+ * @param engine optional Query Engine
14
+ * @returns Lens instance
15
+ */
16
+ export const createLens = (schema, context, engine) => new Lens(schema, context, engine);
17
+ /**
18
+ * @deprecated
19
+ * Use `createLens` instead
20
+ */
21
+ export const createResource = (schema, context, engine) => new Lens(schema, context, engine);
22
+ export class Lens {
8
23
  constructor(schema, context, engine) {
9
24
  Object.defineProperty(this, "schema", {
10
25
  enumerable: true,
@@ -0,0 +1 @@
1
+ export { createLens, createResource } from "./lens.js";
@@ -1,5 +1,5 @@
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
4
  import ldkit from "../namespaces/ldkit.js";
5
5
  import rdf from "../namespaces/rdf.js";
@@ -36,11 +36,11 @@ export class QueryBuilder {
36
36
  configurable: true,
37
37
  writable: true,
38
38
  value: (iris) => {
39
- return DELETE `
40
- ?s ?p ?o
41
- `.WHERE `
42
- ?s ?p ?o .
43
- 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)} }
44
44
  `.build();
45
45
  }
46
46
  });
@@ -96,36 +96,36 @@ export class QueryBuilder {
96
96
  return SELECT `(count(?iri) as ?count)`.WHERE `${quads}`.build();
97
97
  }
98
98
  getQuery(where, limit = 1000) {
99
- const selectSubQuery = SELECT `
100
- ${this.df.variable("iri")}
101
- `.WHERE `
102
- ${this.getShape(false, true)}
103
- ${where}
104
- `.LIMIT(limit);
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
- }
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
+ }
115
115
  `.build();
116
116
  return query;
117
117
  }
118
118
  getByIrisQuery(iris) {
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
- }
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
+ }
129
129
  `.build();
130
130
  return query;
131
131
  }
@@ -137,7 +137,7 @@ export class QueryBuilder {
137
137
  return INSERT.DATA `${quads}`.build();
138
138
  }
139
139
  deleteDataQuery(quads) {
140
- return $ `DELETE DATA { ${quads} }`.toString();
140
+ return DELETE.DATA `${quads}`.build();
141
141
  }
142
142
  updateQuery(entities) {
143
143
  const deleteQuads = [];
File without changes
File without changes
@@ -1,6 +1,6 @@
1
1
  export { fromRdf, toRdf } from "rdf-literal";
2
- import { DataFactory } from "rdf-data-factory";
3
- export { DataFactory };
2
+ import { DataFactory, DefaultGraph, } from "rdf-data-factory";
3
+ export { DataFactory, DefaultGraph };
4
4
  export const quadsToGraph = (quads) => {
5
5
  const graph = new Map();
6
6
  for (const quad of quads) {
@@ -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,73 @@
1
+ import { braces, parentheses, SparqlBuilder, } from "./sparql_shared_builders.js";
2
+ class SparqlQueryBuilder extends SparqlBuilder {
3
+ OFFSET(number) {
4
+ return this.number(number, "OFFSET");
5
+ }
6
+ LIMIT(number) {
7
+ return this.number(number, "LIMIT");
8
+ }
9
+ ORDER_BY(strings, ...values) {
10
+ return this.template(strings, values, "ORDER BY");
11
+ }
12
+ HAVING(strings, ...values) {
13
+ return this.template(strings, values, "HAVING", parentheses);
14
+ }
15
+ GROUP_BY(strings, ...values) {
16
+ return this.template(strings, values, "GROUP BY");
17
+ }
18
+ WHERE(strings, ...values) {
19
+ return this.template(strings, values, "WHERE", braces);
20
+ }
21
+ FROM_NAMED(stringOrNamedNode) {
22
+ return this.namedNode(stringOrNamedNode, "FROM NAMED");
23
+ }
24
+ FROM(stringOrNamedNode) {
25
+ return this.namedNode(stringOrNamedNode, "FROM");
26
+ }
27
+ SELECT(strings, ...values) {
28
+ return this.template(strings, values, "SELECT");
29
+ }
30
+ SELECT_DISTINCT(strings, ...values) {
31
+ return this.template(strings, values, "SELECT DISTINCT");
32
+ }
33
+ SELECT_REDUCED(strings, ...values) {
34
+ return this.template(strings, values, "SELECT REDUCED");
35
+ }
36
+ CONSTRUCT(strings, ...values) {
37
+ return this.template(strings, values, "CONSTRUCT", braces);
38
+ }
39
+ CONSTRUCT_WHERE(strings, ...values) {
40
+ return this.template(strings, values, "CONSTRUCT WHERE", braces);
41
+ }
42
+ ASK(strings, ...values) {
43
+ return this.template(strings, values, "ASK", braces);
44
+ }
45
+ ASK_FROM(stringOrNamedNode) {
46
+ return this.namedNode(stringOrNamedNode, "ASK\nFROM");
47
+ }
48
+ ASK_FROM_NAMED(stringOrNamedNode) {
49
+ return this.namedNode(stringOrNamedNode, "ASK\nFROM NAMED");
50
+ }
51
+ ASK_WHERE(strings, ...values) {
52
+ return this.template(strings, values, "ASK\nWHERE", braces);
53
+ }
54
+ DESCRIBE(strings, ...values) {
55
+ return this.template(strings, values, "DESCRIBE");
56
+ }
57
+ }
58
+ export const SELECT = Object.assign((strings, ...values) => new SparqlQueryBuilder().SELECT(strings, ...values), {
59
+ DISTINCT: (strings, ...values) => new SparqlQueryBuilder().SELECT_DISTINCT(strings, ...values),
60
+ REDUCED: (strings, ...values) => new SparqlQueryBuilder().SELECT_REDUCED(strings, ...values),
61
+ get ALL() {
62
+ return new SparqlQueryBuilder().SELECT `*`;
63
+ },
64
+ });
65
+ export const CONSTRUCT = Object.assign((strings, ...values) => new SparqlQueryBuilder().CONSTRUCT(strings, ...values), {
66
+ WHERE: (strings, ...values) => new SparqlQueryBuilder().CONSTRUCT_WHERE(strings, ...values),
67
+ });
68
+ export const ASK = Object.assign((strings, ...values) => new SparqlQueryBuilder().ASK(strings, ...values), {
69
+ FROM: (stringOrNamedNode) => new SparqlQueryBuilder().ASK_FROM(stringOrNamedNode),
70
+ FROM_NAMED: (stringOrNamedNode) => new SparqlQueryBuilder().ASK_FROM_NAMED(stringOrNamedNode),
71
+ WHERE: (strings, ...values) => new SparqlQueryBuilder().ASK_WHERE(strings, ...values),
72
+ });
73
+ export const DESCRIBE = (strings, ...values) => new SparqlQueryBuilder().DESCRIBE(strings, ...values);
@@ -0,0 +1,46 @@
1
+ import { DataFactory } from "../rdf.js";
2
+ import { sparql } from "./sparql_tag.js";
3
+ export const braces = (keyword, value) => `${keyword} {\n${value}\n}\n`;
4
+ export const parentheses = (keyword, value) => `${keyword} (${value})\n`;
5
+ const none = (keyword, value) => `${keyword} ${value}\n`;
6
+ export class SparqlBuilder {
7
+ constructor() {
8
+ Object.defineProperty(this, "value", {
9
+ enumerable: true,
10
+ configurable: true,
11
+ writable: true,
12
+ value: ""
13
+ });
14
+ Object.defineProperty(this, "dataFactory", {
15
+ enumerable: true,
16
+ configurable: true,
17
+ writable: true,
18
+ value: new DataFactory()
19
+ });
20
+ }
21
+ build() {
22
+ return this.value;
23
+ }
24
+ sparql(strings, ...values) {
25
+ return !values || values.length < 1
26
+ ? sparql(strings)
27
+ : sparql(strings, ...values);
28
+ }
29
+ template(strings, values, keyword, wrap = none) {
30
+ const inputSparql = this.sparql(strings, ...values);
31
+ this.value += wrap(keyword, inputSparql);
32
+ return this;
33
+ }
34
+ namedNode(stringOrNamedNode, keyword, wrap = none) {
35
+ const namedNode = typeof stringOrNamedNode === "string"
36
+ ? this.dataFactory.namedNode(stringOrNamedNode)
37
+ : stringOrNamedNode;
38
+ const inputSparql = this.sparql `${namedNode}`;
39
+ this.value += wrap(keyword, inputSparql);
40
+ return this;
41
+ }
42
+ number(number, keyword, wrap = none) {
43
+ this.value += wrap(keyword, number.toString());
44
+ return this;
45
+ }
46
+ }
@@ -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,34 @@
1
+ import { braces, SparqlBuilder } from "./sparql_shared_builders.js";
2
+ class SparqlUpdateBuilder extends SparqlBuilder {
3
+ WHERE(strings, ...values) {
4
+ return this.template(strings, values, "WHERE", braces);
5
+ }
6
+ USING_NAMED(stringOrNamedNode) {
7
+ return this.namedNode(stringOrNamedNode, "USING NAMED");
8
+ }
9
+ USING(stringOrNamedNode) {
10
+ return this.namedNode(stringOrNamedNode, "USING");
11
+ }
12
+ INSERT(strings, ...values) {
13
+ return this.template(strings, values, "INSERT", braces);
14
+ }
15
+ INSERT_DATA(strings, ...values) {
16
+ return this.template(strings, values, "INSERT DATA", braces);
17
+ }
18
+ DELETE(strings, ...values) {
19
+ return this.template(strings, values, "DELETE", braces);
20
+ }
21
+ DELETE_DATA(strings, ...values) {
22
+ return this.template(strings, values, "DELETE DATA", braces);
23
+ }
24
+ WITH(stringOrNamedNode) {
25
+ return this.namedNode(stringOrNamedNode, "WITH");
26
+ }
27
+ }
28
+ export const INSERT = Object.assign((strings, ...values) => new SparqlUpdateBuilder().INSERT(strings, ...values), {
29
+ DATA: (strings, ...values) => new SparqlUpdateBuilder().INSERT_DATA(strings, ...values),
30
+ });
31
+ export const DELETE = Object.assign((strings, ...values) => new SparqlUpdateBuilder().DELETE(strings, ...values), {
32
+ DATA: (strings, ...values) => new SparqlUpdateBuilder().DELETE_DATA(strings, ...values),
33
+ });
34
+ export const WITH = (stringOrNamedNode) => new SparqlUpdateBuilder().WITH(stringOrNamedNode);
@@ -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/mod.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { setDefaultContext, setDefaultEngine } from "./library/global.js";
2
- export { createResource } from "./library/resource/mod.js";
2
+ export { createLens, createResource } from "./library/lens/mod.js";
3
3
  export { createNamespace } from "./library/namespaces/namespace.js";
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.4",
6
+ "version": "0.7.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)",
@@ -45,8 +45,6 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@comunica/types": "2.4.0",
48
- "@tpluscode/rdf-string": "0.2.26",
49
- "@tpluscode/sparql-builder": "0.3.23",
50
48
  "asynciterator": "3.7.0",
51
49
  "rdf-data-factory": "1.1.1",
52
50
  "rdf-js": "4.0.2",
@@ -90,10 +90,11 @@ class Encoder {
90
90
  return;
91
91
  }
92
92
  if (property["@multilang"]) {
93
- Object.keys(value).forEach((language) => {
94
- const languageValue = Array.isArray(value[language])
95
- ? value[language]
96
- : [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]];
97
98
  languageValue.forEach((singleValue) => {
98
99
  this.push(nodeId, propertyId, this.df.literal(singleValue, language.length > 0 ? language : undefined));
99
100
  });
@@ -10,7 +10,7 @@ const setDefaultContext = (context) => {
10
10
  exports.setDefaultContext = setDefaultContext;
11
11
  const resolveContext = (context) => {
12
12
  if (!context && !defaultContext) {
13
- throw new Error("No context found. Please create a default context or pass one to createResource function");
13
+ throw new Error("No context found. Please create a default context or pass one to createLens function");
14
14
  }
15
15
  return context || defaultContext;
16
16
  };
@@ -24,7 +24,7 @@ const resolveEngine = (engine) => {
24
24
  (0, exports.setDefaultEngine)(new mod_js_1.QueryEngine());
25
25
  // TODO: consider the consequences here, maybe log a warning?
26
26
  /*throw new Error(
27
- "No engine found. Please create a default engine or pass one to createResource function",
27
+ "No engine found. Please create a default engine or pass one to createLens function",
28
28
  );*/
29
29
  }
30
30
  return engine || defaultEngine;
@@ -1,14 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Resource = exports.createResource = void 0;
3
+ exports.Lens = exports.createResource = exports.createLens = void 0;
4
4
  const global_js_1 = require("../global.js");
5
5
  const mod_js_1 = require("../schema/mod.js");
6
6
  const decoder_js_1 = require("../decoder.js");
7
7
  const query_builder_js_1 = require("./query_builder.js");
8
8
  const query_engine_proxy_js_1 = require("../engine/query_engine_proxy.js");
9
- const createResource = (spec, context, engine) => new Resource(spec, context, engine);
9
+ /**
10
+ * Lens lets you query and update RDF data via data schema using TypeScript native data types.
11
+ *
12
+ * https://ldkit.io/docs/components/lens
13
+ *
14
+ * @param schema data schema which extends `SchemaPrototype`
15
+ * @param context optional `Context` - contains LDkit and query engine configuration
16
+ * @param engine optional Query Engine
17
+ * @returns Lens instance
18
+ */
19
+ const createLens = (schema, context, engine) => new Lens(schema, context, engine);
20
+ exports.createLens = createLens;
21
+ /**
22
+ * @deprecated
23
+ * Use `createLens` instead
24
+ */
25
+ const createResource = (schema, context, engine) => new Lens(schema, context, engine);
10
26
  exports.createResource = createResource;
11
- class Resource {
27
+ class Lens {
12
28
  constructor(schema, context, engine) {
13
29
  Object.defineProperty(this, "schema", {
14
30
  enumerable: true,
@@ -97,4 +113,4 @@ class Resource {
97
113
  return this.updateQuery(q);
98
114
  }
99
115
  }
100
- exports.Resource = Resource;
116
+ exports.Lens = Lens;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createResource = exports.createLens = void 0;
4
+ var lens_js_1 = require("./lens.js");
5
+ Object.defineProperty(exports, "createLens", { enumerable: true, get: function () { return lens_js_1.createLens; } });
6
+ Object.defineProperty(exports, "createResource", { enumerable: true, get: function () { return lens_js_1.createResource; } });