Refactor SessionPool and TableSessionPool to eliminate code duplication
A Node.js client for Apache IoTDB with support for SessionPool and TableSessionPool, providing efficient connection management and comprehensive query capabilities.
npm install iotdb-client-nodejs
import { Session } from 'iotdb-client-nodejs'; const session = new Session({ host: 'localhost', port: 6667, username: 'root', password: 'root', }); await session.open(); // Execute non-query statement await session.executeNonQueryStatement('CREATE DATABASE root.test'); // Execute query statement with default timeout (60 seconds) const result = await session.executeQueryStatement('SHOW DATABASES'); console.log('Columns:', result.columns); console.log('Rows:', result.rows); // Execute query with custom timeout (30 seconds) const customResult = await session.executeQueryStatement('SELECT * FROM root.test.**', 30000); // Insert tablet data await session.insertTablet({ deviceId: 'root.test.device1', measurements: ['temperature', 'humidity'], dataTypes: [3, 3], // FLOAT timestamps: [Date.now(), Date.now() + 1000], values: [ [25.5, 60.0], [26.0, 61.5], ], }); await session.close();
import { SessionPool } from 'iotdb-client-nodejs'; const pool = new SessionPool('localhost', 6667, { username: 'root', password: 'root', maxPoolSize: 10, minPoolSize: 2, maxIdleTime: 60000, waitTimeout: 60000, }); await pool.init(); // Execute queries using the pool const result = await pool.executeQueryStatement('SELECT * FROM root.test.**'); // Execute non-query statements await pool.executeNonQueryStatement( 'CREATE TIMESERIES root.test.device1.temperature WITH DATATYPE=FLOAT' ); // Insert data await pool.insertTablet({ deviceId: 'root.test.device1', measurements: ['temperature'], dataTypes: [3], // FLOAT timestamps: [Date.now()], values: [[25.5]], }); // Get pool statistics console.log('Pool size:', pool.getPoolSize()); console.log('Available:', pool.getAvailableSize()); await pool.close();
import { SessionPool } from 'iotdb-client-nodejs'; const pool = new SessionPool( ['node1.example.com', 'node2.example.com', 'node3.example.com'], 6667, { username: 'root', password: 'root', maxPoolSize: 15, } ); await pool.init(); // Connections will be distributed across all nodes using round-robin
import { Session } from 'iotdb-client-nodejs'; import * as fs from 'fs'; const session = new Session({ host: 'localhost', port: 6667, username: 'root', password: 'root', enableSSL: true, sslOptions: { ca: fs.readFileSync('/path/to/ca.crt'), cert: fs.readFileSync('/path/to/client.crt'), key: fs.readFileSync('/path/to/client.key'), rejectUnauthorized: true, }, }); await session.open();
import { TableSessionPool } from 'iotdb-client-nodejs'; const tablePool = new TableSessionPool('localhost', 6667, { username: 'root', password: 'root', database: 'my_database', // Set default database for table model maxPoolSize: 10, minPoolSize: 2, }); await tablePool.init(); // Execute queries in table mode const result = await tablePool.executeQueryStatement('SHOW DATABASES'); await tablePool.close();
new Session(config: Partial<Config>)
async open(): Promise<void> - Open the sessionasync close(): Promise<void> - Close the sessionasync executeQueryStatement(sql: string, timeoutMs?: number): Promise<QueryResult> - Execute a query with optional timeout (default: 60000ms)async executeNonQueryStatement(sql: string): Promise<void> - Execute a non-query statementasync insertTablet(tablet: Tablet): Promise<void> - Insert tablet dataisOpen(): boolean - Check if session is opennew SessionPool(hosts: string | string[], port: number, config?: Partial<PoolConfig>)
async init(): Promise<void> - Initialize the poolasync close(): Promise<void> - Close all connectionsasync executeQueryStatement(sql: string, timeoutMs?: number): Promise<QueryResult> - Execute a query with optional timeout (default: 60000ms)async executeNonQueryStatement(sql: string): Promise<void> - Execute a non-query statementasync insertTablet(tablet: Tablet): Promise<void> - Insert tablet datagetPoolSize(): number - Get current pool sizegetAvailableSize(): number - Get available connectionsSame as SessionPool but optimized for table model operations. All query methods support the same timeout parameter (default: 60000ms).
interface Config { host: string; port: number; username?: string; password?: string; database?: string; timezone?: string; fetchSize?: number; enableSSL?: boolean; sslOptions?: SSLOptions; }
interface PoolConfig extends Config { maxPoolSize?: number; minPoolSize?: number; maxIdleTime?: number; waitTimeout?: number; }
interface Tablet { deviceId: string; measurements: string[]; dataTypes: number[]; // 0=BOOLEAN, 1=INT32, 2=INT64, 3=FLOAT, 4=DOUBLE, 5=TEXT timestamps: number[]; values: any[][]; }
interface QueryResult { columns: string[]; dataTypes: string[]; rows: any[][]; queryId?: number; }
When using insertTablet, specify data types using these constants:
0 - BOOLEAN1 - INT322 - INT643 - FLOAT4 - DOUBLE5 - TEXTnpm install
npm run build
# Run all tests npm test # Run unit tests only npm run test:unit # Run E2E tests (requires IoTDB instance) export IOTDB_HOST=localhost export IOTDB_PORT=6667 npm run test:e2e
npm run lint
See the examples/ directory for more usage examples:
examples/basic-session.ts - Basic session usageexamples/session-pool.ts - SessionPool usageexamples/table-session-pool.ts - TableSessionPool usageexamples/multi-node.ts - Multi-node configurationexamples/ssl-connection.ts - SSL/TLS connectionContributions are welcome! Please feel free to submit a Pull Request.
Apache License 2.0