Esempio n. 1
0
router.post('/:graph/vertex/:collection', function(req, res) {
  const waitForSync = Boolean(req.queryParams.waitForSync);
  const name = req.pathParams.graph;
  const collection = req.pathParams.collection;
  let g;
  try {
    g = Graph._graph(name);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  checkCollection(g, collection);
  let meta;
  try {
    meta = g[collection].save(req.body, {waitForSync});
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_INVALID_EDGE.code) {
      throw Object.assign(
        new httperr.BadRequest(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  setResponse(res, 'vertex', meta, waitForSync ? CREATED : ACCEPTED);
})
Esempio n. 2
0
router.put('/:graph/edge/:collection/:key', function(req, res) {
  const waitForSync = Boolean(req.queryParams.waitForSync);
  const name = req.pathParams.graph;
  const collection = req.pathParams.collection;
  const key = req.pathParams.key;
  const id = `${collection}/${key}`;
  let g;
  try {
    g = Graph._graph(name);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  checkCollection(g, collection);
  let doc;
  try {
    doc = g[collection].document(id);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_ARANGO_DOCUMENT_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  matchVertexRevision(req, doc._rev);
  const meta = g[collection].replace(id, req.body, {waitForSync});
  setResponse(res, 'edge', meta, waitForSync ? OK : ACCEPTED);
})
Esempio n. 3
0
router.get('/:graph/vertex/:collection/:key', function(req, res) {
  const name = req.pathParams.graph;
  const collection = req.pathParams.collection;
  const key = req.pathParams.key;
  const id = `${collection}/${key}`;
  let g;
  try {
    g = Graph._graph(name);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  checkCollection(g, collection);
  let doc;
  try {
    doc = g[collection].document(id);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_ARANGO_DOCUMENT_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  matchVertexRevision(req, doc._rev);
  setResponse(res, 'vertex', doc, OK);
})
Esempio n. 4
0
router.post('/:graph/edge', function(req, res) {
  const name = req.pathParams.graph;
  let g;
  try {
    g = Graph._graph(name);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  try {
    g._extendEdgeDefinitions(req.body);
  } catch (e) {
    if (e.isArangoError && [
      errors.ERROR_GRAPH_COLLECTION_MULTI_USE.code,
      errors.ERROR_GRAPH_COLLECTION_USE_IN_MULTI_GRAPHS.code,
      errors.ERROR_GRAPH_CREATE_MALFORMED_EDGE_DEFINITION.code
    ].indexOf(e.errorNum) !== -1) {
      throw Object.assign(
        new httperr.BadRequest(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  setResponse(res, 'graph', graphForClient(g), ACCEPTED);
})
Esempio n. 5
0
router.delete('/:graph/vertex/:collection', function(req, res) {
  const dropCollection = Boolean(req.queryParams.dropCollection);
  const name = req.pathParams.graph;
  const defName = req.pathParams.collection;
  let g;
  try {
    g = Graph._graph(name);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  try {
    g._removeVertexCollection(defName, dropCollection);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_IN_ORPHAN_COLLECTION.code) {
      throw Object.assign(
        new httperr.BadRequest(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  setResponse(res, 'graph', graphForClient(g), ACCEPTED);
})
Esempio n. 6
0
router.get('/:graph/edge', function(req, res) {
  const name = req.pathParams.graph;
  let g;
  try {
    g = Graph._graph(name);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  setResponse(res, 'collections', _.map(g._edgeCollections(), (c) => c.name()).sort(), OK);
})
Esempio n. 7
0
router.get('/:graph', function(req, res) {
  const name = req.pathParams.graph;
  let g;
  try {
    g = Graph._graph(name);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  setResponse(res, 'graph', graphForClient(g), OK);
})
Esempio n. 8
0
router.delete('/:graph/edge/:definition', function(req, res) {
  const dropCollection = Boolean(req.queryParams.dropCollection);
  const name = req.pathParams.graph;
  const defName = req.pathParams.definition;
  let g;
  try {
    g = Graph._graph(name);
    g._deleteEdgeDefinition(defName, dropCollection);
  } catch (e) {
    if (e.isArangoError && [
      errors.ERROR_GRAPH_NOT_FOUND.code,
      errors.ERROR_GRAPH_EDGE_COLLECTION_NOT_USED.code,
    ].indexOf(e.errorNum) !== -1) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  setResponse(res, 'graph', graphForClient(g), ACCEPTED);
})
Esempio n. 9
0
router.get('/:graph/vertex', function(req, res) {
  const name = req.pathParams.graph;
  const excludeOrphans = req.queryParams.excludeOrphans;
  let g;
  try {
    g = Graph._graph(name);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  const mapFunc = (
    req.pathParams.collectionObjects
    ? (c) => collectionRepresentation(c, false, false, false)
    : (c) => c.name()
  );
  setResponse(res, 'collections', _.map(g._vertexCollections(excludeOrphans), mapFunc).sort(), OK);
})
Esempio n. 10
0
router.post('/:graph/vertex', function(req, res) {
  const name = req.pathParams.graph;
  let g;
  try {
    g = Graph._graph(name);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  try {
    g._addVertexCollection(req.body.collection);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_VERTEX_COL_DOES_NOT_EXIST.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    if (e.isArangoError && [
      errors.ERROR_GRAPH_WRONG_COLLECTION_TYPE_VERTEX.code,
      errors.ERROR_GRAPH_COLLECTION_USED_IN_EDGE_DEF.code,
      errors.ERROR_GRAPH_COLLECTION_USED_IN_ORPHANS.code
    ].indexOf(e.errorNum) !== -1) {
      throw Object.assign(
        new httperr.BadRequest(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  setResponse(res, 'graph', graphForClient(g), ACCEPTED);
})
Esempio n. 11
0
router.post('/:graph/edge/:collection', function(req, res) {
  const name = req.pathParams.graph;
  const collection = req.pathParams.collection;
  if (!req.body._from || !req.body._to) {
    throw Object.assign(
      new httperr.Gone(errors.ERROR_GRAPH_INVALID_EDGE.message),
      {errorNum: errors.ERROR_GRAPH_INVALID_EDGE.code}
    );
  }
  let g;
  try {
    g = Graph._graph(name);
  } catch (e) {
    if (e.isArangoError && e.errorNum === errors.ERROR_GRAPH_NOT_FOUND.code) {
      throw Object.assign(
        new httperr.NotFound(e.errorMessage),
        {errorNum: e.errorNum, cause: e}
      );
    }
    throw e;
  }
  checkCollection(g, collection);
  let meta;
  try {
    meta = g[collection].save(req.body);
  } catch(e) {
    if (e.errorNum !== errors.ERROR_GRAPH_INVALID_EDGE.code) {
      throw Object.assign(
        new httperr.Gone(e.errorMessage),
        {errorNum: errors.ERROR_GRAPH_INVALID_EDGE.code, cause: e}
      );
    }
    throw e;
  }
  setResponse(res, 'edge', meta, ACCEPTED);
})