Skip to content

Commit cfb7dfc

Browse files
committed
Add idb 7
1 parent 95c59fe commit cfb7dfc

File tree

7 files changed

+64
-32
lines changed

7 files changed

+64
-32
lines changed

packages/app/package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@
3737
"typings:internal": "node ../../scripts/build/use_typings.js ./dist/app.d.ts"
3838
},
3939
"dependencies": {
40-
"@firebase/util": "1.5.1",
41-
"@firebase/logger": "0.3.2",
4240
"@firebase/component": "0.5.12",
41+
"@firebase/logger": "0.3.2",
42+
"@firebase/util": "1.5.1",
43+
"idb": "7.0.1",
4344
"tslib": "^2.1.0"
4445
},
4546
"license": "Apache-2.0",
4647
"devDependencies": {
47-
"rollup": "2.57.0",
4848
"@rollup/plugin-json": "4.1.0",
49+
"rollup": "2.57.0",
4950
"rollup-plugin-replace": "2.2.0",
5051
"rollup-plugin-typescript2": "0.30.0",
5152
"typescript": "4.2.2"
@@ -65,4 +66,4 @@
6566
],
6667
"reportDir": "./coverage/node"
6768
}
68-
}
69+
}

packages/app/src/indexeddb.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,26 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { DBWrapper, openDB } from '@firebase/util';
18+
import { DBSchema, openDB, IDBPDatabase } from 'idb';
1919
import { AppError, ERROR_FACTORY } from './errors';
2020
import { FirebaseApp } from './public-types';
2121
import { HeartbeatsInIndexedDB } from './types';
22+
2223
const DB_NAME = 'firebase-heartbeat-database';
2324
const DB_VERSION = 1;
2425
const STORE_NAME = 'firebase-heartbeat-store';
2526

