A simple, fast, and easy-to-use Node.js client for interacting with the Prithvi in-memory database server over raw TCP. This SDK handles connection management, command execution, and automatic reconnection with optional authentication support.
npm i prithvi-js-sdkconst PrithviClient = require("prithvi-js-sdk");
const client = new PrithviClient("127.0.0.1", 1902);
async function main() {
try {
await client.connect();
console.log("Connected to Prithvi.");
// Optional: Authenticate
await client.auth("your-username");
await client.token(); // Reuses stored token
await client.set("mykey", "myvalue");
const value = await client.get("mykey");
console.log("GET response:", value);
} catch (error) {
console.error("Error:", error.message);
} finally {
client.close();
}
}
main();-
new PrithviClient(host, port)Initializes the client. Defaults:host = '127.0.0.1',port = 1902. -
connect()Establishes TCP connection to the Prithvi server. -
close()Closes the client connection.
-
auth(username)Initiates authentication. Returns and stores aTOKEN <hash>internally. -
token(hash?)Sends the stored token (fromauth) or manually passed token to authenticate session. -
getStoredToken()Returns the currently stored token hash.
-
set(key, value, expiry?)Sets a key with optional expiry (in seconds). -
get(key)Retrieves value for a key. -
del(key)Deletes a key. -
exists(key)Checks existence of a key. -
keys()Returns list of all keys.
-
sadd(key, value)Adds a value to a set. -
smembers(key)Returns all members of a set. -
srem(key, value)Removes a value from a set.
-
lpush(key, value)Prepends value to a list. -
rpush(key, value)Appends value to a list. -
lpop(key)Removes and returns first element. -
rpop(key)Removes and returns last element. -
getList(key)Returns all elements in the list.
-
flush(confirm)Clears all data. Passtrueto confirm. -
save()Persists data to disk. -
load()Loads data from disk. -
quit()Gracefully closes the server-side session. -
help()Returns a list of supported server commands.
- Auto-reconnect is built-in.
- If the connection drops, the client retries 5 times before giving up.
- Token is preserved across reconnects if previously authenticated.
- All methods return Promises — use
async/awaitor.then()/.catch().
await client.connect();
await client.auth("sid");
await client.token();
await client.set("count", "1");
console.log(await client.get("count")); // Output: "1"
console.log("Stored token:", client.getStoredToken());
client.close();Contributions welcome! Open issues, suggest features, or submit PRs.
This project is licensed under the MIT License.