File tree Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Expand file tree Collapse file tree 1 file changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -119,11 +119,29 @@ export abstract class SoftCrudRepository<
119
119
return super . findOne ( filter , options ) ;
120
120
}
121
121
122
+ //To find the primary key/Unique Id field name
123
+ getPkFieldName ( ) {
124
+ const data = this . entityClass . definition . properties ;
125
+ let pk = 'id' ;
126
+ for ( const key in data ) {
127
+ // eslint-disable-next-line no-prototype-builtins
128
+ if ( data . hasOwnProperty ( key ) ) {
129
+ const value = data [ key ] ;
130
+ if ( value . id ) {
131
+ pk = key ;
132
+ break ;
133
+ }
134
+ }
135
+ }
136
+ return pk ;
137
+ }
138
+
122
139
async findById (
123
140
id : ID ,
124
141
filter ?: Filter < T > ,
125
142
options ?: Options ,
126
143
) : Promise < T & Relations > {
144
+ const pk = this . getPkFieldName ( ) ;
127
145
// Filter out soft deleted entries
128
146
if (
129
147
filter ?. where &&
@@ -132,7 +150,7 @@ export abstract class SoftCrudRepository<
132
150
) {
133
151
( filter . where as AndClause < T > ) . and . push ( {
134
152
deleted : false ,
135
- id : id ,
153
+ [ pk ] : id ,
136
154
} as Condition < T > ) ;
137
155
} else if (
138
156
filter ?. where &&
@@ -143,7 +161,7 @@ export abstract class SoftCrudRepository<
143
161
and : [
144
162
{
145
163
deleted : false ,
146
- id : id ,
164
+ [ pk ] : id ,
147
165
} as Condition < T > ,
148
166
{
149
167
or : ( filter . where as OrClause < T > ) . or ,
@@ -154,7 +172,7 @@ export abstract class SoftCrudRepository<
154
172
filter = filter ?? { } ;
155
173
filter . where = {
156
174
deleted : false ,
157
- id : id ,
175
+ [ pk ] : id ,
158
176
} as Condition < T > ;
159
177
}
160
178
You can’t perform that action at this time.
0 commit comments