๐ Connection
This guide explains how to establish, manage, and close connections to the ZTeraDB Server using the core ZTeraDBConnection client engine.
๐ What is ZTeraDBConnection?โ
The ZTeraDBConnection class serves as the primary network broker for your application. It abstracts low-level socket management and handles:
- ๐ Secure Handshakes: Opens and manages TCP/TLS streams directly to the server.
- ๐ซ Session Auth: Handles initial token validations using your configuration keys.
- ๐ High-Throughput Streaming: Executes ZQL payloads and delivers efficient buffer streams.
- ๐ Socket Reuse: Integrates directly with client-side connection pooling layers.
๐ง Architectural Overviewโ
ZTeraDB decouples your application from the underlying target systems by acting as a single database proxy router:
๐ฆ Initializing a Connectionโ
The connection constructor accepts your target infrastructure endpoints along with your initialized configuration layout.
import { ZTeraDBConnection, ZTeraDBConfig } from "@zteradb/client"; // Or using commonJS: const { ZTeraDBConnection, ZTeraDBConfig } = require('@zteradb/client');
const config = new ZTeraDBConfig(JSON.parse(process.env.ZTERADB_CONFIG));
// Signature format: ZTeraDBConnection(ZTeraDBConfig config, string host, int port)
const connection = new ZTeraDBConnection(
config, // ZTeraDBConfig object
"Your ZTeraDB HOST", // Host
"Your ZTeraDB PORT Number" // Port
);
๐ Constructor Parametersโ
| Parameter | Type | Required | Description |
|---|---|---|---|
host | string | Yes | The remote endpoint or network IP address allocated to your cluster runtime. (e.g., Your ZTeraDB HOST) |
port | int | Yes | The active TCP entry port assigned to your instance. Defaults universally to Your ZTeraDB PORT. |
config | ZTeraDBConfig | Yes | An initialized, valid configuration matrix containing your authentication profile. |
๐ Client Methodsโ
1. run(ZTeraDBQuery query): iterableโ
Submits an abstracted ZQL query framework directly to the cluster infrastructure socket.
const query = new ZTeraDBQuery('user').select();
const result = await db.run(query);
- Memory Optimization: This method yields an iterable data stream. Rows are parsed as they arrive over the wire rather than loading the entire payload block into memory at once. It is highly recommended to loop through datasets via
foreach().
2. close(): voidโ
Closes active streaming connections and frees up network socket descriptors on the host device.
await db->close();
๐ก Serverless Tip: Always explicitly invoke
close()at the conclusion of your script, especially inside ephemeral microservice architectures (like AWS Lambda or Bref) to prevent connection leaks.
๐งช Complete Implementation Blueprintโ
// Import required classes
import { ZTeraDBConfig, ZTeraDBConnection, ZTeraDBQuery } from "@zteradb/client"; // Or using commonJS: const { ZTeraDBConfig, ZTeraDBConnection, ZTeraDBQuery } = require('@zteradb/client');
// Set configuration
const config = new ZTeraDBConfig(JSON.parse(process.env.ZTERADB_CONFIG));
//
const db = new ZTeraDBConnection(config, "Your ZTeraDB HOST", "Your ZTeraDB Port number");
// Select all records from user schema
const query = new ZTeraDBQuery("user").select();
// Run and wait for the query response
const result = await db.run(query);
// Iterate the query result
for await (const row of result) {
console.log(row);
}
// Close the ZTeraDB connection
await db.close();
โ ๏ธ Troubleshooting Connection Failuresโ
-
โ Socket Exception / Timeout Errors: Usually points to incorrect network routing or network access controls blocking access.
- Fix: Double-check your host endpoint address and make sure outbound connections on port
7777are permitted by your firewall.
- Fix: Double-check your host endpoint address and make sure outbound connections on port
-
โ Authentication Rejections: The client can reach the server but your security handshake fails.
- Fix: Ensure all required keys (
client_key,access_key,secret_key, anddatabase_id) are mapped properly through your.envloader.
- Fix: Ensure all required keys (
-
โ Resource Leakage Warning: NodeJS warning notifications or high system socket metrics.
- Fix: Wrap execution processes inside a structural
try...finallyblock to make suredb->close()runs regardless of execution errors.
- Fix: Wrap execution processes inside a structural
๐ Next Stepโ
Now that your connection pipeline is established, learn how to build complex data lookups: ๐ ZTeraDB Query Guide