Update examples to showcase nodeUrls, Builder pattern, and explicit session management
4 files changed
tree: 1f03bb0082e5480573a663094030b42b9f741cc8
  1. .github/
  2. examples/
  3. src/
  4. tests/
  5. thrift/
  6. .gitignore
  7. BUILD_INFRASTRUCTURE_ANALYSIS.md
  8. CHANGELOG.md
  9. CI_CD_SUMMARY.md
  10. COMMENT_RESPONSE_SUMMARY.md
  11. CONTRIBUTING.md
  12. DATA_TYPES.md
  13. DATA_TYPES_IMPLEMENTATION_SUMMARY.md
  14. docker-compose-1c1d.yml
  15. docker-compose-3c3d.yml
  16. E2E_FIX_ANALYSIS.md
  17. esbuild.config.js
  18. eslint.config.mjs
  19. ESLINT_9_UPGRADE.md
  20. GLOB_FIX_DETAILS.md
  21. IMPLEMENTATION.md
  22. IMPROVEMENTS_SUMMARY.md
  23. jest.config.js
  24. LICENSE
  25. NPM_DEPRECATIONS_FIXED.md
  26. package-lock.json
  27. package.json
  28. PROJECT_STATUS.md
  29. README.md
  30. tsconfig.json
README.md

Apache IoTDB Node.js Client

License

A Node.js client for Apache IoTDB with support for SessionPool and TableSessionPool, providing efficient connection management and comprehensive query capabilities.

Features

  • Session Management: Single session with query, non-query, and insertTablet operations
  • SessionPool: Connection pooling for high-concurrency scenarios
  • TableSessionPool: Specialized pool for table model operations
  • Multi-Node Support: Round-robin load balancing across multiple IoTDB nodes
  • SSL/TLS Support: Secure connections with customizable SSL options
  • TypeScript Support: Full TypeScript definitions included
  • Comprehensive Testing: Unit and E2E tests included

Installation

npm install iotdb-client-nodejs

Requirements

  • Node.js >= 14.0.0
  • Apache IoTDB >= 1.0.0

Quick Start

Basic Session Usage

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();

SessionPool Usage

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();

Multi-Node Support

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

SSL/TLS Support

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();

TableSessionPool Usage

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();

API Reference

Data Types

IoTDB Node.js client supports all IoTDB data types including BOOLEAN, INT32, INT64, FLOAT, DOUBLE, TEXT, BLOB, STRING, DATE, and TIMESTAMP. See DATA_TYPES.md for comprehensive documentation on:

  • Type mappings between JavaScript and IoTDB
  • Usage examples for each data type
  • Best practices and encoding options
  • Null value handling

Session

Constructor

new Session(config: Partial<Config>)

Methods

  • async open(): Promise<void> - Open the session
  • async close(): Promise<void> - Close the session
  • async 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 statement
  • async insertTablet(tablet: Tablet): Promise<void> - Insert tablet data
  • isOpen(): boolean - Check if session is open

SessionPool

Constructor

new SessionPool(hosts: string | string[], port: number, config?: Partial<PoolConfig>)

Methods

  • async init(): Promise<void> - Initialize the pool
  • async close(): Promise<void> - Close all connections
  • async 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 statement
  • async insertTablet(tablet: Tablet): Promise<void> - Insert tablet data
  • getPoolSize(): number - Get current pool size
  • getAvailableSize(): number - Get available connections

TableSessionPool

Same as SessionPool but optimized for table model operations. All query methods support the same timeout parameter (default: 60000ms).

Types

Config

interface Config {
  host: string;
  port: number;
  username?: string;
  password?: string;
  database?: string;
  timezone?: string;
  fetchSize?: number;
  enableSSL?: boolean;
  sslOptions?: SSLOptions;
}

PoolConfig

interface PoolConfig extends Config {
  maxPoolSize?: number;
  minPoolSize?: number;
  maxIdleTime?: number;
  waitTimeout?: number;
}

Tablet

interface Tablet {
  deviceId: string;
  measurements: string[];
  dataTypes: number[]; // 0=BOOLEAN, 1=INT32, 2=INT64, 3=FLOAT, 4=DOUBLE, 5=TEXT
  timestamps: number[];
  values: any[][];
}

QueryResult

interface QueryResult {
  columns: string[];
  dataTypes: string[];
  rows: any[][];
  queryId?: number;
}

Data Types

When using insertTablet, specify data types using these constants:

  • 0 - BOOLEAN
  • 1 - INT32
  • 2 - INT64
  • 3 - FLOAT
  • 4 - DOUBLE
  • 5 - TEXT

Development

Build

npm install
npm run build

Run Tests

# 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

Linting

npm run lint

Examples

See the examples/ directory for more usage examples:

  • examples/basic-session.ts - Basic session usage
  • examples/session-pool.ts - SessionPool usage
  • examples/table-session-pool.ts - TableSessionPool usage
  • examples/multi-node.ts - Multi-node configuration
  • examples/ssl-connection.ts - SSL/TLS connection

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

Apache License 2.0

References