11import { Inject } from '@nestjs/common' ;
22import {
3+ ObjectTyped ,
34 QueryField ,
45 ResourceObject ,
56 ResourceObjectRelationships ,
7+ RelationKeys
68} from '@klerick/json-api-nestjs-shared' ;
79import {
810 JsonApiTransformerService ,
@@ -27,7 +29,6 @@ import {
2729 postRelationship ,
2830} from '../orm-methods' ;
2931import { MicroOrmUtilService } from './micro-orm-util.service' ;
30- import { RelationKeys } from '@klerick/json-api-nestjs-shared' ;
3132
3233export class MicroOrmService < E extends object , IdKey extends string = 'id' >
3334 implements OrmService < E , IdKey >
@@ -41,14 +42,29 @@ export class MicroOrmService<E extends object, IdKey extends string = 'id'>
4142
4243 async getAll (
4344 query : Query < E , IdKey >
44- ) : Promise < ResourceObject < E , 'array' , null , IdKey > > {
45+ ) : Promise < ResourceObject < E , 'array' , null , IdKey > > ;
46+ async getAll (
47+ query : Query < E , IdKey > ,
48+ transformData ?: boolean ,
49+ additionalQueryParams ?: Record < string , unknown >
50+ ) : Promise < ResourceObject < E , 'array' , null , IdKey > > ;
51+ async getAll (
52+ query : Query < E , IdKey > ,
53+ transformData = true ,
54+ additionalQueryParams ?: Record < string , unknown >
55+ ) : Promise <
56+ ResourceObject < E , 'array' , null , IdKey > | { totalItems : number ; items : E [ ] }
57+ > {
4558 const { page } = query ;
4659 const { totalItems, items } = await getAll . call <
4760 MicroOrmService < E , IdKey > ,
4861 Parameters < typeof getAll < E , IdKey > > ,
4962 ReturnType < typeof getAll < E , IdKey > >
50- > ( this , query ) ;
63+ > ( this , query , additionalQueryParams ) ;
5164
65+ if ( ! transformData ) {
66+ return { totalItems, items } ;
67+ }
5268 const { data, included } = this . jsonApiTransformerService . transformData (
5369 items ,
5470 query
@@ -70,12 +86,29 @@ export class MicroOrmService<E extends object, IdKey extends string = 'id'>
7086 async getOne (
7187 id : number | string ,
7288 query : QueryOne < E , IdKey >
73- ) : Promise < ResourceObject < E , 'object' , null , IdKey > > {
89+ ) : Promise < ResourceObject < E , 'object' , null , IdKey > > ;
90+ async getOne (
91+ id : number | string ,
92+ query : QueryOne < E , IdKey > ,
93+ transformData ?: boolean ,
94+ additionalQueryParams ?: Record < string , unknown >
95+ ) : Promise < ResourceObject < E , 'object' , null , IdKey > | E > ;
96+ async getOne (
97+ id : number | string ,
98+ query : QueryOne < E , IdKey > ,
99+ transformData = true ,
100+ additionalQueryParams ?: Record < string , unknown >
101+ ) : Promise < ResourceObject < E , 'object' , null , IdKey > | E > {
74102 const result = await getOne . call <
75103 MicroOrmService < E , IdKey > ,
76104 Parameters < typeof getOne < E , IdKey > > ,
77105 ReturnType < typeof getOne < E , IdKey > >
78- > ( this , id , query ) ;
106+ > ( this , id , query , additionalQueryParams ) ;
107+
108+ if ( ! transformData ) {
109+ return result ;
110+ }
111+
79112 const { data, included } = this . jsonApiTransformerService . transformData (
80113 result ,
81114 query
@@ -226,4 +259,23 @@ export class MicroOrmService<E extends object, IdKey extends string = 'id'>
226259 data : this . jsonApiTransformerService . transformRel ( result , rel ) ,
227260 } ;
228261 }
262+
263+ async loadRelations (
264+ relationships : PatchData < E , IdKey > [ 'relationships' ] | PostData < E , IdKey > [ 'relationships' ]
265+ ) : Promise < {
266+ [ K in RelationKeys < E > ] : E [ K ] ;
267+ } > {
268+ const result = { } as { [ K in RelationKeys < E > ] : E [ K ] ; } ;
269+
270+ for await ( const item of this . microOrmUtilService . asyncIterateFindRelationships (
271+ relationships as any
272+ ) ) {
273+ const itemProps = ObjectTyped . entries ( item ) . at ( 0 ) ;
274+ if ( ! itemProps ) continue ;
275+ const [ nameProps , data ] = itemProps ;
276+ Reflect . set ( result , nameProps , data ) ;
277+ }
278+
279+ return result ;
280+ }
229281}
0 commit comments