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.
Files changed (62) hide show
  1. package/README.md +47 -22
  2. package/auto.d.ts +1 -0
  3. package/auto.js +12 -12
  4. package/build/FDBCursor.js +238 -333
  5. package/build/FDBCursorWithValue.js +9 -27
  6. package/build/FDBDatabase.js +76 -119
  7. package/build/FDBFactory.js +97 -165
  8. package/build/FDBIndex.js +102 -109
  9. package/build/FDBKeyRange.js +29 -36
  10. package/build/FDBObjectStore.js +177 -185
  11. package/build/FDBOpenDBRequest.js +10 -28
  12. package/build/FDBRequest.js +34 -60
  13. package/build/FDBTransaction.js +89 -139
  14. package/build/FDBVersionChangeEvent.js +10 -29
  15. package/build/fakeIndexedDB.js +3 -5
  16. package/build/index.js +14 -4
  17. package/build/lib/Database.js +12 -16
  18. package/build/lib/FakeDOMStringList.js +66 -0
  19. package/build/lib/FakeEvent.js +10 -14
  20. package/build/lib/FakeEventTarget.js +38 -86
  21. package/build/lib/Index.js +61 -140
  22. package/build/lib/KeyGenerator.js +12 -15
  23. package/build/lib/ObjectStore.js +69 -167
  24. package/build/lib/RecordStore.js +55 -61
  25. package/build/lib/binarySearch.js +22 -29
  26. package/build/lib/canInjectKey.js +10 -34
  27. package/build/lib/cmp.js +13 -15
  28. package/build/lib/enforceRange.js +4 -6
  29. package/build/lib/errors.js +61 -126
  30. package/build/lib/extractKey.js +17 -41
  31. package/build/lib/scheduling.js +3 -5
  32. package/build/lib/structuredClone.js +12 -7
  33. package/build/lib/types.js +1 -2
  34. package/build/lib/validateKeyPath.js +7 -41
  35. package/build/lib/valueToKey.js +14 -16
  36. package/build/lib/valueToKeyRange.js +10 -13
  37. package/lib/FDBCursor.d.ts +2 -0
  38. package/lib/FDBCursorWithValue.d.ts +2 -0
  39. package/lib/FDBDatabase.d.ts +2 -0
  40. package/lib/FDBFactory.d.ts +2 -0
  41. package/lib/FDBIndex.d.ts +2 -0
  42. package/lib/FDBKeyRange.d.ts +2 -0
  43. package/lib/FDBObjectStore.d.ts +2 -0
  44. package/lib/FDBOpenDBRequest.d.ts +2 -0
  45. package/lib/FDBRequest.d.ts +2 -0
  46. package/lib/FDBTransaction.d.ts +2 -0
  47. package/lib/FDBVersionChangeEvent.d.ts +2 -0
  48. package/lib/fakeIndexedDB.d.ts +2 -0
  49. package/package.json +44 -26
  50. package/types.d.ts +29 -0
  51. package/build/lib/fakeDOMStringList.js +0 -16
  52. package/lib/FDBCursor.js +0 -5
  53. package/lib/FDBCursorWithValue.js +0 -5
  54. package/lib/FDBDatabase.js +0 -5
  55. package/lib/FDBFactory.js +0 -5
  56. package/lib/FDBIndex.js +0 -5
  57. package/lib/FDBKeyRange.js +0 -5
  58. package/lib/FDBObjectStore.js +0 -5
  59. package/lib/FDBOpenDBRequest.js +0 -5
  60. package/lib/FDBRequest.js +0 -5
  61. package/lib/FDBTransaction.js +0 -5
  62. 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 objects in the global scope:
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
- require("fake-indexeddb/auto");
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 objects:
57
+ Alternatively, you can explicitly import individual IndexedDB variables:
58
58
 
59
59
  ```js
60
- var indexedDB = require("fake-indexeddb");
61
- var IDBKeyRange = require("fake-indexeddb/lib/FDBKeyRange");
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
- When importing individual classes directly (like `var IDBKeyRange =
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
- ### With Dexie and other IndexedDB API wrappers
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
- const Dexie = require("dexie");
76
- require("fake-indexeddb/auto");
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
- const Dexie = require("dexie");
87
- const indexedDB = require("fake-indexeddb");
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
- ### With Jest
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 this on a single Jest test suite, require `fake-indexeddb/auto` at the beginning of the test
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 `fakeIndexedDB` instance, which lets you have a totally fresh start.
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
- require("fake-indexeddb/auto");
115
- const FDBFactory = require("fake-indexeddb/lib/FDBFactory");
139
+ import "fake-indexeddb/auto";
140
+ import { IDBFactory } from "fake-indexeddb";
116
141
 
117
142
  // Whenever you want a fresh indexedDB
118
- indexedDB = new FDBFactory();
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
- require("core-js/stable");
127
- var indexedDB = require("fake-indexeddb");
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
- var fakeIndexedDB = require("./build/fakeIndexedDB").default;
2
- var FDBCursor = require("./build/FDBCursor").default;
3
- var FDBCursorWithValue = require("./build/FDBCursorWithValue").default;
4
- var FDBDatabase = require("./build/FDBDatabase").default;
5
- var FDBFactory = require("./build/FDBFactory").default;
6
- var FDBIndex = require("./build/FDBIndex").default;
7
- var FDBKeyRange = require("./build/FDBKeyRange").default;
8
- var FDBObjectStore = require("./build/FDBObjectStore").default;
9
- var FDBOpenDBRequest = require("./build/FDBOpenDBRequest").default;
10
- var FDBRequest = require("./build/FDBRequest").default;
11
- var FDBTransaction = require("./build/FDBTransaction").default;
12
- var FDBVersionChangeEvent = require("./build/FDBVersionChangeEvent").default;
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 =