我在realm.js文件中有以下2个模式
class Bill extends Realm.Object {}
Bill.schema = {
name: 'Bill',
primaryKey: 'id',
properties: {
id: { type: 'string', indexed: true },
year: 'int',
month: 'int',
description: 'string',
dateCreated: 'int',
}
};
class User extends Realm.Object {}
User.schema = {
name: 'User',
primaryKey: 'id',
properties: {
id: 'string',
name: 'string?',
email: 'string?'
}
};
const realm = new Realm({schema: [Bill, User]});
export default realm;
当我第一次将我的应用程序发布到AppStore或PlayStore时,这非常有效 . 我需要再次将架构和发布更改为AppStore或PlayStore,我需要处理我的应用程序的新安装或更新,其中架构更改为以下
class Bill extends Realm.Object {}
Bill.schema = {
name: 'Bill',
primaryKey: 'id',
properties: {
id: { type: 'string', indexed: true },
year: 'int',
month: 'int',
description: 'string',
dateCreated: 'int',
dateDeleted: 'int',
}
};
class User extends Realm.Object {}
User.schema = {
name: 'User',
primaryKey: 'id',
properties: {
id: 'string',
name: 'string?',
email: 'string?',
photoUrl: 'string?',
}
};
通过在每个模式中添加一个字段 .
那么我该如何配置我的领域架构版本呢?
我应该配置如下:
const realm = new Realm([
{schema: Bill, schemaVersion: 1},
{schema: User, schemaVersion: 1}
]);
但是这可能会因新安装而崩溃 .
1 回答
您应该为所有数据库设置全局
schemaVersion
,而不是为每个模型设置 . 并执行到当前版本的迁移,如_1521710中所述://遍历所有对象并在新架构中设置name属性
for(let i = 0; i <oldObjects.length; i){
newObjects [i] .name = oldObjects [i] .firstName''oldObjects [i] .lastName;
}
}
}
}) . then(realm => {
const fullName = realm.objects('Person')[0] .name;
});
成功完成迁移后,您的应用程序可以像往常一样访问Realm及其所有对象 .