Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for connection configuration on the constructor #25

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export interface ClusterConfig {
nodes: Array<string>
compression?: Compression
defaultExecutionProfile?: ExecutionProfile
keyspace?: string
auth?: Auth
ssl?: Ssl
}
export const enum Consistency {
Any = 0,
Expand Down
9 changes: 8 additions & 1 deletion src/cluster/cluster_config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::cluster::{
cluster_config::compression::Compression, execution_profile::ExecutionProfile,
cluster_config::compression::Compression,
execution_profile::ExecutionProfile,
scylla_cluster::{Auth, Ssl},
};

pub mod compression;
Expand All @@ -9,4 +11,9 @@ pub struct ClusterConfig {
pub nodes: Vec<String>,
pub compression: Option<Compression>,
pub default_execution_profile: Option<ExecutionProfile>,

// connection fields
pub keyspace: Option<String>,
pub auth: Option<Auth>,
pub ssl: Option<Ssl>,
}
108 changes: 86 additions & 22 deletions src/cluster/scylla_cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ struct ScyllaCluster {
uri: String,
compression: Option<Compression>,
default_execution_profile: Option<ExecutionProfile>,

// connection fields
connection: Option<ConnectionOptions>,
}

#[napi(object)]
Expand All @@ -24,15 +27,15 @@ struct ConnectionOptions {
}

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

#[napi(object)]
#[derive(Clone)]
struct Ssl {
pub struct Ssl {
pub ca_filepath: String,
pub verify_mode: Option<VerifyMode>,
}
Expand All @@ -55,6 +58,9 @@ impl ScyllaCluster {
nodes,
compression,
default_execution_profile,
keyspace,
auth,
ssl,
} = cluster_config;

let uri = nodes.first().expect("at least one node is required");
Expand All @@ -63,6 +69,11 @@ impl ScyllaCluster {
uri: uri.to_string(),
compression,
default_execution_profile,
connection: Some(ConnectionOptions {
keyspace,
auth,
ssl,
}),
}
}

Expand All @@ -76,39 +87,92 @@ impl ScyllaCluster {
let mut builder = scylla::SessionBuilder::new().known_node(self.uri.as_str());

// TODO: We need to think of a better way to deal with keyspace possibly being options
let keyspace = match (&keyspace_or_options, &options) {
let keyspace: Result<Option<String>, napi::Error> = 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),
(Some(Either::B(options)), _) => {
if options.keyspace.is_none() {
Ok(
self
.connection
.as_ref()
.and_then(|conn| conn.keyspace.clone()),
)
} else {
Ok(options.keyspace.clone())
}
}
(None, Some(options)) => {
if options.keyspace.is_none() {
Ok(
self
.connection
.as_ref()
.and_then(|conn| conn.keyspace.clone()),
)
} else {
Ok(options.keyspace.clone())
}
}
(None, None) => Ok(
self
.connection
.as_ref()
.and_then(|conn| conn.keyspace.clone()),
),
};

let auth = match (&keyspace_or_options, &options) {
(Some(Either::A(_)), Some(options)) => Ok(options.auth.clone()),
(Some(Either::A(_)), Some(options)) => Ok(options.auth.clone()), // when keyspace is provided as a string
(Some(Either::A(_)), None) => Ok(self.connection.as_ref().and_then(|conn| conn.auth.clone())), // when keyspace is provided as a string and options is not provided
(Some(Either::B(options)), None) => {
if options.auth.is_none() {
Ok(self.connection.as_ref().and_then(|conn| conn.auth.clone()))
} else {
Ok(options.auth.clone())
}
} // when keyspace is provided as an object
(Some(Either::B(_)), Some(_)) => Err(napi::Error::new(
napi::Status::InvalidArg,
"Options cannot be provided twice",
)),
(Some(Either::B(options)), None) => Ok(options.auth.clone()),
(None, Some(options)) => Ok(options.auth.clone()),
(None, None) => Ok(None),
(Some(Either::A(_)), None) => Ok(None),
)), // when keyspace is provided as an object and options is already provided
(None, Some(options)) => {
if options.auth.is_none() {
Ok(self.connection.as_ref().and_then(|conn| conn.auth.clone()))
} else {
Ok(options.auth.clone())
}
} // when keyspace is not provided and options is provided (shouldn't happen)
(None, None) => Ok(self.connection.as_ref().and_then(|conn| conn.auth.clone())), // when keyspace is not provided and options is not provided
};

let ssl = match (&keyspace_or_options, &options) {
(Some(Either::A(_)), Some(options)) => Ok(options.ssl.clone()),
(Some(Either::A(_)), Some(options)) => {
if options.ssl.is_none() {
Ok(self.connection.as_ref().and_then(|conn| conn.ssl.clone()))
} else {
Ok(options.ssl.clone())
}
},
(Some(Either::B(_)), Some(_)) => Err(napi::Error::new(
napi::Status::InvalidArg,
"Options cannot be provided twice",
)),
(Some(Either::B(options)), None) => Ok(options.ssl.clone()),
(None, Some(options)) => Ok(options.ssl.clone()),
(None, None) => Ok(None),
(Some(Either::A(_)), None) => Ok(None),
(Some(Either::B(options)), None) => {
if options.ssl.is_none() {
Ok(self.connection.as_ref().and_then(|conn| conn.ssl.clone()))
} else {
Ok(options.ssl.clone())
}
},
(None, Some(options)) => {
if options.ssl.is_none() {
Ok(self.connection.as_ref().and_then(|conn| conn.ssl.clone()))
} else {
Ok(options.ssl.clone())
}
},
(None, None) => Ok(self.connection.as_ref().and_then(|conn| conn.ssl.clone())),
(Some(Either::A(_)), None) => Ok(self.connection.as_ref().and_then(|conn| conn.ssl.clone())),
};

if let Some(keyspace) = keyspace.clone()? {
Expand Down
Loading