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 #4

Open
wants to merge 10 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
56 changes: 56 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

# BUCK
buck-out/
\.buckd/
*.keystore

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

*/fastlane/report.xml
*/fastlane/Preview.html
*/fastlane/screenshots

# Bundle artifact
*.jsbundle
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ react-native link realm
```diff
import {compose, applyMiddleware, createStore} from 'redux'
import {persistStore, autoRehydrate} from 'redux-persist'
+ import realmPersistInterface from 'redux-persist-realm'
+ import RealmPersistInterface from 'redux-persist-realm'
+ const storage = RealmPersistInterface.instance;

// add `autoRehydrate` as an enhancer to your store (note: `autoRehydrate` is not a middleware)
const store = createStore(
Expand All @@ -44,7 +45,7 @@ const store = createStore(
)

+ const config = {
+ storage: realmPersistInterface
+ storage
+ }

// begin periodically persisting the store
Expand All @@ -53,4 +54,3 @@ const store = createStore(
```

Don't hesitate to publish [Issues](https://github.com/Osedea/redux-persist-realm/issues) if you see something missing!

194 changes: 107 additions & 87 deletions RealmPersistInterface.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,111 @@
import Realm from 'realm';

class RealmPersistInterface {
constructor() {
this.realm = new Realm({
schema: [{
name: 'Item',
primaryKey: 'name',
properties: {
name: 'string',
content: 'string',
},
}],
});

this.items = this.realm.objects('Item');
}

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);
}
};

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
);
}

callback();
});
});
} catch (error) {
callback(error);
}
};

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

this.realm.delete(item);
});
} catch (error) {
callback(error);
}
};

getAllKeys = (callback) => {
try {
const keys = this.items.map(
(item) => item.name
);

callback(null, keys);
} catch (error) {
callback(error);
}
};
}
var Symbol = require('es6-symbol/polyfill');

const singleton = Symbol();
const singletonEnforcer = Symbol();

export default class RealmPersistInterface {
constructor(enforcer) {
if (enforcer !== singletonEnforcer) {
throw new Error('Cannot construct singleton');
}

this._type = 'RealmPersistInterface';

this.realm = Realm.open({
path: 'redux.realm',
schema: [
{
name: 'Item',
primaryKey: 'name',
properties: {
name: 'string',
content: 'string'
}
}
]
});
}

static get instance() {
if (!this[singleton]) {
this[singleton] = new RealmPersistInterface(singletonEnforcer);
}

return this[singleton];
}

get type() {
return this._type;
}

async check() {
if (!this.items) {
this.realm = await this.realm;
this.items = this.realm.objects('Item');
}
}

const singleton = new RealmPersistInterface();
async getItem(key) {
await this.check();

export default singleton;
return new Promise((resolve, reject) => {
try {
const matches = this.items.filtered(`name = "${key}"`);

if (matches.length > 0 && matches[0]) {
resolve(matches[0].content);
} else {
reject(new Error(`Could not get item with key: '${key}'`));
}
} catch (error) {
reject(error);
}
});
};

async setItem(key, value) {
await this.check();

return new Promise((resolve, reject) => {
try {
this.realm.write(() => {
this.realm.create('Item', { name: key, content: value }, true);
resolve();
});
} catch (error) {
reject(error);
}
});
};

async removeItem(key) {
await this.check();

return new Promise((resolve, reject) => {
try {
this.realm.write(() => {
const item = this.items.filtered(`name = "${key}"`);
this.realm.delete(item);
resolve();
});
} catch (error) {
reject(error);
}
});
};

async getAllKeys() {
await this.check();

return new Promise((resolve, reject) => {
try {
const keys = this.items.map(item => item.name);
resolve(keys);
} catch (error) {
reject(error);
}
});
};
}
14 changes: 11 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 @@ -23,8 +23,16 @@
"url": "https://github.com/Osedea/redux-persist-realm/issues"
},
"homepage": "https://github.com/Osedea/redux-persist-realm#readme",
"dependencies": {
"es6-symbol": "^3.1.1"
},
"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"
}
}