mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-29 00:06:09 +08:00
feat(ios): nbstore swift native binding (#9211)
This commit is contained in:
@@ -1,27 +1,47 @@
|
||||
mod blob;
|
||||
mod doc;
|
||||
mod storage;
|
||||
mod sync;
|
||||
pub mod blob;
|
||||
pub mod doc;
|
||||
pub mod storage;
|
||||
pub mod sync;
|
||||
|
||||
use chrono::NaiveDateTime;
|
||||
use napi::bindgen_prelude::*;
|
||||
use napi_derive::napi;
|
||||
|
||||
fn map_err(err: sqlx::Error) -> napi::Error {
|
||||
napi::Error::from(anyhow::Error::from(err))
|
||||
#[cfg(feature = "use-as-lib")]
|
||||
type Result<T> = anyhow::Result<T>;
|
||||
|
||||
#[cfg(not(feature = "use-as-lib"))]
|
||||
type Result<T> = napi::Result<T>;
|
||||
|
||||
#[cfg(not(feature = "use-as-lib"))]
|
||||
fn map_err(err: sqlx::Error) -> Error {
|
||||
Error::from(anyhow::Error::from(err))
|
||||
}
|
||||
|
||||
#[cfg(feature = "use-as-lib")]
|
||||
fn map_err(err: sqlx::Error) -> anyhow::Error {
|
||||
anyhow::Error::from(err)
|
||||
}
|
||||
|
||||
#[cfg(feature = "use-as-lib")]
|
||||
pub type Data = Vec<u8>;
|
||||
|
||||
#[cfg(not(feature = "use-as-lib"))]
|
||||
pub type Data = Uint8Array;
|
||||
|
||||
#[napi(object)]
|
||||
pub struct DocUpdate {
|
||||
pub doc_id: String,
|
||||
pub created_at: NaiveDateTime,
|
||||
pub data: Uint8Array,
|
||||
#[napi(ts_type = "Uint8Array")]
|
||||
pub data: Data,
|
||||
}
|
||||
|
||||
#[napi(object)]
|
||||
pub struct DocRecord {
|
||||
pub doc_id: String,
|
||||
pub data: Uint8Array,
|
||||
#[napi(ts_type = "Uint8Array")]
|
||||
pub data: Data,
|
||||
pub timestamp: NaiveDateTime,
|
||||
}
|
||||
|
||||
@@ -35,14 +55,16 @@ pub struct DocClock {
|
||||
#[napi(object)]
|
||||
pub struct SetBlob {
|
||||
pub key: String,
|
||||
pub data: Uint8Array,
|
||||
#[napi(ts_type = "Uint8Array")]
|
||||
pub data: Data,
|
||||
pub mime: String,
|
||||
}
|
||||
|
||||
#[napi(object)]
|
||||
pub struct Blob {
|
||||
pub key: String,
|
||||
pub data: Uint8Array,
|
||||
#[napi(ts_type = "Uint8Array")]
|
||||
pub data: Data,
|
||||
pub mime: String,
|
||||
pub size: i64,
|
||||
pub created_at: NaiveDateTime,
|
||||
@@ -64,7 +86,7 @@ pub struct DocStorage {
|
||||
#[napi]
|
||||
impl DocStorage {
|
||||
#[napi(constructor, async_runtime)]
|
||||
pub fn new(path: String) -> napi::Result<Self> {
|
||||
pub fn new(path: String) -> Result<Self> {
|
||||
Ok(Self {
|
||||
storage: storage::SqliteDocStorage::new(path),
|
||||
})
|
||||
@@ -72,19 +94,19 @@ impl DocStorage {
|
||||
|
||||
#[napi]
|
||||
/// Initialize the database and run migrations.
|
||||
pub async fn connect(&self) -> napi::Result<()> {
|
||||
pub async fn connect(&self) -> Result<()> {
|
||||
self.storage.connect().await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn close(&self) -> napi::Result<()> {
|
||||
pub async fn close(&self) -> Result<()> {
|
||||
self.storage.close().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[napi(getter)]
|
||||
pub async fn is_closed(&self) -> napi::Result<bool> {
|
||||
pub async fn is_closed(&self) -> Result<bool> {
|
||||
Ok(self.storage.is_closed())
|
||||
}
|
||||
|
||||
@@ -93,26 +115,22 @@ impl DocStorage {
|
||||
* See https://www.sqlite.org/pragma.html#pragma_wal_checkpoint:~:text=PRAGMA%20schema.wal_checkpoint%3B
|
||||
*/
|
||||
#[napi]
|
||||
pub async fn checkpoint(&self) -> napi::Result<()> {
|
||||
pub async fn checkpoint(&self) -> Result<()> {
|
||||
self.storage.checkpoint().await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn validate(&self) -> napi::Result<bool> {
|
||||
pub async fn validate(&self) -> Result<bool> {
|
||||
self.storage.validate().await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn set_space_id(&self, space_id: String) -> napi::Result<()> {
|
||||
pub async fn set_space_id(&self, space_id: String) -> Result<()> {
|
||||
self.storage.set_space_id(space_id).await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn push_update(
|
||||
&self,
|
||||
doc_id: String,
|
||||
update: Uint8Array,
|
||||
) -> napi::Result<NaiveDateTime> {
|
||||
pub async fn push_update(&self, doc_id: String, update: Uint8Array) -> Result<NaiveDateTime> {
|
||||
self
|
||||
.storage
|
||||
.push_update(doc_id, update)
|
||||
@@ -121,12 +139,12 @@ impl DocStorage {
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_doc_snapshot(&self, doc_id: String) -> napi::Result<Option<DocRecord>> {
|
||||
pub async fn get_doc_snapshot(&self, doc_id: String) -> Result<Option<DocRecord>> {
|
||||
self.storage.get_doc_snapshot(doc_id).await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn set_doc_snapshot(&self, snapshot: DocRecord) -> napi::Result<bool> {
|
||||
pub async fn set_doc_snapshot(&self, snapshot: DocRecord) -> Result<bool> {
|
||||
self
|
||||
.storage
|
||||
.set_doc_snapshot(snapshot)
|
||||
@@ -135,7 +153,7 @@ impl DocStorage {
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_doc_updates(&self, doc_id: String) -> napi::Result<Vec<DocUpdate>> {
|
||||
pub async fn get_doc_updates(&self, doc_id: String) -> Result<Vec<DocUpdate>> {
|
||||
self.storage.get_doc_updates(doc_id).await.map_err(map_err)
|
||||
}
|
||||
|
||||
@@ -144,7 +162,7 @@ impl DocStorage {
|
||||
&self,
|
||||
doc_id: String,
|
||||
updates: Vec<NaiveDateTime>,
|
||||
) -> napi::Result<u32> {
|
||||
) -> Result<u32> {
|
||||
self
|
||||
.storage
|
||||
.mark_updates_merged(doc_id, updates)
|
||||
@@ -153,32 +171,32 @@ impl DocStorage {
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn delete_doc(&self, doc_id: String) -> napi::Result<()> {
|
||||
pub async fn delete_doc(&self, doc_id: String) -> Result<()> {
|
||||
self.storage.delete_doc(doc_id).await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_doc_clocks(&self, after: Option<NaiveDateTime>) -> napi::Result<Vec<DocClock>> {
|
||||
pub async fn get_doc_clocks(&self, after: Option<NaiveDateTime>) -> Result<Vec<DocClock>> {
|
||||
self.storage.get_doc_clocks(after).await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_doc_clock(&self, doc_id: String) -> napi::Result<Option<DocClock>> {
|
||||
pub async fn get_doc_clock(&self, doc_id: String) -> Result<Option<DocClock>> {
|
||||
self.storage.get_doc_clock(doc_id).await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_blob(&self, key: String) -> napi::Result<Option<Blob>> {
|
||||
pub async fn get_blob(&self, key: String) -> Result<Option<Blob>> {
|
||||
self.storage.get_blob(key).await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn set_blob(&self, blob: SetBlob) -> napi::Result<()> {
|
||||
pub async fn set_blob(&self, blob: SetBlob) -> Result<()> {
|
||||
self.storage.set_blob(blob).await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn delete_blob(&self, key: String, permanently: bool) -> napi::Result<()> {
|
||||
pub async fn delete_blob(&self, key: String, permanently: bool) -> Result<()> {
|
||||
self
|
||||
.storage
|
||||
.delete_blob(key, permanently)
|
||||
@@ -187,17 +205,17 @@ impl DocStorage {
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn release_blobs(&self) -> napi::Result<()> {
|
||||
pub async fn release_blobs(&self) -> Result<()> {
|
||||
self.storage.release_blobs().await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn list_blobs(&self) -> napi::Result<Vec<ListedBlob>> {
|
||||
pub async fn list_blobs(&self) -> Result<Vec<ListedBlob>> {
|
||||
self.storage.list_blobs().await.map_err(map_err)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_peer_remote_clocks(&self, peer: String) -> napi::Result<Vec<DocClock>> {
|
||||
pub async fn get_peer_remote_clocks(&self, peer: String) -> Result<Vec<DocClock>> {
|
||||
self
|
||||
.storage
|
||||
.get_peer_remote_clocks(peer)
|
||||
@@ -206,11 +224,7 @@ impl DocStorage {
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_peer_remote_clock(
|
||||
&self,
|
||||
peer: String,
|
||||
doc_id: String,
|
||||
) -> napi::Result<DocClock> {
|
||||
pub async fn get_peer_remote_clock(&self, peer: String, doc_id: String) -> Result<DocClock> {
|
||||
self
|
||||
.storage
|
||||
.get_peer_remote_clock(peer, doc_id)
|
||||
@@ -224,7 +238,7 @@ impl DocStorage {
|
||||
peer: String,
|
||||
doc_id: String,
|
||||
clock: NaiveDateTime,
|
||||
) -> napi::Result<()> {
|
||||
) -> Result<()> {
|
||||
self
|
||||
.storage
|
||||
.set_peer_remote_clock(peer, doc_id, clock)
|
||||
@@ -233,7 +247,7 @@ impl DocStorage {
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_peer_pulled_remote_clocks(&self, peer: String) -> napi::Result<Vec<DocClock>> {
|
||||
pub async fn get_peer_pulled_remote_clocks(&self, peer: String) -> Result<Vec<DocClock>> {
|
||||
self
|
||||
.storage
|
||||
.get_peer_pulled_remote_clocks(peer)
|
||||
@@ -246,7 +260,7 @@ impl DocStorage {
|
||||
&self,
|
||||
peer: String,
|
||||
doc_id: String,
|
||||
) -> napi::Result<DocClock> {
|
||||
) -> Result<DocClock> {
|
||||
self
|
||||
.storage
|
||||
.get_peer_pulled_remote_clock(peer, doc_id)
|
||||
@@ -260,7 +274,7 @@ impl DocStorage {
|
||||
peer: String,
|
||||
doc_id: String,
|
||||
clock: NaiveDateTime,
|
||||
) -> napi::Result<()> {
|
||||
) -> Result<()> {
|
||||
self
|
||||
.storage
|
||||
.set_peer_pulled_remote_clock(peer, doc_id, clock)
|
||||
@@ -269,7 +283,7 @@ impl DocStorage {
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_peer_pushed_clocks(&self, peer: String) -> napi::Result<Vec<DocClock>> {
|
||||
pub async fn get_peer_pushed_clocks(&self, peer: String) -> Result<Vec<DocClock>> {
|
||||
self
|
||||
.storage
|
||||
.get_peer_pushed_clocks(peer)
|
||||
@@ -278,11 +292,7 @@ impl DocStorage {
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn get_peer_pushed_clock(
|
||||
&self,
|
||||
peer: String,
|
||||
doc_id: String,
|
||||
) -> napi::Result<DocClock> {
|
||||
pub async fn get_peer_pushed_clock(&self, peer: String, doc_id: String) -> Result<DocClock> {
|
||||
self
|
||||
.storage
|
||||
.get_peer_pushed_clock(peer, doc_id)
|
||||
@@ -296,7 +306,7 @@ impl DocStorage {
|
||||
peer: String,
|
||||
doc_id: String,
|
||||
clock: NaiveDateTime,
|
||||
) -> napi::Result<()> {
|
||||
) -> Result<()> {
|
||||
self
|
||||
.storage
|
||||
.set_peer_pushed_clock(peer, doc_id, clock)
|
||||
@@ -305,7 +315,7 @@ impl DocStorage {
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn clear_clocks(&self) -> napi::Result<()> {
|
||||
pub async fn clear_clocks(&self) -> Result<()> {
|
||||
self.storage.clear_clocks().await.map_err(map_err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user