26-
let dbPromise: Promise<DBWrapper> | null = null;
27-
function getDbPromise(): Promise<DBWrapper> {
27+
interface AppDB extends DBSchema {
28+
'firebase-heartbeat-store': {
29+
key: string,
30+
value: HeartbeatsInIndexedDB
31+
}
32+
}
33+
34+
let dbPromise: Promise<IDBPDatabase<AppDB>> | null = null;
35+
function getDbPromise(): Promise<IDBPDatabase<AppDB>> {
2836
if (!dbPromise) {
29-
dbPromise = openDB(DB_NAME, DB_VERSION, (db, oldVersion) => {
37+
dbPromise = openDB<AppDB>(DB_NAME, DB_VERSION, {upgrade: (db, oldVersion) => {
3038
// We don't use 'break' in this switch statement, the fall-through
3139
// behavior is what we want, because if there are multiple versions between
3240
// the old version and the current version, we want ALL the migrations
@@ -36,7 +44,7 @@ function getDbPromise(): Promise<DBWrapper> {
3644
case 0:
3745
db.createObjectStore(STORE_NAME);
3846
}
39-
}).catch(e => {
47+
}}).catch(e => {
4048
throw ERROR_FACTORY.create(AppError.STORAGE_OPEN, {
4149
originalErrorMessage: e.message
4250
});
@@ -70,7 +78,7 @@ export async function writeHeartbeatsToIndexedDB(
7078
const tx = db.transaction(STORE_NAME, 'readwrite');
7179
const objectStore = tx.objectStore(STORE_NAME);
7280
await objectStore.put(heartbeatObject, computeKey(app));
73-
return tx.complete;
81+
return tx.done;
7482
} catch (e) {
7583
throw ERROR_FACTORY.create(AppError.STORAGE_WRITE, {
7684
originalErrorMessage: e.message

packages/installations/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"dependencies": {
6565
"@firebase/util": "1.5.1",
6666
"@firebase/component": "0.5.12",
67+
"idb": "7.0.1",
6768
"tslib": "^2.1.0"
6869
}
6970
}

packages/installations/src/helpers/idb-manager.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { DBWrapper, openDB } from '@firebase/util';
18+
import { DBSchema, IDBPDatabase, openDB } from 'idb';
1919
import { AppConfig } from '../interfaces/installation-impl';
2020
import { InstallationEntry } from '../interfaces/installation-entry';
2121
import { getKey } from '../util/get-key';
@@ -25,10 +25,17 @@ const DATABASE_NAME = 'firebase-installations-database';
2525
const DATABASE_VERSION = 1;
2626
const OBJECT_STORE_NAME = 'firebase-installations-store';
2727

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>> {
3037
if (!dbPromise) {
31-
dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, (db, oldVersion) => {
38+
dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, {upgrade: (db, oldVersion) => {
3239
// We don't use 'break' in this switch statement, the fall-through
3340
// behavior is what we want, because if there are multiple versions between
3441
// the old version and the current version, we want ALL the migrations
@@ -38,7 +45,7 @@ function getDbPromise(): Promise<DBWrapper> {
3845
case 0:
3946
db.createObjectStore(OBJECT_STORE_NAME);
4047
}
41-
});
48+
}});
4249
}
4350
return dbPromise;
4451
}
@@ -66,7 +73,7 @@ export async function set<ValueType extends InstallationEntry>(
6673
const objectStore = tx.objectStore(OBJECT_STORE_NAME);
6774
const oldValue = (await objectStore.get(key)) as InstallationEntry;
6875
await objectStore.put(value, key);
69-
await tx.complete;
76+
await tx.done;
7077

7178
if (!oldValue || oldValue.fid !== value.fid) {
7279
fidChanged(appConfig, value.fid);
@@ -81,7 +88,7 @@ export async function remove(appConfig: AppConfig): Promise<void> {
8188
const db = await getDbPromise();
8289
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
8390
await tx.objectStore(OBJECT_STORE_NAME).delete(key);
84-
await tx.complete;
91+
await tx.done;
8592
}
8693

8794
/**
@@ -108,7 +115,7 @@ export async function update<ValueType extends InstallationEntry | undefined>(
108115
} else {
109116
await store.put(newValue, key);
110117
}
111-
await tx.complete;
118+
await tx.done;
112119

113120
if (newValue && (!oldValue || oldValue.fid !== newValue.fid)) {
114121
fidChanged(appConfig, newValue.fid);
@@ -121,5 +128,5 @@ export async function clear(): Promise<void> {
121128
const db = await getDbPromise();
122129
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
123130
await tx.objectStore(OBJECT_STORE_NAME).clear();
124-
await tx.complete;
131+
await tx.done;
125132
}

packages/messaging/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"@firebase/messaging-interop-types": "0.1.0",
5757
"@firebase/util": "1.5.1",
5858
"@firebase/component": "0.5.12",
59+
"idb": "7.0.1",
5960
"tslib": "^2.1.0"
6061
},
6162
"devDependencies": {

packages/messaging/src/internals/idb-manager.ts

+22-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { DBWrapper, deleteDB, openDB } from '@firebase/util';
18+
import { DBSchema, IDBPDatabase, deleteDB, openDB } from 'idb';
1919

2020
import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';
2121
import { TokenDetails } from '../interfaces/token-details';
@@ -26,20 +26,29 @@ export const DATABASE_NAME = 'firebase-messaging-database';
2626
const DATABASE_VERSION = 1;
2727
const OBJECT_STORE_NAME = 'firebase-messaging-store';
2828

29-
let dbPromise: Promise<DBWrapper> | null = null;
30-
function getDbPromise(): Promise<DBWrapper> {
29+
interface MessagingDB extends DBSchema {
30+
'firebase-messaging-store': {
31+
key: string,
32+
value: TokenDetails
33+
}
34+
}
35+
36+
let dbPromise: Promise<IDBPDatabase<MessagingDB>> | null = null;
37+
function getDbPromise(): Promise<IDBPDatabase<MessagingDB>> {
3138
if (!dbPromise) {
3239
dbPromise = openDB(
3340
DATABASE_NAME,
3441
DATABASE_VERSION,
35-
(upgradeDb, oldVersion) => {
36-
// We don't use 'break' in this switch statement, the fall-through behavior is what we want,
37-
// because if there are multiple versions between the old version and the current version, we
38-
// want ALL the migrations that correspond to those versions to run, not only the last one.
39-
// eslint-disable-next-line default-case
40-
switch (oldVersion) {
41-
case 0:
42-
upgradeDb.createObjectStore(OBJECT_STORE_NAME);
42+
{
43+
upgrade: (upgradeDb, oldVersion) => {
44+
// We don't use 'break' in this switch statement, the fall-through behavior is what we want,
45+
// because if there are multiple versions between the old version and the current version, we
46+
// want ALL the migrations that correspond to those versions to run, not only the last one.
47+
// eslint-disable-next-line default-case
48+
switch (oldVersion) {
49+
case 0:
50+
upgradeDb.createObjectStore(OBJECT_STORE_NAME);
51+
}
4352
}
4453
}
4554
);
@@ -81,7 +90,7 @@ export async function dbSet(
8190
const db = await getDbPromise();
8291
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
8392
await tx.objectStore(OBJECT_STORE_NAME).put(tokenDetails, key);
84-
await tx.complete;
93+
await tx.done;
8594
return tokenDetails;
8695
}
8796

@@ -93,7 +102,7 @@ export async function dbRemove(
93102
const db = await getDbPromise();
94103
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
95104
await tx.objectStore(OBJECT_STORE_NAME).delete(key);
96-
await tx.complete;
105+
await tx.done;
97106
}
98107

99108
/** Deletes the DB. Useful for tests. */

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -9015,6 +9015,11 @@ [email protected], iconv-lite@^0.6.2:
90159015
dependencies:
90169016
safer-buffer ">= 2.1.2 < 3.0.0"
90179017

9018+
9019+
version "7.0.1"
9020+
resolved "https://registry.npmjs.org/idb/-/idb-7.0.1.tgz#d2875b3a2f205d854ee307f6d196f246fea590a7"
9021+
integrity sha512-UUxlE7vGWK5RfB/fDwEGgRf84DY/ieqNha6msMV99UsEMQhJ1RwbCd8AYBj3QMgnE3VZnfQvm4oKVCJTYlqIgg==
9022+
90189023
ieee754@^1.1.13, ieee754@^1.1.4:
90199024
version "1.2.1"
90209025
resolved "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"

0 commit comments

Comments
 (0)