@@ -2,6 +2,11 @@ import { JsonApiBody, JsonApiResource } from "../types/jsonapi.type";
22import { fromJsonApi , Model , ModelConstructor , ModelInstance } from "./model" ;
33import Schema from "./schema" ;
44
5+ type RawResultType < T > = {
6+ result : T ;
7+ body : JsonApiBody < T extends ModelInstance < unknown > [ ] ? JsonApiResource [ ] : JsonApiResource > ;
8+ }
9+
510type ExtractDocType < T > =
611 T extends Model < infer U > ? U :
712 T extends Model < infer U > [ ] ? U :
@@ -37,6 +42,7 @@ interface QueryOptions<DocType> {
3742 sort ?: SortQuery < DocType > ;
3843 limit ?: number ;
3944 offset ?: number ;
45+ raw ?: boolean ;
4046}
4147
4248
@@ -99,6 +105,10 @@ class Query<ResultType, DocType> {
99105
100106 options ! : QueryOptions < DocType > ;
101107
108+ raw ! : ( ) => this extends Query < infer ResultType , infer ResultDocType >
109+ ? ResultType extends RawResultType < any > ? this : Query < RawResultType < ResultType > , ResultDocType >
110+ : never ;
111+
102112 schema ! : Schema < DocType > ;
103113
104114 setOptions ! : ( options : QueryOptions < DocType > , overwrite ?: boolean ) => this;
@@ -172,8 +182,14 @@ Query.prototype.exec = async function exec() {
172182 }
173183 ) ;
174184
175- return this . model . fromJsonApi ( response . data ) ;
185+ if ( options . raw ) {
186+ return {
187+ result : this . model . fromJsonApi ( response . data ) ,
188+ body : response . data ,
189+ } as RawResultType < ModelInstance < unknown > [ ] > ;
190+ }
176191
192+ return this . model . fromJsonApi ( response . data ) ;
177193 } else if ( options . op === 'findById' ) {
178194 const response = await client . client . get < JsonApiBody < JsonApiResource | null > > (
179195 `/${ this . model . type } /${ options . id } ` ,
@@ -182,6 +198,13 @@ Query.prototype.exec = async function exec() {
182198 }
183199 ) ;
184200
201+ if ( options . raw ) {
202+ return {
203+ result : this . model . fromJsonApi ( response . data ) ,
204+ body : response . data ,
205+ } as RawResultType < ModelInstance < unknown > > ;
206+ }
207+
185208 return this . model . fromJsonApi ( response . data ) ;
186209 } else if ( options . op === 'findRelationship' ) {
187210 const response = await client . client . get < JsonApiBody < JsonApiResource | null > > (
@@ -191,7 +214,14 @@ Query.prototype.exec = async function exec() {
191214 }
192215 ) ;
193216
194- return fromJsonApi ( response . data )
217+ if ( options . raw ) {
218+ return {
219+ result : fromJsonApi ( response . data ) ,
220+ body : response . data ,
221+ } as RawResultType < ModelInstance < unknown > | ModelInstance < unknown > [ ] > ;
222+ }
223+
224+ return fromJsonApi ( response . data ) ;
195225 }
196226
197227 return ;
@@ -266,6 +296,13 @@ Query.prototype.offset = function (offset) {
266296 return this ;
267297} ;
268298
299+ Query . prototype . raw = function ( ) {
300+ this . setOptions ( {
301+ raw : true ,
302+ } ) ;
303+ return this ;
304+ } ;
305+
269306Query . prototype . setOptions = function ( options , overwrite ) {
270307 if ( overwrite ) {
271308 this . options = options ;
0 commit comments