Gathering detailed insights and metrics for @efaps/ngx-store
Gathering detailed insights and metrics for @efaps/ngx-store
Gathering detailed insights and metrics for @efaps/ngx-store
Gathering detailed insights and metrics for @efaps/ngx-store
Angular decorators to automagically keep variables in HTML5 LocalStorage, SessionStorage, cookies; injectable services for managing and listening to data changes and a bit more.
npm install @efaps/ngx-store
Typescript
Module System
Node Version
NPM Version
71.5
Supply Chain
99.4
Quality
79.8
Maintenance
50
Vulnerability
98.6
License
TypeScript (98.08%)
JavaScript (1.92%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
ISC License
5 Stars
394 Commits
4 Forks
2 Watchers
1 Branches
2 Contributors
Updated on Jun 04, 2025
Latest Version
11.0.0
Package Id
@efaps/ngx-store@11.0.0
Unpacked Size
135.52 kB
Size
29.37 kB
File Count
5
NPM Version
10.8.2
Node Version
20.19.2
Published on
Jun 04, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
This library adds decorators that make it super easy to automagically save and restore variables using HTML5's localStorage
and sessionStorage
. It also provides Angular-Injectable Session- and LocalStorageService.
@LocalStorage()
- to save variable in HTML5 localStorage@SessionStorage()
- to save variable in HTML5 sessionStorage@CookieStorage()
- to save variable as a cookie@SharedStorage()
- to keep variable in temporary memory that can be shared across classes@TempStorage()
- alias for SharedStorage
LocalStorageService
, SessionStorageService
, CookiesStorageService
and SharedStorageService
(read more here)Version from Zoomsphere
@LocalStorage() @CookieStorage() variable: any;
CookieStorage
(decorator closer to variable) has higher priority, hence the value will be read from cookies only. The cookie value will be saved in localStorage
regardless of its content to keep consistency.WebStorageService.clear('all')
- now will remove everything except ngx-store
's config (stored in localStorage
)WEBSTORAGE_CONFIG
@SharedStorage
has now alias @TempStorage
npm i @efaps/ngx-store --save
or npm i ngx-store@RC
for latest versionapp.module.ts
:
1import { NgModule } from '@angular/core'; 2import { WebStorageModule } from '@efaps/ngx-store'; 3 4@NgModule({ 5 imports: [ 6 WebStorageModule, 7 ], 8}) 9export class AppModule {}
Things you should take into consideration while configuring this module:
.save()
method to easily force save of made changes (configurable by mutateObjects
)Array
methods that change array object's value can be disabled (configurable by mutateObjects
)''
), however we recommend to use it, as it helps avoid conflicts with other libraries (configurable by prefix
)'all'
- completely clears current Storage'prefix'
- removes all variables prefixed by ngx-store'decorators'
- removes only variables created by decorating functions (useful when not using prefix)
Default behaviour is specified by setting clearType
, but it's possible to pass this parameter directly into service clear()
method.cookiesScope
can be found in this commentAs this project uses decorating functions, it is important to provide custom configuration in global variable named NGXSTORE_CONFIG
before Angular application load. Here are some ways to do it:
<script>
in index.html
(before Angular sources)
1<script> 2var NGXSTORE_CONFIG = { 3 prefix: 'ngx_', // default: 'ngx_' 4 clearType: 'prefix', // default: 'prefix' 5 mutateObjects: true, // default: true 6 debugMode: false, // you can enable debug logs if you ever meet any bug to localize its source 7 cookiesScope: '', // what you pass here will actually prepend base domain 8 cookiesCheckInterval: 0, // number in ms describing how often cookies should be checked for changes 9 previousPrefix: 'angular2ws_', // you have to set it only if you were using custom prefix in old version ('angular2ws_' is a default value) 10}; 11</script>
webpack.js
file this way:
1plugins: [ 2 new webpack.DefinePlugin({ 3 NGXSTORE_CONFIG: JSON.stringify({ 4 prefix: '', // etc 5 }) 6 }), 7]
Decorating functions can take config object with the following fields:
key: string
- key under the variable will be stored, default key is the variable namemutate: boolean
- enable or disable object mutation for instance, default depends on global configexpires: Date
- for @CookieStorage()
only, specifies expiration date, null = lifetime cookiePretty easy to use decorators. Here is where the real magic happens.
1import { CookieStorage, LocalStorage, SessionStorage } from 'ngx-store'; 2 3export class MySuperComponent { 4 // it will be stored under ${prefix}viewCounts name 5 @LocalStorage() viewCounts: number = 0; 6 // this under name: ${prefix}differentLocalStorageKey 7 @LocalStorage('differentLocalStorageKey') userName: string = ''; 8 // it will be stored under ${prefix}itWillBeRemovedAfterBrowserClose in session storage 9 @SessionStorage({key: 'itWillBeRemovedAfterBrowserClose'}) previousUserNames: Array<string> = []; 10 // it will be read from cookie 'user_id' (can be shared with backend) and saved to localStorage and cookies after change 11 @LocalStorage() @CookieStorage({prefix: '', key: 'user_id'}) userId: string = ''; 12 // it will be stored in a cookie named ${prefix}user_workspaces for 24 hours 13 @CookieStorage({key: 'user_workspaces', expires: new Date(new Date().getTime() + 24 * 60 * 60 * 1000)}) userWorkspaces = []; 14 15 constructor() { 16 this.viewCounts++; 17 this.userName = 'some name stored in localstorage'; 18 this.previousUserNames.push(this.userName); 19 for (let userName of this.previousUserNames) { 20 console.log(userName); 21 } 22 this.previousUserNames.map(userName => userName.split('').reverse().join('')); 23 } 24}
Sharing variables across classes: Decorated variables can be easily shared across different classes, e.g. Angular Components (also after their destruction) without need to create new service for this purpose.
1import { LocalStorage, SharedStorage } from 'ngx-store'; 2 3export class HomeComponent { 4 @SharedStorage() title: string = 'Homepage'; // it will be kept in temp memory until app reload 5 @LocalStorage() userNote: string = 'Leave your note here'; // it will be read from and saved to localStorage 6 7 constructor() { 8 setTimeout(() => { 9 console.log('userNote:', this.userNote); // it should be changed after user's visit to NestedComponent 10 }, 5000); 11 } 12} 13 14export class NestedComponent { 15 @SharedStorage('title') homeTitle: string = ''; 16 @LocalStorage() protected userNote: string = ''; 17 18 constructor() { 19 console.log('homeTitle:', this.homeTitle); // should print 'Homepage' 20 console.log('userNote:', this.userNote); // should print userNote set in HomeComponent 21 this.userNote = "You've visited NestedComponent!"; 22 } 23}
Force save changes: If you need to modify stored object by not a direct assignment, then you can take advantage of .save()
method to force save made changes. Example:
1import { CookieStorage, LocalStorage, SessionStorage, WebstorableArray } from 'ngx-store'; 2 3export class MySuperComponent { 4 @LocalStorage() someObject: any = { c: 3 }; 5 @SessionStorage() arrayOfSomethings: WebstorableArray<number> = <any>[0,1,2,3,4]; 6 @CookieStorage({ mutate: false }) someCookie: {version?: number, content?: string} = {}; 7 8 constructor() { 9 this.someObject.a = 1; 10 this.someObject['b'] = 2; 11 delete this.someObject['c']; 12 for (let i = 0; i < this.arrayOfSomethings.length; i++) { 13 this.arrayOfSomethings[i] += i; 14 } 15 this.someCookie.version++; 16 this.someCookie.content = 'please save me'; 17 // upper changes won't be saved without the lines below 18 this.someObject.save(); 19 this.arrayOfSomethings.save(); 20 this.someCookie = this.someCookie; // it looks weird, but also will do the job even without object mutation 21 } 22}
Limited lifecycle classes in AoT compilation: There is a special case when Service or Component in your application containing decorated variable is being destroyed:
1import { OnDestroy } from '@angular/core'; 2import { LocalStorage } from 'ngx-store'; 3 4export class SomeService implements OnDestroy { // implement the interface 5 @LocalStorage() destroyedVariable: any = {}; 6 7 ngOnDestroy() {} // event empty method is needed to allow ngx-store handle class destruction 8}
Use the services to manage your data:
1import { CookiesStorageService, LocalStorageService, SessionStorageService, SharedStorageService } from 'ngx-store'; 2 3export class MyService { 4 constructor( 5 localStorageService: LocalStorageService, 6 sessionStorageService: SessionStorageService, 7 cookiesStorageService: CookiesStorageService, 8 sharedStorageService: SharedStorageService, 9 ) { 10 console.log('all cookies:'); 11 cookiesStorageService.utility.forEach((value, key) => console.log(key + '=', value)); 12 } 13 14 public saveSomeData(object: Object, array: Array<any>) { 15 this.localStorageService.set('someObject', object); 16 this.sessionStorageService.set('someArray', array); 17 18 this.localStorageService.keys.forEach((key) => { 19 console.log(key + ' =', this.localStorageService.get(key)); 20 }); 21 } 22 23 public clearSomeData(): void { 24 this.localStorageService.clear('decorators'); // removes only variables created by decorating functions 25 this.localStorageService.clear('prefix'); // removes variables starting with set prefix (including decorators) 26 this.sessionStorageService.clear('all'); // removes all session storage data 27 } 28}
Note: Always define default value at the property you are using decorator.
Note: Never use for-in
loop on decorated Arrays without filtering by .hasOwnProperty()
.
Note: Please don't ngx-store circular structures as this library uses JSON.stringify to encode data before saving.
Note: When you change prefix from '' (empty string) old values won't be removed automatically to avoid deleting necessary data. You should handle it manually or set clearType to 'all' for some time.
Contributions are welcome!
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
3 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 2
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
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