44
55module . exports = Level
66
7- var AbstractLevelDOWN = require ( 'abstract-leveldown' ) . AbstractLevelDOWN
8- var inherits = require ( 'inherits' )
9- var Iterator = require ( './iterator' )
10- var serialize = require ( './util/serialize' )
11- var deserialize = require ( './util/deserialize' )
12- var support = require ( './util/support' )
13- var clear = require ( './util/clear' )
14- var createKeyRange = require ( './util/key-range' )
7+ const AbstractLevelDOWN = require ( 'abstract-leveldown' ) . AbstractLevelDOWN
8+ const inherits = require ( 'inherits' )
9+ const Iterator = require ( './iterator' )
10+ const serialize = require ( './util/serialize' )
11+ const deserialize = require ( './util/deserialize' )
12+ const support = require ( './util/support' )
13+ const clear = require ( './util/clear' )
14+ const createKeyRange = require ( './util/key-range' )
1515
16- var DEFAULT_PREFIX = 'level-js-'
16+ const DEFAULT_PREFIX = 'level-js-'
1717
1818function Level ( location , opts ) {
1919 if ( ! ( this instanceof Level ) ) return new Level ( location , opts )
@@ -41,34 +41,33 @@ inherits(Level, AbstractLevelDOWN)
4141Level . prototype . type = 'level-js'
4242
4343Level . prototype . _open = function ( options , callback ) {
44- var req = indexedDB . open ( this . prefix + this . location , this . version )
45- var self = this
44+ const req = indexedDB . open ( this . prefix + this . location , this . version )
4645
4746 req . onerror = function ( ) {
4847 callback ( req . error || new Error ( 'unknown error' ) )
4948 }
5049
51- req . onsuccess = function ( ) {
52- self . db = req . result
50+ req . onsuccess = ( ) => {
51+ this . db = req . result
5352 callback ( )
5453 }
5554
56- req . onupgradeneeded = function ( ev ) {
57- var db = ev . target . result
55+ req . onupgradeneeded = ( ev ) => {
56+ const db = ev . target . result
5857
59- if ( ! db . objectStoreNames . contains ( self . location ) ) {
60- db . createObjectStore ( self . location )
58+ if ( ! db . objectStoreNames . contains ( this . location ) ) {
59+ db . createObjectStore ( this . location )
6160 }
6261 }
6362}
6463
6564Level . prototype . store = function ( mode ) {
66- var transaction = this . db . transaction ( [ this . location ] , mode )
65+ const transaction = this . db . transaction ( [ this . location ] , mode )
6766 return transaction . objectStore ( this . location )
6867}
6968
7069Level . prototype . await = function ( request , callback ) {
71- var transaction = request . transaction
70+ const transaction = request . transaction
7271
7372 // Take advantage of the fact that a non-canceled request error aborts
7473 // the transaction. I.e. no need to listen for "request.onerror".
@@ -82,10 +81,11 @@ Level.prototype.await = function (request, callback) {
8281}
8382
8483Level . prototype . _get = function ( key , options , callback ) {
85- var store = this . store ( 'readonly' )
84+ const store = this . store ( 'readonly' )
85+ let req
8686
8787 try {
88- var req = store . get ( key )
88+ req = store . get ( key )
8989 } catch ( err ) {
9090 return this . _nextTick ( callback , err )
9191 }
@@ -103,10 +103,11 @@ Level.prototype._get = function (key, options, callback) {
103103}
104104
105105Level . prototype . _del = function ( key , options , callback ) {
106- var store = this . store ( 'readwrite' )
106+ const store = this . store ( 'readwrite' )
107+ let req
107108
108109 try {
109- var req = store . delete ( key )
110+ req = store . delete ( key )
110111 } catch ( err ) {
111112 return this . _nextTick ( callback , err )
112113 }
@@ -115,12 +116,13 @@ Level.prototype._del = function (key, options, callback) {
115116}
116117
117118Level . prototype . _put = function ( key , value , options , callback ) {
118- var store = this . store ( 'readwrite' )
119+ const store = this . store ( 'readwrite' )
120+ let req
119121
120122 try {
121123 // Will throw a DataError or DataCloneError if the environment
122124 // does not support serializing the key or value respectively.
123- var req = store . put ( value , key )
125+ req = store . put ( value , key )
124126 } catch ( err ) {
125127 return this . _nextTick ( callback , err )
126128 }
@@ -143,10 +145,10 @@ Level.prototype._iterator = function (options) {
143145Level . prototype . _batch = function ( operations , options , callback ) {
144146 if ( operations . length === 0 ) return this . _nextTick ( callback )
145147
146- var store = this . store ( 'readwrite' )
147- var transaction = store . transaction
148- var index = 0
149- var error
148+ const store = this . store ( 'readwrite' )
149+ const transaction = store . transaction
150+ let index = 0
151+ let error
150152
151153 transaction . onabort = function ( ) {
152154 callback ( error || transaction . error || new Error ( 'aborted by user' ) )
@@ -158,11 +160,13 @@ Level.prototype._batch = function (operations, options, callback) {
158160
159161 // Wait for a request to complete before making the next, saving CPU.
160162 function loop ( ) {
161- var op = operations [ index ++ ]
162- var key = op . key
163+ const op = operations [ index ++ ]
164+ const key = op . key
165+
166+ let req
163167
164168 try {
165- var req = op . type === 'del' ? store . delete ( key ) : store . put ( op . value , key )
169+ req = op . type === 'del' ? store . delete ( key ) : store . put ( op . value , key )
166170 } catch ( err ) {
167171 error = err
168172 transaction . abort ( )
@@ -178,8 +182,11 @@ Level.prototype._batch = function (operations, options, callback) {
178182}
179183
180184Level . prototype . _clear = function ( options , callback ) {
185+ let keyRange
186+ let req
187+
181188 try {
182- var keyRange = createKeyRange ( options )
189+ keyRange = createKeyRange ( options )
183190 } catch ( e ) {
184191 // The lower key is greater than the upper key.
185192 // IndexedDB throws an error, but we'll just do nothing.
@@ -193,8 +200,8 @@ Level.prototype._clear = function (options, callback) {
193200 }
194201
195202 try {
196- var store = this . store ( 'readwrite' )
197- var req = keyRange ? store . delete ( keyRange ) : store . clear ( )
203+ const store = this . store ( 'readwrite' )
204+ req = keyRange ? store . delete ( keyRange ) : store . clear ( )
198205 } catch ( err ) {
199206 return this . _nextTick ( callback , err )
200207 }
@@ -213,9 +220,9 @@ Level.prototype.upgrade = function (callback) {
213220 return this . _nextTick ( callback , new Error ( 'cannot upgrade() before open()' ) )
214221 }
215222
216- var it = this . iterator ( )
217- var batchOptions = { }
218- var self = this
223+ const it = this . iterator ( )
224+ const batchOptions = { }
225+ const self = this
219226
220227 it . _deserializeKey = it . _deserializeValue = identity
221228 next ( )
@@ -230,8 +237,8 @@ Level.prototype.upgrade = function (callback) {
230237 return finish ( err )
231238 }
232239
233- var newKey = self . _serializeKey ( deserialize ( key , true ) )
234- var newValue = self . _serializeValue ( deserialize ( value , true ) )
240+ const newKey = self . _serializeKey ( deserialize ( key , true ) )
241+ const newValue = self . _serializeValue ( deserialize ( value , true ) )
235242
236243 // To bypass serialization on the old key, use _batch() instead of batch().
237244 // NOTE: if we disable snapshotting (#86) this could lead to a loop of
@@ -259,7 +266,7 @@ Level.destroy = function (location, prefix, callback) {
259266 callback = prefix
260267 prefix = DEFAULT_PREFIX
261268 }
262- var request = indexedDB . deleteDatabase ( prefix + location )
269+ const request = indexedDB . deleteDatabase ( prefix + location )
263270 request . onsuccess = function ( ) {
264271 callback ( )
265272 }
0 commit comments