@totvs/storage
v0.0.1-20220307.3
Published
Mingle Storage
Downloads
127
Maintainers
Keywords
Readme
@TOTVS/STORAGE
This Package @totvs/storage provides a service for storing data on the local device,
This package is compatible with Angular, React and Javascript projects.
Installation
Using npm:
$ npm install --save @totvs/storage
// using ES6 modules
import { TotvsStorage } from @totvs/storage;
var storage = new TotvsStorage();
// using CommonJS modules
var storageInstance = require('@totvs/http')
var storage = new storageInstance.TotvsStorage('your bank name');
Methods
appendArrayToArray
Searches for a list stored by the key and concatenates it with the list passed by parameter.
const clients = [ { name: 'Marie', age: 23 }, { name: 'Pether', age: 39 }];
this.storage.appendArrayToArray('myKey', clients).subscribe( res => {
console.log(res);
});
clear
Removes all items from the database and delete the storage
this.storage.clear().subscribe( res => {
console.log(res);
});
exists
Checks whether a value exists within a given key.
this.storage.exists('myKey').subscribe( res => {
console.log(res);
});
get
Returns the value stored in a given key.
this.storage.get('myKey', false).subscribe( res => {
console.log(res);
});
getAll
Returns the value stored in a given key.
this.storage.getAll().subscribe( res => {
console.log(res);
});
getItemAndRemove
Removes the first item from a list from the key.
getItemByField
Searches for the first object found within a list by the value of a field.
const clients = [ { name: 'Marie', age: 23 }, { name: 'Pether', age: 39 }];
this.thfStorageService.set('clientKey', clients).then(() => {});
...
this.thfStorageService.getItemByField('clientKey', 'name', 'Marie').then(client => {
console.log(client);
// console.log result: { name: 'Marie', age: 23 }
});
keys
List with all keys stored.
this.storage.keys().subscribe( res => {
console.log(res);
});
length
Number of keys stored.
this.storage.length().subscribe( res => {
console.log(res);
});
limitedCallWrap
Used to manage the locking and unlocking of resources in the TotvsStorage. Waiting to release the resource that participates in this behavior and later involves the resource passed as a parameter in a lock and unlock behavior.
This method behaves the same as using the methods together: TotvsStorage.requestIdlePromise (), TotvsStorage.lock (), and TotvsStorage.unlook ().
lock
Increments a value in the Totvs Storage lock queue. Used in conjunction with the unlock method to be able to control the execution of a given task with TotvsStorage.requestIdlePromise ().
remove
Removes a value associated with a key.
this.storage.remove('myKey').subscribe( res => {
console.log(res);
});
removeItemFromArray
Removes an object from a list stored by the value of a property.
this.storage.removeItemFromArray('myKey', 'name', 'Marie').subscribe( res => {
console.log(res);
});
requestIdlePromise
Method that checks whether the access to the configured database is released.
Used in conjunction with the lock () and unlock () methods between tasks that can not be performed in parallel, so as not to cause inconsistencies in the data.
// Waiting for release to continue
await this.storage.requestIdlePromise();
this.storage.lock();
// Performs a task that will read and / or write to the configured database.
this.storage.unlock();
It is important to always use it before executing the lock () and unlock () methods to ensure that the task will only be executed if access is free.
set
Writes a value to a given key.
this.storage.set('myKey', 'this is my content').subscribe( res => {
console.log(res);
});
unlock
Decrements a value in the lock queue. Used in conjunction with the lock method to control the execution of a given task with TotvsStorage.requestIdlePromise ().