What is @redis/graph?
@redis/graph is an npm package that provides a client for interacting with RedisGraph, a graph database module for Redis. It allows you to create, query, and manage graph data structures within a Redis database.
What are @redis/graph's main functionalities?
Creating Nodes and Relationships
This feature allows you to create nodes and relationships in a graph. The code sample demonstrates how to create a person node, a city node, and a relationship between them.
const { createClient } = require('@redis/graph');
async function createGraph() {
const client = createClient();
await client.connect();
const query = `
CREATE (p:Person {name: 'John Doe', age: 30})
CREATE (c:City {name: 'New York'})
CREATE (p)-[:LIVES_IN]->(c)
`;
await client.query('myGraph', query);
await client.disconnect();
}
createGraph();
Querying the Graph
This feature allows you to query the graph to retrieve data. The code sample demonstrates how to match a person node and a city node connected by a relationship and return their names.
const { createClient } = require('@redis/graph');
async function queryGraph() {
const client = createClient();
await client.connect();
const query = `
MATCH (p:Person)-[:LIVES_IN]->(c:City)
RETURN p.name, c.name
`;
const result = await client.query('myGraph', query);
console.log(result);
await client.disconnect();
}
queryGraph();
Updating Nodes and Relationships
This feature allows you to update properties of nodes and relationships in the graph. The code sample demonstrates how to update the age property of a person node.
const { createClient } = require('@redis/graph');
async function updateGraph() {
const client = createClient();
await client.connect();
const query = `
MATCH (p:Person {name: 'John Doe'})
SET p.age = 31
`;
await client.query('myGraph', query);
await client.disconnect();
}
updateGraph();
Deleting Nodes and Relationships
This feature allows you to delete nodes and relationships from the graph. The code sample demonstrates how to delete a person node and all its relationships.
const { createClient } = require('@redis/graph');
async function deleteGraph() {
const client = createClient();
await client.connect();
const query = `
MATCH (p:Person {name: 'John Doe'})
DETACH DELETE p
`;
await client.query('myGraph', query);
await client.disconnect();
}
deleteGraph();
Other packages similar to @redis/graph
neo4j-driver
The neo4j-driver package is the official driver for connecting to Neo4j, a popular graph database. It provides similar functionalities for creating, querying, updating, and deleting graph data. Compared to @redis/graph, neo4j-driver is specifically designed for Neo4j and offers more advanced features and optimizations for that database.
gremlin
The gremlin package is a JavaScript client for Apache TinkerPop, a graph computing framework that works with various graph databases like JanusGraph and TinkerGraph. It provides a flexible and powerful way to interact with graph databases using the Gremlin query language. Compared to @redis/graph, gremlin supports a wider range of graph databases and offers more complex querying capabilities.
arangodb
The arangodb package is a client for ArangoDB, a multi-model database that supports graph, document, and key-value data models. It allows you to perform graph operations such as creating nodes and edges, querying, and managing graph data. Compared to @redis/graph, arangodb offers a more versatile database solution with support for multiple data models.
@redis/graph
Example usage:
import { createClient, Graph } from 'redis';
const client = createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
await client.connect();
const graph = new Graph(client, 'graph');
await graph.query(
'CREATE (:Rider { name: $riderName })-[:rides]->(:Team { name: $teamName })',
{
params: {
riderName: 'Buzz Aldrin',
teamName: 'Apollo'
}
}
);
const result = await graph.roQuery(
'MATCH (r:Rider)-[:rides]->(t:Team { name: $name }) RETURN r.name AS name',
{
params: {
name: 'Apollo'
}
}
);
console.log(result.data);