From e5b078b9d984a60d29ad855e7361e77482a8d694 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Fri, 27 Dec 2013 09:09:47 +0100 Subject: [PATCH] Update README.md --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index a776890..4bd16d9 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ npm test // You'll need the dev dependencies to test it It's a subset of MongoDB's API (the most used operations). The current API will not change, but I will add operations as they are needed. Summary of the API: * Creating/loading a database +* Compacting the database * Inserting documents * Finding documents * Counting documents @@ -87,6 +88,16 @@ db.users.loadDatabase(); db.robots.loadDatabase(); ``` +### Compacting the database +Under the hood, NeDB's persistence uses an append-only format, meaning that all updates and deletes actually result in lines added at the end of the datafile. The reason for this is that disk space is very cheap and appends are much faster than rewrites since they don't do a seek. The database is automatically compacted (i.e. put back in the one-line-per-document format) everytime your application restarts. + +You can manually call the compaction function with `yourDatabase.persistence.compactDatafile` which takes a `callback(err)` as argument. + +You can also set automatic compaction at regular intervals with `yourDatabase.persistence.setAutocompactionInterval(interval)`, `interval` in milliseconds (a minimum of 5s is enforced), and stop automatic compaction with `yourDatabase.persistence.stopAutocompaction()`. + +Keep in mind that compaction takes a bit of time (not too much: 130ms for 50k records on my slow machine) and no other operation can happen when it does, most of the projects actually don't need to use it. + + ### Inserting documents The native types are `String`, `Number`, `Boolean`, `Date` and `null`. You can also use arrays and subdocuments (objects). If a field is `undefined`, it will not be saved (this is different from