@igea/oac_backend 1.0.13 → 1.0.15

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@igea/oac_backend",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "Backend service for the OAC project",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -12,5 +12,11 @@
12
12
  "database": "oac",
13
13
  "schema": "public"
14
14
  },
15
+ "fuseki": {
16
+ "protocol": "http",
17
+ "host": "127.0.0.1",
18
+ "port": "3333",
19
+ "dataset": "oac"
20
+ },
15
21
  "jwt_secret": "@igea#"
16
22
  }
@@ -12,5 +12,11 @@
12
12
  "database": "oac",
13
13
  "schema": "public"
14
14
  },
15
+ "fuseki": {
16
+ "protocol": "http",
17
+ "host": "127.0.0.1",
18
+ "port": "3333",
19
+ "dataset": "oac"
20
+ },
15
21
  "jwt_secret": "@igea#"
16
22
  }
@@ -0,0 +1,99 @@
1
+ const express = require('express');
2
+ const router = express.Router();
3
+ const config = require('../config')
4
+ const configFuseki = config.fuseki || {
5
+ "protocol": "http",
6
+ "host": "127.0.0.1",
7
+ "port": "3030",
8
+ "dataset": "oac"
9
+ }
10
+ const fusekiUrl = `${configFuseki.protocol}://${configFuseki.host}:${configFuseki.port}/${configFuseki.dataset}/sparql`;
11
+ const axios = require('axios');
12
+ const Fuseki = require('../models/fuseki');
13
+
14
+ router.get('/count/entities', (req, res) => {
15
+ const query = `
16
+ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
17
+ PREFIX ex: <http://www.cidoc-crm.org/extensions/crmsci/>
18
+
19
+ SELECT ?type (COUNT(?entity) AS ?count)
20
+ WHERE {
21
+ ?entity rdf:type ?type .
22
+ VALUES ?type { ex:S13_Sample ex:S5_Inference_Making }
23
+ }
24
+ GROUP BY ?type`
25
+ axios.post(fusekiUrl, `query=${encodeURIComponent(query)}`, {
26
+ headers: {
27
+ 'Content-Type': 'application/x-www-form-urlencoded',
28
+ 'Accept': 'application/sparql-results+json'
29
+ }
30
+ }).then(response => {
31
+ const bindings = response.data.results.bindings;
32
+ let results = []
33
+ bindings.forEach(result => {
34
+ results.push({
35
+ type: result.type.value,
36
+ count: parseInt(result.count.value)
37
+ });
38
+ });
39
+ res.json({
40
+ success: true,
41
+ data: results,
42
+ message: null
43
+ });
44
+ }).catch(err => {
45
+ res.status(500).json({
46
+ success: false,
47
+ data: null,
48
+ message: `Error: ${err}`
49
+ });
50
+ });
51
+
52
+ });
53
+
54
+ router.get('/export/:format/:entity/:id', (req, res) => {
55
+ let accept = 'application/rdf+xml'
56
+ let filename = `${req.params.entity}_${req.params.id}`;
57
+ switch(req.params.format){
58
+ case 'turtle':
59
+ accept='text/turtle'
60
+ filename += ".ttl"
61
+ break;
62
+ case 'json':
63
+ accept='application/ld+json'
64
+ filename += ".jsonld"
65
+ break;
66
+ case 'n-triples':
67
+ accept='application/n-triples'
68
+ filename += ".nt"
69
+ break;
70
+ case 'trig':
71
+ accept='application/trig'
72
+ filename += ".trig"
73
+ break;
74
+ default:
75
+ filename += ".rdf"
76
+ }
77
+ const uri = 'http://diagnostica/campione/1'
78
+ const query = Fuseki.getQueryDownload2(uri, 4)
79
+ axios.post(fusekiUrl, `query=${encodeURIComponent(query)}`,{
80
+ headers: {
81
+ 'Accept': `${accept}`,
82
+ 'Content-Type': 'application/x-www-form-urlencoded'
83
+ },
84
+ responseType: 'stream'
85
+ }).then(response => {
86
+ res.setHeader('Content-Disposition', `attachment; filename=${filename}`);
87
+ res.setHeader('Content-Type', response.headers['content-type'] || 'application/octet-stream');
88
+ response.data.pipe(res);
89
+ }).catch(err => {
90
+ res.status(500).json({
91
+ success: false,
92
+ data: null,
93
+ message: `Error: ${err}`
94
+ });
95
+ });
96
+
97
+ });
98
+
99
+ module.exports = router
package/src/index.js CHANGED
@@ -10,7 +10,12 @@ const jwtLib = jwtLibFactory({
10
10
  excludePaths: [
11
11
  `/${serviceName}/auth/authenticate`,
12
12
  `/${serviceName}/auth/echo`,
13
- `/${serviceName}/health`
13
+ `/${serviceName}/health`,
14
+
15
+ `/${serviceName}/fuseki/count/entities`,
16
+ `/${serviceName}/fuseki/export/rdf/campione/1`,
17
+ `/${serviceName}/fuseki/export/turtle/campione/1`
18
+
14
19
  ],
15
20
  signOptions: { expiresIn: '15m' }
16
21
  });
