-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathschemas.ts
More file actions
66 lines (57 loc) · 1.87 KB
/
schemas.ts
File metadata and controls
66 lines (57 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { FastifyPluginAsync } from 'fastify';
import { sparqlSelect } from '../../services/sparql.service.js';
import { PREFIXES } from '../../rdf/prefixes.js';
const SM = PREFIXES.shexmap;
const DCT = PREFIXES.dct;
const schemasRoutes: FastifyPluginAsync = async (fastify) => {
// GET /schemas — list all known ShExSchemas with linked ShExMap IDs
fastify.get('/', {
schema: { tags: ['schemas'], summary: 'List all known ShExSchemas' },
}, async () => {
const sparql = `
SELECT ?schema ?title ?description ?source ?mapId
WHERE {
?schema a <${SM}ShExSchema> .
OPTIONAL { ?schema <${DCT}title> ?title }
OPTIONAL { ?schema <${DCT}description> ?description }
OPTIONAL { ?schema <${DCT}source> ?source }
OPTIONAL { ?mapId <${SM}hasSchema> ?schema }
}
ORDER BY ?title
`;
const rows = await sparqlSelect(fastify, sparql);
// Group rows by schema IRI, collecting associated map IDs
const bySchema = new Map<string, {
id: string;
url: string;
title: string;
description?: string;
sourceUrl?: string;
shexMapIds: string[];
}>();
for (const row of rows) {
const url = row['schema']?.value ?? '';
const id = url.split('/').pop() ?? url;
if (!bySchema.has(url)) {
bySchema.set(url, {
id,
url,
title: row['title']?.value ?? id,
description: row['description']?.value,
sourceUrl: row['source']?.value,
shexMapIds: [],
});
}
const mapIri = row['mapId']?.value;
if (mapIri) {
const mapId = mapIri.split('/').pop() ?? mapIri;
const entry = bySchema.get(url)!;
if (!entry.shexMapIds.includes(mapId)) {
entry.shexMapIds.push(mapId);
}
}
}
return [...bySchema.values()];
});
};
export default schemasRoutes;