-
-
Notifications
You must be signed in to change notification settings - Fork 627
/
export.ts
205 lines (181 loc) · 6.91 KB
/
export.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import Dexie from 'dexie';
import { getSchemaString, extractDbSchema } from './helpers';
import { DexieExportedTable, DexieExportJsonStructure } from './json-structure';
import { TSON } from './tson';
export interface ExportOptions {
skipTables?: string[],
noTransaction?: boolean;
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;
}
export interface ExportProgress {
totalTables: number;
completedTables: number;
totalRows: number | undefined;
completedRows: number;
done: boolean;
}
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 = targetTables.map(table => ({
name: table.name,
schema: getSchemaString(table),
rowCount: 0
}));
const {prettyJson} = options!;
const emptyExport: DexieExportJsonStructure = {
formatName: "dexie",
formatVersion: 1,
data: {
databaseName: db.name,
databaseVersion: db.verno,
tables: tables,
data: []
}
};
const {progressCallback} = options!;
const progress: ExportProgress = {
done: false,
completedRows: 0,
completedTables: 0,
totalRows: NaN,
totalTables: tables.length
};
try {
if (options!.noTransaction) {
await exportAll();
} else {
await db.transaction('r', db.tables, exportAll);
}
} finally {
TSON.finalize(); // Free up mem if error has occurred
}
return new Blob(slices,{type: "text/json"});
async function exportAll() {
// Count rows:
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);
// Write first JSON slice
const emptyExportJson = JSON.stringify(emptyExport, undefined, prettyJson ? 2 : undefined);
const posEndDataArray = emptyExportJson.lastIndexOf(']');
const firstJsonSlice = emptyExportJson.substring(0, posEndDataArray);
slices.push(firstJsonSlice);
const filter = options!.filter;
const transform = options!.transform;
for (const {name: tableName} of tables) {
const table = db.table(tableName);
const {primKey} = table.schema;
const inbound = !!primKey.keyPath;
const LIMIT = options!.numRowsPerChunk || DEFAULT_ROWS_PER_CHUNK;
const emptyTableExport: DexieExportedTable = inbound ? {
tableName: table.name,
inbound: true,
rows: []
} : {
tableName: table.name,
inbound: false,
rows: []
};
let emptyTableExportJson = JSON.stringify(emptyTableExport, undefined, prettyJson ? 2 : undefined);
if (prettyJson) {
// Increase indentation according to this:
// {
// ...
// data: [
// ...
// data: [
// 123456<---- here
// ]
// ]
// }
emptyTableExportJson = emptyTableExportJson.split('\n').join('\n ');
}
const posEndRowsArray = emptyTableExportJson.lastIndexOf(']');
slices.push(emptyTableExportJson.substring(0, posEndRowsArray));
let lastKey: any = null;
let lastNumRows = 0;
let mayHaveMoreRows = true;
while (mayHaveMoreRows) {
if (progressCallback) {
// Keep ongoing transaction private
Dexie.ignoreTransaction(()=>progressCallback(progress));
}
const chunkedCollection = lastKey == null ?
table.limit(LIMIT) :
table.where(':id').above(lastKey).limit(LIMIT);
const values = await chunkedCollection.toArray();
if (values.length === 0) break;
if (lastKey != null && lastNumRows > 0) {
// Not initial chunk. Must add a comma:
slices.push(",");
if (prettyJson) {
slices.push("\n ");
}
}
mayHaveMoreRows = values.length === LIMIT;
if (inbound) {
const filteredValues = filter ?
values.filter(value => filter(tableName, value)) :
values;
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));
}
let json = JSON.stringify(tsonValues, undefined, prettyJson ? 2 : undefined);
if (prettyJson) json = json.split('\n').join('\n ');
// 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 = 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()) {
await Dexie.waitFor(TSON.finalize(tsonTuples));
}
let json = JSON.stringify(tsonTuples, undefined, prettyJson ? 2 : undefined);
if (prettyJson) json = json.split('\n').join('\n ');
// 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 = keyvals.length;
lastKey = keys.length > 0 ?
keys[keys.length - 1] :
null;
}
progress.completedRows += values.length;
}
slices.push(emptyTableExportJson.substr(posEndRowsArray)); // "]}"
progress.completedTables += 1;
if (progress.completedTables < progress.totalTables) {
slices.push(",");
}
}
slices.push(emptyExportJson.substr(posEndDataArray));
progress.done = true;
if (progressCallback) {
// Keep ongoing transaction private
Dexie.ignoreTransaction(()=>progressCallback(progress));
}
}
}