import { Firebolt } from 'firebolt-sdk'
// Initialize client
const firebolt = Firebolt();
// Establish connection to Firebolt using environment variables for credentials and configuration
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 a "users" 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 rows
await connection.execute(`
UPDATE users SET age = 31 WHERE id = 1
`);
// Fetch data with a query
const statement = await connection.execute("SELECT * FROM users");
// Fetch the complete result set
const { data, meta } = await statement.fetchResult();
// Log metadata describing the columns of the result set
console.log(meta)
// Outputs:
// [
// Meta { type: 'int null', name: 'id' },
// Meta { type: 'text null', name: 'name' },
// Meta { type: 'int null', name: 'age' }
// ]
// Alternatively, stream the result set row by row
const { data } = await statement.streamResult();
data.on("metadata", metadata => {
console.log(metadata);
});
// Handle metadata event
data.on("error", error => {
console.log(error);
});
const rows = []
for await (const row of data) {
rows.push(row);
}
// Log the collected rows
console.log(rows)
// Outputs:
// [ [ 1, 'Alice', 31 ], [ 2, 'Bob', 25 ] ]