Skip to content

Commit

Permalink
Merge pull request #7 from Daniel-Boll/feature/2-enhance-connect-meth…
Browse files Browse the repository at this point in the history
…od-to-support-authentication-parameters

Enhance connect method to support authentication parameters
  • Loading branch information
Daniel-Boll authored Oct 14, 2023
2 parents 92eb0c1 + 019902a commit 8f3b448
Show file tree
Hide file tree
Showing 9 changed files with 2,467 additions and 1,116 deletions.
26 changes: 17 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

/* auto-generated by NAPI-RS */

export const enum Compression {
None = 0,
Lz4 = 1,
Snappy = 2
}
export interface ClusterConfig {
nodes: Array<string>
compression?: Compression
defaultExecutionProfile?: ExecutionProfile
}
export const enum Consistency {
Any = 0,
One = 1,
Expand All @@ -20,15 +30,13 @@ export interface ExecutionProfile {
consistency?: Consistency
requestTimeout?: number
}
export interface ClusterConfig {
nodes: Array<string>
compression?: Compression
defaultExecutionProfile?: ExecutionProfile
export interface ConnectionOptions {
keyspace?: string
auth?: Auth
}
export const enum Compression {
None = 0,
Lz4 = 1,
Snappy = 2
export interface Auth {
username: string
password: string
}
export type ScyllaCluster = Cluster
export class Cluster {
Expand All @@ -40,7 +48,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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { Consistency, Cluster, Compression, ScyllaSession, Uuid } = nativeBinding
const { Compression, Consistency, Cluster, ScyllaSession, Uuid } = nativeBinding

module.exports.Compression = Compression
module.exports.Consistency = Consistency
module.exports.Cluster = Cluster
module.exports.Compression = Compression
module.exports.ScyllaSession = ScyllaSession
module.exports.Uuid = Uuid
18 changes: 18 additions & 0 deletions src/cluster/cluster_config/compression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use napi::bindgen_prelude::*;

#[napi]
pub enum Compression {
None,
Lz4,
Snappy,
}

impl From<Compression> for Option<scylla::transport::Compression> {
fn from(value: Compression) -> Self {
match value {
Compression::None => None,
Compression::Lz4 => Some(scylla::transport::Compression::Lz4),
Compression::Snappy => Some(scylla::transport::Compression::Snappy),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::cluster::{execution_profile::ExecutionProfile, config::compression::Compression};
use crate::cluster::{
cluster_config::compression::Compression, execution_profile::ExecutionProfile,
};

pub mod compression;

#[napi(object)]
pub struct ClusterConfig {
Expand Down
18 changes: 0 additions & 18 deletions src/cluster/config/compression.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/cluster/config/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/cluster/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod cluster_config;
pub mod execution_profile;
pub mod scylla_cluster;
pub mod config;
61 changes: 54 additions & 7 deletions src/cluster/scylla_cluster.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::session::scylla_session::ScyllaSession;
use crate::cluster::{
config::{cluster_config::ClusterConfig, compression::Compression},
execution_profile::ExecutionProfile
use napi::Either;

use crate::{
cluster::{
cluster_config::{compression::Compression, ClusterConfig},
execution_profile::ExecutionProfile,
},
session::scylla_session::ScyllaSession,
};

#[napi(js_name = "Cluster")]
Expand All @@ -11,6 +15,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 @@ -36,13 +52,44 @@ 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());
}
Expand All @@ -51,6 +98,6 @@ impl ScyllaCluster {
builder = builder.compression(compression.into());
}

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

0 comments on commit 8f3b448

Please sign in to comment.