首页 > 解决方案 > 使用 SQLite 代替存储/本地存储 Ionic 4 Native

问题描述

假设我想将聊天消息本地存储在 Ionic 4 应用程序中,但我不想每秒删除和插入,n我可以使用单独的 SQLite 文件并确保我清理条目并且可以吗?

是的,因此出于安全原因和其他方面的考虑,我正在编写一个聊天应用程序,并得出结论,最好的办法是拥有一个可以存储所有消息的 SQLite 文件。现在我已经听到你的声音了,可能在你开始这部分之前说“嘿!!Ionic 4 和 Cordova 已经有一个插件!它是基于 SQLite 的!!” 您是对的,但是,它存储为键/值对,不允许任何类型的查询或表连接。以下方法可用(get、set、length、delete、forEach)。我想要的是一个简单的 update()。我在这里唯一能看到的是一种 noSQL 方法,其中更新只是删除和重新插入。

以下是存储内容的示例:

{
  conversations: [
    {id: 1, 
      people: ["Christian", "Sara"], 
      messages: [
        {id: 1, author: "Christian" content: "hi",  date: <date>},
        {id: 2, author: "Sara",     content: "foo", date: <date>}, 
        {id: 3 ...}
      ]
    },
    {id: 2, person: "Laura", messages: [{}, {}, {}]
  ]
}

所以基本上我想做的是aINSERT INTO messages WHERE id = :id或类似的东西,而不是类似的东西:

function update(){
  const oldMessages = this.storage.forEach('messages')
    .then(chat => {
      //loop over the chat and store each chat object in the old array,
    } 
 // set an interval that will retrieve the new messages 
//and append them into the old messages, then finally delete the old key/value storage and then
db.set('messagesChristianAndSara', messageArrayOfObjects)
}

这是我想我会发现自己的情况。我宁愿只是想将对象推入或插入。

也许有一种方法,但文档并不清楚。

标签: angularsqliteionic-frameworkionic4ionic-native

解决方案


推荐阅读