Skip to content

Commit 6b318da

Browse files
committed
sync
1 parent e34dcf5 commit 6b318da

4 files changed

Lines changed: 55 additions & 3 deletions

File tree

examples/sync/package.json

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

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export interface RunResult {
1717
}
1818
/** Retrieve next row from an iterator synchronously. Needed for better-sqlite3 API compatibility. */
1919
export declare function iteratorNextSync(iter: RowsIterator): Record
20+
export interface SyncResult {
21+
framesSynced: number
22+
replicationIndex: number
23+
}
2024
export declare class SqliteError {
2125
message: string
2226
code: string
@@ -40,6 +44,8 @@ export declare class Database {
4044
close(): void
4145
defaultSafeIntegers(toggle?: boolean | undefined | null): void
4246
unsafeMode(): void
47+
sync(): SyncResult
48+
syncUntil(replicationIndex: number): SyncResult
4349
}
4450
export declare class Statement {
4551
columns(): unknown[]

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,30 @@ impl Database {
430430
pub fn unsafeMode(&self) -> Result<()> {
431431
todo!();
432432
}
433+
434+
#[napi]
435+
pub fn sync(&self) -> Result<SyncResult> {
436+
let rt = runtime()?;
437+
let result = rt.block_on(async move {
438+
self.db.sync().await.map_err(Error::from)
439+
})?;
440+
Ok(SyncResult {
441+
frames_synced: result.frames_synced() as f64,
442+
replication_index: result.frame_no().unwrap_or(0) as f64,
443+
})
444+
}
445+
446+
#[napi]
447+
pub fn syncUntil(&self, replication_index: f64) -> Result<SyncResult> {
448+
let rt = runtime()?;
449+
let result = rt.block_on(async move {
450+
self.db.sync_until(replication_index as u64).await.map_err(Error::from)
451+
})?;
452+
Ok(SyncResult {
453+
frames_synced: result.frames_synced() as f64,
454+
replication_index: result.frame_no().unwrap_or(0) as f64,
455+
})
456+
}
433457
}
434458

435459
fn is_remote_path(path: &str) -> bool {
@@ -1093,3 +1117,9 @@ fn ensure_logger() {
10931117
.try_init();
10941118
});
10951119
}
1120+
1121+
#[napi(object)]
1122+
pub struct SyncResult {
1123+
pub frames_synced: f64,
1124+
pub replication_index: f64,
1125+
}

wrapper.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,27 @@ class Database {
5252
}
5353

5454
sync() {
55-
throw new Error("not implemented");
55+
try {
56+
const result = this.db.sync();
57+
return {
58+
frames_synced: result.frames_synced,
59+
replication_index: result.replication_index
60+
};
61+
} catch (err) {
62+
throw convertError(err);
63+
}
5664
}
5765

5866
syncUntil(replicationIndex) {
59-
throw new Error("not implemented");
67+
try {
68+
const result = this.db.syncUntil(replicationIndex);
69+
return {
70+
frames_synced: result.frames_synced,
71+
replication_index: result.replication_index
72+
};
73+
} catch (err) {
74+
throw convertError(err);
75+
}
6076
}
6177

6278
/**

0 commit comments

Comments
 (0)