Skip to content

Commit 4647efc

Browse files
authored
feat: sql.js support dump (#18)
* feat: impl for sqlite in main and renderer process * feat: db export for sql.js
1 parent 8cff7ed commit 4647efc

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/local-storage/renderer/main.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class MainApp {
149149
locateFile: () => '../../../node_modules/sql.js/dist/sql-wasm.wasm',
150150
});
151151

152-
const dataPromise = fetch('../../../test.sqlite').then(res => res.arrayBuffer());
152+
const dataPromise = fetch('../sqlite/test-sql.js.db').then(res => res.arrayBuffer());
153153
const [SQL, buf] = await Promise.all([sqlPromise, dataPromise]);
154154
const db = new SQL.Database(new Uint8Array(buf));
155155

@@ -165,6 +165,35 @@ class MainApp {
165165
}
166166
const endTime = Date.now();
167167
this.log(type, `end time: ${endTime}, use ${((endTime - startTime) / 1000).toFixed(2)}s`);
168+
this._dump(db);
169+
}
170+
171+
_dump(db) {
172+
// core dump as file
173+
const binaryArray = db.export();
174+
// download this binaryArray as a file
175+
const blob = new Blob([binaryArray], { type: 'application/octet-stream' });
176+
const url = URL.createObjectURL(blob);
177+
const a = document.createElement('a');
178+
a.download = 'test-sql.js.db';
179+
a.href = url;
180+
a.click();
181+
182+
// const [fileHandle] = await window.showOpenFilePicker({
183+
// types: [
184+
// {
185+
// description: 'sql.js dump file',
186+
// accept: {
187+
// 'text/plain': ['.db'],
188+
// },
189+
// },
190+
// ],
191+
// });
192+
// const writable = await fileHandle.createWritable();
193+
// // Write the contents of the file to the stream.
194+
// await writable.write(binaryArray);
195+
// // Close the file and write the contents to disk.
196+
// await writable.close();
168197
}
169198

170199
async runSQLiteWASMInWorker(type) {
@@ -215,7 +244,7 @@ class MainApp {
215244

216245
worker.onerror = e => console.log('Worker error: ', e);
217246

218-
const buf = await fetch('../../../test.sqlite')
247+
const buf = await fetch('../sqlite/test-sql.js-worker.db')
219248
.then(res => res.arrayBuffer());
220249

221250
worker.postMessage({
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)