{"id":16397,"library":"indexeddb-export-import","title":"IndexedDB Export/Import Utility","description":"indexeddb-export-import is a JavaScript utility for serializing and deserializing IndexedDB databases to and from JSON format. It provides functions to export an entire database to a JSON string, import data from a JSON string into an existing database, and clear all data from a database. The package is currently at version 2.1.5 and maintains an active release cadence, primarily focusing on bug fixes and minor improvements. A key differentiator is its minimal footprint, having zero runtime dependencies since version 2.0.0, making it lightweight for both Node.js (especially Electron apps) and browser environments, where it can be included directly via a `<script>` tag. It requires ES6 (Node.js 8+ or modern browsers) since its v2 release, offering a clean API for database backup, restoration, and development/testing workflows.","status":"active","version":"2.1.5","language":"javascript","source_language":"en","source_url":"https://github.com/Polarisation/indexeddb-export-import","tags":["javascript","IndexedDB","JSON","import","export","serialize","deserialize","backup","restore"],"install":[{"cmd":"npm install indexeddb-export-import","lang":"bash","label":"npm"},{"cmd":"yarn add indexeddb-export-import","lang":"bash","label":"yarn"},{"cmd":"pnpm add indexeddb-export-import","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"While CommonJS `require` works, the package is primarily ES module focused since v2.0.0, so named imports or `import * as` is the recommended modern approach.","wrong":"const IDBExportImport = require('indexeddb-export-import');","symbol":"IDBExportImport","correct":"import * as IDBExportImport from 'indexeddb-export-import';"},{"note":"The functions are exported as named members of the module. Direct destructuring `const { func } = require()` is a valid CommonJS pattern, but `import { func } from '...'` is preferred for ESM.","wrong":"const { exportToJsonString } = require('indexeddb-export-import');","symbol":"exportToJsonString","correct":"import { exportToJsonString } from 'indexeddb-export-import';"},{"note":"Used for restoring database content from a JSON string.","symbol":"importFromJsonString","correct":"import { importFromJsonString } from 'indexeddb-export-import';"}],"quickstart":{"code":"import Dexie from 'dexie';\nimport { exportToJsonString, clearDatabase, importFromJsonString } from 'indexeddb-export-import';\n\nconst db = new Dexie('MyDB');\ndb.version(1).stores({\n  things: 'id++, thing_name, thing_description'\n});\n\ndb.open().then(function() {\n  // Dexie.js provides a convenient way to get the native IDBDatabase object\n  const idbDatabase = db.backendDB(); \n\n  // Export to JSON, clear database, and then import from JSON\n  exportToJsonString(idbDatabase, function(err, jsonString) {\n    if (err) {\n      console.error('Error exporting:', err);\n      return;\n    }\n    console.log('Exported as JSON:', jsonString.substring(0, 100) + '...'); // Log a snippet\n    \n    clearDatabase(idbDatabase, function(err) {\n      if (err) {\n        console.error('Error clearing database:', err);\n        return;\n      }\n      console.log('Database cleared successfully.');\n      \n      importFromJsonString(idbDatabase, jsonString, function(err) {\n        if (err) {\n          console.error('Error importing:', err);\n          return;\n        }\n        console.log('Data imported successfully.');\n        // You can now verify the data is back in the DB\n        db.table('things').count().then(count => {\n          console.log(`Things in DB after import: ${count}`);\n        });\n      });\n    });\n  });\n}).catch(function(e) {\n  console.error('Could not connect to IndexedDB: ' + e);\n});","lang":"typescript","description":"This example demonstrates how to export an IndexedDB database to a JSON string, clear all its object stores, and then re-import the data from the previously exported JSON string. It uses Dexie.js to manage the IndexedDB connection for convenience, which is a common pattern for obtaining an `IDBDatabase` instance."},"warnings":[{"fix":"Upgrade your Node.js runtime to version 8 or higher, or ensure your browser environment supports ES6. If targeting ES5, consider using Babel or downgrade to a v1.x version of `indexeddb-export-import`.","message":"Version 2.0.0 introduced a requirement for ES6, meaning Node.js 8+ or a modern browser. If you need to support older environments (like ES5), you must use a transpiler or stick with the v1.x series of this package.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Update to version 2.1.5 or later to ensure robust handling of object store key mismatches during import. This version fixes issues where import JSON is missing keys or contains keys not present as object store names.","message":"Prior to v2.1.3 and v2.1.5, importing JSON data could lead to bugs if the import JSON structure had keys that did not exactly match the existing object store names in the IndexedDB database. This could result in data not being imported correctly or errors during the process.","severity":"gotcha","affected_versions":"<2.1.5"},{"fix":"If you intend to completely restore a database or prevent key clashes, always call `clearDatabase` before `importFromJsonString`. Ensure your application logic handles potential key conflicts if partial imports are desired.","message":"The `importFromJsonString` function does not delete any existing data in the database before importing. If you import data into a non-empty database, keys could clash, potentially leading to errors or unexpected data merges.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"No direct fix needed unless your project implicitly relied on a removed transitive dependency. This is primarily an internal architecture change with positive effects.","message":"Version 2.0.0 removed all external dependencies, reducing package size and simplifying the dependency tree. While beneficial, this change means any previously implicitly relied-upon transitive dependencies are no longer present.","severity":"breaking","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure the JSON structure exactly matches the object store names in your `IDBDatabase`. Upgrade to `indexeddb-export-import@2.1.5` or later to mitigate issues with missing/extra keys.","cause":"Attempting to import JSON data where some keys in the JSON do not correspond to existing object store names in the IndexedDB database, or vice versa, especially in older versions.","error":"TypeError: Cannot read properties of undefined (reading 'length') OR Database doesn't have object store: 'storeName'"},{"fix":"For browser usage without a bundler, include the library via a `<script>` tag. If using a bundler, configure it to handle CommonJS modules or use ES module `import` syntax if your project supports it.","cause":"Attempting to use CommonJS `require` syntax in a browser environment without a module bundler like Webpack or Rollup.","error":"ReferenceError: require is not defined (in browser)"}],"ecosystem":"npm"}