Skip to content

Commit

Permalink
fix(index.d.ts): add ConnectionOptions and Auth interfaces to provide…
Browse files Browse the repository at this point in the history
… more flexibility in connecting to the cluster

feat(scylla_cluster.rs): add support for ConnectionOptions and Auth structs to allow connecting to the cluster with configurable keyspace and authentication credentials
  • Loading branch information
Daniel-Boll committed Oct 14, 2023
1 parent de926c7 commit 3848141
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
10 changes: 9 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export interface ExecutionProfile {
consistency?: Consistency
requestTimeout?: number
}
export interface ConnectionOptions {
keyspace?: string
auth?: Auth
}
export interface Auth {
username: string
password: string
}
export type ScyllaCluster = Cluster
export class Cluster {
/**
Expand All @@ -34,7 +42,7 @@ export class Cluster {
*/
constructor(clusterConfig: ClusterConfig)
/** Connect to the cluster */
connect(keyspace?: string | undefined | null): Promise<ScyllaSession>
connect(keyspaceOrOptions?: string | ConnectionOptions | undefined | null, options?: ConnectionOptions | undefined | null): Promise<ScyllaSession>
}
export class ScyllaSession {
execute(query: string, parameters?: Array<number | string | Uuid> | undefined | null): Promise<any>
Expand Down
51 changes: 48 additions & 3 deletions src/cluster/scylla_cluster.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use napi::Either;

use crate::{
cluster::{cluster_config::ClusterConfig, execution_profile::ExecutionProfile},
session::scylla_session::ScyllaSession,
Expand All @@ -9,6 +11,18 @@ struct ScyllaCluster {
default_execution_profile: Option<ExecutionProfile>,
}

#[napi(object)]
struct ConnectionOptions {
pub keyspace: Option<String>,
pub auth: Option<Auth>,
}

#[napi(object)]
struct Auth {
pub username: String,
pub password: String,
}

#[napi]
impl ScyllaCluster {
/// Object config is in the format:
Expand All @@ -32,17 +46,48 @@ impl ScyllaCluster {

/// Connect to the cluster
#[napi]
pub async fn connect(&self, keyspace: Option<String>) -> ScyllaSession {
pub async fn connect(
&self,
keyspace_or_options: Option<Either<String, ConnectionOptions>>,
options: Option<ConnectionOptions>,
) -> napi::Result<ScyllaSession> {
let mut builder = scylla::SessionBuilder::new().known_node(self.uri.as_str());

if let Some(keyspace) = keyspace {
let keyspace = match (&keyspace_or_options, &options) {
(Some(Either::A(keyspace)), _) => Ok(Some(keyspace.clone())),
(Some(Either::B(_)), Some(_)) => Err(napi::Error::new(
napi::Status::InvalidArg,
"Options cannot be provided twice",
)),
(Some(Either::B(options)), _) => Ok(options.keyspace.clone()),
(None, Some(options)) => Ok(options.keyspace.clone()),
(None, None) => Ok(None),
};

let auth = match (keyspace_or_options, options) {
(Some(Either::A(_)), Some(options)) => Ok(options.auth),
(Some(Either::B(_)), Some(_)) => Err(napi::Error::new(
napi::Status::InvalidArg,
"Options cannot be provided twice",
)),
(Some(Either::B(options)), None) => Ok(options.auth),
(None, Some(options)) => Ok(options.auth),
(None, None) => Ok(None),
(Some(Either::A(_)), None) => Ok(None),
};

if let Some(keyspace) = keyspace? {
builder = builder.use_keyspace(keyspace, false);
}

if let Some(auth) = auth? {
builder = builder.user(auth.username, auth.password);
}

if let Some(default_execution_profile) = &self.default_execution_profile {
builder = builder.default_execution_profile_handle(default_execution_profile.into_handle());
}

ScyllaSession::new(builder.build().await.unwrap())
Ok(ScyllaSession::new(builder.build().await.unwrap()))
}
}

0 comments on commit 3848141

Please sign in to comment.