Simple syncing between @ngrx store and local storage
Installations
npm install ngrx-store-localstorage
Developer
btroncone
Developer Guide
Module System
ESM
Min. Node Version
Typescript Support
No
Node Version
20.14.0
NPM Version
10.7.0
Statistics
616 Stars
263 Commits
120 Forks
22 Watching
6 Branches
36 Contributors
Updated on 20 Nov 2024
Languages
TypeScript (99.42%)
Shell (0.39%)
JavaScript (0.19%)
Total Downloads
Cumulative downloads
Total Downloads
12,553,389
Last day
-3%
10,583
Compared to previous day
Last week
9.2%
55,104
Compared to previous week
Last month
10.5%
223,079
Compared to previous month
Last year
-3.4%
2,503,621
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
3
ngrx-store-localstorage
Simple syncing between ngrx store and local or session storage.
Dependencies
ngrx-store-localstorage
depends on @ngrx/store and Angular 12+.
Usage
1npm install ngrx-store-localstorage --save
- Wrap localStorageSync in an exported function.
- Include in your meta-reducers array in
StoreModule.forRoot
.
1import { NgModule } from '@angular/core'; 2import { BrowserModule } from '@angular/platform-browser'; 3import { StoreModule, ActionReducerMap, ActionReducer, MetaReducer } from '@ngrx/store'; 4import { localStorageSync } from 'ngrx-store-localstorage'; 5import { reducers } from './reducers'; 6 7 8const reducers: ActionReducerMap<IState> = {todos, visibilityFilter}; 9 10export function localStorageSyncReducer(reducer: ActionReducer<any>): ActionReducer<any> { 11 return localStorageSync({keys: ['todos']})(reducer); 12} 13const metaReducers: Array<MetaReducer<any, any>> = [localStorageSyncReducer]; 14 15@NgModule({ 16 imports: [ 17 BrowserModule, 18 StoreModule.forRoot( 19 reducers, 20 {metaReducers} 21 ) 22 ] 23}) 24export class MyAppModule {}
API
localStorageSync(config: LocalStorageConfig): Reducer
Provide state (reducer) keys to sync with local storage. Returns a meta-reducer.
Arguments
config
An object that matches with theLocalStorageConfig
interface,keys
is the only required property.
LocalStorageConfig
An interface defining the configuration attributes to bootstrap localStorageSync
. The following are properties which compose LocalStorageConfig
:
-
keys
(required) State keys to sync with local storage. The keys can be defined in two different formats:-
string[]
: Array of strings representing the state (reducer) keys. Full state will be synced (e.g.localStorageSync({keys: ['todos']})
). -
object[]
: Array of objects where for each object the key represents the state key and the value represents custom serialize/deserialize options. This can be one of the following:-
An array of properties which should be synced. This allows for the partial state sync (e.g.
localStorageSync({keys: [{todos: ['name', 'status'] }, ... ]})
). -
A reviver function as specified in the JSON.parse documentation.
-
An object with properties that specify one or more of the following:
-
serialize
: A function that takes a state object and returns a plain json object to pass to json.stringify. -
deserialize
: A function that takes that takes the raw JSON from JSON.parse and builds a state object. -
replacer
: A replacer function as specified in the JSON.stringify documentation. -
space
: The space value to pass JSON.stringify. -
reviver
: A reviver function as specified in the JSON.parse documentation. -
filter
: An array of properties which should be synced (same format as the stand-alone array specified above). -
encrypt
: A function that takes a state string and returns an encrypted version of that string. e.g.(state: string) => btoa(state)
-
decrypt
: A function that takes a state string and returns a decrypted version of that string. e.g.(state: string) => atob(state)
-
-
-
-
rehydrate
(optional)boolean
: Pull initial state from local storage on startup, this will default tofalse
. -
storage
(optional)Storage
: Specify an object that conforms to the Web Storage API interface to use, this will default tolocalStorage
. -
removeOnUndefined
(optional)boolean
: Specify if the state is removed from the storage when the new value is undefined, this will default tofalse
. -
storageKeySerializer
(optional)(key: string) => string
: Custom serialize function for storage keys, used to avoid Storage conflicts. -
restoreDates
(boolean? = true): Restore serialized date objects. If you work directly with ISO date strings, set this option tofalse
. -
syncCondition
(optional)(state) => boolean
: When set, sync to storage medium will only occur when this function returns a true boolean. Example:(state) => state.config.syncToStorage
will check the state tree under config.syncToStorage and if true, it will sync to the storage. If undefined or false it will not sync to storage. Often useful for "remember me" options in login. -
checkStorageAvailability
(boolean? = false): Specify if the storage availability checking is expected, i.e. for server side rendering / Universal. -
mergeReducer
(optional)(state: any, rehydratedState: any, action: any) => any
: Defines the reducer to use to merge the rehydrated state from storage with the state from the ngrx store. If unspecified, defaults to performing a full deepmerge on anINIT_ACTION
or anUPDATE_ACTION
.
Usage
Key Prefix
1localStorageSync({ 2 keys: ['todos', 'visibilityFilter'], 3 storageKeySerializer: (key) => `cool_${key}`, 4});
In above example Storage
will use keys cool_todos
and cool_visibilityFilter
keys to store todos
and visibilityFilter
slices of state). The key itself is used by default - (key) => key
.
Target Depth Configuration
1localStorageSync({ 2 keys: [ 3 { feature1: [{ slice11: ['slice11_1'], slice14: ['slice14_2'] }] }, 4 { feature2: ['slice21'] } 5 ], 6});
In this example, feature1.slice11.slice11_1
, feature1.slice14.slice14_2
, and feature2.slice21
will be synced to localStorage.feature1
and localStorage.feature2
.
Known Workarounds
Release Notes / Changelog
From version 10 onwards, check GitHub Releases for release notes. For older versions check the CHANGELOG.md.
Contributing
See CONTRIBUTING.md for instructions on how to contribute.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 5/10 approved changesets -- score normalized to 5
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Reason
30 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-8hc4-vh64-cxmj
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-rc47-6667-2j5j
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-5v2h-r2cx-5xgj
- Warn: Project is vulnerable to: GHSA-rrrm-qjm4-v8hf
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-hj9c-8jmm-8c52
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-x2pg-mjhr-2m5x
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-3g92-w8c5-73pq
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986 / GHSA-64vr-g452-qvp3
- Warn: Project is vulnerable to: GHSA-9cwx-2883-4wfx
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.4
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More