Angular IndexedDB Wrapper

22.0.0 · active · verified Wed Apr 22

ngx-indexed-db is an Angular service that provides a reactive, observable-based wrapper around the browser's IndexedDB API. It simplifies database interactions for Angular applications, supporting both client-side rendering (CSR) and server-side rendering (SSR) since version 19.2.0. The current stable version is 22.0.0. While the exact release cadence isn't explicitly stated, the version numbers often align with Angular's major releases, suggesting active development. Key differentiators include its observable-driven API, full SSR compatibility, and the ability to provide custom IndexedDB implementations via an injection token for enhanced testing or non-browser environments. It also features a robust migration system for evolving database schemas.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates configuring ngx-indexed-db for both NgModule and standalone Angular applications, including defining the database schema and implementing version migrations.

import { NgModule, ApplicationConfig } from '@angular/core';
import { NgxIndexedDBModule, provideIndexedDb, DBConfig } from 'ngx-indexed-db';

// Ahead of time compiles requires an exported function for factories
export function migrationFactory() {
  return {
    1: (db, transaction) => {
      const store = transaction.objectStore('people');
      store.createIndex('country', 'country', { unique: false });
    },
    3: (db, transaction) => {
      const store = transaction.objectStore('people');
      store.createIndex('age', 'age', { unique: false });
    }
  };
}

const dbConfig: DBConfig  = {
  name: 'MyDb',
  version: 3,
  objectStoresMeta: [{
    store: 'people',
    storeConfig: { keyPath: 'id', autoIncrement: true },
    storeSchema: [
      { name: 'name', keypath: 'name', options: { unique: false } },
      { name: 'email', keypath: 'email', options: { unique: false } }
    ]
  }, {
    store: 'animals',
    storeConfig: { keyPath: 'id', autoIncrement: true },
    storeSchema: [
      { name: 'name', keypath: 'name', options: { unique: true } }
    ]
  }],
  migrationFactory
};

// For NgModule-based applications
@NgModule({
  imports: [
    NgxIndexedDBModule.forRoot(dbConfig)
  ]
})
export class AppModule { }

// For Standalone API (main.ts or component providers)
const appConfig: ApplicationConfig = {
  providers: [
    provideIndexedDb(dbConfig)
  ]
};

view raw JSON →