louischatriot/nedb — pull request #171 by pomalbisser (2014-05-14 14:43:52+00:00)
Title: beforeCompactDatafile handler

Hello everybody,

in my case, I am using NeDB as a in-memory cache. Cache entries do have a validity parameter and I like to get rid of old cache entries before compacting. For this reason, I implemented a `beforeCompactDatafile` handler.

```javascript
// Persistent datastore with beforeCompactDatafile handler
var Datastore = require('nedb')
, db = new Datastore({ filename: 'path/to/datafile', beforeCompactDatafile: function(persistence){
//remove outdated cache entries
return true;
}});

//Manually added handler
yourDatabase.beforeCompactDatafile = function(persistence){
//remove outdated cache entries
return true;
};
```

Some basic tests and documentation is included. Eventually it would be "nicer" to put the handler into `Persistence.js`. Let me know what you thing.

Best regards
pom