Skip to content

Commit

Permalink
Allow the user to import into a different database than the one speci…
Browse files Browse the repository at this point in the history
…fied in the file.
  • Loading branch information
tobiasBora committed Apr 29, 2024
1 parent b2fd7c0 commit 6cd5f7b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions addons/dexie-export-import/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface StaticImportOptions {
chunkSizeBytes?: number; // Default: DEFAULT_KILOBYTES_PER_CHUNK ( 1MB )
filter?: (table: string, value: any, key?: any) => boolean;
progressCallback?: (progress: ImportProgress) => boolean;
getNewDatabaseName?: (databaseName: string, dbExport: DexieExportJsonStructure['data']) => Promise<string>;
}

export interface ImportOptions extends StaticImportOptions {
Expand All @@ -132,6 +133,8 @@ export interface ImportOptions extends StaticImportOptions {

```

The `getNewDatabaseName` functions accepts notably the name of the database as specified in the file to import, and must output the name of the new database to create (make sure to delete it inside `getNewDatabaseName` if it already exists).

## ImportProgress

This is the interface sent to the progressCallback.
Expand Down
5 changes: 4 additions & 1 deletion addons/dexie-export-import/src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface StaticImportOptions {
filter?: (table: string, value: any, key?: any) => boolean;
transform?: (table: string, value: any, key?: any) => ({value: any, key?: any});
progressCallback?: (progress: ImportProgress) => boolean;
getNewDatabaseName?: (databaseName: string, dbExport: DexieExportJsonStructure['data']) => Promise<string>;
}

export interface ImportOptions extends StaticImportOptions {
Expand Down Expand Up @@ -42,7 +43,9 @@ export async function importDB(exportedData: Blob | JsonStream<DexieExportJsonSt
const CHUNK_SIZE = options!.chunkSizeBytes || (DEFAULT_KILOBYTES_PER_CHUNK * 1024);
const stream = await loadUntilWeGotEnoughData(exportedData, CHUNK_SIZE);
const dbExport = stream.result.data!;
const db = new Dexie(dbExport.databaseName);
const dbName = await (options?.getNewDatabaseName || (x => (x)))(dbExport.databaseName, dbExport);
dbExport.databaseName = dbName;
const db = new Dexie(dbName);
db.version(dbExport.databaseVersion).stores(extractDbSchema(dbExport));
await importInto(db, stream, options);
return db;
Expand Down

0 comments on commit 6cd5f7b

Please sign in to comment.