BranchDB

Menu

Client SDK - TypeScript/JavaScript

We provide an official client SDK for Node.js, which simplifies network communication and protocol handling.

Below is hands-on walkthrough of all the list of asynchronous methods that BranchDB Client SDK provides.

Client Methods | This is all what you need

MethodDescription
connect()Establishes a connection with the server.
set(key, value, ttl?)Sets a key-value pair, with an optional TTL in seconds.
get(key)Retrieves the value for a given key.
del(key)Deletes a key-value pair.
exists(key)Checks if a key exists.
ttl(key)Returns the remaining time to live for a key.
expire(key, ttl)Sets a new TTL for an existing key.
persist(key)Removes the TTL from a key.
getall()Returns a list of all keys in the database.
flush()Deletes all keys from the database.
disconnect()Terminates the Session TCP connection with BranchDB server.

Install

To install branchdb-client, run:

npm i branchdb-client

Connect and Test

Connect to localhost on port 1981.

import { Branch } from "branchdb-client";

const config = {
  host: process.env.BRANCH_DB_HOST || "localhost",
  port: parseInt(process.env.BRANCH_DB_PORT || "1981"),
  username: "ChandanKhamitkar-Project",
  token: process.env.BRANCH_DB_TOKEN"
};

const client = new Branch(config);

await client.connect();

Example usage of as all methods mentioned above

// Store a token or username or anything
const Response = await client.set("token", "Chandan");

// Store username with Time limit expiry of 30 seconds
const Response = await client.set("user202", {name: "Chandan", age: 21}, 30);

// Fetch stored name via key
const Response = await client.get("user:101");

// Delete stored token
const Response = await client.del("token");

// Check if token exists or not
const Response = await client.exists("token");
// Success response = 'true' if key exists, else 'false'

// Get the remaining time left for key in seconds.
const Response = await client.ttl("token");

// Reset the expiry time for token to 120 seconds.
const Response = await client.expire("token", 120);

// Remove expiry time for token. Make it no-expriy key.
const Response = await client.persist("token");

// Get list of all keys exist in database. 
const Response = await client.getall();
// payload : ['A', 'B', 'C', 'D']

// Delete all keys in database.
const Response = await client.flush();

// Terminate client connection with server.
const Response = await client.disconnect();

We also provide Better Response for every method you call.

Terminal

{

statusCode : 200

success : true

message : Successfully Authenticated.

payload : Here is your token: 833ca2ed8d167

}

Below is the better understanding of response Types.

Terminal

{

statusCode : number

success : boolean

message : string

payload : string | string[] | null

}

// StatuCodes:

// 200 -> On Successfull operation.

// 400 -> Invalid Input from user.

// 404 -> Eg: Key not found.

// 500 -> Server implementation error | Server crash.

// Success is 'true' only if StatuCode = 200

// Payload:

// Will be NULL for most of operations except these methods -> AUTH | GET | GETALL