Graph - Graph Database

Managed graph database powered by ArangoDB for modeling relationships, knowledge graphs, and interconnected data.

Overview

Graph provides a managed graph database optimized for:

  • Knowledge graphs and ontologies
  • Social networks and relationships
  • Recommendation engines
  • Fraud detection
  • Network analysis

Quick Start

import { Tenzro } from 'tenzro';
const tenzro = new Tenzro({ apiKey: 'your-api-key' });
// Create a graph database
const db = await tenzro.graph.create({
projectId: 'project-id',
db_name: 'knowledge-graph',
backend: 'arangodb',
});
// Create collections (nodes)
await tenzro.graph.mutate(db.graph_db_id, `
db._createDocumentCollection('users');
db._createDocumentCollection('products');
db._createEdgeCollection('purchases');
`);
// Insert nodes
await tenzro.graph.mutate(db.graph_db_id, `
db.users.insert({ _key: 'user1', name: 'Alice', age: 30 });
db.users.insert({ _key: 'user2', name: 'Bob', age: 25 });
db.products.insert({ _key: 'prod1', name: 'Widget', price: 29.99 });
`);
// Create relationships (edges)
await tenzro.graph.mutate(db.graph_db_id, `
db.purchases.insert({
_from: 'users/user1',
_to: 'products/prod1',
date: '2024-01-15',
quantity: 2
});
`);
// Query the graph
const { data } = await tenzro.graph.query(db.graph_db_id, `
FOR user IN users
FOR product IN 1..1 OUTBOUND user purchases
RETURN { user: user.name, bought: product.name }
`);
console.log(data); // [{ user: 'Alice', bought: 'Widget' }]

SDK Reference

Create Database

const db = await tenzro.graph.create({
projectId: string,
db_name: string,
backend: 'arangodb', // Currently supported backend
description?: string,
});

Query

const result = await tenzro.graph.query(
graphDbId,
query: string, // AQL query
variables?: Record<string, any> // Bind variables
);
// Returns: { data: any, backend: 'arangodb' }

Mutate

const result = await tenzro.graph.mutate(
graphDbId,
mutation: string, // AQL mutation
variables?: Record<string, any>
);

AQL Query Language

ArangoDB uses AQL (ArangoDB Query Language), similar to SQL but designed for graphs:

Basic Queries

// Get all documents
FOR doc IN users
RETURN doc
// Filter documents
FOR user IN users
FILTER user.age >= 18
RETURN user
// Sort and limit
FOR user IN users
SORT user.created_at DESC
LIMIT 10
RETURN user
// Project specific fields
FOR user IN users
RETURN { name: user.name, email: user.email }

Graph Traversals

// Find direct connections (1 hop)
FOR vertex IN 1..1 OUTBOUND 'users/user1' follows
RETURN vertex
// Find all connections (up to 3 hops)
FOR vertex, edge, path IN 1..3 OUTBOUND 'users/user1' follows
RETURN { user: vertex.name, distance: LENGTH(path.edges) }
// Bidirectional traversal
FOR vertex IN 1..2 ANY 'users/user1' knows
RETURN DISTINCT vertex
// Shortest path
FOR path IN OUTBOUND SHORTEST_PATH 'users/user1' TO 'users/user100' follows
RETURN path.vertices[*].name

Aggregations

// Count and group
FOR user IN users
COLLECT city = user.city WITH COUNT INTO count
RETURN { city, count }
// Sum and average
FOR order IN orders
COLLECT user = order.user_id AGGREGATE total = SUM(order.amount)
RETURN { user, total }

Use Cases

Knowledge Graph

// Create schema for knowledge graph
await tenzro.graph.mutate(graphDbId, `
db._createDocumentCollection('entities');
db._createDocumentCollection('concepts');
db._createEdgeCollection('relations');
`);
// Add entities and relationships
await tenzro.graph.mutate(graphDbId, `
LET apple = (INSERT { name: 'Apple Inc.', type: 'company' } INTO entities RETURN NEW)
LET tech = (INSERT { name: 'Technology', type: 'industry' } INTO concepts RETURN NEW)
INSERT { _from: apple[0]._id, _to: tech[0]._id, type: 'belongs_to' } INTO relations
`);
// Query: Find all companies in technology
const { data } = await tenzro.graph.query(graphDbId, `
FOR concept IN concepts
FILTER concept.name == 'Technology'
FOR entity IN 1..1 INBOUND concept relations
FILTER entity.type == 'company'
RETURN entity.name
`);

Recommendation Engine

// Find products bought by similar users
const { data } = await tenzro.graph.query(graphDbId, `
LET userProducts = (
FOR product IN 1..1 OUTBOUND @userId purchases
RETURN product._key
)
FOR similarUser IN users
FILTER similarUser._key != @userId
LET commonProducts = (
FOR product IN 1..1 OUTBOUND similarUser purchases
FILTER product._key IN userProducts
RETURN 1
)
FILTER LENGTH(commonProducts) >= 3
FOR recommendation IN 1..1 OUTBOUND similarUser purchases
FILTER recommendation._key NOT IN userProducts
COLLECT product = recommendation WITH COUNT INTO score
SORT score DESC
LIMIT 10
RETURN { product: product.name, score }
`, { userId: 'users/user1' });

Social Network

// Find friends of friends (not already connected)
const { data } = await tenzro.graph.query(graphDbId, `
LET directFriends = (
FOR friend IN 1..1 ANY @userId follows
RETURN friend._key
)
FOR fof IN 2..2 ANY @userId follows
FILTER fof._key != @userId
FILTER fof._key NOT IN directFriends
COLLECT person = fof WITH COUNT INTO mutualCount
SORT mutualCount DESC
LIMIT 20
RETURN {
name: person.name,
mutualFriends: mutualCount
}
`, { userId: 'users/user1' });

Bind Variables

Always use bind variables for user input to prevent injection:

// Good - using bind variables
const { data } = await tenzro.graph.query(graphDbId, `
FOR user IN users
FILTER user.email == @email
RETURN user
`, { email: userInput });
// Bad - string interpolation (vulnerable)
// Don't do this:
// `FOR user IN users FILTER user.email == '${userInput}' RETURN user`

Best Practices

  • Use document collections for nodes: Store entity data in document collections
  • Use edge collections for relationships: Edges must have _from and _to fields
  • Index traversal start points: Create indexes on _from and _to for edge collections
  • Limit traversal depth: Use specific depths (1..3) instead of unbounded traversals
  • Use bind variables: Always parameterize user input
  • Batch mutations: Group multiple inserts/updates in single queries

Limits

ResourceLimit
Max collections per database1000
Max documents per collection100 million
Max traversal depth10
Query timeout60 seconds
Max result size100 MB