Velocity
Back to Examples
Beginner Friendly

Supercharged
Todo Application

Learn how to build a lightning-fast task management system using VelocityDB's List and Key-Value primitives.

Architecture

Unlike traditional SQL databases where you'd create tables and schemas, VelocityDB allows you to store entire user states or lists directly in memory. For a Todo app, we can utilize two main approaches: JSON Documents or Linked Lists.

approach_1: JSON Storage

Store the entire todo list as a single JSON object. Best for simple apps with small lists.

SET user:101:todos '[{"id":1, "task":"Ship v1", "done":false}]'

approach_2: List Primitive

Use built-in List commands to append items individually. Better for larger, append-only logs.

RPUSH user:101:todos "Ship v1"

Live Playground

Try it yourself. Every action below sends a real-time command to the Velocity engine simulator.

Interactive Todo Engine

Initialize storage engine
Optimize LSM-tree compaction

Live Binary Protocol Logs

Waiting for connection...
1

Initialize Client

First, connect to your VelocityDB instance. The client manages the connection pool automatically.

const { VelocityClient } = require('velocitydb-client');

const client = new VelocityClient({
  host: '127.0.0.1',
  port: 2005
});

await client.connect();
2

Add Task (API Route)

In your backend API route, simply generate an ID and push the JSON string to the user's list.

app.post('/api/todos', async (req, res) => {
  const { userId, task } = req.body;
  const todo = {
    id: Date.now(),
    text: task,
    completed: false,
    createdAt: new Date().toISOString()
  };
  
  // Store as atomic JSON string in a List
  await client.rpush(`todos:${userId}`, JSON.stringify(todo));
  
  res.json(todo);
});
3

Fetch All Tasks

Retrieve the entire range of the list. `LRANGE 0 -1` fetches everything from the first element to the last.

app.get('/api/todos', async (req, res) => {
  const { userId } = req.query;
  
  // LRANGE key start end
  const items = await client.lrange(`todos:${userId}`, 0, -1);
  
  // Parse JSON strings back to objects
  const todos = items.map(item => JSON.parse(item));
  
  res.json(todos);
});

Quick Commands

Add Item Copy
RPUSH todos:1 "Buy Milk"
List All Copy
LRANGE todos:1 0 -1
Count Items Copy
LLEN todos:1

Performance Tip

For lists with millions of items, avoid `LRANGE 0 -1`. Use pagination with `LRANGE 0 50` to keep your application snappy and memory efficient.