-
Notifications
You must be signed in to change notification settings - Fork 23
Fix unread sync #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix unread sync #369
Conversation
| const { metadataKeys, unread } = params; | ||
| let badgeCounter = 0; | ||
| for (const metadataKey of metadataKeys) { | ||
| const [email] = await getEmailByKey(metadataKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
running a query inside a loop is a bad practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is promise.all ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. load data in batches.
| return db.transaction(async trx => { | ||
| await deleteEmailsByIds([id], trx); | ||
| await deleteEmailContactByEmailId(id, trx); | ||
| await deleteEmailLabelsByEmailId(id, trx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you use cascade triggers there is no need for you to delete relations of emails you already deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pending in other pull request, to handle this. I just change the position
electron_app/src/DBManager.js
Outdated
| return db | ||
| .select('*') | ||
| .table(Table.EMAIL) | ||
| .whereIn('id', emailIds); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't it be better to use a join instead of two queries?
| const emailsId = emailLabels.map(item => item.emailId); | ||
| if (emailsId.length) { | ||
| await deleteEmailLabel({ emailsId, labelId: id }, trx); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, triggers should handle this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pending in other pull request, to handle this. I just change the position
| for (const email of emails) { | ||
| await updateEmail({ | ||
| key: email.key, | ||
| unread: !!unread |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update should also work in batches.
|
|
||
| it('should update email: unread by keys', async () => { | ||
| const keys = ['keyC', 'keyId']; | ||
| await DBManager.updateEmail({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this. You are updating emails that are used by other tests, this could affect them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This query has a test too and I add other test additionally! All passes ok
| `${Table.EMAIL}.id`, | ||
| `${Table.EMAIL_LABEL}.emailId` | ||
| ) | ||
| .whereIn(`${Table.EMAIL_LABEL}.labelId`, labelIds); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tested this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! The test is ok!
| .whereIn('id', emailIds); | ||
| }; | ||
|
|
||
| const updateEmail = ({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this method is so complex, it does many different things depending on the input parameters. I suggest you split it into smaller, simpler functions that do one thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a simple join. And it was tested
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant updateEmail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
separate: updateEmail and updateEmails ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might be an improvement, I'll let you decide.
| .whereIn('id', emailIds); | ||
| }; | ||
|
|
||
| const updateEmail = ({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might be an improvement, I'll let you decide.
No description provided.