Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support redux-persist v5 #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
.idea/
node_modules/

# CMake
cmake-build-debug/

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
159 changes: 83 additions & 76 deletions RealmPersistInterface.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,98 @@
// @flow
import Realm from 'realm';

class RealmPersistInterface {
constructor() {
this.realm = new Realm({
schema: [{
name: 'Item',
primaryKey: 'name',
properties: {
name: 'string',
content: 'string',
},
}],
});
/**
* modified by Stanimir Marinov (https://github.com/sytolk)
* following: https://github.com/leethree/redux-persist-fs-storage/blob/master/index.js
*/

this.items = this.realm.objects('Item');
// Wrap function to support both Promise and callback
async function withCallback<R>(
callback?: ?(error: ?Error, result: R | void) => void,
func: () => Promise<R>
): Promise<R | void> {
try {
const result = await func();
if (callback) {
callback(null, result);
}
return result;
} catch (err) {
if (callback) {
callback(err);
} else {
throw err;
}
}
}

getItem = (key, callback) => {
try {
const matches = this.items.filtered(`name = "${key}"`);

if (matches.length > 0 && matches[0]) {
callback(null, matches[0].content);
} else {
throw new Error(`Could not get item with key: '${key}'`);
}
} catch (error) {
callback(error);
}
const RealmPersistInterface = (
encryptionKey?: Int8Array,
schemaName?: string = 'Item',
keyColumn?: string = 'key',
valueColumn?: string = 'value',
) => {
const config: Object = {
schema: [{
name: schemaName,
primaryKey: keyColumn,
properties: {
[keyColumn]: 'string',
[valueColumn]: 'string',
},
}],
};

setItem = (key, value, callback) => {
try {
this.getItem(key, (error) => {
this.realm.write(() => {
if (error) {
this.realm.create(
'Item',
{
name: key,
content: value,
}
);
} else {
this.realm.create(
'Item',
{
name: key,
content: value,
},
true
);
}
if (encryptionKey) {
config.encryptionKey = encryptionKey;
}

callback();
});
});
} catch (error) {
callback(error);
}
};
const realm = new Realm(config);

removeItem = (key, callback) => {
try {
this.realm.write(() => {
const item = this.items.filtered(`name = "${key}"`);
const items = realm.objects(schemaName);

this.realm.delete(item);
const getItem = (
key: string,
callback?: ?(error: ?Error, result: ?string) => void,
): Promise<?string> =>
withCallback(callback, async () => {
const item = realm.objectForPrimaryKey(schemaName, key);
if (item) {
return item[valueColumn];
}
});

const setItem = (
key: string,
value: string,
callback?: ?(error: ?Error) => void
): Promise<void> =>
withCallback(callback, async () => {
realm.write(() => {
realm.create(schemaName, { key, value }, true);
});
} catch (error) {
callback(error);
}
};
});

getAllKeys = (callback) => {
try {
const keys = this.items.map(
(item) => item.name
);
const removeItem = (
key: string,
callback?: ?(error: ?Error) => void,
): Promise<void> =>
withCallback(callback, async () => {
const item = realm.objectForPrimaryKey(schemaName, key);
if (item) {
realm.write(() => realm.delete(item));
}
});

callback(null, keys);
} catch (error) {
callback(error);
}
};
}
const getAllKeys = (callback?: ?(error: ?Error, keys: ?Array<string>) => void) =>
withCallback(callback, async () => items.map((item) => item[keyColumn]));

const singleton = new RealmPersistInterface();
return {
setItem,
getItem,
removeItem,
getAllKeys,
};
};

export default singleton;
export default RealmPersistInterface;
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-persist-realm",
"version": "0.1.0",
"version": "0.2.0",
"description": "A Realmjs interface for redux-persist",
"main": "RealmPersistInterface.js",
"scripts": {
Expand All @@ -24,7 +24,12 @@
},
"homepage": "https://github.com/Osedea/redux-persist-realm#readme",
"peerDependencies": {
"realm": "^1.8.3",
"redux-persist": "^4.0.0"
"realm": "2.x",
"redux-persist": "5.x"
},
"devDependencies": {
"realm": "^2.0.12",
"redux-persist": "^5.4.0",
"redux": "^3.7.2"
}
}
Loading