@@ -3,6 +3,7 @@ use indicatif::MultiProgress;
33use itertools:: Itertools ;
44use regex:: Regex ;
55use rust_bridge:: { alias, alias_methods} ;
6+ use sea_query:: Alias ;
67use sea_query:: { Expr , NullOrdering , Order , PostgresQueryBuilder , Query } ;
78use sea_query_binder:: SqlxBinder ;
89use serde_json:: json;
@@ -656,8 +657,9 @@ impl Collection {
656657 /// Each object must have a `field` key with the name of the field to order by, and a `direction`
657658 /// key with the value `asc` or `desc`.
658659 /// * `last_row_id` - The id of the last document returned
659- /// * `offset` - The number of documents to skip before returning results.
660- /// * `filter` - A JSON object specifying the filter to apply to the documents.
660+ /// * `offset` - The number of documents to skip before returning results
661+ /// * `filter` - A JSON object specifying the filter to apply to the documents
662+ /// * `keys` - a JSON array specifying the document keys to return
661663 ///
662664 /// # Example
663665 ///
@@ -691,9 +693,33 @@ impl Collection {
691693 self . documents_table_name . to_table_tuple ( ) ,
692694 SIden :: Str ( "documents" ) ,
693695 )
694- . expr ( Expr :: cust ( "*" ) ) // Adds the * in SELECT * FROM
696+ . columns ( [
697+ SIden :: Str ( "id" ) ,
698+ SIden :: Str ( "created_at" ) ,
699+ SIden :: Str ( "source_uuid" ) ,
700+ SIden :: Str ( "version" ) ,
701+ ] )
695702 . limit ( limit) ;
696703
704+ if let Some ( keys) = args. remove ( "keys" ) {
705+ let document_queries = keys
706+ . as_array ( )
707+ . context ( "`keys` must be an array" ) ?
708+ . iter ( )
709+ . map ( |d| {
710+ let key = d. as_str ( ) . context ( "`key` value must be a string" ) ?;
711+ anyhow:: Ok ( format ! ( "'{key}', document #> '{{{key}}}'" ) )
712+ } )
713+ . collect :: < anyhow:: Result < Vec < String > > > ( ) ?
714+ . join ( "," ) ;
715+ query. expr_as (
716+ Expr :: cust ( format ! ( "jsonb_build_object({document_queries})" ) ) ,
717+ Alias :: new ( "document" ) ,
718+ ) ;
719+ } else {
720+ query. column ( SIden :: Str ( "document" ) ) ;
721+ }
722+
697723 if let Some ( order_by) = args. remove ( "order_by" ) {
698724 let order_by_builder =
699725 order_by_builder:: OrderByBuilder :: new ( order_by, "documents" , "document" ) . build ( ) ?;
0 commit comments