@@ -233,111 +233,114 @@ impl Drop for Database {
233233}
234234
235235#[ napi]
236- impl Database {
237- /// Creates a new database instance.
238- ///
239- /// # Arguments
240- ///
241- /// * `path` - The path to the database file.
242- /// * `opts` - The database options.
243- #[ napi( constructor) ]
244- pub fn new ( path : String , opts : Option < Options > ) -> Result < Self > {
245- ensure_logger ( ) ;
246- let rt = runtime ( ) ?;
247- let remote = is_remote_path ( & path) ;
248- let db = if remote {
249- let auth_token = opts
236+ pub async fn connect ( path : String , opts : Option < Options > ) -> Result < Database > {
237+ let remote = is_remote_path ( & path) ;
238+ let db = if remote {
239+ let auth_token = opts
240+ . as_ref ( )
241+ . and_then ( |o| o. authToken . as_ref ( ) )
242+ . cloned ( )
243+ . unwrap_or_default ( ) ;
244+ let mut builder = libsql:: Builder :: new_remote ( path. clone ( ) , auth_token) ;
245+ if let Some ( encryption_key) = opts
246+ . as_ref ( )
247+ . and_then ( |o| o. remoteEncryptionKey . as_ref ( ) )
248+ . cloned ( )
249+ {
250+ let encryption_context = libsql:: EncryptionContext {
251+ key : libsql:: EncryptionKey :: Base64Encoded ( encryption_key) ,
252+ } ;
253+ builder = builder. remote_encryption ( encryption_context) ;
254+ }
255+ builder. build ( ) . await . map_err ( Error :: from) ?
256+ } else if let Some ( options) = & opts {
257+ if let Some ( sync_url) = & options. syncUrl {
258+ let auth_token = options. authToken . as_ref ( ) . cloned ( ) . unwrap_or_default ( ) ;
259+
260+ let encryption_cipher: String = opts
250261 . as_ref ( )
251- . and_then ( |o| o. authToken . as_ref ( ) )
262+ . and_then ( |o| o. encryptionCipher . as_ref ( ) )
252263 . cloned ( )
253- . unwrap_or_default ( ) ;
254- let mut builder = libsql:: Builder :: new_remote ( path. clone ( ) , auth_token) ;
255- if let Some ( encryption_key) = opts
264+ . unwrap_or ( "aes256cbc" . to_string ( ) ) ;
265+ let cipher = libsql:: Cipher :: from_str ( & encryption_cipher) . map_err ( |_| {
266+ throw_sqlite_error (
267+ "Invalid encryption cipher" . to_string ( ) ,
268+ "SQLITE_INVALID_ENCRYPTION_CIPHER" . to_string ( ) ,
269+ 0 ,
270+ )
271+ } ) ?;
272+ let encryption_key = opts
256273 . as_ref ( )
257- . and_then ( |o| o. remoteEncryptionKey . as_ref ( ) )
274+ . and_then ( |o| o. encryptionKey . as_ref ( ) )
258275 . cloned ( )
259- {
276+ . unwrap_or ( "" . to_string ( ) ) ;
277+
278+ let mut builder =
279+ libsql:: Builder :: new_remote_replica ( path. clone ( ) , sync_url. clone ( ) , auth_token) ;
280+
281+ let read_your_writes = options. readYourWrites . unwrap_or ( true ) ;
282+ builder = builder. read_your_writes ( read_your_writes) ;
283+
284+ if encryption_key. len ( ) > 0 {
285+ let encryption_config =
286+ libsql:: EncryptionConfig :: new ( cipher, encryption_key. into ( ) ) ;
287+ builder = builder. encryption_config ( encryption_config) ;
288+ }
289+
290+ if let Some ( remote_encryption_key) = & options. remoteEncryptionKey {
260291 let encryption_context = libsql:: EncryptionContext {
261- key : libsql:: EncryptionKey :: Base64Encoded ( encryption_key ) ,
292+ key : libsql:: EncryptionKey :: Base64Encoded ( remote_encryption_key . to_string ( ) ) ,
262293 } ;
263294 builder = builder. remote_encryption ( encryption_context) ;
264295 }
265- rt. block_on ( builder. build ( ) ) . map_err ( Error :: from) ?
266- } else if let Some ( options) = & opts {
267- if let Some ( sync_url) = & options. syncUrl {
268- let auth_token = options. authToken . as_ref ( ) . cloned ( ) . unwrap_or_default ( ) ;
269-
270- let encryption_cipher: String = opts
271- . as_ref ( )
272- . and_then ( |o| o. encryptionCipher . as_ref ( ) )
273- . cloned ( )
274- . unwrap_or ( "aes256cbc" . to_string ( ) ) ;
275- let cipher = libsql:: Cipher :: from_str ( & encryption_cipher) . map_err ( |_| {
276- throw_sqlite_error (
277- "Invalid encryption cipher" . to_string ( ) ,
278- "SQLITE_INVALID_ENCRYPTION_CIPHER" . to_string ( ) ,
279- 0 ,
280- )
281- } ) ?;
282- let encryption_key = opts
283- . as_ref ( )
284- . and_then ( |o| o. encryptionKey . as_ref ( ) )
285- . cloned ( )
286- . unwrap_or ( "" . to_string ( ) ) ;
287-
288- let mut builder =
289- libsql:: Builder :: new_remote_replica ( path. clone ( ) , sync_url. clone ( ) , auth_token) ;
290-
291- let read_your_writes = options. readYourWrites . unwrap_or ( true ) ;
292- builder = builder. read_your_writes ( read_your_writes) ;
293-
294- if encryption_key. len ( ) > 0 {
295- let encryption_config =
296- libsql:: EncryptionConfig :: new ( cipher, encryption_key. into ( ) ) ;
297- builder = builder. encryption_config ( encryption_config) ;
298- }
299-
300- if let Some ( remote_encryption_key) = & options. remoteEncryptionKey {
301- let encryption_context = libsql:: EncryptionContext {
302- key : libsql:: EncryptionKey :: Base64Encoded (
303- remote_encryption_key. to_string ( ) ,
304- ) ,
305- } ;
306- builder = builder. remote_encryption ( encryption_context) ;
307- }
308296
309- if let Some ( period) = options. syncPeriod {
310- if period > 0.0 {
311- builder = builder. sync_interval ( std:: time:: Duration :: from_secs_f64 ( period) ) ;
312- }
297+ if let Some ( period) = options. syncPeriod {
298+ if period > 0.0 {
299+ builder = builder. sync_interval ( std:: time:: Duration :: from_secs_f64 ( period) ) ;
313300 }
314-
315- rt. block_on ( builder. build ( ) ) . map_err ( Error :: from) ?
316- } else {
317- let builder = libsql:: Builder :: new_local ( & path) ;
318- rt. block_on ( builder. build ( ) ) . map_err ( Error :: from) ?
319301 }
302+
303+ builder. build ( ) . await . map_err ( Error :: from) ?
320304 } else {
321305 let builder = libsql:: Builder :: new_local ( & path) ;
322- rt. block_on ( builder. build ( ) ) . map_err ( Error :: from) ?
323- } ;
324- let conn = db. connect ( ) . map_err ( Error :: from) ?;
325- let default_safe_integers = AtomicBool :: new ( false ) ;
326- let memory = path == ":memory:" ;
327- let timeout = match opts {
328- Some ( ref opts) => opts. timeout . unwrap_or ( 0.0 ) ,
329- None => 0.0 ,
330- } ;
331- if timeout > 0.0 {
332- conn. busy_timeout ( Duration :: from_millis ( timeout as u64 ) )
333- . map_err ( Error :: from) ?
306+ builder. build ( ) . await . map_err ( Error :: from) ?
334307 }
335- Ok ( Database {
336- db,
337- conn : Some ( Arc :: new ( conn) ) ,
338- default_safe_integers,
339- memory,
340- } )
308+ } else {
309+ let builder = libsql:: Builder :: new_local ( & path) ;
310+ builder. build ( ) . await . map_err ( Error :: from) ?
311+ } ;
312+ let conn = db. connect ( ) . map_err ( Error :: from) ?;
313+ let default_safe_integers = AtomicBool :: new ( false ) ;
314+ let memory = path == ":memory:" ;
315+ let timeout = match opts {
316+ Some ( ref opts) => opts. timeout . unwrap_or ( 0.0 ) ,
317+ None => 0.0 ,
318+ } ;
319+ if timeout > 0.0 {
320+ conn. busy_timeout ( Duration :: from_millis ( timeout as u64 ) )
321+ . map_err ( Error :: from) ?
322+ }
323+ Ok ( Database {
324+ db,
325+ conn : Some ( Arc :: new ( conn) ) ,
326+ default_safe_integers,
327+ memory,
328+ } )
329+ }
330+
331+ #[ napi]
332+ impl Database {
333+ /// Creates a new database instance.
334+ ///
335+ /// # Arguments
336+ ///
337+ /// * `path` - The path to the database file.
338+ /// * `opts` - The database options.
339+ #[ napi( constructor) ]
340+ pub fn new ( path : String , opts : Option < Options > ) -> Result < Self > {
341+ ensure_logger ( ) ;
342+ let rt = runtime ( ) ?;
343+ rt. block_on ( connect ( path, opts) )
341344 }
342345
343346 /// Returns whether the database is in memory-only mode.
0 commit comments