|
| 1 | +# \@kalisio/localforage |
| 2 | + |
| 3 | +[](https://github.com/kalisio/feathers-localforage/releases) |
| 4 | +[](https://github.com/kalisio/feathers-localforage/actions/workflows/main.yaml) |
| 5 | +[](https://codeclimate.com/github/kalisio/feathers-localforage) |
| 6 | +[](https://codeclimate.com/github/kalisio/feathers-localforage/coverage) |
| 7 | +[](https://opensource.org/licenses/MIT) |
| 8 | + |
| 9 | +[@kalisio/localforage](https://github.com/kalisio/localforage/) is a database service adapter wrapping `localForage` that persists to either `IndexedDB`, `WebSQL`, or `LocalStorage` making it very useful for mobile and offline-first applications with the additional ability to seamlessly handle Blobs, TypedArrays, and other JS objects. |
| 10 | + |
| 11 | +```bash |
| 12 | +$ npm install --save @kalisio/localforage |
| 13 | +``` |
| 14 | + |
| 15 | +> __Important:__ `@feathersjs-offline/localforage` implements the [Feathers Common database adapter API](https://docs.feathersjs.com/api/databases/common.html) and [querying syntax](https://docs.feathersjs.com/api/databases/querying.html). |
| 16 | +
|
| 17 | +The changelog of this fork is managed through GitHub [Milestones](https://github.com/kalisio/feathers-localforage/milestones) and related issues. |
| 18 | + |
| 19 | +## API |
| 20 | + |
| 21 | +### `service(options)` |
| 22 | + |
| 23 | +Returns a new service instance initialized with the given options. |
| 24 | + |
| 25 | +```js |
| 26 | +const service = require('@feathersjs-offline/localforage'); |
| 27 | + |
| 28 | +app.use('/messages', service({ |
| 29 | + storage: ['IndexedDB', 'localStorage'] |
| 30 | +})); |
| 31 | +app.use('/messages', service({ storage, id, startId, name, store, paginate })); |
| 32 | +``` |
| 33 | + |
| 34 | +__Options:__ |
| 35 | + |
| 36 | +- `storage` (*optional*, default: `'INDEXEDDB'`) - The storage backend. Must be one or more of `'INDEXEDDB'`, `'WEBSQL'`, or `'LOCALSTORAGE'`. The adapter will use the same sequence as fall-back if the desired storage type is not supported on the actual device. Alternatively, you can supply an array of storage backends determining the priority of your choice. |
| 37 | +- `version` (*optional*, default: `1.0`) - `localforage` driver version to use. Currently only `1.0` exists. |
| 38 | +- `size` (*optional*, default `4980736`) - The maximum database size required. Default DB size is _JUST UNDER_ 5MB, as it's the highest size we can use without a prompt in any browser. |
| 39 | +- `id` (*optional*, default: `'id'`) - The name of the id field property. |
| 40 | +- `name` (*optional*, default: `'feathersjs-offline'`) - The name of the underlying localforage database. With local storage, this is used as a key prefix. |
| 41 | +- `storeName` (*optional*, default: `'feathers'`) - The name of the datastore. Depending on the storage backend it could be the name of the key/value table in the database (eg WebSQL) or the key (eg local storage). Must be alphanumeric, with underscores. |
| 42 | +- `store` (*optional*) - An object with id to item assignments to pre-initialize the data store. |
| 43 | +- `dates` (*optional*, default `false`) - Convert ISO-formatted date strings to `Date` objects in result sets. |
| 44 | +- `events` (*optional*) - A list of [custom service events](https://docs.feathersjs.com/api/events.html#custom-events) sent by this service. |
| 45 | +- `paginate` (*optional*) - A [pagination object](https://docs.feathersjs.com/api/databases/common.html#pagination) containing a `default` and `max` page size. |
| 46 | +- `whitelist` (*optional*) - A list of additional query parameters to allow. |
| 47 | +- `multi` (*optional*) - Allow `create` with arrays and `update` and `remove` with `id` `null` to change multiple items. Can be `true` for all methods or an array of allowed methods (e.g. `[ 'remove', 'create' ]`). |
| 48 | +- `reuseKeys` (*optional*, default: `false`) Allow duplicate keys (see `name`) i.e. last definition wins. Mostly useful for demonstration and testing purposes. |
| 49 | + |
| 50 | +## Storing Blobs, TypedArrays, and other JS objects |
| 51 | + |
| 52 | +As this is an implementation on top of `localForage` you can store any type in `@feathersjs-offline/localforage`; you aren't limited to strings like in `localStorage`. Even if `localStorage` is your storage backend, `@feathersjs-offline/localforage` automatically does JSON.parse() and JSON.stringify() when getting/setting values. |
| 53 | + |
| 54 | +`feathers-localforage` supports storing all native JS objects that can be serialized to JSON, as well as ArrayBuffers, Blobs, and TypedArrays. Check the [localForage API docs](https://localforage.github.io/localForage/#data-api-setitem) for a full list of types supported. In addition, setting the option `dates` to `true` will make sure any ISO-formatted dates in your results will in fact be date objects and not text strings. |
| 55 | + |
| 56 | +> All types are supported in every storage backend, though storage limits in `localStorage` make storing many large Blobs impossible. |
| 57 | +
|
| 58 | +We default to `indexedDB` if available and fall-back to `localStorage` as a last resort. |
| 59 | + |
| 60 | + |
| 61 | +## Example |
| 62 | + |
| 63 | +See the [clients](https://docs.feathersjs.com/api/client.html) chapter for more information about using Feathers in the browser and React Native. |
| 64 | + |
| 65 | +### Browser |
| 66 | + |
| 67 | +```html |
| 68 | +<script type="text/javascript" src="//unpkg.com/@feathersjs/client@^4.5.11/dist/feathers.js"></script> |
| 69 | +<script type="text/javascript" src="//unpkg.com/@feathersjs-offline/localforage@^1.0.0/dist/localforage.js"></script> |
| 70 | +<script type="text/javascript"> |
| 71 | + var service = feathersjsOfflineLocalforage.init({ |
| 72 | + storage: ['indexeddb', 'websql', 'localStorage'] |
| 73 | + }); |
| 74 | + var app = feathers().use('/messages', service); |
| 75 | +
|
| 76 | + var messages = app.service('messages'); |
| 77 | +
|
| 78 | + messages.on('created', function(message) { |
| 79 | + console.log('Someone created a message', message); |
| 80 | + }); |
| 81 | +
|
| 82 | + messages.create({ |
| 83 | + text: 'Message created in browser' |
| 84 | + }); |
| 85 | +</script> |
| 86 | +``` |
| 87 | + |
| 88 | +## License |
| 89 | + |
| 90 | +Copyright (c) 2021 by Feathers |
| 91 | + |
| 92 | +Licensed under the [MIT license](LICENSE). |
0 commit comments