diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..d6c04a8c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,94 @@ +{ + "version": "0.2.0", + "compounds": [ + { + "name": "All Packages: Unit Tests", + "configurations": [ + "Data Access: Unit Tests", + "Utils: Unit Tests", + "RUM API: Unit Tests" + ] + } + ], + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Data Access: Unit Tests", + "cwd": "${workspaceFolder}/packages/spacecat-shared-data-access", + "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", + "args": [ + "--timeout", + "999999", + "--colors", + "test/unit/**/*.test.js" + ], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen" + }, + { + "type": "node", + "request": "launch", + "name": "Utils: Unit Tests", + "cwd": "${workspaceFolder}/packages/spacecat-shared-utils", + "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", + "args": [ + "--timeout", + "999999", + "--colors", + "test/unit/**/*.test.js" + ], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen" + }, + { + "type": "node", + "request": "launch", + "name": "RUM API: Unit Tests", + "cwd": "${workspaceFolder}/packages/spacecat-shared-rum-api-client", + "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", + "args": [ + "--timeout", + "999999", + "--colors", + "test/unit/**/*.test.js" + ], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen" + }, + { + "type": "node", + "request": "launch", + "name": "Current Test File", + "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", + "args": [ + "--timeout", + "999999", + "--colors", + "${file}" + ], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "skipFiles": [ + "/**" + ] + }, + { + "type": "node", + "request": "launch", + "name": "Current Test Suite", + "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha", + "args": [ + "--timeout", + "999999", + "--colors", + "${fileDirname}/**/*.test.js" + ], + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "skipFiles": [ + "/**" + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..f433d751 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,21 @@ +{ + "eslint.workingDirectories": [ + "./packages/spacecat-shared-data-access", + "./packages/spacecat-shared-utils", + "./packages/spacecat-shared-rum-api-client" + ], + "eslint.validate": [ + "javascript", + "javascriptreact", + "typescript", + "typescriptreact" + ], + "editor.codeActionsOnSave": { + "source.fixAll.eslint": true + }, + "editor.formatOnSave": true, + "javascript.format.enable": false, + "eslint.alwaysShowStatus": true, + "eslint.debug": true, + "files.eol": "\n" +} \ No newline at end of file diff --git a/packages/spacecat-shared-data-access/README.md b/packages/spacecat-shared-data-access/README.md index 80798ebb..2f76eae3 100644 --- a/packages/spacecat-shared-data-access/README.md +++ b/packages/spacecat-shared-data-access/README.md @@ -102,7 +102,7 @@ The module provides the following DAOs: ## Integrating Data Access in AWS Lambda Functions Our `spacecat-shared-data-access` module includes a wrapper that can be easily integrated into AWS Lambda functions using `@adobe/helix-shared-wrap`. -This integration allows your Lambda functions to access and manipulate data seamlessly. +This integration allows your Lambda functions to access and manipulate data. ### Steps for Integration @@ -111,10 +111,19 @@ This integration allows your Lambda functions to access and manipulate data seam Along with other wrappers and utilities, import the `dataAccessWrapper`. ```javascript - import dataAccessWrapper from '@adobe/spacecat-shared-data-access/wrapper'; + import dataAccessWrapper from '@adobe/spacecat-shared-data-access'; ``` -2. **Modify Your Lambda Wrapper Script** +2. **Provide Required Environment Variables** + + The `dataAccessWrapper` requires the `DYNAMO_TABLE_NAME_DATA` environment variable to be set via AWS + secret assigned to your Lambda function. + + ```javascript + const { DYNAMO_TABLE_NAME_DATA } = context.env; + ``` + +3. **Modify Your Lambda Wrapper Script** Include `dataAccessWrapper` in the chain of wrappers when defining your Lambda handler. @@ -127,7 +136,7 @@ This integration allows your Lambda functions to access and manipulate data seam .with(helixStatus); ``` -3. **Access Data in Your Lambda Function** +4. **Access Data in Your Lambda Function** Use the `dataAccess` object from the context to interact with your data layer. @@ -136,7 +145,7 @@ This integration allows your Lambda functions to access and manipulate data seam const { dataAccess } = context; // Example: Retrieve all sites - const sites = await dataAccess.getSites(); + const sites = await dataAccess.Site.getSites(); // ... more logic ... } ``` @@ -147,7 +156,7 @@ Here's a complete example of a Lambda function utilizing the data access wrapper ```javascript import wrap from '@adobe/helix-shared-wrap'; -import dataAccessWrapper from '@adobe/spacecat-shared-data-access/wrapper'; +import dataAccessWrapper from '@adobe/spacecat-shared-data-access'; import sqsEventAdapter from './sqsEventAdapter'; import sqs from './sqs'; import secrets from '@adobe/helix-shared-secrets'; @@ -156,7 +165,7 @@ import helixStatus from '@adobe/helix-status'; async function run(message, context) { const { dataAccess } = context; try { - const sites = await dataAccess.getSites(); + const sites = await dataAccess.Site.getSites(); // Function logic here } catch (error) { // Error handling diff --git a/packages/spacecat-shared-data-access/src/index.js b/packages/spacecat-shared-data-access/src/index.js index 9ddff3f8..6600c498 100644 --- a/packages/spacecat-shared-data-access/src/index.js +++ b/packages/spacecat-shared-data-access/src/index.js @@ -16,7 +16,21 @@ export * from './service/index.js'; const TABLE_NAME_DATA = 'spacecat-services-data-dev'; +/** + * Wrapper for data access layer + * @param {function} fn - The function to wrap + * @returns {function} - The wrapped function + */ export default function dataAccessWrapper(fn) { + /** + * Wrapper for data access layer. This wrapper will create a data access layer if it is not + * already created. It requires the context to have a log object. It will also use the + * DYNAMO_TABLE_NAME_DATA environment variable to create the data access layer. + * + * @param {object} request - The request object + * @param {object} context - The context object + * @returns {Promise} - The wrapped function + */ return async (request, context) => { if (!context.dataAccess) { const { log } = context; diff --git a/packages/spacecat-shared-data-access/src/readme.md b/packages/spacecat-shared-data-access/src/readme.md index c0610202..be32e7b8 100755 --- a/packages/spacecat-shared-data-access/src/readme.md +++ b/packages/spacecat-shared-data-access/src/readme.md @@ -215,3 +215,19 @@ await user.save(); ## Consideration for Indexes Indexes cost money and complexity. Do not add indexes lightly. Determine which query patterns you truly need and only then introduce additional indexes. + +## Data Access Service + +You can use the data layer by obtaining a service instance through the `createDataAccess` function: + +```javascript +const { createDataAccess } = require('@adobe/spacecat-shared-data-access'); + +const dataAccess = createDataAccess({ + tableNameData: 'spacecat-services-data-dev', +}); + +// You can now use the dataAccess object to interact with the data layer +const sites = await dataAccess.Site.getSites(); +``` + diff --git a/packages/spacecat-shared-data-access/src/service/index.js b/packages/spacecat-shared-data-access/src/service/index.js index 45e7c2e2..4d7ab2df 100644 --- a/packages/spacecat-shared-data-access/src/service/index.js +++ b/packages/spacecat-shared-data-access/src/service/index.js @@ -50,18 +50,12 @@ const createElectroService = (client, config, log) => { }; /** - * Creates a data access object. + * Creates a data access layer for interacting with DynamoDB using ElectroDB. * - * @param {{pkAllSites: string, pkAllLatestAudits: string, indexNameAllLatestAuditScores: string, - * tableNameAudits: string,tableNameLatestAudits: string, indexNameAllSitesOrganizations: string, - * tableNameSites: string, tableNameOrganizations: string, tableNameExperiments: string, - * indexNameAllSites: string, - * tableNameImportJobs: string, pkAllImportJobs: string, indexNameAllImportJobs: string, - * tableNameSiteTopPages: string, indexNameAllOrganizations: string, - * indexNameAllOrganizationsByImsOrgId: string, pkAllOrganizations: string}} config configuration - * @param {Logger} log log - * @param client custom dynamo client - * @returns {object} data access object + * @param {{tableNameData: string}} config - Configuration object containing table name + * @param {object} log - Logger instance, defaults to console + * @param {DynamoDB} [client] - Optional custom DynamoDB client instance + * @returns {object} Data access collections for interacting with entities */ export const createDataAccess = (config, log = console, client = undefined) => { const rawClient = createRawClient(client);