【问题标题】:realm-js - Copy local realm data to sycned realmrealm-js - 将本地领域数据复制到同步领域
【发布时间】:2017-06-27 20:26:11
【问题描述】:

我正在使用realm-js 在使用 React Native 的设备上为用户存储数据,并且在工作流程中有一点我想将本地领域中的所有数据复制到同步领域中(以保持在 ROS 上)。我遇到了一个问题,在我们的模式中,我们通过将引用一个对象的属性添加到另一个对象中来创建伪关系,反之亦然,这样做我们创建了循环数据结构。

这实际上在应用程序中运行良好,但现在我们试图将数据从本地领域复制到同步领域,它似乎崩溃了,因为循环引用。

例如,模式看起来像这样。

class Person extends Realm.Object {}
Person.schema = {
  name: 'Person',
  properties: {
    firstName: {
      type: 'string',
      optional: true,
    },
    staffAccount: {
      type: 'StaffAccount'
    },
  },
};

class StaffAccount extends Realm.Object {}
StaffAccount.schema = {
  name: 'StaffAccount',
  properties: {
    id: {
      type: 'string',
    },
    people: {
      type: 'list',
      objectType: 'Person',
    },
  },
};

在此示例中,当创建一个 Person 时,您将定义一个 staffAccount 属性,并且该员工帐户属性将具有一个 people 属性,该属性具有该员工帐户的人员列表,并且在该列表中将有该人员的初始人员人员架构。

在将数据从一个领域复制到另一个领域时,有什么办法可以解决这个问题?

【问题讨论】:

    标签: javascript react-native realm


    【解决方案1】:

    看看这段代码,它获取本地.realm 文件并将其复制到远程同步领域。代码可以简化,因为看起来您已经知道架构 - 此代码动态加载架构。希望对你有帮助

    // Copy local realm to ROS
    
    const Realm = require('realm');
    
    
    // UPDATE THESE
    const realm_server = 'localhost:9080';
    
    const source_realm_path = './localRealm.realm'; // path on disk
    const target_realm_path = '/syncRealm'; // path on server
    
    
    function copyObject(obj, objSchema, targetRealm) {
        const copy = {};
        for (var key in objSchema.properties) {
            const prop = objSchema.properties[key];
            if (prop.type == 'list') {
                const propObjSchema = targetRealm.schema.find((s) => s.name == prop.objectType)
                copy[key] = obj[key].map((obj) => copyObject(obj, propObjSchema, targetRealm))
            }
            else if (prop.type == 'object') {
                const propObjSchema = targetRealm.schema.find((s) => s.name == prop.objectType)
                copy[key] = obj[key] ? copyObject(obj[key], propObjSchema, targetRealm) : obj[key];
            }
            else {
                copy[key] = obj[key];
            }
        }
        return copy;
    }
    
    function getMatchingObjectInOtherRealm(sourceObj, source_realm, target_realm, class_name) {
        const allObjects = source_realm.objects(class_name);
        const ndx = allObjects.indexOf(sourceObj);
    
        // Get object on same position in target realm
        return target_realm.objects(class_name)[ndx];
    }
    
    function addLinksToObject(sourceObj, targetObj, objSchema, source_realm, target_realm) {
        for (var key in objSchema.properties) {
            const prop = objSchema.properties[key];
            if (prop.hasOwnProperty('objectType')) {
                if (prop['type'] == "list") {
                    var targetList = targetObj[key];
                    sourceObj[key].forEach((linkedObj) => {
                        const obj = getMatchingObjectInOtherRealm(linkedObj, source_realm, target_realm, prop.objectType);
                        targetList.push(obj);
                    });
                }
                else {
                    // Find the position of the linked object
                    const linkedObj = sourceObj[key];
                    if (linkedObj === null) {
                        continue;
                    }
    
                    // Set link to object on same position in target realm
                    targetObj[key] = getMatchingObjectInOtherRealm(linkedObj, source_realm, target_realm, prop.objectType);
                }
            }
        }
    }
    
    function copyRealm(user, local_realm_path, remote_realm_url) {
        // Open the local realm
        const source_realm =  new Realm({path: local_realm_path});
    
        // Create the new realm (with same schema as the source)
        const target_realm = new Realm({
            sync: {
                user: user,
                url:  remote_realm_url,
            },
            schema: require('./realmmodels')
        });
    
        // Copy all objects but ignore links for now
        target_realm.schema.forEach((objSchema) => {
            console.log("copying objects:", objSchema['name']);
            const allObjects = source_realm.objects(objSchema['name']);
            target_realm.write(() => 
              allObjects.forEach((obj) => {
                  // Add this object to the target realm
                      target_realm.create(objSchema.name, copyObject(obj, objSchema, target_realm), true)
            }));
        });
    }
    
    const remote_realm_url = "realm://" + realm_server + target_realm_path;
    copyRealm(Realm.Sync.User.adminUser("ADMIN_TOKEN"), 
        source_realm_path, remote_realm_url);
    console.log("done");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多