当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Graph.edgeCollection方法代码示例

本文整理汇总了TypeScript中arangojs.Graph.edgeCollection方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Graph.edgeCollection方法的具体用法?TypeScript Graph.edgeCollection怎么用?TypeScript Graph.edgeCollection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在arangojs.Graph的用法示例。


在下文中一共展示了Graph.edgeCollection方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: removeEdge

 /**
  * Deletes the edge with the given documentHandle from the collection.
  *
  * @param {string} collectionName edge collection name
  * @param  {string} documentHandle The handle of the edge to retrieve.
  * This can be either the _id or the _key of an edge in the collection,
  * or an edge (i.e. an object with an _id or _key property).
  * @return  {Object} removed Edge
  */
 async removeEdge(collectionName: string, documentHandle: string): Promise<any> {
   if (_.isNil(collectionName)) {
     throw new Error('missing edge collection name');
   }
   if (_.isNil(documentHandle)) {
     throw new Error('missing document handle');
   }
   const collection = this.graph.edgeCollection(collectionName);
   return collection.remove(documentHandle);
 }
开发者ID:restorecommerce,项目名称:chassis-srv,代码行数:19,代码来源:graph.ts

示例2: getOutEdges

 /**
 * get all outgoing edges.
 *
 * @param  {String} collectionName edge collection name
 * @param  {String} documentHandle The handle of the document
 * @return  {[Object]} list of edges
 */
 async getOutEdges(collectionName: string, documentHandle: string):
   Promise<[Object]> {
   if (_.isNil(collectionName)) {
     throw new Error('missing edge collection name');
   }
   if (_.isNil(documentHandle)) {
     throw new Error('missing document handle');
   }
   const collection = this.graph.edgeCollection(collectionName);
   return collection.outEdges(documentHandle);
 }
开发者ID:restorecommerce,项目名称:chassis-srv,代码行数:18,代码来源:graph.ts

示例3: createEdge

  /**
   * Creates a new edge between the vertices fromId and toId with the given data.
   *
   * @param  {string} collectionName name of the edge collection
   * @param  {Object} data The data of the new edge. If fromId and toId are not
   * specified, the data needs to contain the properties _from and _to.
   * @param  {string} fromId The handle of the start vertex of this edge.
   * This can be either the _id of a document in the database, the _key of an
   * edge in the collection, or a document (i.e. an object with an _id or _key property).
   * @param {string} toId The handle of the end vertex of this edge.
   * This can be either the _id of a document in the database, the _key of an
   * edge in the collection, or a document (i.e. an object with an _id or _key property).
   * @return  {Object} edge object
   */
  async createEdge(collectionName: string, data: Object, fromId?: string,
    toId?: string): Promise<Object> {
    if (_.isNil(collectionName)) {
      throw new Error('missing edge collection name');
    }
    if (_.isNil(data)) {
      data = {};
    }

    const collection = this.graph.edgeCollection(collectionName);
    return collection.save(data, fromId, toId);
  }
开发者ID:restorecommerce,项目名称:chassis-srv,代码行数:26,代码来源:graph.ts

示例4: findTreesCommonAncestor

  /**
   * Finds the lowest common ancestor between two nodes of a tree-shaped graph and returns the subtree in that node.
   */
  async findTreesCommonAncestor(nodes: string[], collectionName: string,
    edgeName: string): Promise<any> {
    // preprocessing to get all the roots
    const collection = this.graph.edgeCollection(edgeName);
    const roots = {};
    for (let node of nodes) {
      node = `${collectionName}/${node}`;
      const result = await collection.traversal(node, {
        direction: 'outbound'
      });
      // const result = await this.db.query(`FOR v IN 1..10000 OUTBOUND @vertex GRAPH @graph FILTER "${rawFilter}" RETURN v`, { graph: this.graph.name, vertex: node });

      if (_.isEmpty(result.visited) || _.isEmpty(result.visited.vertices)) {
        if (!roots[node]) {
          roots[node] = [node];
        }

        continue;
      }
      const items = result.visited.vertices;
      const root = _.isArray(items) ? items[items.length - 1] : items;
      if (!roots[root._id]) {
        roots[root._id] = [node];
      } else {
        roots[root._id].push(node);
      }
    }

    const lca = async function LCA(nodeA, nodeList: string[]) {
      if (nodeList.length > 1) {
        const slices = nodeList.slice(1, nodeList.length);
        return lca(nodeA, lca(nodes[0], slices));
      } else {
        const result = [await findCommonAncestor(nodeA, nodeList[0])];
        return result;
      }
    };

    const that = this;
    const findCommonAncestor = async function findCommonAncestor(nodeA, nodeB) {
      const queryTpl = `LET firstPath = (FOR v IN 1..10000
      OUTBOUND @vertex1 GRAPH @graph RETURN v)
        FOR v,e,p IN 1..10000 OUTBOUND @vertex2 GRAPH @graph
          LET pos = POSITION(firstPath, v, true)
          FILTER pos != -1
          LIMIT 1
          let endPath = REVERSE(p.vertices)
          return endPath`;
      const result = await that.db.query(queryTpl, {
        vertex1: nodeA,
        vertex2: nodeB,
        graph: that.graph.name
      });
      if (result.count == 0) {
        throw new Error('Unimplemented: hierarchical resources do not share the same root');
      }
      const item = await result.next();
      return item[0];
    };

    let paths = []; // the edges allow us to build the tree
    for (let root in roots) {
      let ancestor: string;
      if (roots[root].length == 1) {
        ancestor = root;
      } else {
        const list = roots[root];
        let vertex = await lca(list[0], list.slice(1, list.length));
        if (_.isArray(vertex)) {
          vertex = vertex[0];
        }
        ancestor = vertex._id;
      }
      const traversal = await collection.traversal(ancestor, {
        direction: 'inbound',
      });
      const visited = traversal.visited;
      paths = paths.concat(visited.paths);
    }

    return {
      paths: {
        value: encodeMessage(paths)
      }
    };
  }
