Skip to content
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

feat: init db wrapper #7

Closed
wants to merge 34 commits into from
Closed

feat: init db wrapper #7

wants to merge 34 commits into from

Conversation

alinarublea
Copy link
Contributor

@alinarublea alinarublea commented Oct 19, 2023

Please ensure your pull request adheres to the following guidelines:

  • make sure to link the related issues in this description
  • when merging / squashing, make sure the fixed issue references are visible in the commits, for easy compilation of release notes
  • fix unit tests for psi client
  • check if destroy db and sqs has no side effects and mitigate if there are any

Related Issues

Thanks for contributing!

@github-actions
Copy link

This PR will trigger a minor release when merged.

@alinarublea alinarublea marked this pull request as ready for review October 19, 2023 09:38
Copy link

@tripodsan tripodsan left a comment

Choose a reason for hiding this comment

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

I think you start adding unit tests early and keep the coverage by 100%. otherwise it will be very hard to add them later.

src/db-wrapper.js Outdated Show resolved Hide resolved
src/db-wrapper.js Outdated Show resolved Hide resolved
src/db.js Outdated Show resolved Hide resolved
src/db.js Outdated Show resolved Hide resolved
src/service-wrap.js Outdated Show resolved Hide resolved
@dzehnder dzehnder mentioned this pull request Oct 20, 2023
2 tasks
const body = {
message,
timestamp: new Date().toISOString(),
};

// Set up the parameters for the send message command
const params = {
DelaySeconds: 10,
Copy link
Member

Choose a reason for hiding this comment

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

what is this delay for?

import queueWrapper from '../src/queue-wrapper.js';
import SQSQueue from '../src/sqs-queue.js';

describe('Queue Wrapper Tests', () => {
Copy link
Member

Choose a reason for hiding this comment

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

💯

@ekremney
Copy link
Member

@alinarublea please also add tests for the other newly added parts

break;
}
};
'use strict';

Choose a reason for hiding this comment

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

not needed. ESM is always strict.

Suggested change
'use strict';

Comment on lines +19 to +21
if (!context.db) {
context.db = new DB(context);
}

Choose a reason for hiding this comment

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

  • what is the benefit of having this in a wrapper?
  • as mentioned earlier, do not cache directly in context, but in context.attributes
  • do you need to cleanup/close/discard the DB after use?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tripodsan we were following the pattern from storage which is included directly in the context https://github.com/adobe/helix-universal/blob/7a63f8ddc179d5f91c1c170b40bf4bad4a17e769/src/aws-adapter.js#L158, and it s not an attribute. Sure we will cleanup after use.

src/db.js Outdated
return newAudit;
await this.saveRecord(newAudit, TABLE_AUDITS);
this.log.info(`Audit for domain ${site.domain} saved successfully`);
return Promise.resolve(newAudit);
Copy link

@tripodsan tripodsan Oct 26, 2023

Choose a reason for hiding this comment

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

the return type of a async function is automatically a promise.

Suggested change
return Promise.resolve(newAudit);
return newAudit;

src/db.js Outdated
* Saves an audit to the DynamoDB.
* @param {object} site - Site object containing details of the audited site.
* @param {object} audit - Audit object containing the type and result of the audit.
* @returns {Promise<void>} Resolves once audit is saved.

Choose a reason for hiding this comment

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

not true.... returns the new adit object

const now = new Date().toISOString();
const uuid = Date.now().toString();
const uuid = Date.now()
.toString();

const newAudit = {

Choose a reason for hiding this comment

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

maybe it would help to include a typedef for audit and site

either as jsdoc comment @typedef Audit ... or as typescript type

src/index.js Outdated
Comment on lines 51 to 59
if (!message.domain) {
return new Response('', {
status: 400,
headers: {
'x-error': 'Event message does not contain a domain',
},
});
}

Choose a reason for hiding this comment

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

move this up. cheap checks should come first new object allocation.

@@ -10,7 +10,6 @@
* governing permissions and limitations under the License.
*/
import axios from 'axios';

Choose a reason for hiding this comment

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

would be cool to use @adobe/fetch

@@ -136,6 +135,7 @@ function PSIClient(config) {
* @returns {Promise<object>} The processed PageSpeed Insights audit data.
*/
const performPSICheck = async (domain, strategy) => {
// eslint-disable-next-line no-useless-catch

Choose a reason for hiding this comment

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

why not follow the eslint advice here ? :-)

src/queue-wrapper.js Outdated Show resolved Hide resolved
Comment on lines +27 to +29
if (!context.queue) {
context.queue = new SqsQueue(context);
}

Choose a reason for hiding this comment

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

also use context.attributes

context.queue = new SqsQueue(context);
}

return func(request, context);

Choose a reason for hiding this comment

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

probably close/discard queue client ?

@@ -22,11 +21,11 @@ function storage(config) {
ContentType: 'application/json',
};

// eslint-disable-next-line no-useless-catch

Choose a reason for hiding this comment

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

why not follow eslint advice?

@solaris007 solaris007 closed this Dec 5, 2023
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.

5 participants