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 b71d19b commit e678ad8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
4 changes: 1 addition & 3 deletions src/db-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import createDynamoDBService from './db.js';
import serviceWrap from './service-wrap.js';

function wrapper(func) {
export default function dynamoDBWrapper(func) {
const region = process.env.AWS_REGION;
if (!region) {
throw Error('Please define region in secrets');
Expand All @@ -25,5 +25,3 @@ function wrapper(func) {
createDynamoDBService,
);
}
const dynamoDBWrapper = wrapper;
export { dynamoDBWrapper };
39 changes: 19 additions & 20 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,36 @@ import { log } from './util.js';
const TABLE_SITES = 'spacecat-site';
const TABLE_AUDITS = 'spacecat-audit-index';

function DB(context) {
const client = new DynamoDBClient({ region: context.region });
const docClient = DynamoDBDocumentClient.from(client);
class DB {
constructor(context) {
this.client = new DynamoDBClient({ region: context.region });
this.docClient = DynamoDBDocumentClient.from(client);
}

/**
* Save a record to the DynamoDB.
* @param {object} record - The new record to save.
* @param tableName - The name of the table to save the record to.
*/
async function saveRecord(record, tableName) {
async saveRecord(record, tableName) {
try {
const command = new PutCommand({
TableName: tableName,
Item: record,
});
await docClient.send(command);
await this.docClient.send(command);
} catch (error) {
log('error', 'Error saving record: ', error);
}
}

/**
* 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.
*/
async function saveAuditIndex(site, audit) {
async saveAuditIndex(site, audit) {
const now = new Date().toISOString();
const uuid = Date.now().toString();

Expand Down Expand Up @@ -78,16 +81,16 @@ function DB(context) {
],
};
log('info', `Audit for domain ${site.domain} saved successfully at ${now}`);
await saveRecord(newAudit, TABLE_AUDITS);
return newAudit;
await this.saveRecord(newAudit, TABLE_AUDITS);
return Promise.resolve(newAudit);
}

/**
* Save an error that occurred during a Lighthouse audit to the DynamoDB.
* @param {object} site - site audited.
* @param {Error} error - The error that occurred during the audit.
*/
async function saveAuditError(site, error) {
async saveAuditError(site, error) {
const now = new Date().toISOString();
const newAudit = {
siteId: site.id,
Expand All @@ -96,15 +99,16 @@ function DB(context) {
error: error.message,
scores: {},
};
await saveRecord(newAudit, TABLE_AUDITS);
await this.saveRecord(newAudit, TABLE_AUDITS);
}

/**
* Fetches a site by its ID and gets its latest audit.
* @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) {
async getSite(domain, path) {
const commandParams = {
TableName: TABLE_SITES, // Replace with your table name
Key: {
Expand All @@ -115,7 +119,7 @@ function DB(context) {

try {
const command = new GetItemCommand(commandParams);
const response = await client.send(command);
const response = await this.client.send(command);
const item = response.Item;
if (item) {
log('info', `Item retrieved successfully: ${item}`);
Expand All @@ -129,13 +133,8 @@ function DB(context) {
throw error;
}
}
return {
getSite,
saveAuditIndex,
saveAuditError,
};
}

const createDynamoDBService = (context) => DB(context);

export default { createDynamoDBService };
export default function createDynamoDBService(context) {
return new DB(context);
}
2 changes: 1 addition & 1 deletion 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 { dynamoDBWrapper } from './db-wrapper.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 Down
8 changes: 3 additions & 5 deletions src/service-wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
const serviceWrap = (
export default function serviceWrap(
lambdaFn,
lambdaRequest,
lambdaContext,
paramName,
factoryFn,
) => {
) {
if (!lambdaContext[paramName]) {
// 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
lambdaContext[paramName] = factoryFn(lambdaContext);
}
return lambdaFn(lambdaContext);
};

module.exports = serviceWrap;
}

0 comments on commit e678ad8

Please sign in to comment.