fake-indexeddb 3.1.6 → 4.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -22
- package/auto.d.ts +1 -0
- package/auto.js +12 -12
- package/build/FDBCursor.js +238 -333
- package/build/FDBCursorWithValue.js +9 -27
- package/build/FDBDatabase.js +76 -119
- package/build/FDBFactory.js +97 -165
- package/build/FDBIndex.js +102 -109
- package/build/FDBKeyRange.js +29 -36
- package/build/FDBObjectStore.js +177 -185
- package/build/FDBOpenDBRequest.js +10 -28
- package/build/FDBRequest.js +34 -60
- package/build/FDBTransaction.js +89 -139
- package/build/FDBVersionChangeEvent.js +10 -29
- package/build/fakeIndexedDB.js +3 -5
- package/build/index.js +14 -4
- package/build/lib/Database.js +12 -16
- package/build/lib/FakeDOMStringList.js +66 -0
- package/build/lib/FakeEvent.js +10 -14
- package/build/lib/FakeEventTarget.js +38 -86
- package/build/lib/Index.js +61 -140
- package/build/lib/KeyGenerator.js +12 -15
- package/build/lib/ObjectStore.js +69 -167
- package/build/lib/RecordStore.js +55 -61
- package/build/lib/binarySearch.js +22 -29
- package/build/lib/canInjectKey.js +10 -34
- package/build/lib/cmp.js +13 -15
- package/build/lib/enforceRange.js +4 -6
- package/build/lib/errors.js +61 -126
- package/build/lib/extractKey.js +17 -41
- package/build/lib/scheduling.js +3 -5
- package/build/lib/structuredClone.js +12 -7
- package/build/lib/types.js +1 -2
- package/build/lib/validateKeyPath.js +7 -41
- package/build/lib/valueToKey.js +14 -16
- package/build/lib/valueToKeyRange.js +10 -13
- package/lib/FDBCursor.d.ts +2 -0
- package/lib/FDBCursorWithValue.d.ts +2 -0
- package/lib/FDBDatabase.d.ts +2 -0
- package/lib/FDBFactory.d.ts +2 -0
- package/lib/FDBIndex.d.ts +2 -0
- package/lib/FDBKeyRange.d.ts +2 -0
- package/lib/FDBObjectStore.d.ts +2 -0
- package/lib/FDBOpenDBRequest.d.ts +2 -0
- package/lib/FDBRequest.d.ts +2 -0
- package/lib/FDBTransaction.d.ts +2 -0
- package/lib/FDBVersionChangeEvent.d.ts +2 -0
- package/lib/fakeIndexedDB.d.ts +2 -0
- package/package.json +44 -26
- package/types.d.ts +29 -0
- package/build/lib/fakeDOMStringList.js +0 -16
- package/lib/FDBCursor.js +0 -5
- package/lib/FDBCursorWithValue.js +0 -5
- package/lib/FDBDatabase.js +0 -5
- package/lib/FDBFactory.js +0 -5
- package/lib/FDBIndex.js +0 -5
- package/lib/FDBKeyRange.js +0 -5
- package/lib/FDBObjectStore.js +0 -5
- package/lib/FDBOpenDBRequest.js +0 -5
- package/lib/FDBRequest.js +0 -5
- package/lib/FDBTransaction.js +0 -5
- package/lib/FDBVersionChangeEvent.js +0 -5
package/README.md
CHANGED
|
@@ -18,10 +18,10 @@ yarn add --dev fake-indexeddb
|
|
|
18
18
|
|
|
19
19
|
Functionally, it works exactly like IndexedDB except data is not persisted to disk.
|
|
20
20
|
|
|
21
|
-
The easiest way to use it is to import `fake-indexeddb/auto`, which will put all the IndexedDB
|
|
21
|
+
The easiest way to use it is to import `fake-indexeddb/auto`, which will put all the IndexedDB variables in the global scope:
|
|
22
22
|
|
|
23
23
|
```js
|
|
24
|
-
|
|
24
|
+
import "fake-indexeddb/auto";
|
|
25
25
|
|
|
26
26
|
var request = indexedDB.open("test", 3);
|
|
27
27
|
request.onupgradeneeded = function () {
|
|
@@ -54,26 +54,50 @@ request.onsuccess = function (event) {
|
|
|
54
54
|
};
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
Alternatively, you can import individual
|
|
57
|
+
Alternatively, you can explicitly import individual IndexedDB variables:
|
|
58
58
|
|
|
59
59
|
```js
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
import {
|
|
61
|
+
indexedDB,
|
|
62
|
+
IDBCursor,
|
|
63
|
+
IDBCursorWithValue,
|
|
64
|
+
IDBDatabase,
|
|
65
|
+
IDBFactory,
|
|
66
|
+
IDBIndex,
|
|
67
|
+
IDBKeyRange,
|
|
68
|
+
IDBObjectStore,
|
|
69
|
+
IDBOpenDBRequest,
|
|
70
|
+
IDBRequest,
|
|
71
|
+
IDBTransaction,
|
|
72
|
+
IDBVersionChangeEvent,
|
|
73
|
+
} from "fake-indexeddb";
|
|
62
74
|
|
|
63
75
|
// The rest is the same as above.
|
|
64
76
|
```
|
|
65
77
|
|
|
66
|
-
|
|
67
|
-
require("fake-indexeddb/lib/FDBKeyRange");` above), file names of all the objects are like the
|
|
68
|
-
normal IndexedDB ones except with F replacing I, e.g. `FDBIndex` instead of `IDBIndex`.
|
|
78
|
+
Like any imported variable, you can rename it if you want, for instance if you don't want to conflict with built-in IndexedDB variables:
|
|
69
79
|
|
|
70
|
-
|
|
80
|
+
```js
|
|
81
|
+
import {
|
|
82
|
+
indexedDB as fakeIndexedDB,
|
|
83
|
+
} from "fake-indexeddb";
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### ECMAScript modules (`import`) vs CommonJS (`require`)
|
|
87
|
+
|
|
88
|
+
Starting with version 4, fake-indexeddb uses ES modules rather than CommonJS. That means you need to `import` it, not `require` it. If that's a problem for you, stick to version 3 of fake-indexeddb, which supports `require("fake-indexeddb")`. Version 3 has basically all the same functionality as version 4, and I'll try to make any important bug fixes in both versions.
|
|
89
|
+
|
|
90
|
+
### TypeScript
|
|
91
|
+
|
|
92
|
+
As of version 4, fake-indexeddb includes TypeScript types. As you can see in types.d.ts, it's just using TypeScript's built-in IndexedDB types, rather than generating types from the fake-indexeddb code base. The reason I did this is for compatibility with your application code that may already be using TypeScript's IndexedDB types, so if I used something different for fake-indexeddb, it could lead to spurious type errors. In theory this could lead to other errors if there are differences between Typescript's IndexedDB types and fake-indexeddb's API, but currently I'm not aware of any difference. See [issue #23](https://github.com/dumbmatter/fakeIndexedDB/issues/23) for more discussion.
|
|
93
|
+
|
|
94
|
+
### Dexie and other IndexedDB API wrappers
|
|
71
95
|
|
|
72
96
|
If you import `fake-indexeddb/auto` before calling `new Dexie()`, it should work:
|
|
73
97
|
|
|
74
98
|
```js
|
|
75
|
-
|
|
76
|
-
|
|
99
|
+
import Dexie from "dexie";
|
|
100
|
+
import "fake-indexeddb/auto";
|
|
77
101
|
|
|
78
102
|
const db = new Dexie("MyDatabase");
|
|
79
103
|
```
|
|
@@ -83,16 +107,17 @@ The same likely holds true for other IndexedDB API wrappers like idb.
|
|
|
83
107
|
Alternatively, if you don't want to modify the global scope, then you need to explicitly pass the objects to Dexie:
|
|
84
108
|
|
|
85
109
|
```js
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const IDBKeyRange = require("fake-indexeddb/lib/FDBKeyRange");
|
|
110
|
+
import Dexie from "dexie";
|
|
111
|
+
import { indexedDB, IDBKeyRange } from "fake-indexeddb";
|
|
89
112
|
|
|
90
113
|
const db = new Dexie("MyDatabase", { indexedDB: indexedDB, IDBKeyRange: IDBKeyRange });
|
|
91
114
|
```
|
|
92
115
|
|
|
93
|
-
###
|
|
116
|
+
### Jest
|
|
117
|
+
|
|
118
|
+
Out of the box, Jest v27 only supports CommonJS, not ES modules, so you'll have to do a little extra work to opt in to ES modules, either [for all your test files](https://jestjs.io/docs/ecmascript-modules) or by configuring Jest to [transpile fake-indexeddb](https://stackoverflow.com/a/43197503/786644). If you can't do that, you should use fake-indexeddb v3, see the above "ECMAScript modules" section for more info.
|
|
94
119
|
|
|
95
|
-
To use
|
|
120
|
+
To use fake-indexeddb in a single Jest test suite, require `fake-indexeddb/auto` at the beginning of the test
|
|
96
121
|
file, as described above.
|
|
97
122
|
|
|
98
123
|
To use it on all Jest tests without having to require it in each file, add the auto setup script to the `setupFiles` in your Jest config:
|
|
@@ -108,14 +133,14 @@ To use it on all Jest tests without having to require it in each file, add the a
|
|
|
108
133
|
|
|
109
134
|
### Wiping/resetting the indexedDB for a fresh state
|
|
110
135
|
|
|
111
|
-
If you are keeping your tests completely isolated you might want to "reset" the state of the mocked indexedDB. You can do this by creating a new `
|
|
136
|
+
If you are keeping your tests completely isolated you might want to "reset" the state of the mocked indexedDB. You can do this by creating a new instance of `IDBFactory`, which lets you have a totally fresh start.
|
|
112
137
|
|
|
113
138
|
```
|
|
114
|
-
|
|
115
|
-
|
|
139
|
+
import "fake-indexeddb/auto";
|
|
140
|
+
import { IDBFactory } from "fake-indexeddb";
|
|
116
141
|
|
|
117
142
|
// Whenever you want a fresh indexedDB
|
|
118
|
-
indexedDB = new
|
|
143
|
+
indexedDB = new IDBFactory();
|
|
119
144
|
```
|
|
120
145
|
|
|
121
146
|
### With PhantomJS and other really old environments
|
|
@@ -123,8 +148,8 @@ indexedDB = new FDBFactory();
|
|
|
123
148
|
PhantomJS (and other really old environments) are missing tons of modern JavaScript features. In fact, that may be why you use fake-indexeddb in such an environment! Prior to v3.0.0, fake-indexeddb imported core-js and automatically applied its polyfills. However, since most fake-indexeddb users are not using really old environments, I got rid of that runtime dependency in v3.0.0. To work around that, you can import core-js yourself before you import fake-indexeddb, like:
|
|
124
149
|
|
|
125
150
|
```js
|
|
126
|
-
|
|
127
|
-
|
|
151
|
+
import "core-js/stable";
|
|
152
|
+
import "fake-indexeddb/auto";
|
|
128
153
|
```
|
|
129
154
|
|
|
130
155
|
## Quality
|
package/auto.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module "fake-indexeddb/auto";
|
package/auto.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import fakeIndexedDB from "./build/fakeIndexedDB.js";
|
|
2
|
+
import FDBCursor from "./build/FDBCursor.js";
|
|
3
|
+
import FDBCursorWithValue from "./build/FDBCursorWithValue.js";
|
|
4
|
+
import FDBDatabase from "./build/FDBDatabase.js";
|
|
5
|
+
import FDBFactory from "./build/FDBFactory.js";
|
|
6
|
+
import FDBIndex from "./build/FDBIndex.js";
|
|
7
|
+
import FDBKeyRange from "./build/FDBKeyRange.js";
|
|
8
|
+
import FDBObjectStore from "./build/FDBObjectStore.js";
|
|
9
|
+
import FDBOpenDBRequest from "./build/FDBOpenDBRequest.js";
|
|
10
|
+
import FDBRequest from "./build/FDBRequest.js";
|
|
11
|
+
import FDBTransaction from "./build/FDBTransaction.js";
|
|
12
|
+
import FDBVersionChangeEvent from "./build/FDBVersionChangeEvent.js";
|
|
13
13
|
|
|
14
14
|
// http://stackoverflow.com/a/33268326/786644 - works in browser, worker, and Node.js
|
|
15
15
|
var globalVar =
|