Connect to Universql using the official Node.js client.
Universql integrates seamlessly with Snowflake's Node.js client. You can use either the official Snowflake Node.js driver or TypeORM with the Snowflake driver.
# Install Snowflake Node.js driver
npm install snowflake-sdk
# or
yarn add snowflake-sdk
# For TypeORM support
npm install typeorm @typeorm/snowflake-driver
const snowflake = require('snowflake-sdk');
// Create a connection
const connection = snowflake.createConnection({
host: '{universql_server}', // Universql server
username: 'your_username',
password: 'your_password',
warehouse: 'your_warehouse',
database: 'your_database',
schema: 'your_schema'
});
// Connect to Snowflake
connection.connect((err, conn) => {
if (err) {
console.error('Connection error: ', err);
return;
}
// Execute a query
conn.execute({
sqlText: `
SELECT
DATE_TRUNC('month', created_at) as month,
COUNT(*) as user_count,
COUNT(DISTINCT country) as countries
FROM users
GROUP BY 1
ORDER BY 1 DESC
LIMIT 12
`,
complete: (err, stmt, rows) => {
if (err) {
console.error('Failed to execute statement due to the following error: ' + err.message);
} else {
console.log('Successfully executed statement: ' + stmt.getSqlText());
console.log('Monthly user stats:', rows);
}
}
});
});
const snowflake = require('snowflake-sdk');
const util = require('util');
// Create a promisified connection
async function createConnection() {
const connection = snowflake.createConnection({
host: '{universql_server}', // Universql server
username: 'your_username',
password: 'your_password',
warehouse: 'your_warehouse',
database: 'your_database',
schema: 'your_schema'
});
// Promisify connect method
const connectAsync = util.promisify(connection.connect.bind(connection));
await connectAsync();
return connection;
}
// Execute a query with promises
async function executeQuery(connection, sqlText) {
return new Promise((resolve, reject) => {
connection.execute({
sqlText: sqlText,
complete: (err, stmt, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
}
});
});
}
// Usage example
async function main() {
try {
const connection = await createConnection();
const results = await executeQuery(connection, `
SELECT
product_id,
SUM(quantity) as total_sold,
AVG(price) as avg_price
FROM sales
GROUP BY product_id
HAVING total_sold > 100
ORDER BY total_sold DESC
`);
console.log('Query results:', results);
} catch (error) {
console.error('Error:', error);
}
}
import { Entity, Column, PrimaryGeneratedColumn, createConnection } from 'typeorm';
@Entity()
class Product {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column('decimal')
price: number;
@Column()
category: string;
}
// Create connection
const connection = await createConnection({
type: 'snowflake',
name: 'default',
host: '{universql_server}', // Universql server
username: 'your_username',
password: 'your_password',
warehouse: 'your_warehouse',
database: 'your_database',
schema: 'your_schema',
entities: [Product],
synchronize: true
});
// Use the repository pattern
const productRepository = connection.getRepository(Product);
// Query products
const products = await productRepository
.createQueryBuilder('product')
.where('product.price > :price', { price: 100 })
.andWhere('product.category = :category', { category: 'Electronics' })
.getMany();
const snowflake = require('snowflake-sdk');
async function executeBatch(connection, records) {
return new Promise((resolve, reject) => {
connection.execute({
sqlText: `
INSERT INTO products (name, price, category)
VALUES (?, ?, ?)
`,
binds: records.map(record => [
record.name,
record.price,
record.category
]),
complete: (err, stmt) => {
if (err) {
reject(err);
} else {
resolve(stmt.getNumRowsInserted());
}
}
});
});
}
// Usage
const products = [
{ name: 'Product A', price: 99.99, category: 'Electronics' },
{ name: 'Product B', price: 149.99, category: 'Electronics' },
{ name: 'Product C', price: 199.99, category: 'Electronics' }
];
const numInserted = await executeBatch(connection, products);
console.log(`Inserted ${numInserted} records`);
const snowflake = require('snowflake-sdk');
try {
const connection = snowflake.createConnection({
host: '{universql_server}', // Universql server
username: 'your_username',
password: 'your_password'
});
connection.connect((err, conn) => {
if (err) {
console.error('Connection error:', {
message: err.message,
errorCode: err.code,
state: err.state,
statusCode: err.statusCode
});
return;
}
conn.execute({
sqlText: 'SELECT * FROM non_existent_table',
complete: (err, stmt, rows) => {
if (err) {
console.error('Query error:', {
message: err.message,
errorCode: err.code,
sqlState: err.sqlState,
statementId: stmt?.getStatementId()
});
}
}
});
});
} catch (error) {
console.error('Unexpected error:', error);
}
Use Environment Variables
const connection = snowflake.createConnection({
host: '{universql_server}', // Universql server
username: process.env.SNOWFLAKE_USERNAME,
password: process.env.SNOWFLAKE_PASSWORD
});
Connection Pooling
const pool = require('generic-pool');
const snowflakePool = pool.createPool({
create: async () => {
const connection = snowflake.createConnection({
host: '{universql_server}', // Universql server
username: 'your_username',
password: 'your_password'
});
await util.promisify(connection.connect.bind(connection))();
return connection;
},
destroy: (connection) => {
return util.promisify(connection.destroy.bind(connection))();
}
}, {
min: 2,
max: 10
});
Use Parameterized Queries
connection.execute({
sqlText: 'SELECT * FROM users WHERE id = ?',
binds: [userId],
complete: (err, stmt, rows) => {
if (!err) console.log(rows);
}
});