Skip to content

Instantly share code, notes, and snippets.

@1fabiopereira
Created July 14, 2017 19:55
Show Gist options
  • Save 1fabiopereira/cba8e8ac3b3acb81d3f719b22530c630 to your computer and use it in GitHub Desktop.
Save 1fabiopereira/cba8e8ac3b3acb81d3f719b22530c630 to your computer and use it in GitHub Desktop.
Simple Asyncstorage wrapper react-native
import {AsyncStorage} from "react-native";
class Storage {
constructor (name) {
this.name = `@${name.toUpperCase()}`;
}
add (key, value) {
if (typeof value !== "object") {
throw new Error(`Value is not a object: ${String(valeu)}`);
}
return AsyncStorage.getItem(`${this.name}:${key}`, JSON.stringify(value));
}
multiAdd () {
return null;
}
get (key) {
return AsyncStorage.getItem(`@${this.name}:${key}`);
}
multiGet () {
return null;
}
getKeys () {
return null;
}
getAllKeys () {
return null;
}
remove (key) {
return AsyncStorage.remove(`${this.name}:${key}`);
}
multiRemove () {
return null;
}
merge (key, value) {
if (typeof value !== "object") {
throw new Error(`Value is not a object: ${String(valeu)}`);
}
return AsyncStorage.mergeItem(
`@${this.name}:${key}`,
JSON.stringify(value)
);
}
multiMerge () {
return null;
}
drop () {
return null;
}
dropAll () {
return null;
}
}
export default Storage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment