Skip to content

Commit

Permalink
✨ Add a new method to list the keys that have special perfix.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Jul 27, 2024
1 parent 88da31a commit c48479d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ sled = { version = "^0.34", optional = true }
moka = { version = "^0.12", features = ["future"], optional = true }

[features]
default = ["cloudflare"]
cloudflare = [
"dep:wasm-bindgen",
"dep:wasm-bindgen-futures",
Expand Down
38 changes: 38 additions & 0 deletions packages/database/src/providers/kv/cloudflare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,44 @@ impl KVStore for ProxyKV {

Ok(())
}

async fn list_by_prefix(
&self,
prefix: String,
limit: Option<usize>,
cursor: Option<String>,
) -> Result<Vec<String>> {
let env = self.env.kv(self.kv_name.as_str())?;

let ret = SendFuture::new(async move {
let ret = env.list().prefix(prefix);

let ret = if let Some(limit) = limit {
ret.limit(limit as u64)
} else {
ret
};

let ret = if let Some(cursor) = cursor {
ret.cursor(cursor)
} else {
ret
};

ret.execute()
.await
.map_err(|err| anyhow!("Failed to list key-value pair: {:?}", err))
.map(|ret| {
ret.keys
.iter()
.map(|key| key.name.to_owned())
.collect::<Vec<_>>()
})
})
.await;

ret
}
}

pub async fn init_kv(env: Arc<Env>, kv_name: impl ToString) -> Result<ProxyKV> {
Expand Down
6 changes: 6 additions & 0 deletions packages/database/src/providers/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ pub trait KVStore {
async fn get(&self, key: String) -> Result<Option<String>>;
async fn set(&self, key: String, value: String) -> Result<()>;
async fn delete(&self, key: String) -> Result<()>;
async fn list_by_prefix(
&self,
prefix: String,
limit: Option<usize>,
cursor: Option<String>,
) -> Result<Vec<String>>;
}
9 changes: 9 additions & 0 deletions packages/database/src/providers/kv/sled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ impl KVStore for ProxyKV {
async fn delete(&self, key: impl ToString) {
todo!()
}

async fn list_by_prefix(
&self,
prefix: impl ToString,
limit: Option<usize>,
cursor: Option<String>,
) -> Vec<String> {
todo!()
}
}

pub async fn init_kv(path: impl ToString) -> Result<ProxyKV> {
Expand Down
9 changes: 9 additions & 0 deletions packages/database/src/providers/kv/wasmtime_wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ impl KVStore for ProxyKV {
async fn delete(&self, key: impl ToString) {
todo!()
}

async fn list_by_prefix(
&self,
prefix: impl ToString,
limit: Option<usize>,
cursor: Option<String>,
) -> Vec<String> {
todo!()
}
}

pub async fn init_kv() -> Result<ProxyKV> {
Expand Down

0 comments on commit c48479d

Please sign in to comment.