feat(ios): nbstore swift native binding (#9211)

This commit is contained in:
Brooooooklyn
2024-12-27 06:34:30 +00:00
parent 8de0679d70
commit 752e697c6a
40 changed files with 4544 additions and 234 deletions

View File

@@ -1,3 +1,5 @@
use std::ops::Deref;
use super::{storage::SqliteDocStorage, Blob, ListedBlob, SetBlob};
type Result<T> = std::result::Result<T, sqlx::Error>;
@@ -22,7 +24,7 @@ impl SqliteDocStorage {
DO UPDATE SET data=$2, mime=$3, size=$4, deleted_at=NULL;"#,
)
.bind(blob.key)
.bind(blob.data.as_ref())
.bind(blob.data.deref())
.bind(blob.mime)
.bind(blob.data.len() as i64)
.execute(&self.pool)
@@ -67,7 +69,6 @@ impl SqliteDocStorage {
#[cfg(test)]
mod tests {
use napi::bindgen_prelude::Uint8Array;
use sqlx::Row;
use super::*;
@@ -87,7 +88,7 @@ mod tests {
storage
.set_blob(SetBlob {
key: format!("test_{}", i),
data: Uint8Array::from(vec![0, 0]),
data: vec![0, 0].into(),
mime: "text/plain".to_string(),
})
.await
@@ -127,7 +128,7 @@ mod tests {
storage
.set_blob(SetBlob {
key: format!("test_{}", i),
data: Uint8Array::from(vec![0, 0]),
data: vec![0, 0].into(),
mime: "text/plain".to_string(),
})
.await
@@ -175,7 +176,7 @@ mod tests {
storage
.set_blob(SetBlob {
key: format!("test_{}", i),
data: Uint8Array::from(vec![0, 0]),
data: vec![0, 0].into(),
mime: "text/plain".to_string(),
})
.await

View File

@@ -1,3 +1,5 @@
use std::ops::Deref;
use chrono::NaiveDateTime;
use sqlx::{QueryBuilder, Row};
@@ -110,7 +112,7 @@ impl SqliteDocStorage {
WHERE updated_at <= $3;"#,
)
.bind(snapshot.doc_id)
.bind(snapshot.data.as_ref())
.bind(snapshot.data.deref())
.bind(snapshot.timestamp)
.execute(&self.pool)
.await?;
@@ -206,7 +208,6 @@ impl SqliteDocStorage {
#[cfg(test)]
mod tests {
use chrono::{DateTime, Utc};
use napi::bindgen_prelude::Uint8Array;
use super::*;
@@ -252,7 +253,7 @@ mod tests {
storage
.set_doc_snapshot(DocRecord {
doc_id: "test".to_string(),
data: Uint8Array::from(vec![0, 0]),
data: vec![0, 0].into(),
timestamp: Utc::now().naive_utc(),
})
.await
@@ -331,7 +332,7 @@ mod tests {
let snapshot = DocRecord {
doc_id: "test".to_string(),
data: Uint8Array::from(vec![0, 0]),
data: vec![0, 0].into(),
timestamp: Utc::now().naive_utc(),
};
@@ -349,7 +350,7 @@ mod tests {
let snapshot = DocRecord {
doc_id: "test".to_string(),
data: Uint8Array::from(vec![0, 0]),
data: vec![0, 0].into(),
timestamp: Utc::now().naive_utc(),
};
@@ -362,7 +363,7 @@ mod tests {
let snapshot = DocRecord {
doc_id: "test".to_string(),
data: Uint8Array::from(vec![0, 1]),
data: vec![0, 1].into(),
timestamp: DateTime::from_timestamp_millis(Utc::now().timestamp_millis() - 1000)
.unwrap()
.naive_utc(),

View File

@@ -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)
}
}