const schema1 = [
rolesSchema,
userMutedInRoomSchema,
uploadsSchema,
usersForMentionSchema,
contactsSchema,
];
const schema2 = [
rolesSchema,
userMutedInRoomSchema,
uploadsSchema,
usersForMentionSchema,
contactsSchema,
stickersPackagesSchema,
stickersCollectionSchema
];
以上是两个模式 Schema1 是我已经使用的模式,它工作正常 Schema2 是新架构,其中我在联系人架构之后的末尾添加了新表(架构)。我已经遵循了文档,但我找不到任何解释在旧架构中添加新表的内容。下面是我用来初始化在运行时崩溃的新架构的代码
const path = database.replace(/(^w+:|^)///, '');
return this.databases.activeDB = new Realm({
path: `${ path }Value.realm`,
schema:schema2,
schemaVersion:1,
migration: (oldRealm, newRealm) => {
},
});
如果要更新生产应用的架构。然后,您需要编写迁移逻辑并更新架构版本。
Realm.open({
schema: [PersonSchema],
schemaVersion: 1,
migration: (oldRealm, newRealm) => {
// only apply this change if upgrading to schemaVersion 1
if (oldRealm.schemaVersion < 1) {
const oldObjects = oldRealm.objects('Person');
const newObjects = newRealm.objects('Person');
// loop through all objects and set the name property in the new schema
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;
});
参考: https://realm.io/docs/javascript/latest/#performing-a-migration
如果您当前正在开发应用程序,则只需更新 schemaVersion 并添加 deleteRealmIfMigrationNeeded
属性即可删除旧的过时数据
参考: https://realm.io/docs/javascript/latest/#opening-realms