Node Js client
Infleare Node Js Documentation
The InfleareClient
is a TypeScript/JavaScript library for interacting with an Infleare server. It provides methods to manage JSON data using string-based keys and optional or multi-level paths.
Installation
To install the library, use npm:
npm install infleare
Usage
First, import the InfleareClient
class and create an instance:
const { InfleareClient } = require("infleare");
const infleare = new InfleareClient("localhost", 1234, {
username: "user",
password: "pass",
});
API Reference
connect()
Connects to the server and authenticates the infleare.
Returns: Promise<void>
Example:
infleare.client.on('Connected', () => {
console.log('Infleare client Connected successfully');
});
infleare.client.on('error', (err) => {
console.log('Infleare client error', err);
});
infleare.connect();
close()
Closes the connection to the server.
Example:
infleare.close();
console.log("Connection closed");
KEYS()
Retrieves all keys stored on the server.
Returns: Promise<string[]>
Example:
infleare.KEYS()
.then((keys) => {
console.log("Keys:", keys);
})
.catch((err) => {
console.error("Failed to retrieve keys:", err);
});
Output:
["user", "config", "data"]
SET(key, data)
Sets a key-value pair on the server.
Example 1:
infleare.SET("str-1", "This is my first key")
.then(() => {
console.log("Key set successfully");
})
.catch((err) => {
console.error("Failed to set key:", err);
});
Example 2:
infleare.SET("user", { name: "Alice", age: 25 })
.then(() => {
console.log("Key set successfully");
})
.catch((err) => {
console.error("Failed to set key:", err);
});
GET(key)
Retrieves the value associated with a key.
Example:
infleare.GET("user")
.then((value) => {
console.log("Value:", value);
})
.catch((err) => {
console.error("Failed to retrieve value:", err);
});
Output:
{
name: "Alice",
age: 25
}
DELETE(key, path?)
Deletes a key or a specific path inside a JSON object.
Example Output:
infleare.DELETE("user", "age")
.then(() => {
console.log("Deleted key/path successfully");
})
.catch((err) => {
console.error("Failed to delete key/path:", err);
});
UPDATE(key, path?, data)
Updates a value inside a JSON object.
Example Output:
infleare.UPDATE("user", "name", "Bob")
.then(() => {
console.log("Value updated successfully");
})
.catch((err) => {
console.error("Failed to update value:", err);
});
MAP_SET(key, path?, data)
Sets a value inside a JSON map.
Example Output:
infleare.MAP_SET("config", "theme.color", "dark")
.then(() => {
console.log("Map value set");
})
.catch((err) => {
console.error("Failed to set map value:", err);
});
MAP_GET(key, path?)
Retrieves a value from a JSON map.
Example Output:
infleare.MAP_GET("config", "theme.color")
.then((value) => {
console.log("Map value:", value); // Output: "dark"
})
.catch((err) => {
console.error("Failed to retrieve map value:", err);
});
MAP_DELETE(key, path?)
Deletes a value from a JSON map.
Example Output:
infleare.MAP_DELETE("config", "theme.color")
.then(() => {
console.log("Value deleted from map");
})
.catch((err) => {
console.error("Failed to delete value from map:", err);
});
Complete Example
const express = require("express");
const { InfleareClient } = require("infleare");
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
const infleare = new InfleareClient('127.0.0.1', 4775, {
username: 'admin',
password: 'admin123'
});
infleare.client.on('Connected', () => {
console.log('Infleare client Connected successfully');
});
infleare.client.on('error', (err) => {
console.log('Infleare client error', err);
});
(async () => {
await infleare.connect();
})();
app.post("/users",async (req, res) => {
const body = req.body;
const userId = req.query.userId;
const data = await infleare.SET(userId, body);
res.send(data);
});
app.get("/users", async(req, res) => {
const userId = req.query.userId;
const data = await infleare.GET(userId);
res.send(data);
});
const PORT = 6000;
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));
I have read this