Skip to content

Commit c451e29

Browse files
committed
Add support for variables in export SPARQL queries
The variables will be replaced at runtime with the values of the query params provided in the request.
1 parent f5362c5 commit c451e29

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,26 @@ services:
4040
- database:database
4141
```
4242
43-
Note: if you extend the `semtech/mu-export-js-template` with an `export.js` file instead of mounting a volume in `/config`, the `ONBUILD` commands of the `semtech/mu-javascript-template` will not be executed in your image since `ONBUILD` is only executed one level deep.
43+
Note: if you extend the `semtech/mu-export-js-template` with an `export.js` file instead of mounting a volume in `/config`, the `ONBUILD` commands of the `semtech/mu-javascript-template` will not be executed in your image since `ONBUILD` is only executed one level deep.
44+
45+
## Using variables in the export query
46+
You can also define variables in your SPARQL export query which will be replaced with query param values at runtime. Therefore, we use [ES6's tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals).
47+
48+
Import the `template` tag function from `/app/template.js` in your `export.js` and apply the tag function on your query. You can then define variables in your SPARQL query using `${'myVariable'}`. At runtime the variable will be replaced with the value provided in the `myVariable` query param of the request.
49+
50+
An example `export.js` file may look as follows:
51+
52+
```javascript
53+
export default [
54+
{
55+
path: '/example',
56+
format: 'text/csv',
57+
query: `SELECT * FROM <http://mu.semte.ch/application> WHERE { ?s a ${'class'} } LIMIT 100`,
58+
file: 'export-example.csv'
59+
},
60+
...
61+
];
62+
63+
```
64+
65+
A GET request on `/example?class=<http://xmlns.com/foaf/0.1/Person>` will export the first 100 URIs of type `foaf:Person`.

app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ queries.map(function(config) {
1414
},
1515
qs: {
1616
format: config.format,
17-
query: config.query
17+
// replace placeholders in query with query params if it's a template
18+
query: typeof(config.query) === 'string' ? config.query : config.query(req.query)
1819
}
1920
};
2021

template.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function template(strings, ...keys) {
2+
return (function(...values) {
3+
var dict = values[values.length - 1] || {};
4+
var result = [strings[0]];
5+
keys.forEach(function(key, i) {
6+
var value = Number.isInteger(key) ? values[key] : dict[key];
7+
result.push(value, strings[i + 1]);
8+
});
9+
return result.join('');
10+
});
11+
}

0 commit comments

Comments
 (0)