-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinsert.js
62 lines (57 loc) · 1.8 KB
/
insert.js
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
const urlJoin = require('url-join');
const { MIME_TYPES } = require('@semapps/mime-types');
module.exports = {
visibility: 'public',
params: {
resource: {
type: 'multi',
rules: [{ type: 'string' }, { type: 'object' }]
},
contentType: {
type: 'string',
optional: true
},
webId: {
type: 'string',
optional: true
},
graphName: {
type: 'string',
optional: true
},
dataset: {
type: 'string',
optional: true
}
},
async handler(ctx) {
const { resource, contentType, graphName } = ctx.params;
const webId = ctx.params.webId || ctx.meta.webId || 'anon';
let dataset = ctx.params.dataset || ctx.meta.dataset || this.settings.mainDataset;
const rdf =
contentType === MIME_TYPES.JSON
? await ctx.call('jsonld.parser.toRDF', {
input: resource,
options: {
format: 'application/n-quads'
}
})
: resource;
if (!dataset) throw new Error(`No dataset defined for triplestore insert: ${rdf}`);
if (dataset !== '*' && !(await ctx.call('triplestore.dataset.exist', { dataset })))
throw new Error(`The dataset ${dataset} doesn't exist`);
// Handle wildcard
const datasets = dataset === '*' ? await ctx.call('triplestore.dataset.list') : [dataset];
for (dataset of datasets) {
if (datasets.length > 1) this.logger.info(`Inserting into dataset ${dataset}...`);
await this.fetch(urlJoin(this.settings.url, dataset, 'update'), {
body: graphName ? `INSERT DATA { GRAPH <${graphName}> { ${rdf} } }` : `INSERT DATA { ${rdf} }`,
headers: {
'Content-Type': 'application/sparql-update',
'X-SemappsUser': webId,
Authorization: this.Authorization
}
});
}
}
};