UNPKG

jsc8

Version:

The official Macrometa JavaScript SDK.

322 lines 11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Graph = exports.GraphEdgeCollection = exports.GraphVertexCollection = void 0; const collection_1 = require("./collection"); const error_1 = require("./error"); class GraphVertexCollection extends collection_1.BaseCollection { constructor(connection, name, graph) { super(connection, name); this.type = collection_1.CollectionType.DOCUMENT_COLLECTION; this.graph = graph; } document(documentHandle, graceful = false, opts = {}) { const result = this._connection.request({ path: `/_api/graph/${this.graph.name}/vertex/${this._documentHandle(documentHandle)}`, qs: opts, }, (res) => res.body.vertex); if (!graceful) return result; return result.catch((err) => { if ((0, error_1.isC8Error)(err) && err.errorNum === collection_1.DOCUMENT_NOT_FOUND) { return null; } throw err; }); } vertex(documentHandle, graceful = false) { return this.document(documentHandle, graceful); } save(data, opts) { return this._connection.request({ method: "POST", path: `/_api/graph/${this.graph.name}/vertex/${this.name}`, body: data, qs: opts, }, (res) => res.body); } replace(documentHandle, newValue, opts = {}) { const headers = {}; if (typeof opts === "string") { opts = { rev: opts }; } if (opts.rev) { let rev; ({ rev, ...opts } = opts); headers["if-match"] = rev; } return this._connection.request({ method: "PUT", path: `/_api/graph/${this.graph.name}/vertex/${this._documentHandle(documentHandle)}`, body: newValue, qs: opts, headers, }, (res) => res.body.vertex); } update(documentHandle, newValue, opts = {}) { const headers = {}; if (typeof opts === "string") { opts = { rev: opts }; } if (opts.rev) { let rev; ({ rev, ...opts } = opts); headers["if-match"] = rev; } return this._connection.request({ method: "PATCH", path: `/_api/graph/${this.graph.name}/vertex/${this._documentHandle(documentHandle)}`, body: newValue, qs: opts, headers, }, (res) => res.body.vertex); } remove(documentHandle, opts = {}) { const headers = {}; if (typeof opts === "string") { opts = { rev: opts }; } if (opts.rev) { let rev; ({ rev, ...opts } = opts); headers["if-match"] = rev; } return this._connection.request({ method: "DELETE", path: `/_api/graph/${this.graph.name}/vertex/${this._documentHandle(documentHandle)}`, qs: opts, headers, }, (res) => res.body); } } exports.GraphVertexCollection = GraphVertexCollection; class GraphEdgeCollection extends collection_1.EdgeCollection { constructor(connection, name, graph) { super(connection, name); this.type = collection_1.CollectionType.EDGE_COLLECTION; this.type = collection_1.CollectionType.EDGE_COLLECTION; this.graph = graph; } document(documentHandle, graceful = false, opts = {}) { const headers = {}; if (opts["if-none-match"]) { let ifNoneMatch; ({ ["if-none-match"]: ifNoneMatch, ...opts } = opts); headers["if-none-match"] = ifNoneMatch; } if (opts["if-match"]) { let ifMatch; ({ ["if-match"]: ifMatch, ...opts } = opts); headers["if-match"] = ifMatch; } const result = this._connection.request({ path: `/_api/graph/${this.graph.name}/edge/${this._documentHandle(documentHandle)}`, qs: opts, headers, }, (res) => res.body.edge); if (!graceful) return result; return result.catch((err) => { if ((0, error_1.isC8Error)(err) && err.errorNum === collection_1.DOCUMENT_NOT_FOUND) { return null; } throw err; }); } save(data, fromId, toId, opts) { if (fromId !== undefined) { if (toId !== undefined) { data._from = this._documentHandle(fromId); data._to = this._documentHandle(toId); } else { opts = fromId; } } return this._connection.request({ method: "POST", path: `/_api/graph/${this.graph.name}/edge/${this.name}`, body: data, qs: opts, }, (res) => res.body); } replace(documentHandle, newValue, opts = {}) { const headers = {}; if (typeof opts === "string") { opts = { rev: opts }; } if (opts.rev) { let rev; ({ rev, ...opts } = opts); headers["if-match"] = rev; } return this._connection.request({ method: "PUT", path: `/_api/graph/${this.graph.name}/edge/${this._documentHandle(documentHandle)}`, body: newValue, qs: opts, headers, }, (res) => res.body.edge); } update(documentHandle, newValue, opts = {}) { const headers = {}; if (typeof opts === "string") { opts = { rev: opts }; } if (opts.rev) { let rev; ({ rev, ...opts } = opts); headers["if-match"] = rev; } return this._connection.request({ method: "PATCH", path: `/_api/graph/${this.graph.name}/edge/${this._documentHandle(documentHandle)}`, body: newValue, qs: opts, headers, }, (res) => res.body.edge); } remove(documentHandle, opts = {}) { const headers = {}; if (typeof opts === "string") { opts = { rev: opts }; } if (opts.rev) { let rev; ({ rev, ...opts } = opts); headers["if-match"] = rev; } return this._connection.request({ method: "DELETE", path: `/_api/graph/${this.graph.name}/edge/${this._documentHandle(documentHandle)}`, qs: opts, headers, }, (res) => res.body.removed); } } exports.GraphEdgeCollection = GraphEdgeCollection; const GRAPH_NOT_FOUND = 1924; class Graph { constructor(connection, name) { this.name = name; this._connection = connection; } get() { return this._connection.request({ path: `/_api/graph/${this.name}` }, (res) => res.body.graph); } exists() { return this.get().then(() => true, (err) => { if ((0, error_1.isC8Error)(err) && err.errorNum === GRAPH_NOT_FOUND) { return false; } throw err; }); } create(properties = {}) { return this._connection.request({ method: "POST", path: "/_api/graph", body: { ...properties, name: this.name, }, }, (res) => res.body.graph); } drop(dropCollections = false) { return this._connection.request({ method: "DELETE", path: `/_api/graph/${this.name}`, qs: { dropCollections }, }, (res) => res.body.removed); } vertexCollection(collectionName) { return new GraphVertexCollection(this._connection, collectionName, this); } listVertexCollections() { return this._connection.request({ path: `/_api/graph/${this.name}/vertex` }, (res) => res.body.collections); } async vertexCollections() { const names = await this.listVertexCollections(); return names.map((name) => new GraphVertexCollection(this._connection, name, this)); } addVertexCollection(collection) { if ((0, collection_1.isC8Collection)(collection)) { collection = collection.name; } return this._connection.request({ method: "POST", path: `/_api/graph/${this.name}/vertex`, body: { collection }, }, (res) => res.body.graph); } removeVertexCollection(collection, dropCollection = false) { if ((0, collection_1.isC8Collection)(collection)) { collection = collection.name; } return this._connection.request({ method: "DELETE", path: `/_api/graph/${this.name}/vertex/${collection}`, qs: { dropCollection, }, }, (res) => res.body.graph); } edgeCollection(collectionName) { return new GraphEdgeCollection(this._connection, collectionName, this); } listEdgeCollections() { return this._connection.request({ path: `/_api/graph/${this.name}/edge` }, (res) => res.body.collections); } async edgeCollections() { const names = await this.listEdgeCollections(); return names.map((name) => new GraphEdgeCollection(this._connection, name, this)); } addEdgeDefinition(definition) { return this._connection.request({ method: "POST", path: `/_api/graph/${this.name}/edge`, body: definition, }, (res) => res.body.graph); } replaceEdgeDefinition(definitionName, definition) { return this._connection.request({ method: "PUT", path: `/_api/graph/${this.name}/edge/${definitionName}`, body: definition, }, (res) => res.body.graph); } removeEdgeDefinition(definitionName, dropCollection = false) { return this._connection.request({ method: "DELETE", path: `/_api/graph/${this.name}/edge/${definitionName}`, qs: { dropCollection, }, }, (res) => res.body.graph); } addVertexToVertexCollection(collectionName, properties = {}, returnNew = false) { return this._connection.request({ method: "POST", path: `/_api/graph/${this.name}/vertex/${collectionName}`, body: { ...properties, }, qs: { returnNew, }, }, (res) => res.body); } addEdgeToEdgeCollection(edgeCollectionName, properties = {}, returnNew = false) { return this._connection.request({ method: "POST", path: `/_api/graph/${this.name}/edge/${edgeCollectionName}`, body: { ...properties, }, qs: { returnNew, }, }, (res) => res.body); } } exports.Graph = Graph; //# sourceMappingURL=graph.js.map