Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IndexedDB in Angular?

Is there somewhere a material to use the indexedDB in Angular? I am looking for something that helps me to implement in data rescue or better, a database that is in the browser, although it is not the localstore because of the limitation of 5 MB

like image 753
João Eudes Lima Avatar asked Jan 10 '18 00:01

João Eudes Lima


People also ask

What is IndexedDB and how do you use it?

IndexedDB is a way for you to persistently store data inside a user's browser. Because it lets you create web applications with rich query abilities regardless of network availability, your applications can work both online and offline.

When should I use IndexedDB?

You might use IndexedDB to store structured data that's unrelated to any data on the server. An example might be a calendar, a to-do list, or saved games that are played locally. In this case, the application is really a local one, and your web site is just the vehicle for delivering it.

Is IndexedDB better than localStorage?

If you want to store structured data on the client side, IndexedDB is the better choice, especially since localStorage isn't built to store sensitive information. But if you're storing a simple, small amount of key-value pair data, use localStorage.


3 Answers

I use Dexie as it's a more mature and lightweight library and with all features. Compatible with VueJs, React, Angular and Vanilla JS

Alternative ngx-indexed-db

like image 112
João Eudes Lima Avatar answered Oct 09 '22 05:10

João Eudes Lima


use ngx-indexed-db npm library:

npm install ngx-indexed-db

Import the NgxIndexedDBModule and initiate it:

import { NgxIndexedDBModule } from 'ngx-indexed-db';

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

@NgModule({
  ...
  imports: [
    ...
    NgxIndexedDBModule.forRoot(dbConfig)
  ],
  ...
})

Migrations:

import { NgxIndexedDBModule, DBConfig } from 'ngx-indexed-db';

// Ahead of time compiles requires an exported function for factories
export function migrationFactory() {
  // The animal table was added with version 2 but none of the existing tables or data needed
  // to be modified so a migrator for that version is not included.
  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 } }
    ]
  }, {
    // animals added in version 2
    store: 'animals',
    storeConfig: { keyPath: 'id', autoIncrement: true },
    storeSchema: [
      { name: 'name', keypath: 'name', options: { unique: true } },
    ]
  }],
  // provide the migration factory to the DBConfig
  migrationFactory
};

@NgModule({
  ...
  imports: [
    ...
    NgxIndexedDBModule.forRoot(dbConfig)
  ],
  ...
})

NgxIndexedDB service First, import the service:

import { NgxIndexedDBService } from 'ngx-indexed-db';

...
  export class AppComponent {
    constructor(private dbService: NgxIndexedDBService){
    }
  }
like image 4
Mostafa Saadatnia Avatar answered Oct 09 '22 05:10

Mostafa Saadatnia


Easiest way to use IndexedDB. A lightweight, minimalistic wrapper that provides a straightforward API for developers using IndexedDB shared below a link

https://dexie.org

like image 2
Sukesh Hublikar Avatar answered Oct 09 '22 04:10

Sukesh Hublikar