gpt4 book ai didi

javascript - realm-js - 将本地 Realm 数据复制到同步 Realm

转载 作者:行者123 更新时间:2023-11-30 15:13:33 26 4
gpt4 key购买 nike

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

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

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

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 属性,该属性包含具有该员工帐户的人员列表,在该列表中将有 person initial person in人物模式。

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

最佳答案

看看这段代码,它采用本地 .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");

关于javascript - realm-js - 将本地 Realm 数据复制到同步 Realm ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44789273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com