Adds startsWith method to localForage.
- localForage v1.4.0+
- for earlier versions of localforage, please use the v1.1.x releases
npm i localforage-startswith
Adding a <script>
reference to your page will extend the localforage object to also include the .startsWith()
.
Include localforage
with an import statement appropriate for your configuration and import localforage-startswith
right after it.
Normally, localforage-startswith
will extend the prototype of locaforage
to include the startsWith()
method, but unfortunately the typings can't be updated. As a result you should use the exported extendPrototype()
method, which returns the provided localforage instance but with inherited typings that also include the startsWith()
method.
import localForage from 'localforage';
// OR based on your configuration:
// import * as localForage from 'localforage';
import { extendPrototype } from 'localforage-startswith';
extendPrototype(localforage);
// Keep using localForage as usual.
In some ES6 module bundlers .startsWith()
might not automatically be made available to the localforage
object on import.
In this case, import the provided extendPrototype()
method and extend localforage
manually, as shown in the Typescript section.
Retrieves an object with all the items that have keys starting with the provided parameter.
localforage.startsWith('user-1').then(function(results) {
console.log(results);
// prints:
// {
// 'user-1-todo-1': "11aa1111bbcc",
// 'user-1-todo-2': "22aa2222bbcc",
// 'user-1-todo-3': "33aa3333bbcc",
// 'user-1-todo-4': "44aa4444bbcc"
// }
});
Retrieves an array with all the keys starting with the provided parameter.
localforage.keysStartingWith('user-1').then(function(results) {
console.log(results);
// prints:
// [
// 'user-1-todo-1',
// 'user-1-todo-2',
// 'user-1-todo-3',
// 'user-1-todo-4'
// ]
});