@@ -6,17 +6,17 @@ use spin_world::v2::sqlite::{self, RowResult};
6
6
use tokio:: sync:: OnceCell ;
7
7
use tracing:: { instrument, Level } ;
8
8
9
- /// A wrapper around a libSQL connection that implements the [`Connection`] trait.
10
- pub struct LibSqlConnection {
9
+ /// A lazy wrapper around a [`LibSqlConnection`] that implements the [`Connection`] trait.
10
+ pub struct LazyLibSqlConnection {
11
11
url : String ,
12
12
token : String ,
13
13
// Since the libSQL client can only be created asynchronously, we wait until
14
14
// we're in the `Connection` implementation to create. Since we only want to do
15
15
// this once, we use a `OnceCell` to store it.
16
- inner : OnceCell < LibsqlClient > ,
16
+ inner : OnceCell < LibSqlConnection > ,
17
17
}
18
18
19
- impl LibSqlConnection {
19
+ impl LazyLibSqlConnection {
20
20
pub fn new ( url : String , token : String ) -> Self {
21
21
Self {
22
22
url,
@@ -25,10 +25,10 @@ impl LibSqlConnection {
25
25
}
26
26
}
27
27
28
- pub async fn get_client ( & self ) -> Result < & LibsqlClient , v2:: Error > {
28
+ pub async fn get_or_create_connection ( & self ) -> Result < & LibSqlConnection , v2:: Error > {
29
29
self . inner
30
30
. get_or_try_init ( || async {
31
- LibsqlClient :: create ( self . url . clone ( ) , self . token . clone ( ) )
31
+ LibSqlConnection :: create ( self . url . clone ( ) , self . token . clone ( ) )
32
32
. await
33
33
. context ( "failed to create SQLite client" )
34
34
} )
@@ -38,18 +38,18 @@ impl LibSqlConnection {
38
38
}
39
39
40
40
#[ async_trait]
41
- impl Connection for LibSqlConnection {
41
+ impl Connection for LazyLibSqlConnection {
42
42
async fn query (
43
43
& self ,
44
44
query : & str ,
45
45
parameters : Vec < v2:: Value > ,
46
46
) -> Result < v2:: QueryResult , v2:: Error > {
47
- let client = self . get_client ( ) . await ?;
47
+ let client = self . get_or_create_connection ( ) . await ?;
48
48
client. query ( query, parameters) . await
49
49
}
50
50
51
51
async fn execute_batch ( & self , statements : & str ) -> anyhow:: Result < ( ) > {
52
- let client = self . get_client ( ) . await ?;
52
+ let client = self . get_or_create_connection ( ) . await ?;
53
53
client. execute_batch ( statements) . await
54
54
}
55
55
@@ -58,12 +58,13 @@ impl Connection for LibSqlConnection {
58
58
}
59
59
}
60
60
61
+ /// An open connection to a libSQL server.
61
62
#[ derive( Clone ) ]
62
- pub struct LibsqlClient {
63
+ pub struct LibSqlConnection {
63
64
inner : libsql:: Connection ,
64
65
}
65
66
66
- impl LibsqlClient {
67
+ impl LibSqlConnection {
67
68
#[ instrument( name = "spin_sqlite_libsql.create_connection" , skip( token) , err( level = Level :: INFO ) , fields( otel. kind = "client" , db. system = "sqlite" ) ) ]
68
69
pub async fn create ( url : String , token : String ) -> anyhow:: Result < Self > {
69
70
let db = libsql:: Builder :: new_remote ( url, token) . build ( ) . await ?;
@@ -72,7 +73,7 @@ impl LibsqlClient {
72
73
}
73
74
}
74
75
75
- impl LibsqlClient {
76
+ impl LibSqlConnection {
76
77
#[ instrument( name = "spin_sqlite_libsql.query" , skip( self ) , err( level = Level :: INFO ) , fields( otel. kind = "client" , db. system = "sqlite" , otel. name = query) ) ]
77
78
pub async fn query (
78
79
& self ,
0 commit comments