hyperdb-retention
v0.1.7
Published
Delete old data from HyperDB collections based on age
Maintainers
Readme
hyperdb-retention
Automatic retention policy for HyperDB collections.
Deletes documents older than a specified age, keeping your database from growing indefinitely.
Installation
npm install hyperdb-retentionUsage
import { retention } from 'hyperdb-retention'
await retention(db, {
collection: '@myapp/events',
maxAge: 24 * 60 * 60 * 1000 // 24 hours in milliseconds
})For continuous retention, call it inside a setInterval:
setInterval(async () => {
await retention(db, {
collection: '@myapp/events',
maxAge: 24 * 60 * 60 * 1000
})
}, 60 * 1000) // run every minuteBy default, retention uses a field called timestamp to determine the age of a document. You can change this with the timestampField option:
await retention(db, {
collection: '@myapp/messages',
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
timestampField: 'createdAt'
})API
await retention(db, options)
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| collection | string | required | The fully qualified collection name, e.g. '@ns/my-collection' |
| maxAge | number | required | Maximum age of documents in milliseconds |
| timestampField | string | 'timestamp' | The name of the date field used for age comparison |
License
ISC
