Query Middleware
VortexDB allows you to intercept and modify queries before they are executed. This is useful for implementing custom authentication, authorization, and caching logic.
How it Works
Query middleware is implemented as a set of functions that are executed in a chain. Each function can modify the query or terminate the request.
Middleware Types
Type | Execution Time | Example |
---|---|---|
Pre-query | Before execution | Auth check |
Post-query | After execution | Logging |
Error | On failure | Error handling |
Middleware Chain
- Order of Execution: Middleware functions are executed in the order they are defined. The output of one middleware function can be passed as input to the next.
- Short-Circuiting: A middleware function can choose to terminate the request early, preventing subsequent middleware or the actual query from being executed.
Use Cases
- Authentication: You can use query middleware to verify that the user is authenticated before allowing them to execute a query.
- Example: Check for a valid API key or session token.
- Authorization: You can use query middleware to check that the user has the necessary permissions to access the data they are requesting.
- Example: Restrict access to certain fields or documents based on user roles.
- Caching: You can use query middleware to cache the results of frequently executed queries.
- Example: Serve cached data directly if available, bypassing the database.
- Query Rewriting: Modify incoming queries to enforce business rules, add default filters, or optimize performance.
- Logging and Monitoring: Log query details, execution times, and errors for auditing and performance analysis.
- Rate Limiting: Prevent abuse by limiting the number of queries a client can make within a certain time frame.
Implementing Custom Middleware

Custom middleware functions can be written in JavaScript and registered with the VortexDB query engine. This provides a powerful mechanism for extending the database's functionality.