Skip to content

Commit 277142d

Browse files
faizan1620akshatdubeysfbarleendhaliwal
authored
fix(repository): fix(repository): fixed entitytoremove=null with mongodb (#48)
Co-authored-by: akshatdubeysf <77672713+akshatdubeysf@users.noreply.github.com> Co-authored-by: barleendhaliwal <85155575+barleendhaliwal@users.noreply.github.com>
1 parent 4cf6b9c commit 277142d

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/repositories/soft-crud.repository.base.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)