Skip to content

Commit

Permalink
Ability to SkipTables
Browse files Browse the repository at this point in the history
  • Loading branch information
nlaurie committed Feb 5, 2024
1 parent da68afd commit ec840ba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions addons/dexie-export-import/src/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DexieExportedTable, DexieExportJsonStructure } from './json-structure';
import { TSON } from './tson';

export interface ExportOptions {
skipTables?: string[],
noTransaction?: boolean;
numRowsPerChunk?: number;
prettyJson?: boolean;
Expand All @@ -25,8 +26,10 @@ const DEFAULT_ROWS_PER_CHUNK = 2000;

export async function exportDB(db: Dexie, options?: ExportOptions): Promise<Blob> {
options = options || {};
const skipTables = options.skipTables? options.skipTables: []
const targetTables = db.tables.filter((x)=> !skipTables.includes(x.name))
const slices: (string | Blob)[] = [];
const tables = db.tables.map(table => ({
const tables = targetTables.map(table => ({
name: table.name,
schema: getSchemaString(table),
rowCount: 0
Expand All @@ -49,7 +52,7 @@ export async function exportDB(db: Dexie, options?: ExportOptions): Promise<Blob
completedRows: 0,
completedTables: 0,
totalRows: NaN,
totalTables: db.tables.length
totalTables: tables.length
};

try {
Expand All @@ -66,7 +69,7 @@ export async function exportDB(db: Dexie, options?: ExportOptions): Promise<Blob

async function exportAll() {
// Count rows:
const tablesRowCounts = await Promise.all(db.tables.map(table => table.count()));
const tablesRowCounts = await Promise.all(targetTables.map(table => table.count()));
tablesRowCounts.forEach((rowCount, i) => tables[i].rowCount = rowCount);
progress.totalRows = tablesRowCounts.reduce((p,c)=>p+c);

Expand Down
4 changes: 4 additions & 0 deletions addons/dexie-export-import/src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface ImportOptions extends StaticImportOptions {
acceptChangedPrimaryKey?: boolean;
overwriteValues?: boolean;
clearTablesBeforeImport?: boolean;
skipTables?: string[],
noTransaction?: boolean;
chunkSizeBytes?: number; // Default: DEFAULT_KILOBYTES_PER_CHUNK ( 1MB )
filter?: (table: string, value: any, key?: any) => boolean;
Expand Down Expand Up @@ -68,6 +69,7 @@ export async function importInto(db: Dexie, exportedData: Blob | JsonStream<Dexi
const readBlobsSynchronously = 'FileReaderSync' in self; // true in workers only.

const dbExport = dbExportFile.data!;
const skipTables = options.skipTables? options.skipTables: []

if (!options!.acceptNameDiff && db.name !== dbExport.databaseName)
throw new Error(`Name differs. Current database name is ${db.name} but export is ${dbExport.databaseName}`);
Expand All @@ -91,6 +93,7 @@ export async function importInto(db: Dexie, exportedData: Blob | JsonStream<Dexi

if (options!.clearTablesBeforeImport) {
for (const table of db.tables) {
if(skipTables.includes(table.name) ) continue;
await table.clear();
}
}
Expand All @@ -104,6 +107,7 @@ export async function importInto(db: Dexie, exportedData: Blob | JsonStream<Dexi
async function importAll () {
do {
for (const tableExport of dbExport.data) {
if(skipTables.includes(tableExport.tableName)) continue;
if (!tableExport.rows) break; // Need to pull more!
if (!(tableExport.rows as any).incomplete && tableExport.rows.length === 0)
continue;
Expand Down

0 comments on commit ec840ba

Please sign in to comment.