fix(electron): export and import (#9767)

This commit is contained in:
forehalo
2025-01-20 08:48:03 +00:00
parent 2e18ae59e3
commit cb53baca89
26 changed files with 332 additions and 453 deletions

View File

@@ -1,11 +1,18 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export declare class DocStorage {
constructor(path: string)
validate(): Promise<boolean>
setSpaceId(spaceId: string): Promise<void>
}
export declare class DocStoragePool {
constructor()
/** Initialize the database and run migrations. */
connect(universalId: string, path: string): Promise<void>
disconnect(universalId: string): Promise<void>
setSpaceId(universalId: string, spaceId: string): Promise<void>
disconnect(universalId: string): Promise<void>
checkpoint(universalId: string): Promise<void>
pushUpdate(universalId: string, docId: string, update: Uint8Array): Promise<Date>
getDocSnapshot(universalId: string, docId: string): Promise<DocRecord | null>
setDocSnapshot(universalId: string, snapshot: DocRecord): Promise<boolean>
@@ -39,6 +46,7 @@ export declare class SqliteConnection {
deleteBlob(key: string): Promise<void>
getBlobKeys(): Promise<Array<string>>
getUpdates(docId?: string | undefined | null): Promise<Array<UpdateRow>>
getDocTimestamps(): Promise<Array<DocTimestampRow>>
deleteUpdates(docId?: string | undefined | null): Promise<void>
getUpdatesCount(docId?: string | undefined | null): Promise<number>
getAllUpdates(): Promise<Array<UpdateRow>>
@@ -93,6 +101,11 @@ export interface DocRecord {
timestamp: Date
}
export interface DocTimestampRow {
docId?: string
timestamp: Date
}
export interface DocUpdate {
docId: string
timestamp: Date

View File

@@ -364,6 +364,7 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}
module.exports.DocStorage = nativeBinding.DocStorage
module.exports.DocStoragePool = nativeBinding.DocStoragePool
module.exports.SqliteConnection = nativeBinding.SqliteConnection
module.exports.mintChallengeResponse = nativeBinding.mintChallengeResponse

View File

@@ -9,6 +9,7 @@ use chrono::NaiveDateTime;
use napi::bindgen_prelude::*;
use napi_derive::napi;
use pool::SqliteDocStoragePool;
use storage::SqliteDocStorage;
#[cfg(feature = "use-as-lib")]
type Result<T> = anyhow::Result<T>;
@@ -99,12 +100,6 @@ impl DocStoragePool {
Ok(())
}
#[napi]
pub async fn disconnect(&self, universal_id: String) -> Result<()> {
self.pool.disconnect(universal_id).await?;
Ok(())
}
#[napi]
pub async fn set_space_id(&self, universal_id: String, space_id: String) -> Result<()> {
self
@@ -115,6 +110,18 @@ impl DocStoragePool {
Ok(())
}
#[napi]
pub async fn disconnect(&self, universal_id: String) -> Result<()> {
self.pool.disconnect(universal_id).await?;
Ok(())
}
#[napi]
pub async fn checkpoint(&self, universal_id: String) -> Result<()> {
self.pool.ensure_storage(universal_id)?.checkpoint().await?;
Ok(())
}
#[napi]
pub async fn push_update(
&self,
@@ -430,3 +437,32 @@ impl DocStoragePool {
Ok(())
}
}
#[napi]
pub struct DocStorage {
storage: SqliteDocStorage,
}
#[napi]
impl DocStorage {
#[napi(constructor, async_runtime)]
pub fn new(path: String) -> Self {
Self {
storage: SqliteDocStorage::new(path),
}
}
#[napi]
pub async fn validate(&self) -> Result<bool> {
Ok(self.storage.validate().await?)
}
#[napi]
pub async fn set_space_id(&self, space_id: String) -> Result<()> {
self.storage.connect().await?;
self.storage.set_space_id(space_id).await?;
println!("clocks {:?}", self.storage.get_doc_clocks(None).await?);
self.storage.close().await;
Ok(())
}
}

View File

@@ -46,9 +46,13 @@ impl SqliteDocStoragePool {
}
pub async fn disconnect(&self, universal_id: String) -> Result<()> {
let storage = self.ensure_storage(universal_id.to_owned())?;
storage.close().await;
self.inner.remove(&universal_id);
let entry = self.inner.entry(universal_id);
if let Entry::Occupied(entry) = entry {
let storage = entry.remove();
storage.close().await;
}
Ok(())
}
}

View File

@@ -25,6 +25,12 @@ pub struct UpdateRow {
pub doc_id: Option<String>,
}
#[napi(object)]
pub struct DocTimestampRow {
pub doc_id: Option<String>,
pub timestamp: NaiveDateTime,
}
#[napi(object)]
pub struct InsertRow {
pub doc_id: Option<String>,
@@ -146,6 +152,20 @@ impl SqliteConnection {
Ok(updates)
}
#[napi]
pub async fn get_doc_timestamps(&self) -> napi::Result<Vec<DocTimestampRow>> {
// get the greatest timestamp of each doc_id
let updates = sqlx::query_as!(
DocTimestampRow,
"SELECT doc_id, MAX(timestamp) as timestamp FROM updates GROUP BY doc_id"
)
.fetch_all(&self.pool)
.await
.map_err(anyhow::Error::from)?;
Ok(updates)
}
#[napi]
pub async fn delete_updates(&self, doc_id: Option<String>) -> napi::Result<()> {
match doc_id {