artoo.js’ store module provides you with a useful Storage
interface. It becomes really handy when some persistent data has to be saved and accessed throughout a scraping session.
If you are unfamiliar with localStorage
and its affiliates, it is highly advisable that you start by reading some documentation to learn their downsides.
The localStorage
is used by default. If you need to change this, you should use the relevant setting.
Note also that artoo.store
can be accessed through the artoo.s
shortcut if needed.
artoo.store
is a function having for alias artoo.store.get
and returning either the whole store as a JavaScript object or only one key if specified.
artoo.store([key]);
Examples
// Retrieving one key
artoo.store('hello');
>>> 'world'
// Retrieving the whole store
artoo.store();
>>> {
hello: 'world',
color: 'blue',
number: 4
}
artoo.store.getAll
is the same as calling artoo.store
without specifying a key, meaning that you will retrieve the whole store as a JavaScript object.
artoo.store.keys
is a function returning an array of the keys in the store.
Example
artoo.store.keys();
>>> ['hello', 'color', 'number'];
artoo.store.set
lets you set keys to the store while taking care of JSON stringification for you. Contrary to localStorage
standard interface, you need not worry about what you insert into the store when using artoo.store
module.
artoo.store.set(key, value);
Examples
// Setting a string
artoo.store.set('hello', 'world');
// Setting a number
artoo.store.set('number', 4);
// Setting an object
artoo.store.set('object', {key1: 'ok', key2: 'ko'});
artoo.store.pushTo
is a little helper that enables you to push to a store array without having to retrieve it first and reset it after the item is pushed.
artoo.store.pushTo(arrayKey, item);
Note that artoo will stop you if you try to push to a non-array value.
artoo.store.update
is a little helper that enables you to update a store object without having to retrieve it first and reset it after the object is updated.
artoo.store.update(objectKey, {key1: 'modified key'});
Note that artoo will stop you if you try to push to a non-object value.
artoo.store.remove
simply removes the given key from the store.
artoo.store.remove(key);
artoo.store.removeAll
simply cleans the store of everything.
artoo.store.removeAll();
Note that artoo.store.clean
also exists as a alias for this method.