Basic wrapper for file storage strongly inspired by react hooks. It allows us to storage variable passed through reference (such as object, array and other possible JSON values). Make react-like data flow with no effort!
yarn add use-fs-storage
or
npm i --save use-fs-storage
import useStorage from 'use-fs-storage';
// first use
(async () => {
const [users, setUsers] = await useStorage('users.json', {
defaultValue: [],
immediatelySync: true, // sync right after create store
overrideDefault: true, // if specified path exists it will override default value
});
console.log(users); // []
await setUsers([1, 2, 3]); // save value of "users" variable to "users.json"
console.log(users); // [1, 2, 3]
await setUsers(previousValue => previousValue.map(item => item * 2));
console.log(users); /// [2, 4, 6] - same as "users.json" value
})();
// second use
(async () => {
const [users, setUsers] = await useStorage('users.json', {
defaultValue: [],
immediatelySync: true,
overrideDefault: true,
});
console.log(users); // [2, 4, 6]
await setUsers(previousValue => previousValue.map(item => item * 2));
console.log(users); /// [4, 8, 12] - same as "users.json" value
})();
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.