From 057be2d4a2e24a1cae07710bb3a948d55fa90872 Mon Sep 17 00:00:00 2001 From: alinarublea Date: Thu, 19 Oct 2023 11:14:27 +0200 Subject: [PATCH] feat: init db wrapper --- src/db-wrapper.js | 24 ++++++++++++++++++++++++ src/db.js | 21 +++++++++++++-------- src/index.js | 12 +++++------- src/service-wrap.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 src/db-wrapper.js create mode 100644 src/service-wrap.js diff --git a/src/db-wrapper.js b/src/db-wrapper.js new file mode 100644 index 00000000..65f4e9ba --- /dev/null +++ b/src/db-wrapper.js @@ -0,0 +1,24 @@ +/* + * Copyright 2023 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +import createDynamoDBService from './db.js'; +import serviceWrap from './service-wrap.js'; + +function wrapper(func) { + return (params) => serviceWrap( + func, + params, + '__ow_dynamodb', + createDynamoDBService, + ); +} +const dynamoDBWrapper = wrapper; +export default { dynamoDBWrapper }; diff --git a/src/db.js b/src/db.js index d1f43b3d..a189f7eb 100644 --- a/src/db.js +++ b/src/db.js @@ -16,8 +16,8 @@ import { log } from './util.js'; const TABLE_SITES = 'spacecat-site'; const TABLE_AUDITS = 'spacecat-audit-index'; -function DB(config) { - const client = new DynamoDBClient({ region: config.region }); +function DB(params) { + const client = new DynamoDBClient({ region: params.region }); const docClient = DynamoDBDocumentClient.from(client); /** @@ -48,10 +48,10 @@ function DB(config) { const newAudit = { id: uuid, - siteId: site.id, + siteId: `${site.domain}/${site.path}`, audit_date: now, type: 'psi', - is_live: site.isLive, + is_live: false, content_publication_date: '', git_hashes: [], tag_manager: '', @@ -100,11 +100,12 @@ function DB(config) { } /** * Fetches a site by its ID and gets its latest audit. - * @param {string} siteId - The ID of the site to fetch. + * @param {string} domain - The domain of the site to fetch. + * @param {string} path - The path of the site to fetch. * @returns {Promise} Site document with its latest audit. */ async function getSite(domain, path) { - const params = { + const commandParams = { TableName: TABLE_SITES, // Replace with your table name Key: { Domain: { S: domain }, // Partition key @@ -113,7 +114,7 @@ function DB(config) { }; try { - const command = new GetItemCommand(params); + const command = new GetItemCommand(commandParams); const response = await client.send(command); const item = response.Item; if (item) { @@ -135,4 +136,8 @@ function DB(config) { }; } -export default DB; +const createDynamoDBService = (params) => Object.freeze({ + getInstance: () => DB(params), +}); + +export default { createDynamoDBService }; diff --git a/src/index.js b/src/index.js index d0c80753..ab7e19ff 100644 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,7 @@ import wrap from '@adobe/helix-shared-wrap'; import { logger } from '@adobe/helix-universal-logger'; import { helixStatus } from '@adobe/helix-status'; import SQSQueue from './sqs-queue.js'; -import DB from './db.js'; // Assuming the exported content of './db' is default exported +import { dynamoDBWrapper } from './db-wrapper.js'; // Assuming the exported content of './db' is default exported import PSIClient from './psi-client.js'; // Assuming the exported content of './psi-client' is default exported /** @@ -24,9 +24,9 @@ import PSIClient from './psi-client.js'; // Assuming the exported content of './ * @returns {Response} a response */ async function run(request, context) { - const db = DB({ - region: process.env.REGION, - }); + const { + __ow_dynamodb: db, + } = context; const sqsQueue = SQSQueue(); const { message } = JSON.parse(context.invocation.event.Records[0].body); @@ -36,11 +36,8 @@ async function run(request, context) { }); const site = { - id: message.siteId, - githubURL: message.githubURL, domain: message.domain, path: message.path, - isLive: message.isLive, }; const auditResult = await psiClient.runAudit(`https://${site.domain}/${site.path}`); const auditResultMin = await db.saveAuditIndex(site, auditResult); @@ -50,6 +47,7 @@ async function run(request, context) { export const main = wrap(run) .with(helixStatus) + .with(dynamoDBWrapper) .with(logger.trace) .with(logger) .with(secrets); diff --git a/src/service-wrap.js b/src/service-wrap.js new file mode 100644 index 00000000..1ea3f44b --- /dev/null +++ b/src/service-wrap.js @@ -0,0 +1,29 @@ +/* + * Copyright 2023 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +const serviceWrap = ( + lambdaFn, + lambdaParams, + paramName, + factoryFn, +) => { + if (!lambdaParams[paramName]) { + const service = factoryFn(lambdaParams); + + // pass params by reference and not value so later modifications + // of the params are accessible to the wrap + // eslint-disable-next-line no-param-reassign + lambdaParams[paramName] = service.getInstance(lambdaParams); + } + return lambdaFn(lambdaParams); +}; + +module.exports = serviceWrap;