Skip to content

Commit e34dcf5

Browse files
committed
remote
1 parent 31e2bd4 commit e34dcf5

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

examples/remote/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
"author": "",
1212
"license": "ISC",
1313
"dependencies": {
14-
"libsql": "^0.1.0"
14+
"libsql": "../.."
1515
}
1616
}

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
export interface Options {
77
timeout?: number
8+
authToken?: string
9+
syncUrl?: string
10+
syncPeriod?: number
811
}
912
export declare function databasePrepareSync(db: Database, sql: string): Statement
1013
export interface RunResult {

src/lib.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ pub struct Database {
172172
#[napi(object)]
173173
pub struct Options {
174174
pub timeout: Option<f64>,
175+
pub authToken: Option<String>,
176+
pub syncUrl: Option<String>,
177+
pub syncPeriod: Option<f64>,
175178
}
176179

177180
impl Drop for Database {
@@ -187,8 +190,37 @@ impl Database {
187190
ensure_logger();
188191
let rt = runtime()?;
189192
let remote = is_remote_path(&path);
193+
190194
let db = if remote {
191-
todo!("Remote databases are not supported yet");
195+
let auth_token = opts
196+
.as_ref()
197+
.and_then(|o| o.authToken.as_ref())
198+
.cloned()
199+
.unwrap_or_default();
200+
201+
let builder = libsql::Builder::new_remote(path.clone(), auth_token);
202+
rt.block_on(builder.build()).map_err(Error::from)?
203+
} else if let Some(options) = &opts {
204+
if let Some(sync_url) = &options.syncUrl {
205+
let auth_token = options
206+
.authToken
207+
.as_ref()
208+
.cloned()
209+
.unwrap_or_default();
210+
211+
let mut builder = libsql::Builder::new_remote_replica(path.clone(), sync_url.clone(), auth_token);
212+
213+
if let Some(period) = options.syncPeriod {
214+
if period > 0.0 {
215+
builder = builder.sync_interval(std::time::Duration::from_secs_f64(period));
216+
}
217+
}
218+
219+
rt.block_on(builder.build()).map_err(Error::from)?
220+
} else {
221+
let builder = libsql::Builder::new_local(&path);
222+
rt.block_on(builder.build()).map_err(Error::from)?
223+
}
192224
} else {
193225
let builder = libsql::Builder::new_local(&path);
194226
rt.block_on(builder.build()).map_err(Error::from)?
@@ -197,7 +229,7 @@ impl Database {
197229
let default_safe_integers = AtomicBool::new(false);
198230
let memory = path == ":memory:";
199231
let timeout = match opts {
200-
Some(opts) => opts.timeout.unwrap_or(0.0),
232+
Some(ref opts) => opts.timeout.unwrap_or(0.0),
201233
None => 0.0,
202234
};
203235
if timeout > 0.0 {

0 commit comments

Comments
 (0)