@semapps/ontologies 0.9.2 → 0.9.3
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/actions/findPrefix.js +3 -0
- package/actions/get.js +7 -18
- package/actions/list.js +2 -6
- package/actions/register.js +13 -13
- package/package.json +3 -3
- package/service.js +7 -7
- package/sub-services/registry.js +6 -33
package/actions/findPrefix.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const fetch = require('node-fetch');
|
|
2
|
+
const { isURL } = require('../utils');
|
|
2
3
|
|
|
3
4
|
module.exports = {
|
|
4
5
|
visibility: 'public',
|
|
@@ -8,6 +9,8 @@ module.exports = {
|
|
|
8
9
|
async handler(ctx) {
|
|
9
10
|
const { uri } = ctx.params;
|
|
10
11
|
|
|
12
|
+
if (!isURL(uri)) throw new Error(`The uri param of the ontologies.findPrefix action must be a full URL`);
|
|
13
|
+
|
|
11
14
|
const response = await fetch(`http://prefix.cc/?q=${encodeURIComponent(uri)}`, { redirect: 'manual' });
|
|
12
15
|
|
|
13
16
|
if (response.status === 302) {
|
package/actions/get.js
CHANGED
|
@@ -10,25 +10,14 @@ module.exports = {
|
|
|
10
10
|
let { prefix, namespace, uri } = ctx.params;
|
|
11
11
|
let ontology;
|
|
12
12
|
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
if (
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
ontology = await ctx.call('ontologies.registry.getByNamespace', { namespace });
|
|
20
|
-
} else if (uri) {
|
|
21
|
-
const ontologies = await ctx.call('ontologies.registry.list');
|
|
22
|
-
ontology = ontologies.find(o => uri.startsWith(o.namespace));
|
|
23
|
-
}
|
|
13
|
+
if (prefix) {
|
|
14
|
+
ontology = this.ontologies[prefix] || false;
|
|
15
|
+
} else if (namespace) {
|
|
16
|
+
ontology = Object.values(this.ontologies).find(o => o.namespace === namespace);
|
|
17
|
+
} else if (uri) {
|
|
18
|
+
ontology = Object.values(this.ontologies).find(o => uri.startsWith(o.namespace));
|
|
24
19
|
} else {
|
|
25
|
-
|
|
26
|
-
ontology = this.ontologies[prefix] || false;
|
|
27
|
-
} else if (namespace) {
|
|
28
|
-
ontology = Object.values(this.ontologies).find(o => o.namespace === namespace);
|
|
29
|
-
} else if (uri) {
|
|
30
|
-
ontology = Object.values(this.ontologies).find(o => uri.startsWith(o.namespace));
|
|
31
|
-
}
|
|
20
|
+
throw new Error('You must provide a prefix, namespace or uri parameter');
|
|
32
21
|
}
|
|
33
22
|
|
|
34
23
|
// Must return null if no results, because the cache JsonSerializer cannot handle undefined values
|
package/actions/list.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
module.exports = {
|
|
2
2
|
visibility: 'public',
|
|
3
3
|
cache: true,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
return await ctx.call('ontologies.registry.list');
|
|
7
|
-
} else {
|
|
8
|
-
return Object.values(this.ontologies);
|
|
9
|
-
}
|
|
4
|
+
handler() {
|
|
5
|
+
return Object.values(this.ontologies);
|
|
10
6
|
}
|
|
11
7
|
};
|
package/actions/register.js
CHANGED
|
@@ -12,16 +12,10 @@ module.exports = {
|
|
|
12
12
|
optional: true
|
|
13
13
|
},
|
|
14
14
|
preserveContextUri: { type: 'boolean', default: false },
|
|
15
|
-
|
|
15
|
+
persist: { type: 'boolean', default: false }
|
|
16
16
|
},
|
|
17
17
|
async handler(ctx) {
|
|
18
|
-
let { prefix, namespace, owl, jsonldContext, preserveContextUri,
|
|
19
|
-
|
|
20
|
-
const ontology = await this.actions.get({ prefix }, { parentCtx: ctx });
|
|
21
|
-
|
|
22
|
-
if (!overwrite && ontology) {
|
|
23
|
-
throw new Error(`Cannot register ${prefix} ontology. An ontology with the prefix is already registered`);
|
|
24
|
-
}
|
|
18
|
+
let { prefix, namespace, owl, jsonldContext, preserveContextUri, persist } = ctx.params;
|
|
25
19
|
|
|
26
20
|
if (preserveContextUri === true) {
|
|
27
21
|
if (!jsonldContext || !arrayOf(jsonldContext).every(context => isURL(context))) {
|
|
@@ -31,14 +25,20 @@ module.exports = {
|
|
|
31
25
|
}
|
|
32
26
|
}
|
|
33
27
|
|
|
34
|
-
if (
|
|
28
|
+
if (persist) {
|
|
29
|
+
if (!this.settings.persistRegistry)
|
|
30
|
+
throw new Error(`Cannot persist ontology because the persistRegistry setting is false`);
|
|
31
|
+
if (owl || jsonldContext) throw new Error(`The owl and jsonldContext params cannot be persisted`);
|
|
32
|
+
|
|
35
33
|
await ctx.call('ontologies.registry.updateOrCreate', {
|
|
36
34
|
prefix,
|
|
37
|
-
namespace
|
|
38
|
-
owl,
|
|
39
|
-
jsonldContext,
|
|
40
|
-
preserveContextUri
|
|
35
|
+
namespace
|
|
41
36
|
});
|
|
37
|
+
|
|
38
|
+
this.ontologies[prefix] = {
|
|
39
|
+
prefix,
|
|
40
|
+
namespace
|
|
41
|
+
};
|
|
42
42
|
} else {
|
|
43
43
|
this.ontologies[prefix] = {
|
|
44
44
|
prefix,
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@semapps/ontologies",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3",
|
|
4
4
|
"description": "SemApps ontologies",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Virtual Assembly",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@semapps/triplestore": "0.9.
|
|
8
|
+
"@semapps/triplestore": "0.9.3",
|
|
9
9
|
"moleculer-db": "^0.8.16",
|
|
10
10
|
"node-fetch": "^2.6.6"
|
|
11
11
|
},
|
|
@@ -15,5 +15,5 @@
|
|
|
15
15
|
"engines": {
|
|
16
16
|
"node": ">=14"
|
|
17
17
|
},
|
|
18
|
-
"gitHead": "
|
|
18
|
+
"gitHead": "5c2f6a4d17379b25af3c2361e832ed10373841c3"
|
|
19
19
|
}
|
package/service.js
CHANGED
|
@@ -26,8 +26,7 @@ module.exports = {
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
async started() {
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
this.ontologies = {};
|
|
31
30
|
await this.registerAll();
|
|
32
31
|
},
|
|
33
32
|
actions: {
|
|
@@ -42,12 +41,13 @@ module.exports = {
|
|
|
42
41
|
},
|
|
43
42
|
methods: {
|
|
44
43
|
async registerAll() {
|
|
45
|
-
if (this.settings.persistRegistry)
|
|
44
|
+
if (this.settings.persistRegistry) {
|
|
45
|
+
await this.broker.waitForServices(['ontologies.registry']);
|
|
46
|
+
const persistedOntologies = await this.broker.call('ontologies.registry.list');
|
|
47
|
+
this.ontologies = { ...this.ontologies, ...persistedOntologies };
|
|
48
|
+
}
|
|
46
49
|
for (const ontology of this.settings.ontologies) {
|
|
47
|
-
|
|
48
|
-
throw new Error(`Cannot register ontologies ! At least one ontology is incorrect.`);
|
|
49
|
-
}
|
|
50
|
-
await this.actions.register({ ...ontology, overwrite: true });
|
|
50
|
+
await this.actions.register(ontology);
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
}
|
package/sub-services/registry.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const DbService = require('moleculer-db');
|
|
2
2
|
const { TripleStoreAdapter } = require('@semapps/triplestore');
|
|
3
|
-
const { isURL } = require('../utils');
|
|
4
3
|
|
|
5
4
|
module.exports = {
|
|
6
5
|
name: 'ontologies.registry',
|
|
@@ -13,59 +12,33 @@ module.exports = {
|
|
|
13
12
|
async getByPrefix(ctx) {
|
|
14
13
|
const { prefix } = ctx.params;
|
|
15
14
|
const [ontology] = await this._find(ctx, { query: { prefix } });
|
|
16
|
-
return
|
|
15
|
+
return ontology && { prefix: ontology.prefix, namespace: ontology.namespace };
|
|
17
16
|
},
|
|
18
17
|
async getByNamespace(ctx) {
|
|
19
18
|
const { namespace } = ctx.params;
|
|
20
19
|
const [ontology] = await this._find(ctx, { query: { namespace } });
|
|
21
|
-
return
|
|
20
|
+
return ontology && { prefix: ontology.prefix, namespace: ontology.namespace };
|
|
22
21
|
},
|
|
23
22
|
async list(ctx) {
|
|
24
23
|
const ontologies = await this._list(ctx, {});
|
|
25
|
-
return ontologies.rows.map(({ prefix, namespace
|
|
26
|
-
return this.parse({ prefix, namespace, owl, jsonldContext, preserveContextUri });
|
|
27
|
-
});
|
|
24
|
+
return ontologies.rows.map(({ prefix, namespace }) => ({ prefix, namespace }));
|
|
28
25
|
},
|
|
29
26
|
async updateOrCreate(ctx) {
|
|
30
|
-
|
|
27
|
+
const { prefix, namespace } = ctx.params;
|
|
31
28
|
|
|
32
29
|
const ontology = await this.actions.getByPrefix({ prefix }, { parentCtx: ctx });
|
|
33
30
|
|
|
34
|
-
if (!isURL(jsonldContext)) jsonldContext = JSON.stringify(jsonldContext);
|
|
35
|
-
|
|
36
31
|
if (ontology) {
|
|
37
32
|
await this._update(ctx, {
|
|
38
33
|
'@id': ontology['@id'],
|
|
39
|
-
namespace
|
|
40
|
-
owl,
|
|
41
|
-
jsonldContext,
|
|
42
|
-
preserveContextUri
|
|
34
|
+
namespace
|
|
43
35
|
});
|
|
44
36
|
} else {
|
|
45
37
|
await this._create(ctx, {
|
|
46
38
|
prefix,
|
|
47
|
-
namespace
|
|
48
|
-
owl,
|
|
49
|
-
jsonldContext,
|
|
50
|
-
preserveContextUri
|
|
39
|
+
namespace
|
|
51
40
|
});
|
|
52
41
|
}
|
|
53
42
|
}
|
|
54
|
-
},
|
|
55
|
-
methods: {
|
|
56
|
-
parse(ontology) {
|
|
57
|
-
// If the jsonldContext is not an URL, it is an object to be parsed
|
|
58
|
-
if (ontology?.jsonldContext && !isURL(ontology.jsonldContext)) {
|
|
59
|
-
ontology.jsonldContext = JSON.parse(ontology.jsonldContext);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (ontology?.preserveContextUri === 'true') {
|
|
63
|
-
ontology.preserveContextUri = true;
|
|
64
|
-
} else if (ontology?.preserveContextUri === 'false') {
|
|
65
|
-
ontology.preserveContextUri = false;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return ontology;
|
|
69
|
-
}
|
|
70
43
|
}
|
|
71
44
|
};
|