如何在React naitve中进行领域数据库的迁移?

时间:2020-05-25 13:55:24

标签: react-native realm database-schema realm-mobile-platform realm-migration

我正在为我的应用程序使用领域数据库。我想向架构添加2个字段:电子邮件和密码。它给出了一个错误,错误是: 由于以下错误,需要迁移: 属性Note.email已添加 属性Note.password已添加

我知道迁移功能的一般格式,但我不知道如何编写用于添加字段的迁移代码 我已经阅读了领域网站上的文档以及类似的问题和代码,但是主题都没有帮助我。

旧模式:

Map()

新架构:

export const NoteSchema = {
    name: 'Note',
    primaryKey:'id',
    properties: {
        id: 'int',    // primary key
        time_date:'string',
        note_title:{ type: 'string', indexed: true },
        note_body: { type: 'string', optional: true },
    }
};

旧数据库选项

export const NoteSchema = {
    name: 'Note',
    primaryKey:'id',
    properties: {
        id: 'int',    // primary key
        time_date:'string',
        note_title:{ type: 'string', indexed: true },
        note_body: { type: 'string', optional: true },
        email:{ type: 'string', optional: false },
        password:{ type: 'string', optional: false }
    }
};

新的数据库选项

 const databaseOptions = {
     path: 'noteListApp.realm',
     schema:[NoteSchema],
     schemVersion:0,
 }


export const inserNewNote = newNote => new Promise((resolve,reject)=> {
    Realm.open(databaseOptions).then(realm=>{
            realm.write(()=>{
                realm.create(NOTE_SCHEMA,newNote,true);
                resolve(newNote)
            })
    }).catch((error)=>reject (error))
})

0 个答案:

没有答案