Quick Tutorial
This tutorial will help you get started with RushDB by walking through a simple example of creating and querying a small social network using the RushDB SDK.
Prerequisites
- Create a RushDB account and get an API token (see Get API Token)
- Your preferred programming language: Python, TypeScript/JavaScript, or any HTTP client for REST API
Step 1: Initialize RushDB
Choose your preferred SDK:
- TypeScript
- Python
- REST API
import RushDB from '@rushdb/javascript-sdk';
// Initialize with your API token
const db = new RushDB('your-api-token');
// Or with additional configuration options
// const db = new RushDB('your-api-token', {
// url: 'https://api.rushdb.com',
// timeout: 5000
// });
from rushdb import RushDB
# Connect with your API token
db = RushDB("your-api-token")
# Set your API token for future requests
export TOKEN="your-api-token"
Step 2: Create Records with Labels
Let's create two users in our social network:
- TypeScript
- Python
- REST API
// Create users with the Person [label](../concepts/labels.md)
const alice = await db.records.create({
label: "PERSON",
data: {
name: 'Alice',
age: 28,
interests: ['coding', 'hiking']
}
});
const bob = await db.records.create({
label: "PERSON",
data: {
name: 'Bob',
age: 32,
interests: ['photography', 'travel']
}
});
# Create users with the Person [label](../concepts/labels.md)
alice = db.records.create(
label="PERSON",
data={
"name": "Alice",
"age": 28,
"interests": ["coding", "hiking"]
}
)
bob = db.records.create(
label="PERSON",
data={
"name": "Bob",
"age": 32,
"interests": ["photography", "travel"]
}
)
# Create Alice with PERSON label
curl -X POST "https://api.rushdb.com/api/v1/records" \
-H "Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"label": "PERSON",
"data": {
"name": "Alice",
"age": 28,
"interests": ["coding", "hiking"]
}
}'
# Save the ID from the response for Alice
export ALICE_ID="response_id_here"
# Create Bob with PERSON label
curl -X POST "https://api.rushdb.com/api/v1/records" \
-H "Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"label": "PERSON",
"data": {
"name": "Bob",
"age": 32,
"interests": ["photography", "travel"]
}
}'
# Save the ID from the response for Bob
export BOB_ID="response_id_here"
Step 3: Create Relationships
Let's make Alice and Bob friends:
- TypeScript
- Python
- REST API
// Create a FRIENDS_WITH relationship between Alice and Bob
await alice.attach({
target: bob,
options: {
type: "FRIENDS_WITH"
}
});
# Create a FRIENDS_WITH relationship between Alice and Bob
alice.attach(
target=bob,
options={
"type": "FRIENDS_WITH"
}
)
# Create a FRIENDS_WITH relationship between Alice and Bob
curl -X POST "https://api.rushdb.com/api/v1/relationships/$ALICE_ID" \
-H "Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"targetIds": ["'$BOB_ID'"],
"type": "FRIENDS_WITH"
}'
Step 4: Query Records with Search
Let's find all people who are interested in outdoor activities:
- TypeScript
- Python
- REST API
// Find all people who are interested in outdoor activities using [where](../concepts/search/where.md) conditions
const outdoorsy = await db.records.find({
labels: ['PERSON'],
where: {
interests: { $in: ['hiking', 'travel'] }
}
});
console.log('Found:', outdoorsy.map(person => person.data.name));
# Find all people who are interested in outdoor activities using [where](../concepts/search/where.md) conditions
outdoorsy = db.records.find({
"where": {
"interests": {"$in": ["hiking", "travel"]}
},
"labels": ["PERSON"]
})
print('Found:', [person.data["name"] for person in outdoorsy])
# Find all people who are interested in outdoor activities
curl -X POST "https://api.rushdb.com/api/v1/records/search" \
-H "Token: $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"labels": ["PERSON"],
"where": {
"interests": {
"$in": ["hiking", "travel"]
}
}
}'
Step 5: Using Transactions (Optional)
Transactions ensure data consistency by making a series of operations atomic:
- TypeScript
- Python
- REST API
// Begin a transaction
const transaction = await db.transactions.begin();
try {
// Create a post
const post = await db.records.create({
label: "POST",
data: {
title: "My Hiking Adventure",
content: "Today I went hiking in the mountains...",
createdAt: new Date().toISOString()
},
transaction
});
// Create a relationship between Alice and the post
await alice.attach({
target: post,
options: {
type: "CREATED",
properties: {
at: new Date().toISOString()
}
},
transaction
});
// Commit the transaction
await transaction.commit();
console.log("Post created and linked to Alice successfully!");
} catch (error) {
// Roll back the transaction if anything fails
await transaction.rollback();
console.error("Error occurred, transaction rolled back:", error);
}
# Using a transaction with context manager
with db.transactions.begin() as transaction:
# Create a post
post = db.records.create(
label="POST",
data={
"title": "My Hiking Adventure",
"content": "Today I went hiking in the mountains...",
"createdAt": "2024-05-17T10:30:00.000Z"
},
transaction=transaction
)
# Create a relationship between Alice and the post
alice.attach(
target=post,
options={
"type": "CREATED",
"direction": "out",
"properties": {
"at": "2024-05-17T10:30:00.000Z"
}
},
transaction=transaction
)
# The transaction will automatically commit if no errors occur
# or roll back if an exception is raised
# Start a transaction
TRANSACTION_ID=$(curl -X POST "https://api.rushdb.com/api/v1/transactions" \
-H "Token: $TOKEN" \
-H "Content-Type: application/json" \
| jq -r '.id')
# Create a post with the transaction ID
POST_ID=$(curl -X POST "https://api.rushdb.com/api/v1/records" \
-H "Token: $TOKEN" \
-H "Content-Type: application/json" \
-H "X-Transaction-ID: $TRANSACTION_ID" \
-d '{
"label": "POST",
"data": {
"title": "My Hiking Adventure",
"content": "Today I went hiking in the mountains...",
"createdAt": "'$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")'"
}
}' \
| jq -r '.id')
# Create relationship within the transaction
curl -X POST "https://api.rushdb.com/api/v1/relationships/$ALICE_ID" \
-H "Token: $TOKEN" \
-H "Content-Type: application/json" \
-H "X-Transaction-ID: $TRANSACTION_ID" \
-d '{
"targetIds": ["'$POST_ID'"],
"type": "CREATED"
}'
# Commit the transaction
curl -X POST "https://api.rushdb.com/api/v1/transactions/$TRANSACTION_ID/commit" \
-H "Token: $TOKEN"
Next Steps
- Learn about basic concepts:
- Explore the SDK documentation in more detail:
- Try working with transactions for ensuring data consistency
- Check out tutorials on specific use cases in the Tutorials section