stellasvenue-baseservice
v1.0.8
Published
Base service for AWS SDK integrations
Downloads
26
Maintainers
Readme
Here’s a sample README.md file for your BaseService class.
BaseService Class
The BaseService class provides a utility layer for working with AWS services, including DynamoDB, S3, EventBridge, and Lambda. It is designed to handle common operations such as CRUD actions, event publishing, file uploads, and utility functions.
Features • DynamoDB Operations: • Create, Read, Update, and Delete items. • Query by primary key, sort key prefix, and secondary indexes (GSIs). • S3 Operations: • Upload JSON objects or readable streams to S3. • Retrieve files from S3. • EventBridge Integration: • Publish custom events to EventBridge. • Lambda Invocation: • Invoke AWS Lambda functions asynchronously. • Utility Functions: • Data sanitization. • Date and time utilities. • Reserved keyword validation.
Installation
Prerequisites • Node.js (v14 or higher) • AWS SDK for JavaScript v3 (@aws-sdk/client-s3, @aws-sdk/client-dynamodb, etc.)
Install Dependencies
Run the following command in your project directory:
npm install @aws-sdk/client-s3 @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb @aws-sdk/client-eventbridge @aws-sdk/client-lambda @aws-sdk/lib-storage
Usage
Import the Class
const BaseService = require('./BaseService'); const service = new BaseService();
Example Usage
Store an Item in DynamoDB
await service.storeDB('User#123', 'Profile', { name: 'John Doe', age: 30 });
Query Items by Sort Key Prefix
const items = await service.getDBBySKPrefix('User#123', 'Order#'); console.log(items);
Publish an Event to EventBridge
await service.putEvents('UserRegistered', { userId: '123', email: '[email protected]' });
Upload a File to S3
await service.logObject('user-logs.json', { action: 'login', timestamp: new Date() });
Configuration
The BaseService class relies on the following environment variables:
Variable Name Description SYSTEMTABLE DynamoDB table name. MESSAGE_BUCKET S3 bucket name for storing files.
Methods
DynamoDB Methods • storeDB(PK, SK, data): Stores an item in DynamoDB. • getDB(PK, SK): Retrieves an item by its primary key. • getDBBySKPrefix(PK, SKPrefix): Queries items by Sort Key prefix. • getDbGSI2PK(GSI2PKValue): Queries items using the GSI2PK secondary index. • updateDB(PK, SK, data, optionalKeys): Updates an item, optionally updating GSIs. • deleteDBItem(PK, SK): Deletes an item by primary key.
S3 Methods • logObject(fileName, data): Uploads a JSON object to S3. • uploadReadableStream(fileName, readableStream): Uploads a file from a readable stream.
EventBridge Methods • putEvents(eventName, data): Publishes an event to EventBridge.
Lambda Methods • invokeStep(functionName, payload): Invokes a Lambda function asynchronously.
Utility Methods • sanitizeData(data): Converts dates to ISO strings and removes undefined values. • isWeekend(dateStr): Determines if a given date is a weekend. • subtractOneHour(time): Subtracts one hour from a given time string (HH:mm).
Error Handling
All methods include basic error logging and rethrow the error for higher-level handling.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author
Developed by Jermaine Watkins.
For any inquiries or suggestions, feel free to reach out!
You can adjust the content as needed for your specific project context.
