@netwerk-digitaal-erfgoed/ld-workbench
Version:
LDWorkbench is a Linked Data Transformation tool designed to use only SPARQL as transformation language.
49 lines • 1.98 kB
JavaScript
import App from '@triply/triplydb';
const pattern = /^triplydb:\/\/([a-z0-9-]+)\/([a-z0-9-]+)$/;
class TriplyDB {
constructor($dsn) {
this.$dsn = $dsn;
TriplyDB.assertValidDsn($dsn);
this.accountname = $dsn.match(pattern)[1];
this.datasetname = $dsn.match(pattern)[2];
}
static assertValidDsn(value) {
if (!pattern.test(value))
throw new Error('A TriplyDB path should look like this: "triplydb://accountname/dataset"');
}
toString() {
return this.$dsn;
}
validate() {
if (process.env.TOKEN === undefined) {
throw new Error('To publish to triply you need an environment variable "TOKEN" with your TriplyDB API Token, see https://docs.triply.cc/triply-api/#creating-an-api-token');
}
try {
this.app = App.get({ token: process.env.TOKEN });
}
catch (e) {
throw new Error(`Failed to create TriplyDB App: ${e.message}`);
}
return this;
}
get path() {
return this.$datasetUrl ?? this.$dsn;
}
async write(pipeline, progress) {
const filenames = Array.from(pipeline.stages.keys()).map(stageName => pipeline.stages.get(stageName).destinationPath);
await this.app.getAccount(this.accountname)
.then(async (account) => account.ensureDataset(this.datasetname))
.then(async (dataset) => {
const appInfo = await this.app.getInfo();
this.$datasetUrl = `${appInfo.consoleUrl}/${this.accountname}/${this.datasetname}`;
progress.line(`uploading data to ${this.$dsn}`);
await dataset.importFromFiles(filenames, { mergeGraphs: true });
})
.catch(e => {
throw new Error(`Failed to upload your data to TriplyDB ${this.accountname}/${this.datasetname}: ${e.message}.`);
});
}
}
TriplyDB.$id = 'TriplyDB';
export default TriplyDB;
//# sourceMappingURL=triply-db.js.map