Created
January 2, 2017 19:22
-
-
Save mpaleo/6d2d9511df84223446a357984917f07b to your computer and use it in GitHub Desktop.
Lowdb react native driver based on RNFS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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