@@ -14,6 +14,9 @@ struct ScyllaCluster {
14
14
uri : String ,
15
15
compression : Option < Compression > ,
16
16
default_execution_profile : Option < ExecutionProfile > ,
17
+
18
+ // connection fields
19
+ connection : Option < ConnectionOptions > ,
17
20
}
18
21
19
22
#[ napi( object) ]
@@ -24,15 +27,15 @@ struct ConnectionOptions {
24
27
}
25
28
26
29
#[ napi( object) ]
27
- #[ derive( Clone ) ]
28
- struct Auth {
30
+ #[ derive( Clone , Debug ) ]
31
+ pub struct Auth {
29
32
pub username : String ,
30
33
pub password : String ,
31
34
}
32
35
33
36
#[ napi( object) ]
34
37
#[ derive( Clone ) ]
35
- struct Ssl {
38
+ pub struct Ssl {
36
39
pub ca_filepath : String ,
37
40
pub verify_mode : Option < VerifyMode > ,
38
41
}
@@ -55,6 +58,9 @@ impl ScyllaCluster {
55
58
nodes,
56
59
compression,
57
60
default_execution_profile,
61
+ keyspace,
62
+ auth,
63
+ ssl,
58
64
} = cluster_config;
59
65
60
66
let uri = nodes. first ( ) . expect ( "at least one node is required" ) ;
@@ -63,6 +69,11 @@ impl ScyllaCluster {
63
69
uri : uri. to_string ( ) ,
64
70
compression,
65
71
default_execution_profile,
72
+ connection : Some ( ConnectionOptions {
73
+ keyspace,
74
+ auth,
75
+ ssl,
76
+ } ) ,
66
77
}
67
78
}
68
79
@@ -76,39 +87,92 @@ impl ScyllaCluster {
76
87
let mut builder = scylla:: SessionBuilder :: new ( ) . known_node ( self . uri . as_str ( ) ) ;
77
88
78
89
// TODO: We need to think of a better way to deal with keyspace possibly being options
79
- let keyspace = match ( & keyspace_or_options, & options) {
90
+ let keyspace: Result < Option < String > , napi :: Error > = match ( & keyspace_or_options, & options) {
80
91
( Some ( Either :: A ( keyspace) ) , _) => Ok ( Some ( keyspace. clone ( ) ) ) ,
81
- ( Some ( Either :: B ( _) ) , Some ( _) ) => Err ( napi:: Error :: new (
82
- napi:: Status :: InvalidArg ,
83
- "Options cannot be provided twice" ,
84
- ) ) ,
85
- ( Some ( Either :: B ( options) ) , _) => Ok ( options. keyspace . clone ( ) ) ,
86
- ( None , Some ( options) ) => Ok ( options. keyspace . clone ( ) ) ,
87
- ( None , None ) => Ok ( None ) ,
92
+ ( Some ( Either :: B ( options) ) , _) => {
93
+ if options. keyspace . is_none ( ) {
94
+ Ok (
95
+ self
96
+ . connection
97
+ . as_ref ( )
98
+ . and_then ( |conn| conn. keyspace . clone ( ) ) ,
99
+ )
100
+ } else {
101
+ Ok ( options. keyspace . clone ( ) )
102
+ }
103
+ }
104
+ ( None , Some ( options) ) => {
105
+ if options. keyspace . is_none ( ) {
106
+ Ok (
107
+ self
108
+ . connection
109
+ . as_ref ( )
110
+ . and_then ( |conn| conn. keyspace . clone ( ) ) ,
111
+ )
112
+ } else {
113
+ Ok ( options. keyspace . clone ( ) )
114
+ }
115
+ }
116
+ ( None , None ) => Ok (
117
+ self
118
+ . connection
119
+ . as_ref ( )
120
+ . and_then ( |conn| conn. keyspace . clone ( ) ) ,
121
+ ) ,
88
122
} ;
89
123
90
124
let auth = match ( & keyspace_or_options, & options) {
91
- ( Some ( Either :: A ( _) ) , Some ( options) ) => Ok ( options. auth . clone ( ) ) ,
125
+ ( Some ( Either :: A ( _) ) , Some ( options) ) => Ok ( options. auth . clone ( ) ) , // when keyspace is provided as a string
126
+ ( 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
127
+ ( Some ( Either :: B ( options) ) , None ) => {
128
+ if options. auth . is_none ( ) {
129
+ Ok ( self . connection . as_ref ( ) . and_then ( |conn| conn. auth . clone ( ) ) )
130
+ } else {
131
+ Ok ( options. auth . clone ( ) )
132
+ }
133
+ } // when keyspace is provided as an object
92
134
( Some ( Either :: B ( _) ) , Some ( _) ) => Err ( napi:: Error :: new (
93
135
napi:: Status :: InvalidArg ,
94
136
"Options cannot be provided twice" ,
95
- ) ) ,
96
- ( Some ( Either :: B ( options) ) , None ) => Ok ( options. auth . clone ( ) ) ,
97
- ( None , Some ( options) ) => Ok ( options. auth . clone ( ) ) ,
98
- ( None , None ) => Ok ( None ) ,
99
- ( Some ( Either :: A ( _) ) , None ) => Ok ( None ) ,
137
+ ) ) , // when keyspace is provided as an object and options is already provided
138
+ ( None , Some ( options) ) => {
139
+ if options. auth . is_none ( ) {
140
+ Ok ( self . connection . as_ref ( ) . and_then ( |conn| conn. auth . clone ( ) ) )
141
+ } else {
142
+ Ok ( options. auth . clone ( ) )
143
+ }
144
+ } // when keyspace is not provided and options is provided (shouldn't happen)
145
+ ( None , None ) => Ok ( self . connection . as_ref ( ) . and_then ( |conn| conn. auth . clone ( ) ) ) , // when keyspace is not provided and options is not provided
100
146
} ;
101
147
102
148
let ssl = match ( & keyspace_or_options, & options) {
103
- ( Some ( Either :: A ( _) ) , Some ( options) ) => Ok ( options. ssl . clone ( ) ) ,
149
+ ( Some ( Either :: A ( _) ) , Some ( options) ) => {
150
+ if options. ssl . is_none ( ) {
151
+ Ok ( self . connection . as_ref ( ) . and_then ( |conn| conn. ssl . clone ( ) ) )
152
+ } else {
153
+ Ok ( options. ssl . clone ( ) )
154
+ }
155
+ } ,
104
156
( Some ( Either :: B ( _) ) , Some ( _) ) => Err ( napi:: Error :: new (
105
157
napi:: Status :: InvalidArg ,
106
158
"Options cannot be provided twice" ,
107
159
) ) ,
108
- ( Some ( Either :: B ( options) ) , None ) => Ok ( options. ssl . clone ( ) ) ,
109
- ( None , Some ( options) ) => Ok ( options. ssl . clone ( ) ) ,
110
- ( None , None ) => Ok ( None ) ,
111
- ( Some ( Either :: A ( _) ) , None ) => Ok ( None ) ,
160
+ ( Some ( Either :: B ( options) ) , None ) => {
161
+ if options. ssl . is_none ( ) {
162
+ Ok ( self . connection . as_ref ( ) . and_then ( |conn| conn. ssl . clone ( ) ) )
163
+ } else {
164
+ Ok ( options. ssl . clone ( ) )
165
+ }
166
+ } ,
167
+ ( None , Some ( options) ) => {
168
+ if options. ssl . is_none ( ) {
169
+ Ok ( self . connection . as_ref ( ) . and_then ( |conn| conn. ssl . clone ( ) ) )
170
+ } else {
171
+ Ok ( options. ssl . clone ( ) )
172
+ }
173
+ } ,
174
+ ( None , None ) => Ok ( self . connection . as_ref ( ) . and_then ( |conn| conn. ssl . clone ( ) ) ) ,
175
+ ( Some ( Either :: A ( _) ) , None ) => Ok ( self . connection . as_ref ( ) . and_then ( |conn| conn. ssl . clone ( ) ) ) ,
112
176
} ;
113
177
114
178
if let Some ( keyspace) = keyspace. clone ( ) ? {
0 commit comments