@@ -50,6 +55,9 @@ getPort.default({
50
55
  const usersRouter = require('./controllers/users.js')
51
56
  app.use(`/${serviceName}/users`, usersRouter)
52
57
 
58
+ const fusekiRouter = require('./controllers/fuseki.js')
59
+ app.use(`/${serviceName}/fuseki`, fusekiRouter)
60
+
53
61
  app.listen(port, async () => {
54
62
  console.log(`${serviceName} listening on port ${port}`)
55
63
  await register()
@@ -0,0 +1,89 @@
1
+ class Fuseki {
2
+
3
+ /**
4
+ * Return the query to retrieve the definition of an entity
5
+ * @param {string} entityUrl
6
+ * @returns
7
+ */
8
+ static getQueryDownload(entityUrl){
9
+ //http://diagnostica/campione/1
10
+ return `
11
+ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
12
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
13
+ PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
14
+ PREFIX owl: <http://www.w3.org/2002/07/owl#>
15
+ PREFIX dcterms: <http://purl.org/dc/terms/>
16
+ PREFIX foaf: <http://xmlns.com/foaf/0.1/>
17
+ PREFIX schema: <http://schema.org/>
18
+
19
+ CONSTRUCT {
20
+ <${entityUrl}> ?p1 ?o1 .
21
+ ?o1 ?p2 ?o2 .
22
+ ?o2 ?p3 ?o3 .
23
+ ?o3 ?p4 ?o4 .
24
+ }
25
+ WHERE {
26
+ <${entityUrl}> ?p1 ?o1 .
27
+ OPTIONAL {
28
+ ?o1 ?p2 ?o2 .
29
+ OPTIONAL {
30
+ ?o2 ?p3 ?o3 .
31
+ OPTIONAL {
32
+ ?o3 ?p4 ?o4 .
33
+ }
34
+ }
35
+ }
36
+ }
37
+ `;
38
+ }
39
+
40
+ static getQueryDownload2(entityUrl, depth = 3) {
41
+ const prefixes = `
42
+ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
43
+ PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
44
+ PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
45
+ PREFIX owl: <http://www.w3.org/2002/07/owl#>
46
+ PREFIX dcterms: <http://purl.org/dc/terms/>
47
+ PREFIX foaf: <http://xmlns.com/foaf/0.1/>
48
+ PREFIX schema: <http://schema.org/>
49
+ `;
50
+
51
+ // Generate CONSTRUCT triples
52
+ let constructTriples = [`<${entityUrl}> ?p1 ?o1 .`];
53
+ for (let i = 1; i <= depth; i++) {
54
+ const subject = i === 1 ? `?o1` : `?o${i}`;
55
+ const predicate = `?p${i + 1}`;
56
+ const object = `?o${i + 1}`;
57
+ constructTriples.push(`${subject} ${predicate} ${object} .`);
58
+ }
59
+
60
+ // Generate nested OPTIONAL blocks in WHERE
61
+ let where = `<${entityUrl}> ?p1 ?o1 .\n`;
62
+ let indent = '';
63
+ for (let i = 1; i <= depth; i++) {
64
+ const subject = i === 1 ? `?o1` : `?o${i}`;
65
+ const predicate = `?p${i + 1}`;
66
+ const object = `?o${i + 1}`;
67
+ where += `${indent}OPTIONAL {\n`;
68
+ indent += ' ';
69
+ where += `${indent}${subject} ${predicate} ${object} .\n`;
70
+ }
71
+ // Close all OPTIONALs
72
+ for (let i = 0; i < depth; i++) {
73
+ indent = indent.slice(0, -2);
74
+ where += `${indent}}\n`;
75
+ }
76
+
77
+ return `
78
+ ${prefixes}
79
+ CONSTRUCT {
80
+ ${constructTriples.join('\n ')}
81
+ }
82
+ WHERE {
83
+ ${where}
84
+ }`;
85
+ }
86
+
87
+ }
88
+
89
+ module.exports = Fuseki;