Skip to content

Instantly share code, notes, and snippets.

@mpaleo
Created January 2, 2017 19:22
Show Gist options
  • Save mpaleo/6d2d9511df84223446a357984917f07b to your computer and use it in GitHub Desktop.
Save mpaleo/6d2d9511df84223446a357984917f07b to your computer and use it in GitHub Desktop.
Lowdb react native driver based on RNFS
import RNFS from 'react-native-fs';
module.exports = {
read: (source) =>
{
return RNFS.exists(source).then((fileExists) =>
{
if (fileExists)
{
return RNFS.readFile(source).then((data) => JSON.parse(data));
}
else
{
return RNFS.writeFile(source, '{}', 'utf8').then((success) =>
{
return {};
}).catch((error) =>
{
console.error(error);
});
}
});
},
write: (dest, obj) =>
{
RNFS.exists(dest).then((fileExists) =>
{
if (fileExists)
{
return RNFS.writeFile(dest, JSON.stringify(obj, null, 2));
}
else
{
throw new Error('File doesn\'t exists');
}
}).catch((error) =>
{
console.error(error);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment