@@ -12,6 +12,7 @@ import { lowerCase } from 'lodash';
12
12
import { AbstractClass } from 'type-fest' ;
13
13
import {
14
14
EnhancedResource ,
15
+ DBType as GetDBType ,
15
16
ID ,
16
17
isSortablePaginationInput ,
17
18
NotFoundException ,
@@ -41,22 +42,21 @@ import { $, e } from './reexports';
41
42
*/
42
43
export const RepoFor = <
43
44
TResourceStatic extends ResourceShape < any > ,
44
- DBType extends ( TResourceStatic [ 'DB' ] & { } ) [ '__element__' ] ,
45
- HydratedShape extends objectTypeToSelectShape < DBType > = ( TResourceStatic [ 'DB' ] & { } ) [ '*' ] ,
45
+ DBPathNode extends GetDBType < TResourceStatic > ,
46
+ DBType extends DBPathNode [ '__element__' ] ,
47
+ HydratedShape extends objectTypeToSelectShape < DBType > ,
46
48
> (
47
49
resourceIn : TResourceStatic ,
48
- options : {
49
- hydrate ?: ShapeFn < $ . TypeSet < DBType > , HydratedShape > ;
50
- } = { } ,
50
+ {
51
+ hydrate,
52
+ } : {
53
+ hydrate : ShapeFn < $ . TypeSet < DBType > , HydratedShape > ;
54
+ } ,
51
55
) => {
52
56
type Dto = $ . computeObjectShape < DBType [ '__pointers__' ] , HydratedShape > ;
53
57
54
58
const resource = EnhancedResource . of ( resourceIn ) ;
55
-
56
- const hydrate = e . shape (
57
- resource . db ,
58
- ( options . hydrate ?? ( ( obj : any ) => obj [ '*' ] ) ) as any ,
59
- ) as ( scope : unknown ) => HydratedShape ;
59
+ const dbType = resource . db as any as $ . $expr_PathNode ;
60
60
61
61
@Injectable ( )
62
62
abstract class Repository extends CommonRepository {
@@ -125,7 +125,7 @@ export const RepoFor = <
125
125
offset : ( input . page - 1 ) * input . count ,
126
126
limit : input . count + 1 ,
127
127
} ) ) ;
128
- const items = e . select ( thisPage , ( obj ) => ( {
128
+ const items = e . select ( thisPage , ( obj : any ) => ( {
129
129
...this . hydrate ( obj ) ,
130
130
limit : input . count ,
131
131
} ) ) ;
@@ -179,8 +179,8 @@ export const RepoFor = <
179
179
180
180
async readMany ( ids : readonly ID [ ] ) : Promise < readonly Dto [ ] > {
181
181
const query = e . params ( { ids : e . array ( e . uuid ) } , ( { ids } ) =>
182
- e . select ( this . resource . db , ( obj : any ) => ( {
183
- ...( this . hydrate ( obj ) as any ) ,
182
+ e . select ( dbType , ( obj : any ) => ( {
183
+ ...this . hydrate ( obj ) ,
184
184
filter : e . op ( obj . id , 'in' , e . array_unpack ( ids ) ) ,
185
185
} ) ) ,
186
186
) ;
@@ -189,7 +189,7 @@ export const RepoFor = <
189
189
}
190
190
191
191
async list ( input : PaginationInput ) {
192
- const all = e . select ( this . resource . db , ( obj : any ) => {
192
+ const all = e . select ( dbType , ( obj : any ) => {
193
193
const filters = many ( this . listFilters ( obj , input ) ) . filter ( isNotFalsy ) ;
194
194
const filter =
195
195
filters . length === 0
@@ -209,15 +209,15 @@ export const RepoFor = <
209
209
210
210
async create ( input : Omit < InsertShape < DBType > , `@${string } `> ) : Promise < Dto > {
211
211
const query = e . select (
212
- e . insert ( this . resource . db , input as any ) ,
212
+ ( e . insert as any ) ( dbType , input ) ,
213
213
this . hydrate as any ,
214
214
) ;
215
215
return ( await this . db . run ( query ) ) as Dto ;
216
216
}
217
217
218
218
async update (
219
219
existing : Pick < Dto , 'id' > ,
220
- input : UpdateShape < TResourceStatic [ 'DB' ] & { } > ,
220
+ input : UpdateShape < DBPathNode > ,
221
221
) : Promise < Dto > {
222
222
const object = e . cast (
223
223
this . resource . db ,
0 commit comments