From d4e9c972140becbd2664c5137e4cb48f372c7ed3 Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:27:58 -0400 Subject: [PATCH 01/12] wip feat: decoupling blobs from idb --- .../RecordingDbServiceIndexedDbLive.svelte.ts | 116 ++++++++++++++---- 1 file changed, 94 insertions(+), 22 deletions(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index fa29204ac..0aba08560 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -5,9 +5,24 @@ import type { Recording } from './RecordingDbService'; import { RecordingsDbService } from './RecordingDbService'; const DB_NAME = 'RecordingDB' as const; -const DB_VERSION = 1 as const; const RECORDING_STORE = 'recordings' as const; +const DB_VERSION = 2 as const; +const RECORDING_METADATA_STORE = 'recordingMetadata' as const; +const RECORDING_BLOB_STORE = 'recordingBlobs' as const; + +interface RecordingsDbSchema extends DBSchema { + recordingMetadata: { + key: Recording['id']; + value: Omit; + indexes: { 'by-createdAt': number }; + }; + recordingBlobs: { + key: Recording['id']; + value: { id: Recording['id']; blob: Blob }; + }; +} + interface RecordingsDbSchema extends DBSchema { recordings: { key: Recording['id']; @@ -19,17 +34,42 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( RecordingsDbService, Effect.sync(() => { const db = openDB(DB_NAME, DB_VERSION, { - upgrade(db) { - const isRecordingStoreObjectStoreExists = db.objectStoreNames.contains(RECORDING_STORE); - if (!isRecordingStoreObjectStoreExists) { - db.createObjectStore(RECORDING_STORE, { keyPath: 'id' }); + upgrade(db, oldVersion, newVersion, transaction) { + if (oldVersion < 1) { + // This handles the case of a fresh install + db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id' }); + db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id' }); + } + + if (oldVersion < 2) { + // This handles the upgrade from version 1 to 2 + const recordingsStore = transaction.objectStore(RECORDING_STORE); + const metadataStore = db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id' }); + const blobStore = db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id' }); + + // Delete the old store after migration + db.deleteObjectStore(RECORDING_STORE); } }, }); + return { addRecording: (recording) => Effect.tryPromise({ - try: async () => (await db).add(RECORDING_STORE, recording), + try: async () => { + const { blob, ...metadata } = recording; + const tx = (await db).transaction( + [RECORDING_METADATA_STORE, RECORDING_BLOB_STORE], + 'readwrite', + ); + const recordingMetadataStore = tx.objectStore(RECORDING_METADATA_STORE); + const recordingBlobStore = tx.objectStore(RECORDING_BLOB_STORE); + await Promise.all([ + recordingMetadataStore.add(metadata), + recordingBlobStore.add({ id: recording.id, blob }), + tx.done, + ]); + }, catch: (error) => new WhisperingError({ title: 'Error adding recording to indexedDB', @@ -39,17 +79,35 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( }), updateRecording: (recording) => Effect.tryPromise({ - try: async () => (await db).put(RECORDING_STORE, $state.snapshot(recording)), + try: async () => { + const { blob, ...metadata } = recording; + await Promise.all([ + (await db).put(RECORDING_METADATA_STORE, metadata), + (await db).put(RECORDING_BLOB_STORE, { id: recording.id, blob }), + ]); + }, catch: (error) => new WhisperingError({ - title: 'Error editing recording in indexedDB', + title: 'Error updating recording in indexedDB', description: error instanceof Error ? error.message : 'Please try again.', error, }), }), deleteRecordingById: (id) => Effect.tryPromise({ - try: async () => (await db).delete(RECORDING_STORE, id), + try: async () => { + const tx = (await db).transaction( + [RECORDING_METADATA_STORE, RECORDING_BLOB_STORE], + 'readwrite', + ); + const recordingMetadataStore = tx.objectStore(RECORDING_METADATA_STORE); + const recordingBlobStore = tx.objectStore(RECORDING_BLOB_STORE); + await Promise.all([ + recordingMetadataStore.delete(id), + recordingBlobStore.delete(id), + tx.done, + ]); + }, catch: (error) => new WhisperingError({ title: 'Error deleting recording from indexedDB', @@ -60,28 +118,42 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( deleteRecordingsById: (ids) => Effect.tryPromise({ try: async () => { - const tx = (await db).transaction(RECORDING_STORE, 'readwrite'); - await Promise.all([...ids.map((id) => tx.store.delete(id)), tx.done]); + const tx = (await db).transaction( + [RECORDING_METADATA_STORE, RECORDING_BLOB_STORE], + 'readwrite', + ); + const recordingMetadataStore = tx.objectStore(RECORDING_METADATA_STORE); + const recordingBlobStore = tx.objectStore(RECORDING_BLOB_STORE); + for (const id of ids) { + await recordingMetadataStore.delete(id); + await recordingBlobStore.delete(id); + } + await tx.done; }, catch: (error) => new WhisperingError({ - title: 'Error deleting recording from indexedDB', + title: 'Error deleting recordings from indexedDB', description: error instanceof Error ? error.message : 'Please try again.', error, }), }), - getAllRecordings: Effect.tryPromise({ - try: async () => (await db).getAll(RECORDING_STORE), - catch: (error) => - new WhisperingError({ - title: 'Error getting all recordings from indexedDB', - description: error instanceof Error ? error.message : 'Please try again.', - error, - }), - }), getRecording: (id) => Effect.tryPromise({ - try: async () => (await db).get(RECORDING_STORE, id), + try: async () => { + const tx = (await db).transaction( + [RECORDING_METADATA_STORE, RECORDING_BLOB_STORE], + 'readonly', + ); + const recordingMetadataStore = tx.objectStore(RECORDING_METADATA_STORE); + const recordingBlobStore = tx.objectStore(RECORDING_BLOB_STORE); + const metadata = await recordingMetadataStore.get(id); + const blobData = await recordingBlobStore.get(id); + await tx.done; + if (metadata && blobData) { + return { ...metadata, blob: blobData.blob }; + } + return null; + }, catch: (error) => new WhisperingError({ title: 'Error getting recording from indexedDB', From d90a4afd7e785e0d73b5b26051dfd2d8d6286790 Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:37:38 -0400 Subject: [PATCH 02/12] feat: working recording db separated into two schemas! --- .../RecordingDbServiceIndexedDbLive.svelte.ts | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index 0aba08560..55f733ab2 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -34,7 +34,7 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( RecordingsDbService, Effect.sync(() => { const db = openDB(DB_NAME, DB_VERSION, { - upgrade(db, oldVersion, newVersion, transaction) { + async upgrade(db, oldVersion, newVersion, transaction) { if (oldVersion < 1) { // This handles the case of a fresh install db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id' }); @@ -42,13 +42,24 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( } if (oldVersion < 2) { - // This handles the upgrade from version 1 to 2 const recordingsStore = transaction.objectStore(RECORDING_STORE); const metadataStore = db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id' }); const blobStore = db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id' }); + const recordings = await recordingsStore.getAll(); + await Promise.all( + recordings.map(async (recording) => { + const { blob, ...metadata } = recording; + await Promise.all([ + metadataStore.add(metadata), + blobStore.add({ id: recording.id, blob }), + ]); + }), + ); + // Delete the old store after migration db.deleteObjectStore(RECORDING_STORE); + await transaction.done; } }, }); @@ -137,6 +148,31 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( error, }), }), + getAllRecordings: Effect.tryPromise({ + try: async () => { + const tx = (await db).transaction( + [RECORDING_METADATA_STORE, RECORDING_BLOB_STORE], + 'readonly', + ); + const recordingMetadataStore = tx.objectStore(RECORDING_METADATA_STORE); + const recordingBlobStore = tx.objectStore(RECORDING_BLOB_STORE); + const metadata = await recordingMetadataStore.getAll(); + const blobs = await recordingBlobStore.getAll(); + await tx.done; + return metadata + .map((recording) => { + const blob = blobs.find((blob) => blob.id === recording.id)?.blob; + return blob ? { ...recording, blob } : null; + }) + .filter((r) => r !== null); + }, + catch: (error) => + new WhisperingError({ + title: 'Error getting recordings from indexedDB', + description: error instanceof Error ? error.message : 'Please try again.', + error, + }), + }), getRecording: (id) => Effect.tryPromise({ try: async () => { From fee25369e4002b57e6a21e9c7c26895b270db056 Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:41:02 -0400 Subject: [PATCH 03/12] refactor: dbPromise --- .../RecordingDbServiceIndexedDbLive.svelte.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index 55f733ab2..b49e4dca7 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -33,7 +33,7 @@ interface RecordingsDbSchema extends DBSchema { export const RecordingsDbServiceLiveIndexedDb = Layer.effect( RecordingsDbService, Effect.sync(() => { - const db = openDB(DB_NAME, DB_VERSION, { + const dbPromise = openDB(DB_NAME, DB_VERSION, { async upgrade(db, oldVersion, newVersion, transaction) { if (oldVersion < 1) { // This handles the case of a fresh install @@ -69,7 +69,7 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( Effect.tryPromise({ try: async () => { const { blob, ...metadata } = recording; - const tx = (await db).transaction( + const tx = (await dbPromise).transaction( [RECORDING_METADATA_STORE, RECORDING_BLOB_STORE], 'readwrite', ); @@ -93,8 +93,8 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( try: async () => { const { blob, ...metadata } = recording; await Promise.all([ - (await db).put(RECORDING_METADATA_STORE, metadata), - (await db).put(RECORDING_BLOB_STORE, { id: recording.id, blob }), + (await dbPromise).put(RECORDING_METADATA_STORE, metadata), + (await dbPromise).put(RECORDING_BLOB_STORE, { id: recording.id, blob }), ]); }, catch: (error) => @@ -107,7 +107,7 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( deleteRecordingById: (id) => Effect.tryPromise({ try: async () => { - const tx = (await db).transaction( + const tx = (await dbPromise).transaction( [RECORDING_METADATA_STORE, RECORDING_BLOB_STORE], 'readwrite', ); @@ -129,7 +129,7 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( deleteRecordingsById: (ids) => Effect.tryPromise({ try: async () => { - const tx = (await db).transaction( + const tx = (await dbPromise).transaction( [RECORDING_METADATA_STORE, RECORDING_BLOB_STORE], 'readwrite', ); @@ -150,7 +150,7 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( }), getAllRecordings: Effect.tryPromise({ try: async () => { - const tx = (await db).transaction( + const tx = (await dbPromise).transaction( [RECORDING_METADATA_STORE, RECORDING_BLOB_STORE], 'readonly', ); @@ -176,7 +176,7 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( getRecording: (id) => Effect.tryPromise({ try: async () => { - const tx = (await db).transaction( + const tx = (await dbPromise).transaction( [RECORDING_METADATA_STORE, RECORDING_BLOB_STORE], 'readonly', ); From 56175e593b3268c9777e15d4652cacd524fca559 Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:44:18 -0400 Subject: [PATCH 04/12] refactor: RecordingsDbSchemaV2 --- .../lib/services/RecordingDbServiceIndexedDbLive.svelte.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index b49e4dca7..6d3eb6442 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -11,7 +11,7 @@ const DB_VERSION = 2 as const; const RECORDING_METADATA_STORE = 'recordingMetadata' as const; const RECORDING_BLOB_STORE = 'recordingBlobs' as const; -interface RecordingsDbSchema extends DBSchema { +interface RecordingsDbSchemaV2 extends DBSchema { recordingMetadata: { key: Recording['id']; value: Omit; @@ -23,13 +23,15 @@ interface RecordingsDbSchema extends DBSchema { }; } -interface RecordingsDbSchema extends DBSchema { +interface RecordingsDbSchemaV1 extends DBSchema { recordings: { key: Recording['id']; value: Recording; }; } +type RecordingsDbSchema = RecordingsDbSchemaV2 & RecordingsDbSchemaV1; + export const RecordingsDbServiceLiveIndexedDb = Layer.effect( RecordingsDbService, Effect.sync(() => { From 3f1f57c31fd4dd4cc42e07af3e1f92e20858fb02 Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:45:15 -0400 Subject: [PATCH 05/12] chore: remove unused index in types --- .../src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index 6d3eb6442..df8ac594c 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -15,7 +15,6 @@ interface RecordingsDbSchemaV2 extends DBSchema { recordingMetadata: { key: Recording['id']; value: Omit; - indexes: { 'by-createdAt': number }; }; recordingBlobs: { key: Recording['id']; From 82ed26fdd962a9b76dac9b1195858152b4d5e45b Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:53:27 -0400 Subject: [PATCH 06/12] feat: migration steps --- .../lib/services/RecordingDbServiceIndexedDbLive.svelte.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index df8ac594c..858fc8254 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -36,15 +36,16 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( Effect.sync(() => { const dbPromise = openDB(DB_NAME, DB_VERSION, { async upgrade(db, oldVersion, newVersion, transaction) { - if (oldVersion < 1) { - // This handles the case of a fresh install + if (oldVersion === 0) { db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id' }); db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id' }); } if (oldVersion < 2) { const recordingsStore = transaction.objectStore(RECORDING_STORE); - const metadataStore = db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id' }); + const metadataStore = db.createObjectStore(RECORDING_METADATA_STORE, { + keyPath: 'id', + }); const blobStore = db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id' }); const recordings = await recordingsStore.getAll(); From 302a607e9e129e6d252d0341d60c3d0ba130660b Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 13:53:39 -0400 Subject: [PATCH 07/12] chore: comments --- .../src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index 858fc8254..ae529d0e2 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -37,11 +37,13 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( const dbPromise = openDB(DB_NAME, DB_VERSION, { async upgrade(db, oldVersion, newVersion, transaction) { if (oldVersion === 0) { + // Fresh install db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id' }); db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id' }); } if (oldVersion < 2) { + // Upgrade from v1 to v2 const recordingsStore = transaction.objectStore(RECORDING_STORE); const metadataStore = db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id', From 084bb99c36f1b8343cec32b3f1f4626fd0efced0 Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 16:22:23 -0400 Subject: [PATCH 08/12] refactor: more explicit upgrade numbers --- .../src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index ae529d0e2..f89953c75 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -42,7 +42,7 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id' }); } - if (oldVersion < 2) { + if (oldVersion === 1 && newVersion === 2) { // Upgrade from v1 to v2 const recordingsStore = transaction.objectStore(RECORDING_STORE); const metadataStore = db.createObjectStore(RECORDING_METADATA_STORE, { From aefcd4cbaf81817f1758a2672565347aaf8dcff1 Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 16:46:18 -0400 Subject: [PATCH 09/12] refactor: line orders --- .../lib/services/RecordingDbServiceIndexedDbLive.svelte.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index f89953c75..bbca85cff 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -5,9 +5,9 @@ import type { Recording } from './RecordingDbService'; import { RecordingsDbService } from './RecordingDbService'; const DB_NAME = 'RecordingDB' as const; -const RECORDING_STORE = 'recordings' as const; - const DB_VERSION = 2 as const; + +const RECORDING_STORE = 'recordings' as const; const RECORDING_METADATA_STORE = 'recordingMetadata' as const; const RECORDING_BLOB_STORE = 'recordingBlobs' as const; From ae167b477bb37b0f2de4a9103fc43cc2aac48a1b Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 16:57:26 -0400 Subject: [PATCH 10/12] refactor: all transaction.db.createObjecStore --- .../RecordingDbServiceIndexedDbLive.svelte.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index bbca85cff..2d2156420 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -38,17 +38,19 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( async upgrade(db, oldVersion, newVersion, transaction) { if (oldVersion === 0) { // Fresh install - db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id' }); - db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id' }); + transaction.db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id' }); + transaction.db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id' }); } if (oldVersion === 1 && newVersion === 2) { // Upgrade from v1 to v2 const recordingsStore = transaction.objectStore(RECORDING_STORE); - const metadataStore = db.createObjectStore(RECORDING_METADATA_STORE, { + const metadataStore = transaction.db.createObjectStore(RECORDING_METADATA_STORE, { + keyPath: 'id', + }); + const blobStore = transaction.db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id', }); - const blobStore = db.createObjectStore(RECORDING_BLOB_STORE, { keyPath: 'id' }); const recordings = await recordingsStore.getAll(); await Promise.all( @@ -62,7 +64,7 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( ); // Delete the old store after migration - db.deleteObjectStore(RECORDING_STORE); + transaction.db.deleteObjectStore(RECORDING_STORE); await transaction.done; } }, From 81ea2728c7755477bd7f3aa18d89844c9a6adc8b Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 17:00:21 -0400 Subject: [PATCH 11/12] rename: DEPRECATED_RECORDING_STORE --- .../lib/services/RecordingDbServiceIndexedDbLive.svelte.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index 2d2156420..7c74ea546 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -7,9 +7,9 @@ import { RecordingsDbService } from './RecordingDbService'; const DB_NAME = 'RecordingDB' as const; const DB_VERSION = 2 as const; -const RECORDING_STORE = 'recordings' as const; const RECORDING_METADATA_STORE = 'recordingMetadata' as const; const RECORDING_BLOB_STORE = 'recordingBlobs' as const; +const DEPRECATED_RECORDING_STORE = 'recordings' as const; interface RecordingsDbSchemaV2 extends DBSchema { recordingMetadata: { @@ -44,7 +44,7 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( if (oldVersion === 1 && newVersion === 2) { // Upgrade from v1 to v2 - const recordingsStore = transaction.objectStore(RECORDING_STORE); + const recordingsStore = transaction.objectStore(DEPRECATED_RECORDING_STORE); const metadataStore = transaction.db.createObjectStore(RECORDING_METADATA_STORE, { keyPath: 'id', }); @@ -64,7 +64,7 @@ export const RecordingsDbServiceLiveIndexedDb = Layer.effect( ); // Delete the old store after migration - transaction.db.deleteObjectStore(RECORDING_STORE); + transaction.db.deleteObjectStore(DEPRECATED_RECORDING_STORE); await transaction.done; } }, From d58fe69e172fd11f04700a5222f0ad5cfa762a52 Mon Sep 17 00:00:00 2001 From: Braden Wong <13159333+braden-w@users.noreply.github.com> Date: Thu, 15 Aug 2024 17:01:19 -0400 Subject: [PATCH 12/12] refactor: use constants in keys of interface --- .../lib/services/RecordingDbServiceIndexedDbLive.svelte.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts index 7c74ea546..71b03c57d 100644 --- a/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts +++ b/apps/app/src/lib/services/RecordingDbServiceIndexedDbLive.svelte.ts @@ -12,18 +12,18 @@ const RECORDING_BLOB_STORE = 'recordingBlobs' as const; const DEPRECATED_RECORDING_STORE = 'recordings' as const; interface RecordingsDbSchemaV2 extends DBSchema { - recordingMetadata: { + [RECORDING_METADATA_STORE]: { key: Recording['id']; value: Omit; }; - recordingBlobs: { + [RECORDING_BLOB_STORE]: { key: Recording['id']; value: { id: Recording['id']; blob: Blob }; }; } interface RecordingsDbSchemaV1 extends DBSchema { - recordings: { + [DEPRECATED_RECORDING_STORE]: { key: Recording['id']; value: Recording; };