Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create async storage with NGXS #156

Open
SteveLemire opened this issue May 19, 2020 · 0 comments
Open

How to create async storage with NGXS #156

SteveLemire opened this issue May 19, 2020 · 0 comments

Comments

@SteveLemire
Copy link

I was able to manage our own storage service that provides functionality to support async storage with the help of StorageMap library that use IndexedDb and AsyncStorageEngine.

  1. Install StorageMap .

  2. Install AsyncStorageEngine

  3. Create a new storage service and implement AsyncStorageEngine with missing members like this:

export class StorageService implements AsyncStorageEngine {
  constructor(private storageMap: StorageMap}
  1. In your app.module.ts, configure your NgxsModule and other packages like this:
    NgxsModule.forRoot(
      [
        // Add your state classes here
      ],
      {
        developmentMode: !environment.production,
        selectorOptions: {
          // https://www.ngxs.io/concepts/select#selector-options
          // These Selector Settings are recommended in preparation for NGXS v4
          suppressErrors: false,
          injectContainerState: false,
        },
      },
    ),
    // https://github.com/ngxs-labs/async-storage-plugin
    NgxsAsyncStoragePluginModule.forRoot(StorageService),
   // https://github.com/ngxs-labs/async-storage-plugin
    NgxsDataPluginModule.forRoot([NGXS_DATA_STORAGE_PLUGIN]),
  1. In your state class, add the following decorators:
@Persistence({
  useClass: StorageService,
})
@StateRepository()
@State<MyStateModel>({...

Et voilà!
You can now use your async StorageService with NGXS!

Note: This will not work with IE11 and target type ES5 because ngxs-lab/data @persistence decorator use the Package class that is only available with ES6 (ES2015). I was not able get this work event if I tried with the following packages:

Originally posted by @SteveLemire in ngxs-labs/data#299 (comment)

@SteveLemire SteveLemire changed the title I was able to manage our own storage service that provides functionality to support async storage with the help of [StorageMap ](https://www.npmjs.com/package/@ngx-pwa/local-storage) library that use _IndexedDb_ and [AsyncStorageEngine](https://github.com/ngxs-labs/async-storage-plugin). How to create async storage with NGXS May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant