Skip to content

TypeScript SDK Installation

The DBX TypeScript SDK provides a type-safe, modern interface for interacting with DBX APIs. It includes full TypeScript support, comprehensive error handling, and built-in connection management via NAPI bindings.

Overview

The TypeScript SDK is designed for modern JavaScript and TypeScript applications, providing:

  • Full TypeScript support with comprehensive type definitions
  • Promise-based API with async/await support
  • Automatic connection management and retry logic
  • Built-in error handling and validation
  • Support for both REST and WebSocket APIs
  • High-performance NAPI bindings for optimal performance

Installation

NPM

npm install @0dbx/redis

Yarn

yarn add @0dbx/redis

PNPM

pnpm add @0dbx/redis

Quick Start

Basic Setup

import { DbxRedisClient } from "@0dbx/redis";
 
// Create a client instance
const client = new DbxRedisClient("http://localhost:3000");
 
// Test the connection
async function testConnection() {
  try {
    const result = await client.string.get("test-key");
    console.log("Connected to DBX:", result);
  } catch (error) {
    console.error("Connection failed:", error);
  }
}
 
testConnection();

TypeScript Configuration

Add the following to your tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "node",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "declaration": true,
    "outDir": "./dist"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

Configuration

Client Options

// Basic client
const client = new DbxRedisClient("http://localhost:3000");
 
// Client with timeout
const clientWithTimeout = DbxRedisClient.withTimeout("http://localhost:3000", 5000);

Environment Variables

You can configure the client using environment variables:

export DBX_BASE_URL=http://localhost:3000
export DBX_TIMEOUT=5000
export DBX_LOG_LEVEL=info
import { DbxRedisClient } from "@0dbx/redis";
 
const client = new DbxRedisClient(process.env.DBX_BASE_URL!);

Client Initialization

Basic Client

import { DbxRedisClient } from "@0dbx/redis";
 
const client = new DbxRedisClient("http://localhost:3000");

Client with Timeout

const client = DbxRedisClient.withTimeout("http://localhost:3000", 5000);

WebSocket Client

import { DbxWsClient } from "@0dbx/redis";
 
const wsClient = new DbxWsClient("ws://localhost:3000/redis_ws");

API Clients

The SDK provides separate clients for different data types:

String Client

// Access string operations
const stringClient = client.string;
 
// Basic operations
await stringClient.set("my-key", "my-value");
const value = await stringClient.get("my-key");
await stringClient.delete("my-key");

Set Client

// Access set operations
const setClient = client.set;
 
// Basic operations
await setClient.addMember("my-set", "member1");
const members = await setClient.getMembers("my-set");
await setClient.removeMember("my-set", "member1");

WebSocket String Client

// Access WebSocket string operations
const wsStringClient = wsClient.string;
 
// Basic operations
await wsStringClient.set("my-key", "my-value");
const value = await wsStringClient.get("my-key");

WebSocket Set Client

// Access WebSocket set operations
const wsSetClient = wsClient.set;
 
// Basic operations
await wsSetClient.addMember("my-set", "member1");
const members = await wsSetClient.getMembers("my-set");

Error Handling

try {
  const value = await client.string.get("non-existent-key");
} catch (error) {
  if (error.message.includes("not found")) {
    console.log("Key doesn't exist");
  } else {
    console.error("Unexpected error:", error);
  }
}

Connection Management

The SDK automatically manages connections and provides connection status:

// Get the base URL
const baseUrl = client.get_base_url();
console.log("Connected to:", baseUrl);
 
// WebSocket client base URL
const wsBaseUrl = wsClient.get_base_url();
console.log("WebSocket connected to:", wsBaseUrl);

Performance Considerations

Connection Pooling

The SDK uses connection pooling for optimal performance:

// The client automatically manages connection pooling
const client = new DbxRedisClient("http://localhost:3000");
 
// Multiple concurrent requests use the same connection pool
const promises = [client.string.get("key1"), client.string.get("key2"), client.string.get("key3")];
 
const results = await Promise.all(promises);

Batch Operations

For better performance with multiple operations:

// Use batch operations when possible
const batchResults = await client.string.batch([
  { type: "set", key: "key1", value: "value1" },
  { type: "set", key: "key2", value: "value2" },
  { type: "get", key: "key3" },
]);

Migration from Previous Versions

If you're migrating from a previous version:

// Old way (if applicable)
// import { DbxClient } from "@0dbx/redis";
// const client = new DbxClient({ baseUrl: "http://localhost:3000" });
 
// New way
import { DbxRedisClient } from "@0dbx/redis";
const client = new DbxRedisClient("http://localhost:3000");

Troubleshooting

Common Issues

Module not found
# Make sure you're using the correct package name
npm install @0dbx/redis
Connection timeout
// Use a longer timeout
const client = DbxRedisClient.withTimeout("http://localhost:3000", 10000);
TypeScript errors
// Add to tsconfig.json
{
  "compilerOptions": {
    "skipLibCheck": true,
    "esModuleInterop": true
  }
}

Next Steps