Skip to content

overwriteDatabaseFile

Replace the contents of the SQLite database file.

Usage

Access or destructure overwriteDatabaseFile from the SQLocal client.

javascript
import { SQLocal } from 'sqlocal';

export const { overwriteDatabaseFile } = new SQLocal('database.sqlite3');

NOTE

If you are using the Kysely Query Builder or Drizzle ORM for type-safe queries, you will initialize the client with a child class of SQLocal. See the corresponding setup page. Usage is the same otherwise.

The overwriteDatabaseFile method takes a database file as a File, Blob, ArrayBuffer, or Uint8Array object and returns a Promise to replace the SQLocal instance's associated database file with the one provided.

For example, you can download a database file from your server to replace the local file.

javascript
const response = await fetch('https://example.com/download?id=12345');
const databaseFile = await response.blob();
await overwriteDatabaseFile(databaseFile);

Or, your app may allow the user to import a database file.

javascript
const fileInput = document.querySelector('input[type="file"]');
const databaseFile = fileInput.files[0];
await overwriteDatabaseFile(databaseFile);

Released under the MIT License