Skip to content

Commit 142179c

Browse files
authored
fix(repository): add deleted property in fields filter (#92)
GH-91
1 parent 7fea742 commit 142179c

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

.husky/commit-msg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4-
npx --no-install commitlint --edit
4+
npx --no-install commitlint --edit

.vscode/launch.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"configurations": [
3+
{
4+
"type": "node",
5+
"request": "launch",
6+
"name": "Run Mocha tests",
7+
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
8+
"runtimeArgs": [
9+
"-r",
10+
"${workspaceRoot}/node_modules/source-map-support/register"
11+
],
12+
"cwd": "${workspaceRoot}",
13+
"autoAttachChildProcesses": true,
14+
"args": [
15+
"--config",
16+
"${workspaceRoot}/.mocharc.json",
17+
"${workspaceRoot}/dist/__tests__/**/*.js",
18+
"-t",
19+
"0"
20+
]
21+
}
22+
]
23+
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/unit/repository/soft-crud.repository.unit.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,38 @@ describe('SoftCrudRepository', () => {
288288
expect(e.message).to.be.equal('EntityNotFound');
289289
}
290290
});
291+
it('should not return soft deleted entry by id, without using deleted in fields filter', async () => {
292+
try {
293+
await repo.findById(3, {
294+
fields: {
295+
id: true,
296+
email: true,
297+
},
298+
});
299+
fail();
300+
} catch (e) {
301+
expect(e.message).to.be.equal('EntityNotFound');
302+
}
303+
});
304+
it('should return requested fields only when not using deleted in fields filter', async () => {
305+
const customer = await repo.findById(4, {
306+
fields: {
307+
id: true,
308+
email: true,
309+
},
310+
});
311+
expect(customer).to.not.have.property('deleted');
312+
});
313+
it('should return requested fields matched with fields filter', async () => {
314+
const customer = await repo.findById(4, {
315+
fields: {
316+
id: true,
317+
email: true,
318+
deleted: true,
319+
},
320+
});
321+
expect(customer).to.have.property('deleted');
322+
});
291323
});
292324

293325
describe('findByIdIncludeSoftDelete', () => {

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '@loopback/repository';
1313
import {Count} from '@loopback/repository/src/common-types';
1414
import {HttpErrors} from '@loopback/rest';
15-
import {Options} from 'loopback-datasource-juggler';
15+
import {AnyObject, Options} from 'loopback-datasource-juggler';
1616
import {IAuthUser} from 'loopback4-authentication';
1717

1818
import {ErrorKeys} from '../error-keys';
@@ -175,8 +175,19 @@ export abstract class SoftCrudRepository<
175175
[pk]: id,
176176
} as Condition<T>;
177177
}
178-
const entity = await super.findById(id, filter, options);
178+
const finalFilter: Filter<T> = {};
179+
Object.assign(filter, finalFilter);
180+
if (finalFilter.fields) {
181+
finalFilter.fields = {
182+
...finalFilter.fields,
183+
deleted: true,
184+
};
185+
}
186+
const entity = await super.findById(id, finalFilter, options);
179187
if (entity && !entity.deleted) {
188+
if (filter.fields && !(filter.fields as AnyObject).deleted) {
189+
delete entity.deleted;
190+
}
180191
return entity;
181192
} else {
182193
throw new HttpErrors.NotFound(ErrorKeys.EntityNotFound);

0 commit comments

Comments
 (0)