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 057be2d commit 95ff60d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/db-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import createDynamoDBService from './db.js';
import serviceWrap from './service-wrap.js';

function wrapper(func) {
return (params) => serviceWrap(
return (request, context) => serviceWrap(
func,
params,
request,
context,
'__ow_dynamodb',
createDynamoDBService,
);
Expand Down
8 changes: 4 additions & 4 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(params) {
const client = new DynamoDBClient({ region: params.region });
function DB() {
const client = new DynamoDBClient({ region: process.env.REGION });
const docClient = DynamoDBDocumentClient.from(client);

/**
Expand Down Expand Up @@ -136,8 +136,8 @@ function DB(params) {
};
}

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

export default { createDynamoDBService };
11 changes: 6 additions & 5 deletions src/service-wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
*/
const serviceWrap = (
lambdaFn,
lambdaParams,
lambdaRequest,
lambdaContext,
paramName,
factoryFn,
) => {
if (!lambdaParams[paramName]) {
const service = factoryFn(lambdaParams);
if (!lambdaContext[paramName]) {
const service = factoryFn(lambdaContext);

// 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);
lambdaContext[paramName] = service.getInstance(lambdaContext);
}
return lambdaFn(lambdaParams);
return lambdaFn(lambdaContext);
};

module.exports = serviceWrap;

0 comments on commit 95ff60d

Please sign in to comment.