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<
119119 return super . findOne ( filter , options ) ;
120120 }
121121
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+
122139 async findById (
123140 id : ID ,
124141 filter ?: Filter < T > ,
125142 options ?: Options ,
126143 ) : Promise < T & Relations > {
144+ const pk = this . getPkFieldName ( ) ;
127145 // Filter out soft deleted entries
128146 if (
129147 filter ?. where &&
@@ -132,7 +150,7 @@ export abstract class SoftCrudRepository<
132150 ) {
133151 ( filter . where as AndClause < T > ) . and . push ( {
134152 deleted : false ,
135- id : id ,
153+ [ pk ] : id ,
136154 } as Condition < T > ) ;
137155 } else if (
138156 filter ?. where &&
@@ -143,7 +161,7 @@ export abstract class SoftCrudRepository<
143161 and : [
144162 {
145163 deleted : false ,
146- id : id ,
164+ [ pk ] : id ,
147165 } as Condition < T > ,
148166 {
149167 or : ( filter . where as OrClause < T > ) . or ,
@@ -154,7 +172,7 @@ export abstract class SoftCrudRepository<
154172 filter = filter ?? { } ;
155173 filter . where = {
156174 deleted : false ,
157- id : id ,
175+ [ pk ] : id ,
158176 } as Condition < T > ;
159177 }
160178
You can’t perform that action at this time.
0 commit comments