ZTeraDB Connection Guide
This guide explains how to create and manage a connection to ZTeraDB Server using the ZTeraDBConnection.
π What is ZTeraDBConnection?
ZTeraDBConnection is the class responsible for:
- Opening a secure connection to ZTeraDB Server
- Authenticating with your keys
- Executing ZQL queries
- Streaming results efficiently
- Managing connection pooling
- Handling network communication
- Closing the connection safely
It works together with ZTeraDBConfig.
You donβt need to manage sockets or databases yourself β ZTeraDB does all the heavy lifting.
π§ How Connection Works (Simple Diagram)
Your App
β
ZTeraDBConnection
β
ZTeraDB Server / Your Hosted ZTeraDB Server
β
Your Actual Databases
π¦ Creating a Connection
import { ZTeraDBConnection } from "zteradb";
const config = JSON.parse(process.env.ZTERADB_CONFIG);
const connection = new ZTeraDBConnection(
config, // ZTeraDBConfig object
"db.zteradb.com", // Host
7777 // Port
);
β Make sure you passed the same config from your .env file
β Ensure your host and port are correct for your ZTeraDB instance
π§ͺ Example: Full Working Connection
import { ZTeraDBConnection, ZTeraDBQuery } from "zteradb";
const config = JSON.parse(process.env.ZTERADB_CONFIG);
const db = new ZTeraDBConnection(config, "db1.zteradb.com", 7777);
const query = new ZTeraDBQuery("user").select();
const result = await db.run(query);
for await (const row of result) {
console.log(row);
}
db.close();
π Constructor Parameters
βοΈ configβ
An instance of ZTeraDBConfig.
config = new ZTeraDBConfig([...]);
π hostβ
The ZTeraDB endpoint assigned to your database.
Example:
"db1.zteradb.com"
π portβ
ZTeraDB communication port (TCP).
Default for most clusters: 7777
π Methods of ZTeraDBConnection
1οΈβ£ run(query)β
Use the run() method to execute queries created with ZTeraDBQuery.
query = new ZTeraDBQuery("user")->select();
const result = await connection.run(query);
Returned value β Async Generatorβ
This means you can iterate results like:
for await (const row of result) {
console.log(row);
}
Returns an async iterable stream
Efficient for large data sets
Avoids loading all rows into memory
2οΈβ£ close()β
Closes all active TCP connections.
connection.close();
Always close after finishing your queries β especially in serverless environments.
β Best Practice: Put Config in .env
Example .env:
ZTERADB_CONFIG={
"clientKey": "<Your ZTeraDB client key>",
"accessKey": "<Your ZTeraDB access key>",
"secretKey": "<Your ZTeraDB secret key>",
"databaseID": "<Your ZTeraDB database id>",
"env": "<dev|staging|qa|prod>",
"responseDataType": "json"
}
Load it:
const config = JSON.parse(process.env.ZTERADB_CONFIG);
β οΈ Common Errors & Fixes
β Wrong host or portβ
β Check your ZTeraDB dashboard for correct host
β Passing wrong config fieldsβ
β Ensure all environment variables are set
β JSON parse errorβ
β Ensure .env contains valid JSON
β Connection closed earlyβ
β Call await db.close() only after all queries
π Youβre ready to run queries!
Continue to:
π zteradb-query.md