Skip to content

Conversation

@erikaperugachi
Copy link
Contributor

No description provided.

const { metadataKeys, unread } = params;
let badgeCounter = 0;
for (const metadataKey of metadataKeys) {
const [email] = await getEmailByKey(metadataKey);
Copy link
Contributor

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.

Copy link
Contributor Author

@erikaperugachi erikaperugachi Aug 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is promise.all ok?

Copy link
Contributor

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);
Copy link
Contributor

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.

Copy link
Contributor Author

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

return db
.select('*')
.table(Table.EMAIL)
.whereIn('id', emailIds);
Copy link
Contributor

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);
}
Copy link
Contributor

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.

Copy link
Contributor Author

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
Copy link
Contributor

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({
Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested this?

Copy link
Contributor Author

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 = ({
Copy link
Contributor

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.

Copy link
Contributor Author

@erikaperugachi erikaperugachi Aug 23, 2018

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant updateEmail.

Copy link
Contributor Author

@erikaperugachi erikaperugachi Aug 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

separate: updateEmail and updateEmails ?

Copy link
Contributor

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 = ({
Copy link
Contributor

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.

@erikaperugachi erikaperugachi merged commit a6cce89 into Criptext:master Aug 23, 2018
@erikaperugachi erikaperugachi deleted the event branch August 23, 2018 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants