Add TTL (Time-To-Live) for Document Expiration
Description
TinyDB lacks a built-in mechanism for document expiration. This is problematic for applications that need to store temporary data or implement caching. Currently, users must manually track document age and periodically remove expired documents.
Context
Many document databases (like MongoDB) provide TTL mechanisms for automatic document expiration. This feature would align TinyDB with other document stores while maintaining its lightweight philosophy.
Approach
- Create a new middleware called TTLMiddleware that tracks document creation/expiration time
- Documents can specify TTL via a special field (like _ttl)
- Automatically exclude expired documents from query results
- Provide a method to explicitly purge expired documents from storage
Hi! I'm interested in implementing this feature. I've created a working prototype of a TTLMiddleware that:
- Supports both
_ttl(seconds) and_expires_at(timestamp) fields - Automatically filters expired documents from queries
- Provides a
purge_expired()method to remove expired docs from storage - Optionally supports auto-purge at configurable intervals
The implementation follows TinyDB's existing middleware patterns and includes comprehensive tests. I'd be happy to submit a PR if this approach aligns with the project's direction.
One thing to note: Due to TinyDB's table caching, applications may need to
clear the cache (db._tables = {}) or reopen the database to see filtered
results after expiration. Would you prefer a different approach to handle this,
or is documenting this behavior sufficient?
Let me know if you'd like me to proceed with a PR!