An offline storage solution for Angular apps
npm install ngx-warehousehttps://phillipcurl.github.io/ngx-warehouse
- About
- Installation
- Documentation
- Development
- License
An offline storage solution for Angular apps built on top of LocalForage.
Install via npm:
``bash`
$ npm install --save ngx-warehouse
or Yarn:
`bash`
$ yarn add ngx-warehouse
Then include in your apps module:
`typescript
import { Component, NgModule } from '@angular/core';
import { NgxWarehouseModule } from 'ngx-warehouse';
@NgModule({
imports: [
NgxWarehouseModule
]
})
export class MyModule {}
`
`typescript
import { NgxWarehouseModule, WarehouseConfig, DRIVER_TYPE } from 'ngx-warehouse';
const config: WarehouseConfig = {
driver: DRIVER_TYPE.DEFAULT,
name: 'Your App',
version: 1.0,
storeName: 'key_value_pairs', // Should be alphanumeric, with underscores.
description: 'A description of your app'
};
@NgModule({
declarations: [...],
imports: [
...
NgxWarehouseModule.configureWarehouse(config),
...
],
bootstrap: [...]
})
`
The following DRIVER_TYPE's are available:
* DEFAULT - The warehouse will first try to connect to IndexedDB, then WebSQL, and finally LocalStorage if the first two fail.
* INDEXEDDB - Force the connection to IndexedDB.
* WEBSQL - Force the connection to WebSQL.
* LOCALSTORAGE - Force the connection to LocalStorage.
Now you're ready to use ngx-warehouse in your app:
`typescript
import { Warehouse } from 'ngx-warehouse';
@Component({
...
})
export class MyComponent implements OnInit {
constructor(public warehouse: Warehouse) { }
ngOnInit() {
this.warehouse.get('key').subscribe(
data => console.log(data),
error => console.log(error)
);
}
}
`
You may also find it useful to view the demo source.
Saves an item to the current offline data store. The following data types are valid:
* Array
* ArrayBuffer
* Blob
* Float32Array
* Float64Array
* Int8Array
* Int16Array
* Int32Array
* Number
* Object
* Uint8Array
* Uint8ClampedArray
* Uint16Array
* Uint32Array
* String
`typescript`
Warehouse.set('key', value).subscribe(
(item) => {
// do something with newly saved item
},
(error) => {
// handle the error
}
);
Gets an item from the storage library and supplies the result to a callback.
If the key does not exist, getItem() will return null.
Even if undefined is saved, null will be returned by getItem(). This is due to a
limitation in localStorage, and for compatibility reasons localForage cannot
store the value undefined.
`typescript`
Warehouse.get('key').subscribe(
(data) => {
// do something with the data
},
(error) => {
// handle the error
}
);
Removes the value of a key from the offline store.
`typescript`
Warehouse.remove('key').subscribe(
(success) => {
// do something on success
},
(error) => {
// handle the error
}
);
USE WITH CAUTION:
Removes every key from the database, returning it to a blank slate.
`typescript`
Warehouse.destroy().subscribe(
(success) => {
// do something on success
},
(error) => {
// handle the error
}
);
Gets the number of keys in the offline store (i.e. its “length”).
`typescript`
Warehouse.count().subscribe(
(success) => {
// do something on success
},
(error) => {
// handle the error
}
);
Get the name of a key based on its ID.
This method is inherited from the localStorage API, but is acknowledged to be kinda weird.
`typescript`
Warehouse.count().subscribe(
(success) => {
// do something on success
},
(error) => {
// handle the error
}
);
Get the list of all keys in the datastore.
`typescript`
Warehouse.count().subscribe(
(success) => {
// do something on success
},
(error) => {
// handle the error
}
);
`Documentation
All documentation is auto-generated from the source via typedoc and can be viewed here:
https://phillipcurl.github.io/ngx-warehouse/docs/Development
$3
* Install Node.js and NPM (should come with)
* Install local dev dependencies: npm install while current directory is this repo$3
Run npm start to start a development server on port 8000 with auto reload + tests.$3
Run npm test to run tests once or npm run test:watch to continually run tests.$3
* Bump the version in package.json (once the module hits 1.0 this will become automatic)
`bash
npm run release
``MIT