Last active
January 14, 2022 04:13
-
-
Save jfrancos/bb11ec6cada6a8fc0addc6d688f66ede to your computer and use it in GitHub Desktop.
Turn an RxDB `bulkInsert` into an exported indexeddb.json file using Node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'fake-indexeddb/auto'; | |
import { readFile, writeFile } from 'fs/promises'; | |
import { exportToJsonString } from 'indexeddb-export-import'; | |
import { getDatabase } from './Database'; | |
const inFile = 'bulkInsertableData.json'; | |
const outFile = 'indexeddb.json'; | |
const collectionName = 'collection'; | |
const prepIndexedDB = async () => { | |
const buffer = await readFile(inFile); | |
const object = JSON.parse(buffer.toString()); | |
const db = await getDatabase(); | |
await db[collectionName].bulkInsert(object); | |
await db.remove(); | |
const request = indexedDB.open(`${db.name}.db`); | |
request.onsuccess = () => { | |
exportToJsonString(request.result, (err: Event, idbAsString: string) => { | |
if (err) { | |
console.log({ err }); | |
} | |
writeFile(outFile, idbAsString); | |
}); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where
getDatabase
is as per the examples e.g.And I use it like
Then to restore: