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

Commit 449a8e4

Browse files
authored
Increase abstract-leveldown parity (#692)
- Add db property to chained batch - Remove type checks that are also performed by abstract-leveldown Goes hand in hand with Level/abstract-leveldown#364.
1 parent 71a6aa3 commit 449a8e4

File tree

6 files changed

+36
-105
lines changed

6 files changed

+36
-105
lines changed

lib/batch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ var getCallback = require('./common').getCallback
44
var getOptions = require('./common').getOptions
55

66
function Batch (levelup) {
7-
this._levelup = levelup
7+
// TODO (next major): remove this._levelup alias
8+
this.db = this._levelup = levelup
89
this.batch = levelup.db.batch()
910
this.ops = []
1011
this.length = 0

lib/levelup.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ LevelUP.prototype.isClosed = function () {
169169
}
170170

171171
LevelUP.prototype.get = function (key, options, callback) {
172-
if (key === null || key === undefined) {
173-
throw new ReadError('get() requires a key argument')
174-
}
175-
176172
var promise
177173

178174
callback = getCallback(options, callback)
@@ -202,10 +198,6 @@ LevelUP.prototype.get = function (key, options, callback) {
202198
}
203199

204200
LevelUP.prototype.put = function (key, value, options, callback) {
205-
if (key === null || key === undefined) {
206-
throw new WriteError('put() requires a key argument')
207-
}
208-
209201
var self = this
210202
var promise
211203

@@ -232,10 +224,6 @@ LevelUP.prototype.put = function (key, value, options, callback) {
232224
}
233225

234226
LevelUP.prototype.del = function (key, options, callback) {
235-
if (key === null || key === undefined) {
236-
throw new WriteError('del() requires a key argument')
237-
}
238-
239227
var self = this
240228
var promise
241229

@@ -266,14 +254,11 @@ LevelUP.prototype.batch = function (arr, options, callback) {
266254
return new Batch(this)
267255
}
268256

269-
if (!Array.isArray(arr)) {
270-
throw new WriteError('batch() requires an array argument')
271-
}
272-
273257
var self = this
274258
var promise
275259

276-
callback = getCallback(options, callback)
260+
if (typeof arr === 'function') callback = arr
261+
else callback = getCallback(options, callback)
277262

278263
if (!callback) {
279264
callback = promisify()

test/argument-checking-test.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

test/get-put-del-test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,4 @@ module.exports = function (test, testCommon) {
107107
], done)
108108
})
109109
})
110-
111-
test('get() / put() / del(): throw if no key is provided', function (t) {
112-
discardable(t, testCommon, function (db, done) {
113-
t.throws(db.get.bind(db), /^ReadError: get\(\) requires a key argument/, 'no-arg get() throws')
114-
t.throws(db.put.bind(db), /^WriteError: put\(\) requires a key argument/, 'no-arg put() throws')
115-
t.throws(db.del.bind(db), /^WriteError: del\(\) requires a key argument/, 'no-arg del() throws')
116-
done()
117-
})
118-
})
119110
}

test/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ function suite (options) {
66
var testCommon = common(options)
77
var test = testCommon.test
88

9-
require('./argument-checking-test')(test, testCommon)
109
require('./manifest-test')(test, testCommon)
1110
require('./batch-test')(test, testCommon)
1211
if (testCommon.encodings) require('./binary-test')(test, testCommon)

test/null-and-undefined-test.js

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,41 @@
1-
var errors = require('../lib/levelup').errors
21
var after = require('after')
32
var discardable = require('./util/discardable')
43

54
module.exports = function (test, testCommon) {
65
test('null & undefined keys & values: cause error', function (t) {
76
discardable(t, testCommon, function (db, done) {
8-
t.throws(db.get.bind(db, null), /^ReadError: get\(\) requires a key argument/)
9-
t.throws(db.get.bind(db, undefined), /^ReadError: get\(\) requires a key argument/)
10-
t.throws(db.del.bind(db, null), /^WriteError: del\(\) requires a key argument/)
11-
t.throws(db.del.bind(db, undefined), /^WriteError: del\(\) requires a key argument/)
12-
t.throws(db.put.bind(db, null, 'foo'), /^WriteError: put\(\) requires a key argument/)
13-
t.throws(db.put.bind(db, undefined, 'foo'), /^WriteError: put\(\) requires a key argument/)
14-
15-
var next = after(6, done)
16-
17-
db.put('foo', null, function (err, value) {
18-
t.is(err.message, 'value cannot be `null` or `undefined`')
19-
next()
20-
})
21-
22-
db.put('foo', undefined, function (err, value) {
23-
t.is(err.message, 'value cannot be `null` or `undefined`')
24-
next()
25-
})
26-
27-
db.batch([{ key: 'foo', value: undefined, type: 'put' }], function (err) {
28-
t.is(err.message, 'value cannot be `null` or `undefined`')
29-
next()
30-
})
31-
32-
db.batch([{ key: 'foo', value: null, type: 'put' }], function (err) {
33-
t.is(err.message, 'value cannot be `null` or `undefined`')
34-
next()
35-
})
36-
37-
db.batch([{ key: undefined, value: 'bar', type: 'put' }], function (err) {
38-
t.ok(err instanceof Error)
39-
t.ok(err instanceof errors.LevelUPError)
40-
next()
41-
})
42-
43-
db.batch([{ key: null, value: 'bar', type: 'put' }], function (err) {
44-
t.ok(err instanceof Error)
45-
t.ok(err instanceof errors.LevelUPError)
46-
next()
7+
var next = after(12, done)
8+
9+
;[null, undefined].forEach(function (nullish) {
10+
db.get(nullish, function (err) {
11+
t.is(err.message, 'key cannot be `null` or `undefined`')
12+
next()
13+
})
14+
15+
db.del(nullish, function (err) {
16+
t.is(err.message, 'key cannot be `null` or `undefined`')
17+
next()
18+
})
19+
20+
db.put(nullish, 'value', function (err) {
21+
t.is(err.message, 'key cannot be `null` or `undefined`')
22+
next()
23+
})
24+
25+
db.put('foo', nullish, function (err, value) {
26+
t.is(err.message, 'value cannot be `null` or `undefined`')
27+
next()
28+
})
29+
30+
db.batch([{ key: nullish, value: 'bar', type: 'put' }], function (err) {
31+
t.is(err.message, 'key cannot be `null` or `undefined`')
32+
next()
33+
})
34+
35+
db.batch([{ key: 'foo', value: nullish, type: 'put' }], function (err) {
36+
t.is(err.message, 'value cannot be `null` or `undefined`')
37+
next()
38+
})
4739
})
4840
})
4941
})

0 commit comments

Comments
 (0)