Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Destroy with location and prefix only #116

Merged
merged 3 commits into from
May 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,10 @@ Level.prototype._close = function (callback) {
callback()
}

Level.destroy = function (db, callback) {
if (typeof db === 'object') {
var prefix = db.prefix || DEFAULT_PREFIX
var location = db.location
} else {
Level.destroy = function (location, prefix, callback) {
if (typeof prefix === 'function') {
callback = prefix
prefix = DEFAULT_PREFIX
location = db
}
var request = indexedDB.deleteDatabase(prefix + location)
request.onsuccess = function () {
Expand Down
11 changes: 6 additions & 5 deletions test/custom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = function (leveljs, test, testCommon) {
// NOTE: in chrome (at least) indexeddb gets buggy if you try and destroy a db,
// then create it again, then try and destroy it again. these avoid doing that

test('test levelup .destroy w/ string', function (t) {
test('test levelup .destroy', function (t) {
var location = testCommon.location()
var db = levelup(leveljs(location))
db.put('key', 'value', function (err) {
Expand All @@ -112,19 +112,20 @@ module.exports = function (leveljs, test, testCommon) {
})
})

test('test levelup .destroy w/ db instance', function (t) {
test('test levelup .destroy and custom prefix', function (t) {
var location = testCommon.location()
var db = levelup(leveljs(location))
var prefix = 'CUSTOM-PREFIX-'
var db = levelup(leveljs(location, { prefix: prefix }))
db.put('key', 'value', function (err) {
t.notOk(err, 'no error')
db.get('key', { asBuffer: false }, function (err, value) {
t.notOk(err, 'no error')
t.equal(value, 'value', 'should have value')
db.close(function (err) {
t.notOk(err, 'no error')
leveljs.destroy(db.db, function (err) {
leveljs.destroy(location, prefix, function (err) {
t.notOk(err, 'no error')
var db2 = levelup(leveljs(location))
var db2 = levelup(leveljs(location, { prefix: prefix }))
db2.get('key', { asBuffer: false }, function (err, value) {
t.ok(err && err.notFound, 'key is not there')
db2.close(t.end.bind(t))
Expand Down