开发者ID:restorecommerce,项目名称:chassis-srv,代码行数:89,代码来源:graph.ts

示例5: traversal

  /**
  * collection traversal - Performs a traversal starting from the given
  * startVertex and following edges contained in this edge collection.
  *
  * @param {String} collectionName collection name
  * @param  {String | String[]} startVertex Start vertex or vertices.
  * This can be either the _id of a document in the database,
  * the _key of an edge in the collection, or a document
  * (i.e. an object with an _id or _key property).
  * @param  {any} opts opts.direction opts.filter, opts.visitor,
  * opts.init, opts.expander, opts.sort
  * @return  {[Object]} edge traversal path
  */
  async traversal(startVertex: string | string[], opts: any, collectionName?: string,
    edgeName?: string, data_flag?: boolean, path_flag?: boolean,
    aql?: boolean): Promise<Object> {
    let collection;
    let traversedData;
    if (_.isNil(startVertex)) {
      throw new Error('missing start vertex name');
    }
    if (opts.lowest_common_ancestor) {
      return this.findTreesCommonAncestor(startVertex as string[],
        collectionName, edgeName);
    }

    let response: any = {
      vertex_fields: [],
      data: {},
      paths: {}
    };
    if (aql && aql == true) {
      // get all the first level childrens for the start vertex
      let result = await this.getAllChildrenNodes(startVertex as string, edgeName);
      let finalResponse = [];
      for (let item of result._result) {
        finalResponse.push(_.omit(item, ['_key', '_id', '_rev']));
      }
      response.data.value = Buffer.from(JSON.stringify(finalResponse));
      return response;
    }

    const vertex = startVertex as string;
    if (_.isArray(vertex)) {
      throw new Error('Invalid number of starting vertices for traversal: ' + vertex.length);
    }
    for (let key in opts) {
      if (_.isEmpty(opts[key])) {
        delete opts[key];
      }
    }

    if (opts && opts.filter) {
      opts.filter = this.traversalFilter(opts.filter);
    } else if (opts && opts.expander) {
      opts.expander = this.traversalExpander(opts.expander);
    }

    if (!opts) {
      // make outbound traversal by default if not provided
      opts = {};
      opts.direction = 'outbound';
    }

    try {
      if (collectionName) {
        collection = this.graph.edgeCollection(collectionName);
        traversedData = await collection.traversal(vertex, opts);
      } else {
        traversedData = await this.graph.traversal(vertex, opts);
      }
    } catch (err) {
      throw { code: err.code, message: err.message };
    }
    let encodedData = new Set<Object>();
    if (data_flag) {
      if (traversedData.visited && traversedData.visited.vertices) {
        traversedData.visited.vertices = this.arrUnique(traversedData.visited.vertices);
        for (let vertice of traversedData.visited.vertices) {
          response.vertex_fields.push(_.pick(vertice, ['_id', '_key', '_rev', 'id']));
          encodedData.add(_.omit(vertice, ['_key', '_rev']));
        }
        response.data.value = encodeMessage(Array.from(encodedData));
      }
    }

    if (path_flag) {
      if (traversedData.visited && traversedData.visited.paths) {
        traversedData.visited.paths = this.arrUnique(traversedData.visited.paths);
        const encodedPaths = encodeMessage(traversedData.visited.paths);
        response.paths.value = encodedPaths;
      }
    }

    return response;
  }
开发者ID:restorecommerce,项目名称:chassis-srv,代码行数:96,代码来源:graph.ts


注:本文中的arangojs.Graph.edgeCollection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。