Skip to main content

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:

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
// });

Step 2: Create Records with Labels

Let's create two users in our social network:

// 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']
}
});

Step 3: Create Relationships

Let's make Alice and Bob friends:

// Create a FRIENDS_WITH relationship between Alice and Bob
await alice.attach({
target: bob,
options: {
type: "FRIENDS_WITH"
}
});

Let's find all people who are interested in outdoor activities:

// 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));

Step 5: Using Transactions (Optional)

Transactions ensure data consistency by making a series of operations atomic:

// 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);
}

Next Steps