test-level

0.0.3 • Public • Published

test-level

Create temporary levelup databases, with unique names generated by tmpgen. Can remove directory on test end, db.close or before process exit (on node >= 0.12.11). Does not include levelup, leveldown or memdown - so you get to choose the versions. API unstable, expect breaking changes.

npm status Travis build status AppVeyor build status Dependency status

example

npm i levelup leveldown level-sublevel test-level

const disk = require('test-level')('my-module/*', { wrap: 'sublevel' })
 
const db1 = disk()
const db2 = disk()
 
// /tmp/my-module/1454283677055
// /tmp/my-module/1454283677055.001
console.log(db1.location, db2.location)
 
const sub = db1.sublevel('beep')

see also

Differences from level-test:

  • In level-test, opts.clean removes the directory before opening the db, in test-level it removes the directory after you're done
  • No browser support as of yet
  • Does not fallback to memdown if leveldown failed to load
  • Does not include memdown, leveldown or levelup

usage

npm i levelup leveldown memdown level-sublevel test-level existent

const level = require('test-level')
    , existent = require('existent')
 
// Create a levelup+memdown factory, with node-hat as
// name generator (see `npm docs tmpgen` for details)
const mem = level({ mem: true, valueEncoding: 'utf8', gen: 'hat' })
 
// Same as
const mem2 = level({ db: require('memdown'), gen: 'hat' })
const mem3 = level({ db: 'memdown', gen: 'hat' })
 
// Create a db and override the valueEncoding (or
// any other levelup option) of the factory
const db1 = mem({ valueEncoding: 'json' })
 
// Create a levelup+leveldown factory. Each db gets a unique temporary
// directory with the default monotonic-timestamp name generator and is
// wrapped with level-sublevel.
const diskA = level({ wrap: ['sublevel'] })
const db2 = diskA()
const sub = db2.sublevel('beep')
 
// Same, at custom tmp location, with wrapper options
const diskB = level('my-module/*', {
  wrap: [ ['sublevel', { valueEncoding: 'json' }] ]
})
 
const db3 = diskB()
 
// Remove created dbs before process exit (ignored if mem is true)
const diskC = level('beep-*', { clean: true })
 
// Create db in a subdirectory
const db4 = diskC('special-name')
 
;[db1, db2, db3, db4].forEach((db, i) => {
  console.log('db %d', i+1, existent.sync(db.location), db.location)
})
 
process.on('exit', function(){
  console.log('\nExiting\n')
  ;[db1, db2, db3, db4].forEach((db, i) => {
    // db4 is removed
    console.log('db %d', i+1, existent.sync(db.location), db.location)
  })
})

Output:

db 1 false 8a2fe3f361ff5365dd956da54fcca014
db 2 true /tmp/test-level/1454283677010
db 3 true /tmp/my-module/1454283677055
db 4 true /tmp/beep-1454283677058/special-name

Exiting

db 1 false 8a2fe3f361ff5365dd956da54fcca014
db 2 true /tmp/test-level/1454283677010
db 3 true /tmp/my-module/1454283677055
db 4 false /tmp/beep-1454283677058/special-name

tape test helper

[todo: write docs or move that stuff to another module]

api

main(arg[,opts])

...

install

With npm do:

npm install test-level

license

MIT © Vincent Weevers

Package Sidebar

Install

npm i test-level

Weekly Downloads

2

Version

0.0.3

License

MIT

Last publish

Collaborators

  • vweevers