Skip to content

Commit

Permalink
Proposal for adding transform to export/import
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander committed Dec 13, 2023
1 parent 3f27079 commit 24de368
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
14 changes: 12 additions & 2 deletions addons/dexie-export-import/src/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ExportOptions {
numRowsPerChunk?: number;
prettyJson?: boolean;
filter?: (table: string, value: any, key?: any) => boolean;
transform?: (table: string, value: any, key?: any) => ({value: any, key?: any});
progressCallback?: (progress: ExportProgress) => boolean;
}

Expand Down Expand Up @@ -76,6 +77,7 @@ export async function exportDB(db: Dexie, options?: ExportOptions): Promise<Blob
slices.push(firstJsonSlice);

const filter = options!.filter;
const transform = options!.transform;

for (const {name: tableName} of tables) {
const table = db.table(tableName);
Expand Down Expand Up @@ -138,7 +140,11 @@ export async function exportDB(db: Dexie, options?: ExportOptions): Promise<Blob
values.filter(value => filter(tableName, value)) :
values;

const tsonValues = filteredValues.map(value => TSON.encapsulate(value));
const transformedValues = transform ?
filteredValues.map(value => transform(tableName, value).value) :
filteredValues;

const tsonValues = transformedValues.map(value => TSON.encapsulate(value));
if (TSON.mustFinalize()) {
await Dexie.waitFor(TSON.finalize(tsonValues));
}
Expand All @@ -149,14 +155,18 @@ export async function exportDB(db: Dexie, options?: ExportOptions): Promise<Blob
// By generating a blob here, we give web platform the opportunity to store the contents
// on disk and release RAM.
slices.push(new Blob([json.substring(1, json.length - 1)]));
lastNumRows = filteredValues.length;
lastNumRows = transformedValues.length;
lastKey = values.length > 0 ?
Dexie.getByKeyPath(values[values.length -1], primKey.keyPath as string) :
null;
} else {
const keys = await chunkedCollection.primaryKeys();
let keyvals = keys.map((key, i) => [key, values[i]]);
if (filter) keyvals = keyvals.filter(([key, value]) => filter(tableName, value, key));
if (transform) keyvals = keyvals.map(([key, value]) => {
const transformResult = transform(tableName, value, key);
return [transformResult.key, transformResult.value];
});

const tsonTuples = keyvals.map(tuple => TSON.encapsulate(tuple));
if (TSON.mustFinalize()) {
Expand Down
14 changes: 13 additions & 1 deletion addons/dexie-export-import/src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface StaticImportOptions {
noTransaction?: boolean;
chunkSizeBytes?: number; // Default: DEFAULT_KILOBYTES_PER_CHUNK ( 1MB )
filter?: (table: string, value: any, key?: any) => boolean;
transform?: (table: string, value: any, key?: any) => ({value: any, key?: any});
progressCallback?: (progress: ImportProgress) => boolean;
}

Expand All @@ -21,6 +22,7 @@ export interface ImportOptions extends StaticImportOptions {
noTransaction?: boolean;
chunkSizeBytes?: number; // Default: DEFAULT_KILOBYTES_PER_CHUNK ( 1MB )
filter?: (table: string, value: any, key?: any) => boolean;
transform?: (table: string, value: any, key?: any) => ({value: any, key?: any});
progressCallback?: (progress: ImportProgress) => boolean;
}

Expand Down Expand Up @@ -138,11 +140,21 @@ export async function importInto(db: Dexie, exportedData: Blob | JsonStream<Dexi
}

const filter = options!.filter;
const filteredRows = filter ?
const transform = options!.transform;

let filteredRows = filter ?
tableExport.inbound ?
rows.filter(value => filter(tableName, value)) :
rows.filter(([key, value]) => filter(tableName, value, key)) :
rows;
if (transform) {
filteredRows = filteredRows.map(tableExport.inbound ?
value => transform(tableName, value).value :
([key, value]) => {
const res = transform(tableName, value, key)
return [res.key, res.value];
});
}
const [keys, values] = tableExport.inbound ?
[undefined, filteredRows] :
[filteredRows.map(row=>row[0]), rows.map(row=>row[1])];
Expand Down

0 comments on commit 24de368

Please sign in to comment.