Skip to content
This repository was archived by the owner on Jan 20, 2020. It is now read-only.

Commit 42bb445

Browse files
author
Luciano Nooijen
committed
Delete query improvements
1 parent 866f0a0 commit 42bb445

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

controllers/authors.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ const modifyAuthor = async (id, author) => {
4040
return modifiedAuthor[0];
4141
};
4242

43-
const deleteAuthor = async id => {
44-
return new Promise(resolve => {
43+
const deleteAuthor = async id =>
44+
new Promise(resolve => {
4545
knex('users')
4646
.where({ author_id: id })
4747
.update({ author_id: null }) // foreign key
@@ -53,7 +53,6 @@ const deleteAuthor = async id => {
5353
.then(data => resolve(data[0])),
5454
); // eslint-disable-line
5555
});
56-
};
5756

5857
module.exports = {
5958
listAuthors,

controllers/users.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ const modifyUser = async (id, user) => {
7474
};
7575

7676
const deleteUser = async id =>
77-
knex('users')
78-
.returning(['id'])
79-
.where({ id })
80-
.delete();
77+
new Promise(resolve =>
78+
knex('users')
79+
.returning(['id'])
80+
.where({ id })
81+
.delete()
82+
.then(data => resolve(data[0])),
83+
); // eslint-disable-line
8184

8285
module.exports = {
8386
listUsers,

tests/database/users.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ describe('Test if Users CRUD operations are working correctly', () => {
8383

8484
test('Deleting an User should return undefined', async () => {
8585
expect.assertions(2);
86-
return deleteUser(1)
87-
.then(data => expect(data.id).toBe(undefined))
88-
.then(async () => expect(await getUser(1)).toBeUndefined());
86+
return deleteUser(2)
87+
.then(data => expect(data.id).toBe(2))
88+
.then(async () => expect(await getUser(2)).toBeUndefined());
8989
});
9090
});

0 commit comments

Comments
 (0)