55 Request ,
66 Transporter ,
77} from './types' ;
8- import { compact , get , isEmpty , isNil , omit , omitBy } from 'lodash' ;
8+ import { compact , get , isEmpty , isNil , omit , omitBy , snakeCase } from 'lodash' ;
99
1010export function buildUrl ( parts : ( number | string ) [ ] ) : string {
1111 return compact ( parts )
@@ -83,6 +83,9 @@ export async function executePull({
8383 deletedField,
8484 transporter,
8585} : OrionPullExecuteOptions ) : Promise < any [ ] > {
86+ const references = extractReferences ( collection . schema ) ;
87+ const keys = Object . keys ( references ) ;
88+
8689 const request = {
8790 url,
8891 wrap,
@@ -96,27 +99,28 @@ export async function executePull({
9699 limit : batchSize ,
97100 ...params ,
98101 } ,
99- } ;
102+ } as any ;
103+
104+ if ( keys . length ) {
105+ request . params . include = keys . join ( ',' ) ;
106+ }
100107
101108 const response = await executeRequest ( transporter , request ) ;
102109
103110 if ( collection && response . length ) {
104- const primaryPath = collection . schema . primaryPath ;
105- const references = extractReferences ( collection . schema ) ;
106-
107- for ( const item of response ) {
108- if ( ! item [ deletedField ] ) {
109- for ( const [ key , value ] of Object . entries ( references ) ) {
110- const rows = await executePull ( {
111- url : `${ url } /${ item [ primaryPath ] } /${ key } ` ,
112- transporter,
113- wrap,
114- batchSize,
115- deletedField,
116- } ) ;
111+ return response ;
112+ }
113+
114+ for ( const item of response ) {
115+ if ( ! item [ deletedField ] ) {
116+ for ( const [ key , value ] of Object . entries ( references ) ) {
117+ const property = snakeCase ( value ) ;
118+ const reference = collection . database . collections [ value ] ;
117119
118- const schema = collection . database . collections [ value ] . schema ;
119- item [ key ] = rows . map ( ( row ) => row [ schema . primaryPath ] ) ;
120+ if ( reference ) {
121+ item [ key ] = item [ property ] . map (
122+ ( row : any ) => row [ reference . schema . primaryPath ]
123+ ) ;
120124 }
121125 }
122126 }
0 commit comments