React native realm数据迁移

React native realm

2020年1月13日

realm 数据库

https://realm.io/
https://realm.io/cn/docs/javascript/latest/
https://realm.io/cn/docs/objc/latest/#section

realm studio


React native realm数据迁移_第1张图片
image.png

数据迁移过程

import Realm from 'realm';

//数据库的定义
...

//数据迁移过程
var schemas = [
    {
        schema: [student],
        schemaVersion: 1,
        migration: (oldRealm, newRealm) => {
          // only apply this change if upgrading to schemaVersion 1
          if (oldRealm.schemaVersion < 1) {
            const oldObjects = oldRealm.objects('student');
            const newObjects = newRealm.objects('student');
            // loop through all objects and set the name property in the new schema
            for (let i = 0; i < oldObjects.length; i++) {
               console.log('item start');
              newObjects[i].studypattern = 0;
            }
          }
        }
    },
]
let nextSchemaIndex = Realm.schemaVersion(Realm.defaultPath);
console.log(Realm.defaultPath);
if (nextSchemaIndex != -1) {
    while (nextSchemaIndex < schemas.length) {
      //  var migratedRealm = new Realm(schemas[nextSchemaIndex++]);
     //   migratedRealm.close();
    const migratedRealm = new Realm({ ...schemas[nextSchemaIndex] });
      nextSchemaIndex += 1;
      migratedRealm.close();
    }
}
export default new Realm({ schema: [person,student, teacher] ,schemaVersion: 1});

schemaVersion: number是表示数据库的版本的。

你可能感兴趣的:(React native realm数据迁移)