
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
angular-async-local-storage
Advanced tools
Efficient local storage module for Angular : simple API based on native localStorage API, but internally stored via the asynchronous IndexedDB API for performance, and wrapped in RxJS observables to be homogeneous with other Angular modules.
Efficient local storage module for Angular :
localStorage
API,IndexedDB
API,Observables
.The author of this library organizes Angular courses (based in Paris (France), but open to travel). You can find details here (in French).
For now, Angular does not provide a local storage module, and almost every app needs some local storage. There is 2 native JavaScript APIs available :
The localStorage
API is simple to use but synchronous, so if you use it too often,
your app will soon begin to freeze.
The IndexedDB
API is asynchronous and efficient, but it's a mess to use :
you'll soon be caught by the callback hell, as it does not support Promises yet.
Mozilla has done a very great job with the localForage library :
a simple API based on native localStorage
,
but internally stored via the asynchronous IndexedDB
for performance.
But it is written in ES5 and then it's a mess to include into Angular.
This module is based on the same idea as localForage, but in ES6/ES2015 and additionnaly wrapped into RxJS Observables to be homogeneous with other Angular modules.
Install via npm :
# For Angular 5 :
npm install angular-async-local-storage
# For Angular 4 and TypeScript >= 2.3 :
npm install angular-async-local-storage@2
Then include the AsyncLocalStorage
module in your app root module (just once, do NOT re-import it in your sub modules).
import { AsyncLocalStorageModule } from 'angular-async-local-storage';
@NgModule({
imports: [
BrowserModule,
AsyncLocalStorageModule,
...
]
...
})
export class AppModule {}
Now you just have to inject the service where you need it :
import { AsyncLocalStorage } from 'angular-async-local-storage';
@Injectable()
export class YourService {
constructor(protected localStorage: AsyncLocalStorage) {}
}
The API follows the native localStorage API, except it's asynchronous via RxJS Observables.
let user: User = { firstName: 'Henri', lastName: 'Bergson' };
this.localStorage.setItem('user', user).subscribe(() => {});
You can store any value, without worrying about stringifying.
To delete one item :
this.localStorage.removeItem('user').subscribe(() => {});
To delete all items :
this.localStorage.clear().subscribe(() => {});
this.localStorage.getItem<User>('user').subscribe((user) => {
user.firstName; // should be 'Henri'
});
As any data can be stored, you can type your data. But don't forget it's client-side storage : always check the data, as it could have been forged or deleted.
Not finding an item is not an error, it succeeds but returns null
.
this.localStorage.getItem('notexisting').subscribe((data) => {
data; // null
});
Errors are unlikely to happen, but in an app it's better to catch any potential error.
this.localStorage.setItem('color', 'red').subscribe(() => {
// Done
}, () => {
// Error
});
You DO need to subscribe, even if you don't have something specific to do after writing in local storage (because it's how RxJS Observables work).
You do NOT need to unsubscribe : the observable autocompletes (like in the HttpClient
service).
The last version of this library requires Angular 5.
If you need support for previous versions of Angular, stay on older versions, like mentionned in Getting started.
This module supports AoT pre-compiling.
This module supports Universal server-side rendering via a mock storage.
All browsers supporting IndexedDB, ie. all current browsers : Firefox, Chrome, Opera, Safari, Edge and IE10+.
Local storage is required only for apps, and given that you won't do an app in older browsers, current browsers support is far enough.
Even so, IE9 is supported but use native localStorage as a fallback, so internal operations are synchronous (the public API remains asynchronous-like).
This module is not impacted by IE/Edge missing IndexedDB features.
MIT
FAQs
Efficient local storage module for Angular : simple API based on native localStorage API, but internally stored via the asynchronous IndexedDB API for performance, and wrapped in RxJS observables to be homogeneous with other Angular modules.
The npm package angular-async-local-storage receives a total of 185 weekly downloads. As such, angular-async-local-storage popularity was classified as not popular.
We found that angular-async-local-storage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.