Trino JavaScript Client
The `trino-client` library provides an official JavaScript/TypeScript client for interacting with Trino (formerly PrestoSQL) instances from Node.js environments. It enables developers to execute SQL queries, retrieve results, and manage connections programmatically. The current stable version is 0.2.9, with new releases occurring frequently, typically on a monthly or bi-monthly basis, primarily for dependency updates and minor improvements. A key differentiator introduced in version 0.2.0 is the adoption of async iterators for query results, offering an efficient and modern way to process potentially large datasets streamingly. The library supports secure connections via SSL/TLS and provides robust error handling for Trino interactions.
Common errors
-
TypeError: TrinoClient is not a constructor
cause Attempting to instantiate `TrinoClient` with the `new` keyword after version 0.2.0.fixUse the static factory method `TrinoClient.create(config)` instead of `new TrinoClient(config)`. -
Error: self-signed certificate in certificate chain
cause Connecting to a Trino server over HTTPS with a self-signed or untrusted SSL/TLS certificate without disabling certificate verification.fixIf in a development environment, set `requestVerification: false` in your `QueryConfig`. For production, ensure the Trino server uses a certificate issued by a trusted Certificate Authority (CA). -
SyntaxError: Named export 'TrinoClient' not found
cause Trying to use CommonJS `require()` syntax (e.g., `const { TrinoClient } = require('trino-client');`) in a module that primarily uses ES modules, or an incorrect named import.fixEnsure your environment is configured for ES modules and use `import { TrinoClient } from 'trino-client';`. -
Error: Query failed: line X:Y: Table 'catalog.schema.table' does not exist
cause The specified catalog, schema, or table in your query does not exist, or the configured user lacks sufficient permissions to access it.fixVerify the `catalog`, `schema`, `user`, and table name in your `QueryConfig` and SQL query. Check Trino server logs for more details on access rights or table existence.
Warnings
- breaking The `TrinoClient.query` method no longer returns a promise resolving to a single result object but an async iterator of result pages. Direct access to `columns` or `data` properties on the initial promise resolution will fail.
- breaking The `TrinoClient` class constructor was made private in v0.2.0. Instantiating via `new TrinoClient()` will result in a TypeError, as the constructor is no longer publicly accessible.
- gotcha When connecting to Trino via HTTPS, ensure proper SSL/TLS configuration. Using `requestVerification: false` (equivalent to `rejectUnauthorized: false` in Node.js `https` module) disables certificate validation. This is a security risk and should only be used in development environments with self-signed certificates.
Install
-
npm install trino-client -
yarn add trino-client -
pnpm add trino-client
Imports
- TrinoClient
const TrinoClient = require('trino-client').TrinoClient;import { TrinoClient } from 'trino-client'; - Trino
import { Trino } from 'trino-client';import type { Trino } from 'trino-client'; - QueryConfig
import { QueryConfig } from 'trino-client';import type { QueryConfig } from 'trino-client';
Quickstart
import { TrinoClient, type QueryConfig, type Trino } from 'trino-client';
async function runTrinoQuery() {
const config: QueryConfig = {
serverUrl: process.env.TRINO_SERVER_URL || 'http://localhost:8080',
catalog: process.env.TRINO_CATALOG || 'memory',
schema: process.env.TRINO_SCHEMA || 'default',
user: process.env.TRINO_USER || 'testuser',
// Optional: For SSL/TLS connections, enable 'https' and manage 'requestVerification'
// https: true,
// requestVerification: false // Use with caution for self-signed certs in dev
// For Basic Authentication, pass an 'auth' object, e.g.,
// auth: new (await import('trino-client')).BasicAuth('username', 'password'),
};
let trinoClient: Trino; // Type for the client instance
try {
// Starting from v0.2.0, TrinoClient constructor is private. Use static factory method.
trinoClient = TrinoClient.create(config);
console.log(`Connected to Trino at ${config.serverUrl}`);
const query = 'SELECT * FROM system.runtime.nodes LIMIT 1';
console.log(`Executing query: "${query}"`);
// The query method now returns an async iterator (since v0.2.0)
const queryResults = await trinoClient.query(query);
console.log('Query results:');
for await (const { columns, data } of queryResults) {
if (columns) {
console.log('Columns:', columns.map(c => c.name).join(', '));
}
if (data) {
data.forEach(row => console.log(row));
}
}
console.log('Query finished successfully.');
} catch (error) {
console.error('Failed to run Trino query:', error instanceof Error ? error.message : String(error));
}
}
runTrinoQuery();