Package Exports
- treva
- treva/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (treva) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
📖 TREVA: A Secure Trace Events Activity Package
TREVA is a robust Node.js package designed for storing and managing event logs in a MongoDB database with built-in authenticated data encryption. This package simplifies the process of logging events while ensuring the confidentiality and integrity of sensitive data.
✨ Features
Secure Data Encryption: Utilizes
AES-256-GCMauthenticated encryption to protect sensitive event data at rest.Flexible Event Logging: Allows for adding and retrieving both encrypted and unencrypted events.
Dynamic Collection Naming: Automatically creates dedicated collections for each
eventTypefor efficient data organization.Environment-Based Configuration: Securely loads critical connection and encryption keys from environment variables.
📦 Installation
To get started, install the TREVA package and its dependencies via npm.
npm install treva⚙️ Configuration
The package relies on two crucial environment variables for secure operation. You must set these before running your application.
MONGO_URI: The full MongoDB connection string. For example:
mongodb://localhost:27017/treva.ENCRYPTION_KEY: A strong, randomly generated 32-byte encryption key for
AES-256-GCM. It is critical to keep this key a secret.
# Example .env file content
MONGO_URI="mongodb://localhost:27017/treva"
ENCRYPTION_KEY="your-strong-random-key-here-of-32-bytes"🚀 Usage
Connect to the Database
First, you must establish a connection to your MongoDB instance. Ensure you have added
MONGO_URIin your .env.
import { Database } from "treva";
async function initialize() {
try {
await Database.connect();
console.log("Database connection established.");
} catch (err) {
console.error("Failed to connect to database:", err);
}
}
initialize();Add an Event
The
addEventmethod is used to securely log a new event into your database.
Method Signature:
EventLogger.addEvent({
eventType: string;
eventName: string;
data: object;
encryption?: boolean;
isOwn?: boolean;
});Parameters:
eventType: (Required) The type of event being logged (e.g.,"user","payment").eventName: (Required) The specific name of the event (e.g.,"user_login","credit_card_transaction").data: (Required) The event payload, which must be a JSON object.encryption: (Optional) A boolean flag to determine if the event's data should be encrypted before storage. Defaults tofalse.isOwn: (Optional) A boolean flag to control where the event is stored. Defaults totrue.
Behavior:
Collection Management: If
isOwnistrue, the event will be stored in a dedicated collection named<eventType>_event_logs. IfisOwnisfalse, the event will be stored in the mainmaster_event_logscollection.Data Encryption: If
encryptionis set totrue, thedatapayload is encrypted using the providedENCRYPTION_KEYbefore it is saved to the database. Otherwise, the data is stored as-is.Mandatory Fields:
eventType,eventName, anddataare all required parameters. If any of these are missing, the method will return an error.
Retrieve Events
The getEvents method allows you to retrieve event logs with flexible filtering and pagination.
EventLogger.getEvents({
eventType: string;
eventName: string;
filter?: object;
isEncrypted?: boolean;
page?: number;
limit?: number;
});Parameters:
eventType: (Required) The type of event to retrieve (e.g.,"user","payment").eventName: (Required) The specific name of the event (e.g.,"user_login","credit_card_transaction").filter: (Optional) An object to apply additional filters on the event'sdatafield.isFromMaster: (Optional) A boolean flag that, when set totrue, forces the query to run on themaster_event_logscollection.isEncrypted: (Optional) A boolean flag to filter events by their encryption status.truefetches only encrypted data which will be automatically decrypted before being returned, whilefalsefetches only unencrypted data.page: (Optional) The page number for pagination. Defaults to1.limit: (Optional) The maximum number of events to return per page. Defaults to10.
🛡️ Security
The security of your data depends entirely on the ENCRYPTION_KEY.
Do not hardcode your key. Always use a secure environment variable.
Protect the key. Store it securely and do not commit it to version control (e.g., using
.gitignore).Use a strong key. A randomly generated 32-byte string is recommended.
📜 License
This project is licensed under the MIT License.