|
| 1 | +#mst-persist |
| 2 | + |
| 3 | +[](https://npmjs.org/package/mst-persist) |
| 4 | +[](https://github.com/agilgur5/mst-persist/releases) |
| 5 | +[](https://github.com/agilgur5/mst-persist/commits/master) |
| 6 | + |
| 7 | +[](https://npmjs.org/package/mst-persist) |
| 8 | +[](https://npmjs.org/package/mst-persist) |
| 9 | +[](https://npmjs.org/package/mst-persist) |
| 10 | +[](https://npmjs.org/package/mst-persist) |
| 11 | + |
| 12 | +[](https://npmjs.org/package/mst-persist) |
| 13 | + |
| 14 | +Persist and hydrate MobX-state-tree stores. |
| 15 | + |
| 16 | +##Installation |
| 17 | + |
| 18 | +`npm i -S agilgur5/mst-persist` |
| 19 | + |
| 20 | +The`mst-persist` NPM package is not currently owned by me, so please install via GitHub for now. |
| 21 | +Will be looking to get ownership of the package[from the owner](https://github.com/pinqy520/mobx-persist/issues/37#issuecomment-361479253) shortly. |
| 22 | + |
| 23 | +##Usage |
| 24 | + |
| 25 | +```javascript |
| 26 | +import {types }from'mobx-state-tree' |
| 27 | +importlocalForagefrom'localForage' |
| 28 | +import {persist }from'mst-persist' |
| 29 | + |
| 30 | +constSomeStore=types.model('Store', { |
| 31 | + name:'John Doe', |
| 32 | + age:32 |
| 33 | +}) |
| 34 | + |
| 35 | +constsomeStore=SomeStore.create() |
| 36 | + |
| 37 | +persist('some', someStore, { |
| 38 | + storage: localForage,// or AsyncStorage in react-native. |
| 39 | +// default: localStorage |
| 40 | + jsonify:false// if you use AsyncStorage, this shoud be true |
| 41 | +// default: true |
| 42 | + whitelist: ['name']// only these keys will be persisted |
| 43 | +}).then(()=>console.log('someStore has been hydrated')) |
| 44 | + |
| 45 | +``` |
| 46 | + |
| 47 | +###API |
| 48 | + |
| 49 | +####`persist(key, store, options)` |
| 50 | + |
| 51 | +- arguments |
| 52 | +-**key***string* The key of your storage engine that you want to persist to. |
| 53 | +-**store***MST store* The store to be persisted. |
| 54 | +-**options***object* Additional configuration options. |
| 55 | +-**storage***[localForage](https://github.com/localForage/localForage) / AsyncStorage / localStorage*[localForage](https://github.com/localForage/localForage)-style storage API. localStorage for Web (default), AsyncStorage for React Native. |
| 56 | +-**jsonify***bool* Enables serialization as JSON (default:`true`). |
| 57 | +-**whitelist***Array\<string\>* Only these keys will be persisted (defaults to all keys). |
| 58 | +-**blacklist***Array\<string\>* These keys will not be persisted (defaults to all keys). |
| 59 | + |
| 60 | +- returns a void Promise |
| 61 | + |
| 62 | +##How it works |
| 63 | + |
| 64 | +Basically just a small wrapper around MST's`onSnapshot` and`applySnapshot`. |
| 65 | +The source code is currently shorter than this README, so take a look under the hood! :) |
| 66 | + |
| 67 | +##Credits |
| 68 | + |
| 69 | +Inspiration for parts of the code and API came from[`redux-persist`](https://github.com/rt2zz/redux-persist),[`mobx-persist`](https://github.com/pinqy520/mobx-persist), and[this MST persist PoC gist](https://gist.github.com/benjick/c48dd2db575e79c7b0b1043de4556ebc) |