node.js - 打开在另一个 .JS 文件中定义的 Realm-JS

标签 node.js react-native realm

我正在尝试在react-native中使用realm-js,所以我所做的就是在我的操作文件夹中创建一个realm-js类,如LoginActions.js,它将在用户登录后写入数据。

我的问题是如何在另一个 .js 文件中打开相同的架构?

Realm-js 在 LoginActions.js 中定义:

class Login {
  get token() {
    return this.token;
  }
}

Login.schema = {
  name: 'Login',
  primaryKey: 'id',
  properties: {
    id: 'int',
    token: 'string',
  }
};

然后我使用我的一些函数打开要在此架构中写入和更新的 Realm 。

喜欢:

// If Login User Success
Realm.open({ schema: [Login] })
.then(realm => {
  realm.write(() => {
    realm.create('Login', { id: 1, token });
  });
});

现在如果想将此架构打开到另一个 .js 文件中。我将如何打开它? 我想要的主要格式类似于 Loading.js:

import ????? from ????;

或者

Realm.open(????????)
.then(realm => {
  realm.write(() => {
    const isUser = realm.create('Login', { id: 1, token }, true);
    if(isUser.length == 1) {
        // User Logs In Directly to Main Screen
    } else {
        // Show them Login Screen
    }
  });
});

最佳答案

您可以保存 Realm 实例并稍后在其他 .js 文件中使用它。

不使用 Redux(推荐)

这是我建议使用的方式,即使您在应用程序中使用 Redux。

架构和 Realm 创建发生在realm.js 文件中,该文件稍后会导入到代码中所需的任何位置。

realm.js 文件看起来像(有 2 个示例架构):

import Realm from 'realm'; 

class Favorite extends Realm.Object {}
Favorite.schema = {
  name: 'Favorite',
  primaryKey: 'id', 
  properties: {
    id:     'string', 
    type:     'string', 
    favorite: 'bool', 
    alert: 'bool', 
    tag:     'string', 
    artistId: 'int'  
  }
};

class Friend extends Realm.Object {}
Friend.schema = {
  name: 'Friend',
  primaryKey: 'fbId',
  properties: {
    name: 'string',
    fbId:'string',  
    email: 'string',
    foto: 'string',
    concerts: 'int[]',
    artists: 'int[]'
  }
};

var schemasArray = [
  Favorite, 
  Friend

export default new Realm({schema: schemasArray});

然后从应用程序中的任何组件或文件中,您可以简单地执行以下操作:

import realm from './realm'

并按照 Realm 文档中的方式使用它:

try {
  if(realm){
    realm.write(() => {
      //delete all objects first
      var allFavorites = realm.objects('Favorite');
      realm.delete(allFavorites);
    });
  }
} catch (e) {
  console.log(`Error deleting favorites: ${schema} - ${e}`);
  reject();
}

Redux方式

如果您使用 Redux,您可以先将 Realm 实例保存到应用程序状态,然后通过状态中的 props 将其传递给任何组件。 编辑:最后结果非常相似,但是您添加了更多样板代码以将 Realm 保存到商店中。

文件 1

Realm.open({ schema: [Login] })
.then(realm => {
  this.saveRealm( realm )
});

操作

saveRealm 是一个操作:

export function saveRealm( realm ) {
  return {
    type: types.SAVE_REALM,
    realm
  }
}

reducer

这将在 reducer 中处理:

export default function reducer(state = initialState, action = {}) {

  switch (action.type) {
    case types.SAVE_REALM:
      return Object.assign({}, state, {
        realm: action.realm
      });

      //other actions
    default:
       return state;
  }

文件2

最后在其他文件 .js 中只需使用保存的 Realm 实例,无需再次打开它。

this.props.realm.write(() => {
    const isUser = this.props.realm.create('Login', { id: 1, token }, true);
   //rest of your code...
  });

您需要像往常一样使用mapStateToProps:

function mapStateToProps(state) {
  return {
    realm: state.realm,

编辑:将“不使用 Redux”作为推荐选项。添加更多代码示例代码。

关于node.js - 打开在另一个 .JS 文件中定义的 Realm-JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46566527/

相关文章:

javascript - 将支持 node.js 中的原始套接字,例如创建ping数据包?

node.js - bash:bower:找不到命令,我不知道该怎么办

javascript - 在这个例子中 setState 是如何工作的?

ios - 将 Realm 与 Crashlytics 结合使用时的 NSUncaughtExceptionHandler

ios - Realm iOS 的模糊安装说明

javascript - 仅当不存在时才添加到 Realm 列表

node.js - 为什么我们不在 NodeJS 应用程序中使用 "express.use"?

javascript - 如何使用 Nodemailer 使用我的公司域发送电子邮件?

android - 找不到满足版本限制的 'com.android.support:appcompat-v7' 版本

javascript - React-Native/Firebase 插入并按时间戳排序