UNPKG

graphdb

Version:

Javascript client library supporting GraphDB and RDF4J REST API.

466 lines (442 loc) 20.7 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); } function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); } function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); } function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } var BaseRepositoryClient = require('../repository/base-repository-client'); var ConsoleLogger = require('../logging/console-logger'); var RepositoryService = require('../service/repository-service'); var NamespaceService = require('../service/namespace-service'); var StatementsService = require('../service/statements-service'); var QueryService = require('../service/query-service'); var UploadService = require('../service/upload-service'); var DownloadService = require('../service/download-service'); var TransactionService = require('../service/transaction-service'); /** * RDF repository client implementation realizing specific operations. * * @class * @author Mihail Radkov * @author Svilen Velikov */ var RDFRepositoryClient = /*#__PURE__*/function (_BaseRepositoryClient) { /** * @param {RepositoryClientConfig} repositoryClientConfig */ function RDFRepositoryClient(repositoryClientConfig) { var _this; _classCallCheck(this, RDFRepositoryClient); _this = _callSuper(this, RDFRepositoryClient, [repositoryClientConfig]); _this.initServices(); return _this; } /** * @inheritDoc */ _inherits(RDFRepositoryClient, _BaseRepositoryClient); return _createClass(RDFRepositoryClient, [{ key: "getLogger", value: function getLogger() { return new ConsoleLogger({ name: 'RDFRepositoryClient' }); } /** * Instantiates dependent services. */ }, { key: "initServices", value: function initServices() { var httpRequestExecutor = this.execute.bind(this); var parseExecutor = this.parse.bind(this); this.repositoryService = new RepositoryService(httpRequestExecutor); this.namespaceService = new NamespaceService(httpRequestExecutor); this.statementsService = new StatementsService(httpRequestExecutor, this.parserRegistry, parseExecutor); this.queryService = new QueryService(httpRequestExecutor, parseExecutor); this.uploadService = new UploadService(httpRequestExecutor); this.downloadService = new DownloadService(httpRequestExecutor); this.transactionService = new TransactionService(httpRequestExecutor, this.repositoryClientConfig); } /** * Retrieves the size of the repository. * * Effectively returns how much statements are in the repository. * * If one or multiple context are provided, the operation will be restricted * upon each of them. * * @param {string|string[]} [context] context or contexts to restrict the * size calculation. Will be encoded as N-Triple if it is not already one * @return {Promise<number>} a promise resolving to the total number of * statements in the repository */ }, { key: "getSize", value: function getSize(context) { return this.repositoryService.getSize(context).execute(); } /** * Retrieves all present namespaces as a collection of {@link Namespace}. * * @return {Promise<Namespace[]>} promise resolving to a collection of * {@link Namespace} */ }, { key: "getNamespaces", value: function getNamespaces() { return this.namespaceService.getNamespaces().execute(); } /** * Retrieves the namespace for the given prefix as {@link NamedNode}. * * For example if <code>rdfs</code> is provided as prefix that would result in * a {@link NamedNode} corresponding to following namespace: * <code>http://www.w3.org/2000/01/rdf-schema#</code> * * Note: This method should be invoked only with prefixes. Anything else would * result in an error from the server. * * @param {string} prefix prefix of the namespace to be retrieved * @return {Promise<NamedNode>} promise resolving to {@link NamedNode} * @throws {Error} if the prefix parameter is not supplied */ }, { key: "getNamespace", value: function getNamespace(prefix) { return this.namespaceService.getNamespace(prefix).execute(); } /** * Creates or updates the namespace for the given prefix. * * If the provided prefix or namespace parameter is not a string or * {@link NamedNode} then the method will throw an error. * * @param {string} prefix prefix of the namespace to be created/updated * @param {string|NamedNode} namespace the namespace to be created/updated * @return {Promise<void>} promise that will be resolved if the create/update * request is successful * @throws {Error} if the prefix or namespace parameter are not provided */ }, { key: "saveNamespace", value: function saveNamespace(prefix, namespace) { return this.namespaceService.saveNamespace(prefix, namespace).execute(); } /** * Deletes a namespace that corresponds to the given prefix. * * For example if <code>rdfs</code> is provided as prefix that would delete * the following namespace: <code>http://www.w3.org/2000/01/rdf-schema#</code> * * Note: This method should be invoked only with prefixes. Anything else would * result in an error from the server. * * @param {string} prefix prefix of the namespace to be deleted * @return {Promise<void>} promise that will be resolved if the deletion is * successful * @throws {Error} if the prefix parameter is not provided */ }, { key: "deleteNamespace", value: function deleteNamespace(prefix) { return this.namespaceService.deleteNamespace(prefix).execute(); } /** * Deletes all namespace declarations in the repository. * * @return {Promise<void>} promise that will be resolved after * successful deletion */ }, { key: "deleteNamespaces", value: function deleteNamespaces() { return this.namespaceService.deleteNamespaces().execute(); } /** * Fetch rdf data from statements endpoint using provided parameters. * * Provided values will be automatically converted to N-Triples if they are * not already encoded as such. * * @param {GetStatementsPayload} payload is an object holding the request * parameters. * @return {Promise<string|Quad>} resolves with plain string or Quad according * to provided response type. */ }, { key: "get", value: function get(payload) { return this.statementsService.get(payload).execute(); } /** * Executes request to query a repository. * * Only POST request with a valid QueryPayload is supported. * * @param {GetQueryPayload} payload is an object holding request parameters * required by the query POST endpoint. * @return {Promise} the client can subscribe to the stream events and consume * the emitted strings or Quads depending on the provided response type as * soon as they are available. * @throws {Error} if the payload is misconfigured */ }, { key: "query", value: function query(payload) { return this.queryService.query(payload).execute(); } /** * Executes a request with a sparql query against <code>/statements</code> * endpoint to update repository data. * * If <code>contentType</code> is set to * <code>application/x-www-form-urlencoded</code> then query and request * parameters from the payload are encoded as query string and sent as request * body. * * If <code>contentType</code> is set to * <code>application/sparql-update</code> then the query is sent unencoded as * request body. * * @param {UpdateQueryPayload} payload * @return {Promise<void>} promise that will be resolved if the update is * successful or rejected in case of failure * @throws {Error} if the payload is misconfigured */ }, { key: "update", value: function update(payload) { return this.queryService.update(payload).execute(); } /** * Saves the provided statement payload in the repository. * * The payload will be converted to a quad or a collection of quads in case * there are multiple contexts. * * After the conversion, the produced quad(s) will be serialized to Turtle or * Trig format and send to the repository as payload. * * See {@link #addQuads()}. * * @param {AddStatementPayload} payload holding request parameters * @return {Promise<void>} promise that will be resolved if the addition is * successful or rejected in case of failure * @throws {Error} if the payload is not provided or the payload has null * subject, predicate and/or object */ }, { key: "add", value: function add(payload) { return this.statementsService.add(payload).execute(); } /** * Serializes the provided quads to Turtle format and sends them to the * repository as payload. * * If any of the quads have a graph, then the text will be serialized to the * Trig format which is an extended version of Turtle supporting contexts. * * @param {Quad[]} quads collection of quads to be sent as Turtle/Trig text * @param {string|string[]} [context] restricts the insertion to the given * context. Will be encoded as N-Triple if it is not already one * @param {string} [baseURI] used to resolve relative URIs in the data * @return {Promise<void>} promise that will be resolved if the addition is * successful or rejected in case of failure */ }, { key: "addQuads", value: function addQuads(quads, context, baseURI) { return this.statementsService.addQuads(quads, context, baseURI).execute(); } /** * Overwrites the repository's data by serializing the provided quads to * Turtle format and sending them to the repository as payload. * * If any of the quads have a graph, then the text will be serialized to the * Trig format which is an extended version of Turtle supporting contexts. * * The overwrite will be restricted if the context parameter is specified. * * @param {Quad[]} quads collection of quads to be sent as Turtle/Trig text * @param {string|string[]} [context] restricts the insertion to the given * context. Will be encoded as N-Triple if it is not already one * @param {string} [baseURI] used to resolve relative URIs in the data * @return {Promise<void>} promise that will be resolved if the overwrite is * successful or rejected in case of failure */ }, { key: "putQuads", value: function putQuads(quads, context, baseURI) { return this.statementsService.putQuads(quads, context, baseURI).execute(); } /** * Deletes statements in the repository based on the provided subject, * predicate, object and or contexts. Each of them is optional and acts as * statements filter which effectively narrows the scope of the deletion. * * Providing context or contexts will restricts the operation to one or more * specific contexts in the repository. * * Provided values will be automatically converted to N-Triples if they are * not already encoded as such. * * @param {String} [subject] resource subject * @param {String} [predicate] resource predicate * @param {String} [object] resource object * @param {String[]|String} [contexts] resource or resources context * @return {Promise<void>} promise that will be resolved if the deletion is * successful or rejected in case of failure */ }, { key: "deleteStatements", value: function deleteStatements(subject, predicate, object, contexts) { return this.statementsService.deleteStatements(subject, predicate, object, contexts).execute(); } /** * Deletes all statements in the repository. * * @return {Promise<void>} promise that will be resolved if the deletion is * successful or rejected in case of failure */ }, { key: "deleteAllStatements", value: function deleteAllStatements() { return this.statementsService.deleteAllStatements().execute(); } /** * Fetch rdf data from statements endpoint using provided parameters. * * The request is configured so that expected response should be a readable * stream. * * Provided request params will be automatically converted to N-Triples if * they are not already encoded as such. * * @param {GetStatementsPayload} payload is an object holding request params * * @return {Promise<WritableStream>} the client can subscribe to the readable * stream events and consume the emitted strings depending on the provided * response type as soon as they are available. */ }, { key: "download", value: function download(payload) { return this.downloadService.download(payload).execute(); } /** * Executes a POST request against the <code>/statements</code> endpoint. The * statements which have to be added are provided through a readable stream. * This method is useful for library client who wants to upload a big data set * into the repository. * * @param {ReadableStream} readStream * @param {string} contentType is one of RDF mime type formats, * application/x-rdftransaction' for a transaction document or * application/x-www-form-urlencoded * @param {NamedNode|string} [context] optional context to restrict the * operation. Will be encoded as N-Triple if it is not already one * @param {string} [baseURI] optional uri against which any relative URIs * found in the data would be resolved. * * @return {Promise<void>} a promise that will be resolved when the stream has * been successfully consumed by the server */ }, { key: "upload", value: function upload(readStream, contentType, context, baseURI) { return this.uploadService.upload(readStream, contentType, context, baseURI).execute(); } /** * Executes a PUT request against the <code>/statements</code> endpoint. The * statements which have to be updated are provided through a readable stream. * This method is useful for overriding large set of statements that might be * provided as a readable stream e.g. reading from file. * * @param {ReadableStream} readStream * @param {string} contentType * @param {NamedNode|string} context restrict the operation. Will be encoded * as N-Triple if it is not already one * @param {string} [baseURI] optional uri against which any relative URIs * found in the data would be resolved. * * @return {Promise<void>} a promise that will be resolved when the stream has * been successfully consumed by the server */ }, { key: "overwrite", value: function overwrite(readStream, contentType, context, baseURI) { return this.uploadService.overwrite(readStream, contentType, context, baseURI).execute(); } /** * Uploads the file specified by the provided file path to the server. * * See {@link #upload} * * @param {string} filePath path to a file to be streamed to the server * @param {string} contentType MIME type of the file's content * @param {string|string[]} [context] restricts the operation to the given * context. Will be encoded as N-Triple if it is not already one * @param {string} [baseURI] used to resolve relative URIs in the data * * @return {Promise<void>} a promise that will be resolved when the file has * been successfully consumed by the server */ }, { key: "addFile", value: function addFile(filePath, contentType, context, baseURI) { return this.uploadService.addFile(filePath, contentType, context, baseURI).execute(); } /** * Uploads the file specified by the provided file path to the server * overwriting any data in the server's repository. * * The overwrite will be restricted if the context parameter is specified. * * See {@link #overwrite} * * @param {string} filePath path to a file to be streamed to the server * @param {string} contentType MIME type of the file's content * @param {string} [context] restricts the operation to the given context. * Will be encoded as N-Triple if it is not already one * @param {string} [baseURI] used to resolve relative URIs in the data * * @return {Promise<void>} a promise that will be resolved when the file has * been successfully consumed by the server */ }, { key: "putFile", value: function putFile(filePath, contentType, context, baseURI) { return this.uploadService.putFile(filePath, contentType, context, baseURI).execute(); } /** * Starts a transaction and produces a {@link TransactionalRepositoryClient}. * * The transactions ID is extracted from the <code>location</code> header and * is used as endpoint for the produced TransactionalRepositoryClient. * * If no transaction isolation level is provided, the server will use its * default isolation level. * * @param {string} [isolationLevel] an optional parameter to specify the * transaction's level of isolation; for possible values see * {@link TransactionIsolationLevel} * @return {Promise<TransactionalRepositoryClient>} transactional client */ }, { key: "beginTransaction", value: function beginTransaction(isolationLevel) { return this.transactionService.beginTransaction(isolationLevel); } }]); }(BaseRepositoryClient); module.exports = RDFRepositoryClient;