Using SQLite in the Browser - rtlehr/mobile-app-start GitHub Wiki

๐Ÿ’พ Using SQLite in the Browser with @capacitor-community/sqlite

This guide explains how to enable and use SQLite in the browser during development using the @capacitor-community/sqlite plugin. SQLite does not run natively in browsers, so the plugin uses a WebAssembly + IndexedDB backend when running in a web environment.


โœ… What This Enables

  • Allows development and testing of SQLite logic in the browser.
  • Schema, queries, and logic mirror native SQLite.
  • Uses jeep-sqlite as a custom web component internally.

๐Ÿ“ฆ Prerequisites

Install the plugin (if not already done):

npm install @capacitor-community/sqlite
npx cap sync

๐Ÿ›  Setup: Web Support for SQLite

1. Import and Initialize SQLite for Web

Edit your main.ts:

import { enableProdMode } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { environment } from './environments/environment';

import { Capacitor } from '@capacitor/core';
import { defineCustomElements } from '@ionic/pwa-elements/loader';

if (environment.production) {
  enableProdMode();
}

if (Capacitor.getPlatform() === 'web') {
  import('@capacitor-community/sqlite/dist/esm/web').then(async (module) => {
    const sqlite = new module.CapacitorSQLiteWeb();
    customElements.define('jeep-sqlite', sqlite.constructor);
    await sqlite.initWebStore(); // โœ… Important: enables browser DB
  });

  defineCustomElements(window); // Required if using Ionic PWA Elements
}

bootstrapApplication(AppComponent)
  .catch(err => console.error(err));

2. Platform Check in SQLite Service

Update your SQLite service to check for web environment:

import { Capacitor } from '@capacitor/core';
import { CapacitorSQLite, SQLiteConnection } from '@capacitor-community/sqlite';

const platform = Capacitor.getPlatform();
const sqlite = new SQLiteConnection(CapacitorSQLite);

if (platform === 'web') {
  await CapacitorSQLite.initWebStore(); // Ensures web DB is ready
}

const db = await sqlite.createConnection('mydb', false, 'no-encryption', 1);
await db.open();

// Create table example
await db.execute(`CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, name TEXT);`);

๐Ÿงช Development Notes

  • The database is backed by IndexedDB, not a file.
  • Data is persistent across browser reloads (within same origin).
  • You can inspect data in browser DevTools โ†’ Application โ†’ IndexedDB โ†’ jeep-sqlite-store.

โš ๏ธ Limitations

Feature Browser Support
Standard SQL syntax โœ… Yes
Real SQLite file โŒ No
Data persistence โœ… Yes (IndexedDB)
Export/import โš ๏ธ Possible with base64
Performance โš ๏ธ Slower than native

๐Ÿ“ฆ Optional: Install Ionic PWA Elements (if using modals, camera, etc.)

npm install @ionic/pwa-elements

Then in main.ts:

import { defineCustomElements } from '@ionic/pwa-elements/loader';
defineCustomElements(window);

โœ… Summary

  • You can run SQLite in the browser using @capacitor-community/sqlite + WebAssembly
  • Works seamlessly with the same logic as your native app
  • Great for development before deploying to Android/iOS