Skip to content

Commit 1912280

Browse files
committed
Update app, installations, messaging to use new idb methods
1 parent 6785a68 commit 1912280

File tree

9 files changed

+220
-118
lines changed

9 files changed

+220
-118
lines changed

packages/app/src/indexeddb.ts

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

18-
import { DB, openDb } from 'idb';
18+
import { DBWrapper, openDB } from '@firebase/util';
1919
import { AppError, ERROR_FACTORY } from './errors';
2020
import { FirebaseApp } from './public-types';
2121
import { HeartbeatsInIndexedDB } from './types';
2222
const DB_NAME = 'firebase-heartbeat-database';
2323
const DB_VERSION = 1;
2424
const STORE_NAME = 'firebase-heartbeat-store';
2525

26-
let dbPromise: Promise<DB> | null = null;
27-
function getDbPromise(): Promise<DB> {
26+
let dbPromise: Promise<DBWrapper> | null = null;
27+
function getDbPromise(): Promise<DBWrapper> {
2828
if (!dbPromise) {
29-
dbPromise = openDb(DB_NAME, DB_VERSION, upgradeDB => {
29+
dbPromise = openDB(DB_NAME, DB_VERSION, (db, oldVersion) => {
3030
// We don't use 'break' in this switch statement, the fall-through
3131
// behavior is what we want, because if there are multiple versions between
3232
// the old version and the current version, we want ALL the migrations
3333
// that correspond to those versions to run, not only the last one.
3434
// eslint-disable-next-line default-case
35-
switch (upgradeDB.oldVersion) {
35+
switch (oldVersion) {
3636
case 0:
37-
upgradeDB.createObjectStore(STORE_NAME);
37+
db.createObjectStore(STORE_NAME);
3838
}
3939
}).catch(e => {
4040
throw ERROR_FACTORY.create(AppError.STORAGE_OPEN, {
@@ -53,7 +53,7 @@ export async function readHeartbeatsFromIndexedDB(
5353
return db
5454
.transaction(STORE_NAME)
5555
.objectStore(STORE_NAME)
56-
.get(computeKey(app));
56+
.get(computeKey(app)) as Promise<HeartbeatsInIndexedDB | undefined>;
5757
} catch (e) {
5858
throw ERROR_FACTORY.create(AppError.STORAGE_GET, {
5959
originalErrorMessage: e.message

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

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

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

28-
let dbPromise: Promise<DB> | null = null;
29-
function getDbPromise(): Promise<DB> {
28+
let dbPromise: Promise<DBWrapper> | null = null;
29+
function getDbPromise(): Promise<DBWrapper> {
3030
if (!dbPromise) {
31-
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDB => {
31+
dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, (db, oldVersion) => {
3232
// We don't use 'break' in this switch statement, the fall-through
3333
// behavior is what we want, because if there are multiple versions between
3434
// the old version and the current version, we want ALL the migrations
3535
// that correspond to those versions to run, not only the last one.
3636
// eslint-disable-next-line default-case
37-
switch (upgradeDB.oldVersion) {
37+
switch (oldVersion) {
3838
case 0:
39-
upgradeDB.createObjectStore(OBJECT_STORE_NAME);
39+
db.createObjectStore(OBJECT_STORE_NAME);
4040
}
4141
});
4242
}
@@ -52,7 +52,7 @@ export async function get(
5252
return db
5353
.transaction(OBJECT_STORE_NAME)
5454
.objectStore(OBJECT_STORE_NAME)
55-
.get(key);
55+
.get(key) as Promise<InstallationEntry>;
5656
}
5757

5858
/** Assigns or overwrites the record for the given key with the given value. */
@@ -64,7 +64,7 @@ export async function set<ValueType extends InstallationEntry>(
6464
const db = await getDbPromise();
6565
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
6666
const objectStore = tx.objectStore(OBJECT_STORE_NAME);
67-
const oldValue = await objectStore.get(key);
67+
const oldValue = (await objectStore.get(key)) as InstallationEntry;
6868
await objectStore.put(value, key);
6969
await tx.complete;
7070

@@ -98,7 +98,9 @@ export async function update<ValueType extends InstallationEntry | undefined>(
9898
const db = await getDbPromise();
9999
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
100100
const store = tx.objectStore(OBJECT_STORE_NAME);
101-
const oldValue: InstallationEntry | undefined = await store.get(key);
101+
const oldValue: InstallationEntry | undefined = (await store.get(
102+
key
103+
)) as InstallationEntry;
102104
const newValue = updateFn(oldValue);
103105

104106
if (newValue === undefined) {

packages/messaging/src/helpers/migrate-old-database.test.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { FakePushSubscription } from '../testing/fakes/service-worker';
2828
import { base64ToArray } from './array-base64-translator';
2929
import { expect } from 'chai';
3030
import { getFakeTokenDetails } from '../testing/fakes/token-details';
31-
import { openDb } from 'idb';
31+
import { openDB } from '@firebase/util';
3232

3333
describe('migrateOldDb', () => {
3434
it("does nothing if old DB didn't exist", async () => {
@@ -179,14 +179,11 @@ describe('migrateOldDb', () => {
179179
});
180180

181181
async function put(version: number, value: object): Promise<void> {
182-
const db = await openDb('fcm_token_details_db', version, upgradeDb => {
183-
if (upgradeDb.oldVersion === 0) {
184-
const objectStore = upgradeDb.createObjectStore(
185-
'fcm_token_object_Store',
186-
{
187-
keyPath: 'swScope'
188-
}
189-
);
182+
const db = await openDB('fcm_token_details_db', version, (db, oldVersion) => {
183+
if (oldVersion === 0) {
184+
const objectStore = db.createObjectStore('fcm_token_object_Store', {
185+
keyPath: 'swScope'
186+
});
190187
objectStore.createIndex('fcmSenderId', 'fcmSenderId', {
191188
unique: false
192189
});

packages/messaging/src/helpers/migrate-old-database.ts

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

18-
import { deleteDb, openDb } from 'idb';
18+
import { deleteDB, openDB } from '@firebase/util';
1919

2020
import { TokenDetails } from '../interfaces/token-details';
2121
import { arrayToBase64 } from './array-base64-translator';
@@ -88,83 +88,87 @@ export async function migrateOldDatabase(
8888

8989
let tokenDetails: TokenDetails | null = null;
9090

91-
const db = await openDb(OLD_DB_NAME, OLD_DB_VERSION, async db => {
92-
if (db.oldVersion < 2) {
93-
// Database too old, skip migration.
94-
return;
95-
}
96-
97-
if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
98-
// Database did not exist. Nothing to do.
99-
return;
100-
}
101-
102-
const objectStore = db.transaction.objectStore(OLD_OBJECT_STORE_NAME);
103-
const value = await objectStore.index('fcmSenderId').get(senderId);
104-
await objectStore.clear();
91+
const db = await openDB(
92+
OLD_DB_NAME,
93+
OLD_DB_VERSION,
94+
async (db, oldVersion, newVersion, upgradeTransaction) => {
95+
if (oldVersion < 2) {
96+
// Database too old, skip migration.
97+
return;
98+
}
10599

106-
if (!value) {
107-
// No entry in the database, nothing to migrate.
108-
return;
109-
}
100+
if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
101+
// Database did not exist. Nothing to do.
102+
return;
103+
}
110104

111-
if (db.oldVersion === 2) {
112-
const oldDetails = value as V2TokenDetails;
105+
const objectStore = upgradeTransaction.objectStore(OLD_OBJECT_STORE_NAME);
106+
const value = await objectStore.index('fcmSenderId').get(senderId);
107+
await objectStore.clear();
113108

114-
if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {
109+
if (!value) {
110+
// No entry in the database, nothing to migrate.
115111
return;
116112
}
117113

118-
tokenDetails = {
119-
token: oldDetails.fcmToken,
120-
createTime: oldDetails.createTime ?? Date.now(),
121-
subscriptionOptions: {
122-
auth: oldDetails.auth,
123-
p256dh: oldDetails.p256dh,
124-
endpoint: oldDetails.endpoint,
125-
swScope: oldDetails.swScope,
126-
vapidKey:
127-
typeof oldDetails.vapidKey === 'string'
128-
? oldDetails.vapidKey
129-
: arrayToBase64(oldDetails.vapidKey)
130-
}
131-
};
132-
} else if (db.oldVersion === 3) {
133-
const oldDetails = value as V3TokenDetails;
134-
135-
tokenDetails = {
136-
token: oldDetails.fcmToken,
137-
createTime: oldDetails.createTime,
138-
subscriptionOptions: {
139-
auth: arrayToBase64(oldDetails.auth),
140-
p256dh: arrayToBase64(oldDetails.p256dh),
141-
endpoint: oldDetails.endpoint,
142-
swScope: oldDetails.swScope,
143-
vapidKey: arrayToBase64(oldDetails.vapidKey)
144-
}
145-
};
146-
} else if (db.oldVersion === 4) {
147-
const oldDetails = value as V4TokenDetails;
148-
149-
tokenDetails = {
150-
token: oldDetails.fcmToken,
151-
createTime: oldDetails.createTime,
152-
subscriptionOptions: {
153-
auth: arrayToBase64(oldDetails.auth),
154-
p256dh: arrayToBase64(oldDetails.p256dh),
155-
endpoint: oldDetails.endpoint,
156-
swScope: oldDetails.swScope,
157-
vapidKey: arrayToBase64(oldDetails.vapidKey)
114+
if (oldVersion === 2) {
115+
const oldDetails = value as V2TokenDetails;
116+
117+
if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {
118+
return;
158119
}
159-
};
120+
121+
tokenDetails = {
122+
token: oldDetails.fcmToken,
123+
createTime: oldDetails.createTime ?? Date.now(),
124+
subscriptionOptions: {
125+
auth: oldDetails.auth,
126+
p256dh: oldDetails.p256dh,
127+
endpoint: oldDetails.endpoint,
128+
swScope: oldDetails.swScope,
129+
vapidKey:
130+
typeof oldDetails.vapidKey === 'string'
131+
? oldDetails.vapidKey
132+
: arrayToBase64(oldDetails.vapidKey)
133+
}
134+
};
135+
} else if (oldVersion === 3) {
136+
const oldDetails = value as V3TokenDetails;
137+
138+
tokenDetails = {
139+
token: oldDetails.fcmToken,
140+
createTime: oldDetails.createTime,
141+
subscriptionOptions: {
142+
auth: arrayToBase64(oldDetails.auth),
143+
p256dh: arrayToBase64(oldDetails.p256dh),
144+
endpoint: oldDetails.endpoint,
145+
swScope: oldDetails.swScope,
146+
vapidKey: arrayToBase64(oldDetails.vapidKey)
147+
}
148+
};
149+
} else if (oldVersion === 4) {
150+
const oldDetails = value as V4TokenDetails;
151+
152+
tokenDetails = {
153+
token: oldDetails.fcmToken,
154+
createTime: oldDetails.createTime,
155+
subscriptionOptions: {
156+
auth: arrayToBase64(oldDetails.auth),
157+
p256dh: arrayToBase64(oldDetails.p256dh),
158+
endpoint: oldDetails.endpoint,
159+
swScope: oldDetails.swScope,
160+
vapidKey: arrayToBase64(oldDetails.vapidKey)
161+
}
162+
};
163+
}
160164
}
161-
});
165+
);
162166
db.close();
163167

164168
// Delete all old databases.
165-
await deleteDb(OLD_DB_NAME);
166-
await deleteDb('fcm_vapid_details_db');
167-
await deleteDb('undefined');
169+
await deleteDB(OLD_DB_NAME);
170+
await deleteDB('fcm_vapid_details_db');
171+
await deleteDB('undefined');
168172

169173
return checkTokenDetails(tokenDetails) ? tokenDetails : null;
170174
}

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

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

18-
import { DB, deleteDb, openDb } from 'idb';
18+
import { DBWrapper, deleteDB, openDB } from '@firebase/util';
1919

2020
import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';
2121
import { TokenDetails } from '../interfaces/token-details';
@@ -26,15 +26,15 @@ 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<DB> | null = null;
30-
function getDbPromise(): Promise<DB> {
29+
let dbPromise: Promise<DBWrapper> | null = null;
30+
function getDbPromise(): Promise<DBWrapper> {
3131
if (!dbPromise) {
32-
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDb => {
32+
dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, (upgradeDb, oldVersion) => {
3333
// We don't use 'break' in this switch statement, the fall-through behavior is what we want,
3434
// because if there are multiple versions between the old version and the current version, we
3535
// want ALL the migrations that correspond to those versions to run, not only the last one.
3636
// eslint-disable-next-line default-case
37-
switch (upgradeDb.oldVersion) {
37+
switch (oldVersion) {
3838
case 0:
3939
upgradeDb.createObjectStore(OBJECT_STORE_NAME);
4040
}
@@ -52,7 +52,7 @@ export async function dbGet(
5252
const tokenDetails = await db
5353
.transaction(OBJECT_STORE_NAME)
5454
.objectStore(OBJECT_STORE_NAME)
55-
.get(key);
55+
.get(key) as TokenDetails;
5656

5757
if (tokenDetails) {
5858
return tokenDetails;
@@ -96,7 +96,7 @@ export async function dbRemove(
9696
export async function dbDelete(): Promise<void> {
9797
if (dbPromise) {
9898
(await dbPromise).close();
99-
await deleteDb(DATABASE_NAME);
99+
await deleteDB(DATABASE_NAME);
100100
dbPromise = null;
101101
}
102102
}

packages/messaging/src/testing/setup.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import chaiAsPromised from 'chai-as-promised';
1919
import sinonChai from 'sinon-chai';
2020

2121
import { dbDelete } from '../internals/idb-manager';
22-
import { deleteDb } from 'idb';
22+
import { deleteDB } from '@firebase/util';
2323
import { restore } from 'sinon';
2424
import { use } from 'chai';
2525

@@ -29,5 +29,5 @@ use(sinonChai);
2929
afterEach(async () => {
3030
restore();
3131
await dbDelete();
32-
await deleteDb('fcm_token_details_db');
32+
await deleteDB('fcm_token_details_db');
3333
});

packages/util/index.node.ts

+3
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ export * from './src/utf8';
3939
export * from './src/exponential_backoff';
4040
export * from './src/formatters';
4141
export * from './src/compat';
42+
// This can't be used in Node but it will cause errors if libraries import
43+
// these methods and they aren't here.
44+
export * from './src/indexeddb';

packages/util/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ export * from './src/utf8';
3434
export * from './src/exponential_backoff';
3535
export * from './src/formatters';
3636
export * from './src/compat';
37+
export * from './src/indexeddb';

0 commit comments

Comments
 (0)