Skip to content

Commit

Permalink
Merge pull request #4 from kanko-travel/pagniation
Browse files Browse the repository at this point in the history
return cursor into the previous page when paging through results
  • Loading branch information
umran authored Jul 18, 2024
2 parents a22ca46 + bbc526e commit 06f6b34
Show file tree
Hide file tree
Showing 8 changed files with 667 additions and 130 deletions.
1 change: 1 addition & 0 deletions model/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct Connection<T> {
#[derive(Clone, Debug, Serialize, JsonSchema)]
pub struct PageInfo {
pub next_cursor: Option<Cursor>,
pub prev_cursor: Option<Cursor>,
}

impl<T: JsonSchema> JsonSchema for Connection<T> {
Expand Down
16 changes: 5 additions & 11 deletions model/src/crud/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,23 @@ where
F: Model,
{
pub async fn fetch_page(&self, executor: &mut PgConnection) -> Result<Connection<S>, Error> {
let nodes = self.fetch_all(executor).await?;

self.select.paginate(nodes)
}

pub async fn fetch_all(&self, executor: &mut PgConnection) -> Result<Vec<S>, Error> {
let filters = self.select.build_filters_with_foreign::<F>()?;
let select_clause = self.prepare_select_clause();

let (statement, var_bindings) = self.select.prepare(filters, select_clause.into());
let (statement, var_bindings) = self.select.prepare(filters, select_clause.into())?;

let result = build_query_as::<S>(&statement, var_bindings)
let nodes = build_query_as::<S>(&statement, var_bindings)
.fetch_all(executor)
.await?;

Ok(result)
self.select.paginate(nodes)
}

pub async fn fetch_one(&self, executor: &mut PgConnection) -> Result<S, Error> {
let filters = self.select.build_filters_with_foreign::<F>()?;
let select_clause = self.prepare_select_clause();

let (statement, var_bindings) = self.select.prepare(filters, select_clause.into());
let (statement, var_bindings) = self.select.prepare(filters, select_clause.into())?;

let result = build_query_as::<S>(&statement, var_bindings)
.fetch_one(executor)
Expand All @@ -108,7 +102,7 @@ where
let filters = self.select.build_filters_with_foreign::<F>()?;
let select_clause = self.prepare_select_clause();

let (statement, var_bindings) = self.select.prepare(filters, select_clause.into());
let (statement, var_bindings) = self.select.prepare(filters, select_clause.into())?;

let result = build_query_as::<S>(&statement, var_bindings)
.fetch_optional(executor)
Expand Down
Loading

0 comments on commit 06f6b34

Please sign in to comment.