Giter Site home page Giter Site logo

How to create multiple stores? about ngforage HOT 7 CLOSED

alorel avatar alorel commented on August 16, 2024
How to create multiple stores?

from ngforage.

Comments (7)

Alorel avatar Alorel commented on August 16, 2024 5

@Dedme Inject it like anything else...

import { DedicatedInstanceFactory, NgForage } from 'ngforage';
import { Injectable } from '@angular/core';

@Injectable({providedIn: 'root'})
export class MyService {
    private readonly ngForage1: NgForage;
    private readonly ngForage2: NgForage;

	public constructor(dif: DedicatedInstanceFactory) {
		this.ngForage1 = dif.createNgForage({name: 'foo'});
		this.ngForage2 = dif.createNgForage({name: 'RTFM'});
	}
}

from ngforage.

Alorel avatar Alorel commented on August 16, 2024

Hey. ngforage automatically handles this internally - it checks the configuration hash before every operation and calls localforage.createInstance() if the hash has changed.

At the moment, this is roughly how things work:

class MyComponent implements OnInit {
   private readonly ngf: NgForage;

   public ngOnInit(): void {
      this.ngf.name = 'foo'; // creates a store with the name 'foo';
   }

   public someMethod(): void {
      this.ngf.getItem('a'); // Gets an item from store 'foo';
      this.ngf.name = 'bar'; // creates a store called 'bar';
      this.ngf.getItem('b'); // Gets an item from store 'foo';
      this.nfg.name = 'foo'; // Switches back to store 'foo', reuses existing store.
      this.ngf.getItem('c'); // Gets an item from store 'foo';

       // The hash change occurs for any configuration change, not just the name.
   }
}

For components you could simply add it to the providers, which would create a component-specific instance of ngforage:

@Component({
   providers: [NgForage]
});

Such an approach does not currently exist for injectables and will be added in a future release; unfortunately, I don't know when I'll have time to implement it.

from ngforage.

benson-jala avatar benson-jala commented on August 16, 2024

Thank you for timely response.

from ngforage.

Dedme avatar Dedme commented on August 16, 2024

Hello, i am looking to use multiple named stores in services across my application, so this is in Injectables.
Is there a way i am able to manually configure something if the D.I. isn't there yet?

from ngforage.

Dedme avatar Dedme commented on August 16, 2024

Just wanted to close this out with that i solved this current by using the clone functionality.

in my service i am importing the defualt NGForage instance.

private ngf: NgForage;
constructor(ngfd: NgForage) {
  this.ngf = ngfd.clone({storeName: 'newName'});
}

from ngforage.

Alorel avatar Alorel commented on August 16, 2024

@Dedme You could also use the dedicated instance factory: https://alorel.github.io/ngforage/docs/injectables/DedicatedInstanceFactory.html

from ngforage.

Dedme avatar Dedme commented on August 16, 2024

from ngforage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.