Firebolt Node.js SDK
This guide explains how to install, configure, and use the Firebolt Node.js SDK to connect to a Firebolt database from a Node.js application. You can use this SDK to programmatically connect to a Firebolt database, run queries, and manage database resources.
Installation
The Firebolt Node.js SDK is published on the NPM registry. Installing the SDK integrates Firebolt functionality into your application, allowing you to perform database operations and manage resources programmatically.
To install using npm, run the following command:
npm install firebolt-sdk --save
To install using Yarn, run the following command:
yarn add firebolt-sdk
Authentication
After installation, you must authenticate before you can use the SDK to establish connections, run queries, and manage database resources. The SDK supports two authentication methods:
Managed Firebolt (Service Account)
The following code example sets up a connection using your Firebolt service account credentials:
const connection = await firebolt.connect({
auth: {
client_id: '12345678-90123-4567-8901-234567890123',
client_secret: 'secret',
},
engineName: 'engine_name',
account: 'account_name',
database: 'database',
});
In the previous code example, the following apply:
client_id and client_secret are your service account credentials. Follow the Firebolt's guide on how to create one and get its id and secret.
engineName is the name of the engine which you want to run your queries on.
database is the target databaset to store your tables.
account is the account within your organisation. Your account is not the same as your user name.
Firebolt Core (Self-Hosted)
For self-hosted Firebolt Core instances, no authentication is required. Simply use FireboltCore() as the auth method:
import { Firebolt, FireboltCore } from 'firebolt-sdk'
const connection = await firebolt.connect({
auth: FireboltCore(),
database: 'database',
engineEndpoint: 'http://localhost:3473', // Required for Core
});
Note: Firebolt Core connections require engineEndpoint to be specified. Resource management (engines, databases) and async queries are not available for Core connections.
Example
In the following code example, credentials are stored in environment variables. For bash and similar shells you can set them by running export FIREBOLT_CLIENT_ID=<your_client_id> where is the id you want to set. This method prevents hardcoding sensitive information in your code so it can be safely commited to a version control system such as Git. Many IDEs, including IntelliJ IDEA, allow the configuration of environment variables in their run configurations.
import { Firebolt } from 'firebolt-sdk'
const firebolt = Firebolt();
const connection = await firebolt.connect({
auth: {
client_id: process.env.FIREBOLT_CLIENT_ID,
client_secret: process.env.FIREBOLT_CLIENT_SECRET,
},
account: process.env.FIREBOLT_ACCOUNT,
database: process.env.FIREBOLT_DATABASE,
engineName: process.env.FIREBOLT_ENGINE_NAME
});
// Create table
await connection.execute(`
CREATE TABLE IF NOT EXISTS users (
id INT,
name STRING,
age INT
)
`);
// Insert sample data
await connection.execute(`
INSERT INTO users (id, name, age) VALUES
(1, 'Alice', 30),
(2, 'Bob', 25)
`);
// Update some rows
await connection.execute(`
UPDATE users SET age = 31 WHERE id = 1
`);
// Fetch data
const statement = await connection.execute("SELECT * FROM users");
// fetch statement result
const { data, meta } = await statement.fetchResult();
console.log(meta)
// Outputs:
// [
// Meta { type: 'int null', name: 'id' },
// Meta { type: 'text null', name: 'name' },
// Meta { type: 'int null', name: 'age' }
// ]
// or stream result
const { data } = await statement.streamResult();
data.on("metadata", metadata => {
console.log(metadata);
});
data.on("error", error => {
console.log(error);
});
const rows = []
for await (const row of data) {
rows.push(row);
}
console.log(rows)
// Outputs:
// [ [ 1, 'Alice', 31 ], [ 2, 'Bob', 25 ] ]
Contents
About
The Firebolt client for Node.js. firebolt-sdk provides common methods for quering Firebolt databases, fetching and streaming results, and engine management.
firebolt-sdk supports Node.js >= v20.
Documentation
Firebolt's Node.js documentation
Usage
Create connection
const connection = await firebolt.connect(connectionOptions);
ConnectionOptions
type AccessTokenAuth = {
accessToken: string;
};
type ClientCredentialsAuth = {
client_id: string;
client_secret: string;
};
type PreparedStatementParamStyle = "native" | "fb_numeric";
type FireboltCoreAuth = {
type: "firebolt-core";
};
type ConnectionOptions = {
auth: AccessTokenAuth | ServiceAccountAuth | FireboltCoreAuth;
database: string;
engineName?: string;
engineEndpoint?: string; // Required for Firebolt Core connections
account?: string;
preparedStatementParamStyle?: PreparedStatementParamStyle;
};
AccessToken
Instead of passing client id/secret directly,
you can also manage authentication outside of node sdk
and pass accessToken when creating the connection
const connection = await firebolt.connect({
auth: {
accessToken: "access_token",
},
engineName: 'engine_name',
account: 'account_name',
database: 'database',
});
Client credentials
Use client credentials to authenticate as follows:
const connection = await firebolt.connect({
auth: {
client_id: 'b1c4918c-e07e-4ab2-868b-9ae84f208d26',
client_secret: 'secret',
},
engineName: 'engine_name',
account: 'account_name',
database: 'database',
});
engineName
You can omit engineName and execute AQL queries on such connection.
Firebolt Core
For self-hosted Firebolt Core instances, use FireboltCore() authentication and provide engineEndpoint:
import { Firebolt, FireboltCore } from 'firebolt-sdk'
const connection = await firebolt.connect({
auth: FireboltCore(),
database: 'database',
engineEndpoint: 'http://localhost:3473', // Required for Core
});
Important notes for Firebolt Core:
- No authentication credentials are required
engineEndpoint is required (must be specified)
- Resource management (
resourceManager) is not available
- Async queries (
executeAsync) are not supported
- Streaming queries (
executeStream) are supported
Token caching
Driver implements a caching mechanism for access tokens. If you are using the same client id or secret for multiple connections, the driver will cache the access token and reuse it for subsequent connections.
This behavior can be disabled by setting useCache to false in the connection options.
const connection = await firebolt.connect({
auth: {
client_id: 'b1c4918c-e07e-4ab2-868b-9ae84f208d26',
client_secret: 'secret',
},
engineName: 'engine_name',
account: 'account_name',
database: 'database',
useCache: false
});
Server-side prepared statement
Driver has the option to use server-side prepared statements, so all parameters are set on the server side, preventing SQL injection attacks.
This behavior can be enabled by setting preparedStatementParamStyle to fb_numeric in the connection options, otherwise, prepared statements will retain default behavior(same behavior if value is set to native) and queries will be formatted client side.
const connection = await firebolt.connect({
auth: {
client_id: 'b1c4918c-e07e-4ab2-868b-9ae84f208d26',
client_secret: 'secret',
},
engineName: 'engine_name',
account: 'account_name',
database: 'database',
preparedStatementParamStyle: 'fb_numeric'
});
Test connection
Test the connection using the following example script:
const firebolt = Firebolt();
await firebolt.testConnection(connectionOptions)
which will perform authentication and a simple select 1 query
Engine URL
Firebolt engine URLs use the following format:
<engine-name>.<account-name>.<region>.app.firebolt.io
For example: your-engine.your-account.us-east-1.app.firebolt.io. You can find and copy your engine endpoint name in the Firebolt web UI.
Execute Query
const statement = await connection.execute(query, executeQueryOptions);
Execute Query with set flags
This approach is useful if you want to modify the execution of a particular query, for example setting a timeout or modifying a time zone for date calculations.
const statement = await connection.execute(query, {
settings: { statement_timeout: 1000 }
});
Full list of flags that can be passed here is available on this documentation page.
Note: the flags set in this way will be active only for the duration of the query. If you require a session-level flag, which would apply to all the queries executed on this connection, use the SQL syntax to set it:
await connection.execute("SET statement_timeout=1000")
ExecuteQueryOptions
export type ExecuteQueryOptions = {
parameters?: unknown[];
settings?: QuerySettings;
response?: ResponseSettings;
};
parameters
The parameters field is used to specify replacements for ? symbol in the query as follows:
For example:
const statement = await connection.execute("select ?, ?", {
parameters: ["foo", 1]
});
The previous query produces: select 'foo', 1 query
Format Tuple:
import { Tuple } from 'firebolt-sdk'
const statement = await connection.execute("select ? where bar in ?", {
parameters: [
1,
new Tuple(['foo'])
]
});
Named parameters
The namedParameters field is used to specify replacements for :name tokens in the query.
For example:
const statement = await connection.execute("select :foo, :bar", {
namedParameters: { foo: "foo", bar: 123 }
});
The previous query will produce: select 'foo', 123 query
QuerySettings
| Parameter |
Required |
Default |
Description |
| output_format |
|
JSON_COMPACT |
Specifies format of selected data |
You can also use QuerySettings to specify set flags.
For example: { statement_timeout: 1000 }
Full list of available settings can be found on this page.
ResponseSettings
| Parameter |
Required |
Default |
Description |
| normalizeData |
|
false |
Maps each row in response from array format to object |
| bigNumberAsString |
|
false |
Hydrate BigNumber as String |
Fetch result