@@ -20,7 +20,9 @@ export class Resource extends BaseResource {
20
20
21
21
private manager : ModelManager ;
22
22
23
- private propertiesObject : Record < string , any > ;
23
+ private propertiesObject : Record < string , Property > ;
24
+
25
+ private idProperty : Property ;
24
26
25
27
constructor ( args : {
26
28
model : DMMF . Model ;
@@ -35,6 +37,7 @@ export class Resource extends BaseResource {
35
37
this . enums = getEnums ( clientModule ) ;
36
38
this . manager = this . client [ lowerCase ( model . name ) ] ;
37
39
this . propertiesObject = this . prepareProperties ( ) ;
40
+ this . idProperty = this . properties ( ) . find ( ( p ) => p . isId ( ) ) ! ;
38
41
}
39
42
40
43
public databaseName ( ) : string {
@@ -69,15 +72,18 @@ export class Resource extends BaseResource {
69
72
70
73
public async find (
71
74
filter : Filter ,
72
- params : Record < string , any > = { } ,
75
+ params : {
76
+ limit ?: number ;
77
+ offset ?: number ;
78
+ sort ?: {
79
+ sortBy ?: string ;
80
+ direction ?: 'asc' | 'desc' ;
81
+ } ;
82
+ } = { } ,
73
83
) : Promise < Array < BaseRecord > > {
74
84
const { limit = 10 , offset = 0 , sort = { } } = params ;
75
- const { direction, sortBy } = sort as {
76
- direction : 'asc' | 'desc' ;
77
- sortBy : string ;
78
- } ;
79
85
80
- const orderBy = this . buildSortBy ( sortBy , direction ) ;
86
+ const orderBy = this . buildSortBy ( sort ) ;
81
87
const results = await this . manager . findMany ( {
82
88
where : convertFilter ( this . model . fields , filter ) ,
83
89
skip : offset ,
@@ -90,7 +96,12 @@ export class Resource extends BaseResource {
90
96
) ;
91
97
}
92
98
93
- private buildSortBy ( path : string , direction : 'asc' | 'desc' ) {
99
+ private buildSortBy ( sort : { sortBy ?: string ; direction ?: 'asc' | 'desc' } = { } ) {
100
+ let { sortBy : path } = sort ;
101
+ const { direction = 'desc' } = sort ;
102
+
103
+ if ( ! path ) path = this . idProperty . path ( ) ;
104
+
94
105
const [ basePath , sortBy ] = path . split ( '.' ) ;
95
106
const sortByProperty = this . property ( basePath ) ;
96
107
@@ -108,7 +119,7 @@ export class Resource extends BaseResource {
108
119
109
120
return {
110
121
[ basePath ] : direction ,
111
- }
122
+ } ;
112
123
}
113
124
114
125
public async findOne ( id : string | number ) : Promise < BaseRecord | null > {
0 commit comments