@@ -9,6 +9,9 @@ import { JsonSerializer } from "../../Mapping/Json/Serializer";
99import * as stream from "readable-stream" ;
1010import { RavenCommandResponsePipeline } from "../../Http/RavenCommandResponsePipeline" ;
1111import { StringBuilder } from "../../Utility/StringBuilder" ;
12+ import { ServerResponse } from "../../Types" ;
13+ import { QueryTimings } from "../Queries/Timings/QueryTimings" ;
14+ import { StringUtil } from "../../Utility/StringUtil" ;
1215
1316export interface QueryCommandOptions {
1417 metadataOnly ?: boolean ;
@@ -102,19 +105,13 @@ export class QueryCommand extends RavenCommand<QueryResult> {
102105 fromCache : boolean ,
103106 bodyCallback ?: ( body : string ) => void ) : Promise < QueryResult > {
104107
105- const rawResult = await RavenCommandResponsePipeline . create < QueryResult > ( )
108+ const rawResult = await RavenCommandResponsePipeline . create < ServerResponse < QueryResult > > ( )
106109 . collectBody ( bodyCallback )
107110 . parseJsonAsync ( )
108111 . jsonKeysTransform ( "DocumentQuery" , conventions )
109112 . process ( bodyStream ) ;
110- const queryResult = conventions . objectMapper
111- . fromObjectLiteral < QueryResult > ( rawResult , {
112- typeName : QueryResult . name ,
113- nestedTypes : {
114- indexTimestamp : "date" ,
115- lastQueryTime : "date"
116- }
117- } , new Map ( [ [ QueryResult . name , QueryResult ] ] ) ) ;
113+
114+ const queryResult = QueryCommand . _mapToLocalObject ( rawResult , conventions ) ;
118115
119116 if ( fromCache ) {
120117 queryResult . durationInMs = - 1 ;
@@ -127,4 +124,32 @@ export class QueryCommand extends RavenCommand<QueryResult> {
127124
128125 return queryResult ;
129126 }
127+
128+ private static _mapToLocalObject ( json : ServerResponse < QueryResult > , conventions : DocumentConventions ) : QueryResult {
129+ const { indexTimestamp, lastQueryTime, timings, ...otherProps } = json ;
130+
131+ const overrides : Partial < QueryResult > = {
132+ indexTimestamp : conventions . dateUtil . parse ( indexTimestamp ) ,
133+ lastQueryTime : conventions . dateUtil . parse ( lastQueryTime ) ,
134+ timings : QueryCommand . _mapTimingsToLocalObject ( timings )
135+ } ;
136+
137+ return Object . assign ( new QueryResult ( ) , otherProps , overrides ) ;
138+ }
139+
140+ private static _mapTimingsToLocalObject ( timings : ServerResponse < QueryTimings > ) {
141+ if ( ! timings ) {
142+ return undefined ;
143+ }
144+
145+ const mapped = new QueryTimings ( ) ;
146+ mapped . durationInMs = timings . durationInMs ;
147+ mapped . timings = timings . timings ? { } : undefined ;
148+ if ( timings . timings ) {
149+ Object . keys ( timings . timings ) . forEach ( time => {
150+ mapped . timings [ StringUtil . uncapitalize ( time ) ] = QueryCommand . _mapTimingsToLocalObject ( timings . timings [ time ] ) ;
151+ } ) ;
152+ }
153+ return mapped ;
154+ }
130155}
0 commit comments