npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

idb-observer

v1.0.8

Published

IndexedDB API with rxjs Observable

Downloads

27

Readme

idb-observer

Observable typescript client library for IndexedDB with API like some documents DB.

[!NOTE] Peer-dependencies: RxJs v>6 should be installed.

how to install

with npm:

npm i idb-observer

with yarn:

yarn add idb-observer

how it works

This package introduces two classes for IndexedDb. It contains IdClient and IdbApi. The first one, IdClient to initialize the DB and IdbApi to manipulate entities in it.

new client

IdClient includes follow methods:

  • .init() - to initialize and get db object,
  • .upgrade() - for the first time and whеn needs upgrade version.
    Somthing like this helps:
import IdbClient, { IdbApi } from 'idb-observer';
...
const client = new IdbClient('test');
const idbApi = new IdbApi();
const collection = 'posts'; //for example

client.upgrade().subscribe((db) => {
  if (!db.objectStoreNames.contains(collection)) {
    db.createObjectStore(collection, { keyPath: 'id', autoIncrement: false });
  };
});
client.init().subscribe((db)=> { idbApi.init(db, collection); });

[!TIP] Use the upgrade subscription to create objectStore, indexes etc., see doc. Use the init subscription to get a database instance and pass it to the API object.

api methods

  • .create
  • .list
  • .get
  • .update
  • .remove
  • .clear

sample

Create and manipulate the Post object:

interface Post {
 id?: string;
 text: string;
 title: string;
 description: string;
 author: string;
};
const id = '#111';
const post: Post = {
  id,
  text: 'Test of post',
  title: 'This is Post #111',
  description: 'description',
  author: 'Viktor Bobrofff'
};

idbApi.create<Post>(post).subscribe((id)=>console.log('created', id));
idbApi.update<Post>(id, {author: 'Ivan Ivanov'}).subscribe((id)=>console.log('updated', id));
idbApi.list<Post>().subscribe((posts) => console.log('list', posts));
idbApi.get<Post>(id).subscribe((post)=>console.log('get', post));
idbApi.remove(id).subscribe();
idbApi.clear().subscribe();

[!WARNING] Don't forget to unsubsribe!

safe extract

Since database access methods and api initialization are asynchronous, exceptions are possible.
Use for safe extract:

  • .safe() - safe extract wrapper
  • .safeList() - safe list method.

[!IMPORTANT] Using secure extraction, the method will only execute after the api has been initialized.

If the API is not initialized at the time the method is called, it will waiting for init.
Opening the DB and executing in different streams:

const api = new IdbApi();
const events$ = api.safe(api.list<Event>).pipe(tap((e:Event) => console.log(e)));
const list$ = api.safeList<Event>();
const get$ = api.safe(()=>api.get<Event>('target1'));
   ...
const client = new IdbClient();
client.init().subscribe((db)=>api.init(db, 'events'));

Opening the DB and executing in common stream (no safe wraps):

const client = new IdbClient();
const api = new IdbApi();
  ...
const cash$ = this.client.init.pipe(
  tap((db)=> this.idbApi.init(db, 'events')),
  switchMap(()=>this.idbApi.list<Event>())
)

|API method|params|result| |--- |--- |--- | |.clear| (collection?: string) | Observable<void> | |.create<T>| (doc: T, key?: IDBValidKey, collection?: string) | Observable<string | undefined> | |.list<T> | (query?: IDBKeyRange, collection?: string) | Observable<T[]>| |.get<T> | (query: IDBValidKey | IDBKeyRange, collection?: string) | Observable<T | T[] | null>| |.update<T> | (key?: string | number, data?: {}, collection?: string) | Observable<IDBValidKey | null>| |.remove | (key: string | number, collection?: string) | Observable<void>| |.safe<T> | (method: () => Observable) | Observable<T>| |.safeList<T> | (query?: IDBKeyRange, collection?: string) | Observable<T[]>|