realm-js - 将本地领域数据复制到sycned领域

时间:2017-06-27 20:26:11

标签: javascript react-native realm

我正在使用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',
    },
  },
};

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

将数据从一个领域复制到另一个领域时,有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:2)

看看这个采用本地.realm文件的代码,并将其复制到远程同步的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");