15
15
* limitations under the License.
16
16
*/
17
17
18
- import { DBWrapper , openDB } from '@firebase/util ' ;
18
+ import { DBSchema , IDBPDatabase , openDB } from 'idb ' ;
19
19
import { AppConfig } from '../interfaces/installation-impl' ;
20
20
import { InstallationEntry } from '../interfaces/installation-entry' ;
21
21
import { getKey } from '../util/get-key' ;
@@ -25,18 +25,27 @@ const DATABASE_NAME = 'firebase-installations-database';
25
25
const DATABASE_VERSION = 1 ;
26
26
const OBJECT_STORE_NAME = 'firebase-installations-store' ;
27
27
28
- let dbPromise : Promise < DBWrapper > | null = null ;
29
- function getDbPromise ( ) : Promise < DBWrapper > {
28
+ interface InstallationsDB extends DBSchema {
29
+ 'firebase-installations-store' : {
30
+ key : string ;
31
+ value : InstallationEntry | undefined ;
32
+ } ;
33
+ }
34
+
35
+ let dbPromise : Promise < IDBPDatabase < InstallationsDB > > | null = null ;
36
+ function getDbPromise ( ) : Promise < IDBPDatabase < InstallationsDB > > {
30
37
if ( ! dbPromise ) {
31
- dbPromise = openDB ( DATABASE_NAME , DATABASE_VERSION , ( db , oldVersion ) => {
32
- // We don't use 'break' in this switch statement, the fall-through
33
- // behavior is what we want, because if there are multiple versions between
34
- // the old version and the current version, we want ALL the migrations
35
- // that correspond to those versions to run, not only the last one.
36
- // eslint-disable-next-line default-case
37
- switch ( oldVersion ) {
38
- case 0 :
39
- db . createObjectStore ( OBJECT_STORE_NAME ) ;
38
+ dbPromise = openDB ( DATABASE_NAME , DATABASE_VERSION , {
39
+ upgrade : ( db , oldVersion ) => {
40
+ // We don't use 'break' in this switch statement, the fall-through
41
+ // behavior is what we want, because if there are multiple versions between
42
+ // the old version and the current version, we want ALL the migrations
43
+ // that correspond to those versions to run, not only the last one.
44
+ // eslint-disable-next-line default-case
45
+ switch ( oldVersion ) {
46
+ case 0 :
47
+ db . createObjectStore ( OBJECT_STORE_NAME ) ;
48
+ }
40
49
}
41
50
} ) ;
42
51
}
@@ -66,7 +75,7 @@ export async function set<ValueType extends InstallationEntry>(
66
75
const objectStore = tx . objectStore ( OBJECT_STORE_NAME ) ;
67
76
const oldValue = ( await objectStore . get ( key ) ) as InstallationEntry ;
68
77
await objectStore . put ( value , key ) ;
69
- await tx . complete ;
78
+ await tx . done ;
70
79
71
80
if ( ! oldValue || oldValue . fid !== value . fid ) {
72
81
fidChanged ( appConfig , value . fid ) ;
@@ -81,7 +90,7 @@ export async function remove(appConfig: AppConfig): Promise<void> {
81
90
const db = await getDbPromise ( ) ;
82
91
const tx = db . transaction ( OBJECT_STORE_NAME , 'readwrite' ) ;
83
92
await tx . objectStore ( OBJECT_STORE_NAME ) . delete ( key ) ;
84
- await tx . complete ;
93
+ await tx . done ;
85
94
}
86
95
87
96
/**
@@ -108,7 +117,7 @@ export async function update<ValueType extends InstallationEntry | undefined>(
108
117
} else {
109
118
await store . put ( newValue , key ) ;
110
119
}
111
- await tx . complete ;
120
+ await tx . done ;
112
121
113
122
if ( newValue && ( ! oldValue || oldValue . fid !== newValue . fid ) ) {
114
123
fidChanged ( appConfig , newValue . fid ) ;
@@ -121,5 +130,5 @@ export async function clear(): Promise<void> {
121
130
const db = await getDbPromise ( ) ;
122
131
const tx = db . transaction ( OBJECT_STORE_NAME , 'readwrite' ) ;
123
132
await tx . objectStore ( OBJECT_STORE_NAME ) . clear ( ) ;
124
- await tx . complete ;
133
+ await tx . done ;
125
134
}
0 commit comments