Skip to content

Commit

Permalink
feat: integrating limit offset across ridb (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
elribonazo authored Jan 26, 2025
2 parents 73c57c4 + ea61f7d commit 17d5129
Show file tree
Hide file tree
Showing 68 changed files with 2,466 additions and 3,393 deletions.
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,4 @@ jobs:
run: |
npm install
npm run build
npm run docs
npx multi-semantic-release
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.0.0"
"version": "independent"
}
4,307 changes: 1,237 additions & 3,070 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/ridb-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## @trust0/ridb-core [1.3.0-rc.1](https://github.com/trust0-project/RIDB/compare/@trust0/[email protected]...@trust0/[email protected]) (2025-01-26)

### Features

* adding $nin operator to match array elements with negative criteria ([56df6bd](https://github.com/trust0-project/RIDB/commit/56df6bd3359709e04686365976e2362ca0f2a5ff))

## @trust0/ridb-core [1.2.0](https://github.com/trust0-project/RIDB/compare/@trust0/[email protected]...@trust0/[email protected]) (2025-01-26)

### Features
Expand Down
2 changes: 1 addition & 1 deletion packages/ridb-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ serde = { version = "1.0.195", features = ["derive"] }
serde-wasm-bindgen = "0.6.3"
console = "0.15.8"
serde_json = "1.0.111"
web-sys = { version = "0.3.69", features = ["Storage", "IdbTransaction", "IdbVersionChangeEvent", "IdbObjectStoreParameters","IdbIndexParameters", "Event", "DomException", "IdbCursor", "IdbKeyRange", "IdbObjectStore", "IdbRequest", "IdbTransactionMode", "IdbOpenDbRequest", "console", "Window", "Request", "Response", "IdbDatabase", "IdbFactory", "DomStringList", "IdbIndex"] }
web-sys = { version = "0.3.69", features = ["Storage","IdbOpenDbOptions","IdbCursorDirection","IdbCursorWithValue", "IdbTransaction", "IdbVersionChangeEvent", "IdbObjectStoreParameters","IdbIndexParameters", "Event", "DomException", "IdbCursor", "IdbKeyRange", "IdbObjectStore", "IdbRequest", "IdbTransactionMode", "IdbOpenDbRequest", "console", "Window", "Request", "Response", "IdbDatabase", "IdbFactory", "DomStringList", "IdbIndex"] }
wasm-bindgen-test = {version="^0.3.42"}
sha2 = "0.11.0-pre.4"
base64 = "0.22.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/ridb-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"publishConfig": {
"access": "public"
},
"version": "1.2.0",
"version": "1.3.0-rc.1",
"main": "./pkg/ridb_core.js",
"types": "./pkg/ridb_core.d.ts",
"sideEffects": [
Expand Down
54 changes: 48 additions & 6 deletions packages/ridb-core/src/collection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
use js_sys::{Object, Reflect};
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;
use crate::query::options::QueryOptions;
use crate::schema::Schema;
use crate::storage::{HookType, Storage};

fn get_u32_option(options: &JsValue, key: &str) -> Result<Option<u32>, JsValue> {
if options.is_undefined() {
return Ok(None);
}

let value = Reflect::get(options, &JsValue::from_str(key))?;

// If the value is undefined, we treat it as `None`.
if value.is_undefined() {
return Ok(None);
}

// If it's a number, convert to u32.
if let Some(n) = value.as_f64() {
return Ok(Some(n as u32));
}

// Otherwise, we bail out with a descriptive error.
Err(JsValue::from_str(&format!(
"Expected '{}' to be undefined or a number.",
key
)))
}

#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
export type InternalsRecord = {
Expand Down Expand Up @@ -49,6 +74,11 @@ export type Doc<T extends SchemaType> = {
__version?: number;
};
export type QueryOptions = {
limit?: number;
offset?: number;
}
/**
* Collection is a class that represents a collection of documents in a database.
* @template T - A schema type defining the structure of the documents in the collection.
Expand All @@ -59,13 +89,13 @@ export class Collection<T extends SchemaType> {
*
* @returns A promise that resolves to an array of documents.
*/
find(query: QueryType<T>): Promise<Doc<T>[]>;
find(query: QueryType<T>, options?: QueryOptions): Promise<Doc<T>[]>;
/**
* count all documents in the collection.
*
* @returns A promise that resolves to an array of documents.
*/
count(query: QueryType<T>): Promise<number>;
count(query: QueryType<T>, options?: QueryOptions): Promise<number>;
/**
* Finds a single document in the collection by its ID.
*
Expand Down Expand Up @@ -143,11 +173,14 @@ impl Collection {
/// This function is asynchronous and returns a `JsValue` representing
/// the documents found in the collection.
#[wasm_bindgen]
pub async fn find(&mut self, query_js: JsValue) -> Result<JsValue, JsValue> {
pub async fn find(&mut self, query_js: JsValue, options_js:JsValue) -> Result<JsValue, JsValue> {
let options = self.parse_query_options(options_js)?;

// No index available, perform a regular find
let docs = self.storage.find(
&self.name,
query_js
query_js,
options
).await?;

// Process and return the result
Expand All @@ -168,13 +201,22 @@ impl Collection {
)
}

pub fn parse_query_options(&self, options: JsValue) -> Result<QueryOptions, JsValue> {
// Use the helper to extract and validate both limit and offset.
let limit = get_u32_option(&options, "limit")?;
let offset = get_u32_option(&options, "offset")?;

Ok(QueryOptions { limit, offset })
}

/// counts and returns all documents in the collection.
///
/// This function is asynchronous and returns a `Schema` representing
/// the documents found in the collection.
#[wasm_bindgen]
pub async fn count(&self, query_js: JsValue) -> Result<JsValue, JsValue> {
match self.storage.count(&self.name, query_js).await {
pub async fn count(&self, query_js: JsValue, options_js:JsValue) -> Result<JsValue, JsValue> {
let options = self.parse_query_options(options_js)?;
match self.storage.count(&self.name, query_js, options).await {
Ok(count) => Ok(count),
Err(e) => Err(js_sys::Error::new(&format!("Failed to count documents: {:?}", e)).into())
}
Expand Down
Loading

0 comments on commit 17d5129

Please sign in to comment.