keyv-nedb-core

1.0.1 • Public • Published

keyv-nedb-core

nedb-core storage adapter for Keyv

Needs testing!

Install

npm install --save keyv keyv-nedb-core

Usage

const Keyv = require('keyv')
const KeyvNedbAdapter = require('keyv-nedb-core')
 
let adapter = new KeyvNedbAdapter({
    filename: path.resolve('/path/to/myDatabase.db'),
    autoload: true
})
 
const keyv = new Keyv({store: adapter})

The options passed to the KeyvNedbAdapter constructor are passed straight to nedb-core's Datastore constructor, unmodified.

The datastore is exposed on the adapter as .nedb:

adapter.nedb.findOne({key: 'somekey'}, (err, doc) => console.log(doc))

You can run any of nedb-core's methods.

Removing expired keys

The adapter exposes one function called evictExpired which accepts no arguments and returns a promise that resolves with the value true once all expired keys have been removed from the cache. In order to make this work, the adapter must be able to deserialize your data.

If you're using keyv's default deserializer json-buffer this function will work out of the box. If you provided your own deserializer, pass it to the constructor as a separate parameter after nedb's options:

const Keyv = require('keyv')
const KeyvNedbAdapter = require('keyv-nedb-core')
const moment = require('moment') // date manipulation lib
 
function deserialize (str) {
  let obj = JSON.parse(str)
  // convert a String to a Date:
  obj.creationDate = moment(obj.creationDate, 'DD/MM/YYYY').toDate()
  return obj
}
 
const nedbOptions = {
    filename: path.resolve('/path/to/myDatabase.db'),
    autoload: true
}
 
let adapter = new KeyvNedbAdapter(nedbOptions, deserialize)
 
const keyv = new Keyv({store: adapter, deserialize: deserialize})
 
// [...]
 
adapter.evictExpired().then(() => console.log('All cleaned up!'))
 

You can enable the automated eviction of expired keys. In the options object include the key automaticEviction and set it to a number higher than zero. The eviction will be run every time an internal counter reaches that number; the counter increases by 1 every time you do a set, get or delete and it's reset to zero when you clear. So if you set automaticEviction to 2000 and use get 4000 times, the function will run twice.

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i keyv-nedb-core

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

9.18 kB

Total Files

5

Last publish

Collaborators

  • mysidestheyaregone