Connecting to Snowflake
This comparison shows the difference between callback-based and Promise-based connection handling.
info
See Authentication and MFA for information about upcoming changes to username/password authentication.
Without this library: Use callbacks to handle connection results, with additional complexity to check if the connection is ready.
With this library: Simply await the connection - clean and straightforward.
Examples
tip
- Click “WITHOUT Helper Library” to see the original code
- Click “WITH Helper Library” to see the improved code using this library
- WITHOUT Helper Library
- WITH Helper Library
Based on an example from Snowflake SDK documentation.
// Not using the Promise Helper Library
const connection = snowflake.createConnection({
account: "your-account",
username: "your-username",
password: "your-password",
});
// Callback-based connection - harder to coordinate with other async code
connection.connect(function (err, conn) {
// Connection logic must be inside callback or you need additional coordination
if (err) {
console.error("Unable to connect: " + err.message);
return;
}
console.log("Successfully connected to Snowflake");
});
// Outside the callback, you need to check if connection is ready
const isConnectionValid = await connection.isValidAsync();
if (isConnectionValid) {
// Now you can use the connection...
}
// Using the Promise Helper Library
const connection = promisifyConnection(
snowflake.createConnection({
account: "your-account",
username: "your-username",
password: "your-password",
})
);
// Clean, Promise-based connection
try {
await connection.connect();
console.log("Successfully connected to Snowflake");
} catch (error) {
console.error("Unable to connect:", error);
return;
}
// Connection is ready - execute queries immediately
// const { resultsPromise } = connection.execute({ sqlText: "SELECT 1" });