Add into_model
& into_json
for Cursor
(#1112)
This commit is contained in:
parent
18215871ba
commit
e76cbb9fe1
@ -8,6 +8,9 @@ use sea_query::{
|
|||||||
};
|
};
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
#[cfg(feature = "with-json")]
|
||||||
|
use crate::JsonValue;
|
||||||
|
|
||||||
/// Cursor pagination
|
/// Cursor pagination
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Cursor<S>
|
pub struct Cursor<S>
|
||||||
@ -140,6 +143,32 @@ where
|
|||||||
}
|
}
|
||||||
Ok(buffer)
|
Ok(buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Construct a [Cursor] that fetch any custom struct
|
||||||
|
pub fn into_model<M>(self) -> Cursor<SelectModel<M>>
|
||||||
|
where
|
||||||
|
M: FromQueryResult,
|
||||||
|
{
|
||||||
|
Cursor {
|
||||||
|
query: self.query,
|
||||||
|
table: self.table,
|
||||||
|
order_columns: self.order_columns,
|
||||||
|
last: self.last,
|
||||||
|
phantom: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Construct a [Cursor] that fetch JSON value
|
||||||
|
#[cfg(feature = "with-json")]
|
||||||
|
pub fn into_json(self) -> Cursor<SelectModel<JsonValue>> {
|
||||||
|
Cursor {
|
||||||
|
query: self.query,
|
||||||
|
table: self.table,
|
||||||
|
order_columns: self.order_columns,
|
||||||
|
last: self.last,
|
||||||
|
phantom: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S> QueryOrder for Cursor<S>
|
impl<S> QueryOrder for Cursor<S>
|
||||||
|
@ -2,7 +2,8 @@ pub mod common;
|
|||||||
|
|
||||||
pub use common::{features::*, setup::*, TestContext};
|
pub use common::{features::*, setup::*, TestContext};
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::{entity::prelude::*, FromQueryResult};
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
#[sea_orm_macros::test]
|
#[sea_orm_macros::test]
|
||||||
#[cfg(any(
|
#[cfg(any(
|
||||||
@ -199,5 +200,38 @@ pub async fn cursor_pagination(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
vec![Model { id: 6 }, Model { id: 7 }]
|
vec![Model { id: 6 }, Model { id: 7 }]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Fetch custom struct
|
||||||
|
|
||||||
|
#[derive(FromQueryResult, Debug, PartialEq)]
|
||||||
|
struct Row {
|
||||||
|
id: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut cursor = cursor.into_model::<Row>();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
cursor.first(2).all(db).await?,
|
||||||
|
vec![Row { id: 6 }, Row { id: 7 }]
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
cursor.first(3).all(db).await?,
|
||||||
|
vec![Row { id: 6 }, Row { id: 7 }]
|
||||||
|
);
|
||||||
|
|
||||||
|
// Fetch JSON value
|
||||||
|
|
||||||
|
let mut cursor = cursor.into_json();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
cursor.first(2).all(db).await?,
|
||||||
|
vec![json!({ "id": 6 }), json!({ "id": 7 })]
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
cursor.first(3).all(db).await?,
|
||||||
|
vec![json!({ "id": 6 }), json!({ "id": 7 })]
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user