databricks-cli
Version:
A CLI tool for quick Databricks tasks
51 lines (50 loc) • 1.44 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConnection = getConnection;
exports.getSharedSession = getSharedSession;
exports.closeConnection = closeConnection;
const sql_1 = require("@databricks/sql");
const config_1 = require("./config");
let sharedClient = null;
let sharedSession = null;
async function getConnection() {
if (sharedClient) {
return sharedClient;
}
const config = (0, config_1.getConfig)();
const client = new sql_1.DBSQLClient();
if (!config.host || !config.token || !config.path) {
throw new Error("Databricks configuration not found. Please run `databricks-cli init` first.");
}
try {
await client.connect({
token: config.token,
host: config.host,
path: config.path,
});
sharedClient = client;
return client;
}
catch (error) {
console.error("Connection error:", error);
throw error;
}
}
async function getSharedSession() {
if (sharedSession) {
return sharedSession;
}
const client = await getConnection();
sharedSession = await client.openSession();
return sharedSession;
}
async function closeConnection() {
if (sharedSession) {
await sharedSession.close();
sharedSession = null;
}
if (sharedClient) {
await sharedClient.close();
sharedClient = null;
}
}
;