You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 2, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+22-52Lines changed: 22 additions & 52 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# level-js
2
2
3
-
> An [`abstract-leveldown`][abstract-leveldown] compliant store on top of [IndexedDB][indexeddb], which is in turn implemented on top of [LevelDB][leveldb] which brings this whole shebang full circle.
3
+
> An [`abstract-leveldown`][abstract-leveldown] compliant store on top of [IndexedDB][indexeddb], which is ~~in turn implemented on top of [LevelDB][leveldb] which brings this whole shebang full circle~~.
@@ -35,13 +35,11 @@ Here are the goals of `level-js`:
35
35
36
36
- Store large amounts of data in modern browsers
37
37
- Pass the full [`abstract-leveldown`][abstract-leveldown] test suite
38
-
- Support [`Buffer`][buffer] keys and values
39
-
- Support all key types of IndexedDB Second Edition
40
-
- Support all value types of the [structured clone algorithm][structured-clone-algorithm] except for `null` and `undefined`
38
+
- Support string and [`Buffer`][buffer] keys and values
41
39
- Be as fast as possible
42
-
- Sync with [multilevel](https://github.com/juliangruber/multilevel) over ASCII or binary transports.
40
+
-~~Sync with [multilevel](https://github.com/juliangruber/multilevel) over ASCII or binary transports.~~
43
41
44
-
Being `abstract-leveldown` compliant means you can use many of the [Level modules][awesome] on top of this library. For some demos of it working, see [**@brycebaril**](https://github.com/brycebaril)'s presentation [Path of the NodeBases Jedi](http://brycebaril.github.io/nodebase_jedi/#/vanilla).
42
+
Being `abstract-leveldown` compliant means you can use many of the [Level modules][awesome] on top of this library.
45
43
46
44
## Example
47
45
@@ -80,90 +78,63 @@ const value = await db.get('hello')
80
78
81
79
## Type Support
82
80
83
-
Unlike [`leveldown`][leveldown], `level-js` does not stringify keys or values. This means that in addition to strings and Buffers you can store almost any JavaScript type without the need for [`encoding-down`][encoding-down].
81
+
~~Unlike [`leveldown`][leveldown], `level-js` does not stringify keys or values. This means that in addition to strings and Buffers you can store almost any JavaScript type without the need for [`encoding-down`][encoding-down].~~
84
82
85
83
### Values
86
84
87
-
All value types of the [structured clone algorithm][structured-clone-algorithm] are supported except for `null` and `undefined`. Depending on the environment, this includes:
85
+
~~All value types of the [structured clone algorithm][structured-clone-algorithm] are supported except for `null` and `undefined`. Depending on the environment, this includes:~~
88
86
89
-
- Number, including `NaN`, `Infinity` and `-Infinity`
90
-
- String, Boolean, Date, RegExp, Array, Object
87
+
-~~Number, including `NaN`, `Infinity` and `-Infinity`~~
In addition `level-js` stores [`Buffer`][buffer] values without transformation. This works in all target environments because `Buffer` is a subclass of `Uint8Array`, meaning such values can be passed to `IndexedDB` as-is.
95
93
96
-
When getting or iterating binary values, regardless of whether they were stored as a `Buffer`, `ArrayBuffer` or a view thereof, values will return as a `Buffer`. This behavior can be disabled, in which case `ArrayBuffer` returns as `ArrayBuffer`, typed arrays return as typed arrays and `Buffer` returns as `Uint8Array`:
94
+
When getting or iterating binary values, regardless of whether they were stored as a `Buffer`, `ArrayBuffer` or a view thereof, values will return as a `Buffer`. This behavior can be disabled, in which case ~~`ArrayBuffer` returns as `ArrayBuffer`, typed arrays return as typed arrays and `Buffer` returns as `Uint8Array`~~:
97
95
98
96
```js
99
97
db.get('key', { asBuffer:false })
100
98
db.iterator({ valueAsBuffer:false })
101
99
```
102
100
103
-
If the environment does not support a type, it will throw an error which `level-js` catches and passes to the callbacks of `put` or `batch`. For example, IE does not support typed array values. At the time of writing, Chrome is the only browser that supports all types listed above.
101
+
~~If the environment does not support a type, it will throw an error which `level-js` catches and passes to the callbacks of `put` or `batch`. For example, IE does not support typed array values. At the time of writing, Chrome is the only browser that supports all types listed above.~~
104
102
105
103
### Keys
106
104
107
-
All key types of IndexedDB Second Edition are supported. Depending on the environment, this includes:
105
+
~~All key types of IndexedDB Second Edition are supported. Depending on the environment, this includes:~~
108
106
109
-
- Number, including `Infinity` and `-Infinity`, but not `NaN`
110
-
- Date, except invalid (`NaN`)
107
+
-~~Number, including `Infinity` and `-Infinity`, but not `NaN`~~
108
+
-~~Date, except invalid (`NaN`)~~
111
109
- String
112
110
- ArrayBuffer or a view thereof (typed arrays);
113
-
- Array, except cyclical, empty and sparse arrays. Elements must be valid types themselves.
111
+
-~~Array, except cyclical, empty and sparse arrays. Elements must be valid types themselves.~~
114
112
115
-
In addition you can use [`Buffer`][buffer] keys, giving `level-js` the same power as implementations like `leveldown` and `memdown`. When iterating binary keys, regardless of whether they were stored as `Buffer`, `ArrayBuffer` or a view thereof, keys will return as a `Buffer`. This behavior can be disabled, in which case binary keys will always return as `ArrayBuffer`:
113
+
In addition you can use [`Buffer`][buffer] keys, giving `level-js` the same power as implementations like `leveldown` and `memdown`. When iterating binary keys, regardless of whether they were stored as `Buffer`, `ArrayBuffer` or a view thereof, keys will return as a `Buffer`. This behavior can be disabled, in which case ~~binary keys will always return as `ArrayBuffer`~~:
116
114
117
115
```js
118
116
db.iterator({ keyAsBuffer:false })
119
117
```
120
118
121
119
Note that this behavior is slightly different from values due to the way that IndexedDB works. IndexedDB stores binary _values_ using the structured clone algorithm, which preserves views, but it stores binary _keys_ as an array of octets, so that it is able to compare and sort differently typed keys.
122
120
123
-
If the environment does not support a type, it will throw an error which `level-js` catches and passes to the callbacks of `get`, `put`, `del`, `batch` or an iterator. Exceptions are:
121
+
~~If the environment does not support a type, it will throw an error which `level-js` catches and passes to the callbacks of `get`, `put`, `del`, `batch` or an iterator~~. Exceptions are:
124
122
125
123
-`null` and `undefined`: rejected early by `abstract-leveldown`
126
-
- Binary and array keys: if not supported by the environment, `level-js` falls back to `String(key)`.
124
+
- Binary keys: if not supported by the environment, `level-js` falls back to `String(key)`. If (one of) your target environments does not support binary keys (like IE11), it is recommended to set the `bufferKeys` option of the constructor to `false` and then only use string keys. Otherwise, you risk losing data once the environment does land support for binary keys.
127
125
128
126
### Normalization
129
127
130
128
If you desire normalization for keys and values (e.g. to stringify numbers), wrap `level-js` with [`encoding-down`][encoding-down]. Alternatively install [`level`][level] which conveniently bundles [`levelup`][levelup], `level-js` and `encoding-down`. Such an approach is also recommended if you want to achieve universal (isomorphic) behavior or to smooth over type differences between browsers. For example, you could have [`leveldown`][leveldown] in a backend and `level-js` in the frontend. The `level` package does exactly that.
131
129
132
-
Another reason you might want to use `encoding-down` is that the structured clone algorithm, while rich in types, can be slower than `JSON.stringify`.
130
+
~~Another reason you might want to use `encoding-down` is that the structured clone algorithm, while rich in types, can be slower than `JSON.stringify`.~~
133
131
134
132
### Sort Order
135
133
136
-
Unless `level-js` is wrapped with [`encoding-down`][encoding-down], IndexedDB will sort your keys in the following order:
134
+
~~Unless `level-js` is wrapped with [`encoding-down`][encoding-down], IndexedDB will sort your keys in the following order:~~
137
135
138
-
1. number (numeric)
139
-
2. date (numeric, by epoch offset)
140
136
3. binary (bitwise)
141
137
4. string (lexicographic)
142
-
5. array (componentwise).
143
-
144
-
You can take advantage of this fact with `levelup` streams. For example, if your keys are dates, you can select everything greater than a specific date (let's be happy and ignore timezones for a moment):
145
-
146
-
```js
147
-
constdb=levelup(leveljs('time-db'))
148
-
149
-
db.createReadStream({ gt:newDate('2019-01-01') })
150
-
.pipe(..)
151
-
```
152
-
153
-
Or if your keys are arrays, you can do things like:
154
-
155
-
```js
156
-
constdb=levelup(leveljs('books-db'))
157
-
158
-
awaitdb.put(['Roald Dahl', 'Charlie and the Chocolate Factory'], {})
To achieve this on other `abstract-leveldown` implementations, wrap them with [`encoding-down`][encoding-down] and [`charwise`][charwise] (or similar).
167
138
168
139
#### Known Browser Issues
169
140
@@ -173,7 +144,7 @@ IE11 and Edge yield incorrect results for `{ gte: '' }` if the database contains
173
144
174
145
For interoperability it is recommended to use `Buffer` as your binary type. While we recognize that Node.js core modules are moving towards supporting `ArrayBuffer` and views thereof, `Buffer` remains the primary binary type in the Level ecosystem.
175
146
176
-
That said: if you want to `put()` an `ArrayBuffer` you can! Just know that it will come back as a `Buffer` by default. If you want to `get()` or iterate stored `ArrayBuffer` data as an `ArrayBuffer`, you have a few options. Without `encoding-down`:
147
+
That said: if you want to `put()` an `ArrayBuffer` you can! Just know that it will come back as a `Buffer` by default. ~~If you want to `get()` or iterate stored `ArrayBuffer` data as an `ArrayBuffer`, you have a few options~~. Without `encoding-down`:
177
148
178
149
```js
179
150
constdb=levelup(leveljs('mydb'))
@@ -220,6 +191,7 @@ The optional `options` argument may contain:
220
191
221
192
-`prefix`_(string, default: `'level-js-'`)_: Prefix for `IDBDatabase` name.
222
193
-`version`_(string | number, default: `1`)_: The version to open the database with.
194
+
-`bufferKeys`: ..
223
195
224
196
See [`IDBFactory#open`](https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/open) for more details.
225
197
@@ -276,8 +248,6 @@ To sustain [`Level`](https://github.com/Level) and its activities, become a back
0 commit comments