Skip to content

Commit

Permalink
feat: init db wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
alinarublea committed Oct 19, 2023
1 parent bce63db commit 057be2d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
24 changes: 24 additions & 0 deletions src/db-wrapper.js
Original file line number Diff line number Diff line change
@@ -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 };
21 changes: 13 additions & 8 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down Expand Up @@ -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: '',
Expand Down Expand Up @@ -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<object>} 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
Expand All @@ -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) {
Expand All @@ -135,4 +136,8 @@ function DB(config) {
};
}

export default DB;
const createDynamoDBService = (params) => Object.freeze({
getInstance: () => DB(params),
});

export default { createDynamoDBService };
12 changes: 5 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
29 changes: 29 additions & 0 deletions src/service-wrap.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 057be2d

Please sign in to comment.