Gathering detailed insights and metrics for storage-engine
Gathering detailed insights and metrics for storage-engine
Gathering detailed insights and metrics for storage-engine
Gathering detailed insights and metrics for storage-engine
EventEmitter abstraction on top the React-Native AsyncStorage API
npm install storage-engine
Typescript
Module System
Node Version
NPM Version
85.1
Supply Chain
100
Quality
75.5
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
3,891,233
Last Day
2,133
Last Week
12,364
Last Month
50,897
Last Year
653,283
MIT License
2 Stars
37 Commits
2 Forks
3 Watchers
1 Branches
4 Contributors
Updated on Aug 19, 2019
Minified
Minified + Gzipped
Latest Version
3.0.7
Package Id
storage-engine@3.0.7
Size
12.35 kB
NPM Version
6.9.0
Node Version
10.16.0
Published on
Aug 19, 2019
Cumulative downloads
Total Downloads
Last Day
-10.7%
2,133
Compared to previous day
Last Week
-1.6%
12,364
Compared to previous week
Last Month
0.4%
50,897
Compared to previous month
Last Year
-12.8%
653,283
Compared to previous year
The storage-engine
module is an high level abstraction for React-Native's
AsyncStorage. It allows you to easily customize the AsyncStorage using
plugins. These plugins can be applied to all keys, or just specific key patterns
giving you immense flexibility in the storage layer that you're creating.
The package is released in the public npm registry and can be installed by running:
1npm install --save storage-engine
The AsyncStorage
API has been moved to the React-Native Community as part
of their lean-core initiative and should now be installed as separate package:
1npm install --save @react-native-community/async-storage 2react-native link @react-native-community/async-storage
1import storage, { StorageEngine } from 'storage-engine';
We export a pre-initialized StorageEngine
class by default so you can use
the API directly when you import the module. If you wish, you can create your
own instance by initializing a new StorageEngine
class.
The storage instance has the following methods:
Enhanced AsyncStorage
methods:
The before
method allows you to pre-process any AsyncStorage API call.
You can intercept the request, change values, or even change keys. Want to merge
data with an remote data source? We gotchu, want to cancel a request, hell yeah.
1import storage from 'storage-engine'; 2 3storage.before('key*, another, more*', { 4 setItem: function ({ key, value, method }) { 5 console.log(method); // setItem 6 console.log(key); // key-here 7 console.log(value); // bar 8 9 return { 10 value: 'Completely different value' 11 } 12 } 13}); 14 15storage.after('*', { 16 setItem: function ({ key, value, method }) { 17 console.log(method); // setItem 18 console.log(key); // key-here 19 console.log(value); // Completely different value 20 }, 21 getItem: function ({ key, value, method }) { 22 console.log(method); // getItem 23 console.log(key); // key-here 24 console.log(value); // undefined 25 } 26}) 27 28await storage.setItem('key-here', 'bar'); 29const value = await storage.getItem('key-here'); 30 31console.log(value); // Completely different value
In the example above, we've added a before hook that is triggered when the
setItem
api is called. We can see which key is used, and which value would
be assigned. We've then returned a completely different value from the hook
which would be stored instead of the original provided value.
The before
method accepts the following arguments:
key
A pattern of keys that would need to trigger the assigned before
method. It can be key1, key1, another-key
or use the *
wildcard for all
keys, or even a combination key*, another-key, *bar
, which would match
key1
, key0afaf
, another-key
, foobar
, bbar
etc.methods
An object where the key's are the names of the methods you
would like to pre-process, and the value the (async)function
that would
handle the pre-processing. If you supply a (async)function
instead of an
object, it would be called for all available methods.options
Additional options:
order
: Allows you control the order of execution of your function, placing
before or after other added methods in the execution chain. The order is
set by default to 100
if no option is provided. The higher value, the
important it is to execute this function early. So a 9000
will be executed
before a order 100
and an order of 0
would be executed last.Exactly the same interface as before
, but instead of pre processing this
method allows you to post process the response of the AsyncStorage API
call.
See before for API and example.
The plugin API allows you to load any custom plugin that can enhance your
storage instance. A plugin is basically an (async) function
that is executed
with a bunch of plugin helper functions.
1import storage from 'storage-engine'; 2import { expire } from 'storage-modifiers'; 3 4storage.use('token*', expire, { 5 duration: '30 minutes' 6}); 7 8storage.use('pattern*', async function example(plugin) { 9 const { before, after, destroy, options } = plugin; 10 11 // 12 // Main difference here is that plugins don't need to prefix before and 13 // after functions with the pattern, this is done automatically. 14 // 15 before({ 16 getItem: () => {} 17 }); 18 19 destroy(function () { 20 // 21 // Called when `storage.destroy()` is invoked. 22 // 23 }); 24 25 console.log(options); // { options: 'here'} 26}, { 27 options: 'here' 28});
The plugin API accepts the following arguments:
pattern
The key pattern that it should be triggered on.plugin
The actual plugin which can be an async
or normal JavaScript
function that will be executed with our Plugin API.options
Additional options for the plugin.Please note, we do offer default plugins which are available in a separate module, storage-modifiers which provides useful features such as:
- json: Automatic encoding, and decoding of values
- emit: Emits an event when a given key, or method is accessed (configurable)
- expire: Expires key/values based on a given TTL
- encrypt: Adds an additional layer of protection by encrypting the data you store.
When the plugin is executed by the plugin it will be executed with
an object
that contains the following properties and helper functions.
before
Reference to the before method, but prefixed by default
with the pattern
that was supplied to the plugin.after
Reference to the after method, but prefixed by default
with the pattern
that was supplied to the plugin.enabled
Function that checks if the passed key
argument is enabled by
the pattern that was supplied to the plugin, returns a boolean, enabled(key)
.destroy
Function that allows you to register a (async) clean-up callback
which is executed when the destroy method is called.pattern
The pattern that was supplied to the plugin as first
argument.engine
Reference to the initialized storage instance.options
The options that was supplied to the plugin as third
argument.Destroy the initialized storage
instance by removing all plugins, before,
and after hooks. This will call any destroy
hook that are defined by plugins
but it does not trigger the clear method, so the data that you stored is
untouched.
1import storage from 'storage-engine'; 2 3storage.before('*', { 4 setItem: () => ({ 5 value: 'all your values are this now, lol' 6 }) 7}); 8 9await storage.setItem('foo', 'bar'); 10await storage.getItem('foo') // all your values are this now, lol 11 12await storage.destroy(); 13 14await storage.setItem('foo', 'bar'); 15await storage.getItem('foo') // bar
Provides direct access to the AsyncStorage API that we're wrapping. This API
will not execute any of the plugins or before/after modifications that are
registered. This is the API we use internally to communicate with AsyncStorage
.
1import storage from 'storage-engine'; 2 3await storage.api('setItem', 'foo', 'bar');
The method accepts the following arguments:
method
The name of the method that needs to be executed....args
The args that you would have normally passed into the original API.The rest of the API methods are the API methods that exist on the AsyncStorage API that is provided by React-Native / React-Native Community. Some of the API have been enhanced to allow a more sensible argument or return format. For example, we only allow the async/await interface instead of an additional 3rd callback argument. We're just gonna go briefly over this API and make the assumption that you already have prior knowledge of the existing React-Native AsyncStorage API:
The getItem
allows you to retrieve a previously stored value. When the value
does not exist, a null
will be returned instead.
1const value = await storage.getItem('key'); 2console.log(value); // key
key
The key of the value you want to retrieve.Stores a new item. Yup, that's it.
1await storage.setItem('key', 'value');
key
The key where the value will be stored at.value
The contents that needs to be stored, should be string, unless
a plugin allows you to use a different format.Removes the previously stored value.
1await storage.removeItem('key');
key
The key of the value you want to remove.Merge the contents of the key, with the supplied value.
1await storage.mergeItem('key', '{"json": "here"}');
key
The key of the value you want to update.value
A stringified JSON object that will be merged with the existing valueRemoves all the data that you've stored. Everything. Gone. Forever. Like it never existed. This method is officially approved by Thanos.
1await storage.clear();
Retrieve all keys that are stored in the AsyncStorage.
1const keys = await storage.getAllKeys();
Does anyone actually this method? Why am I even documenting this.
1await storage.flushGetRequests();
Retrieves multiple values at once.
1const [one, two] = await storage.multiGet(['one', 'two']); 2 3// one: { key: one, value: ... } 4// two: { key: two, value: ... }
The data that is returned by the multi API is slightly different, as we've wrapped each item in an object which has
key
, andvalue
property.
Store multiple values at once.
1await storage.multiSet([ 2 ['one', 'value'], 3 ['two', 'another value'] 4]); 5 6// 7// Also supported: 8// 9await storage.multiSet([ 10 { key: 'one', value: 'value' }, 11 { key: 'two', value, 'another value' } 12]);
The data that is returned by the multi API is slightly different, as we've wrapped each item in an object which has
key
, andvalue
property.
Merge multiple values at once.
1await storage.multiMerge([ 2 ['key', '{"json": "blob"}'], 3 ['another', '{"cow": "moo"}'] 4]); 5 6// 7// Also supported: 8// 9await storage.multiMerge([ 10 { key: 'key', value: '{"json": "blob"}'}, 11 { key: 'another', value: '{"cow":"moo"}'} 12]);
The data that is returned by the multi API is slightly different, as we've wrapped each item in an object which has
key
, andvalue
property.
Thanos multiple keys at once.
1await storage.multiRemove(['one', 'two']);
The data that is returned by the multi API is slightly different, as we've wrapped each item in an object which has
key
, andvalue
property.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 3/28 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 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
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-05-19
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