RxDB:indexedDB的踩坑之路

RxDB:indexedDB的踩坑之路

目前國內社區關於RxDB的資料較少,這篇文章是爲了記錄本身使用中遇到的一些問題解決總結,不會涉及到基本知識的科普,若是有同窗有興趣,再另外開一篇文章吧。javascript

圖片描述


Schema中default生成器的實現

// 演示例子?,這是一個Schema的定義
const Schema = {
  "title": "hero schema",
  "version": 0,
  "description": "describes a simple hero",
  "type": "object",
  "properties": {
      "name": {
          "type": "string",
          "default": function(){
              return 'idGenerate' + Math.random().toString(16).substr(2,12)
          }
      }
  },
  "required": ["color"]
}

在RxDB中,Schema在設計之初就應一個純潔的JSON,始終可以解析與字符串化,因此並不支持函數,可是這樣的好處多多,好比……html

圖片描述

那若是咱們但願實現相似上方 這種默認值生成器,該怎麼作呢?java

圖片描述

那就是!使用Middleware-hooks添加鉤子的方式來操做,例如 :git

// 實現例子?
myCollection.preInsert(function(documentData){
    if(!documentData.name){
        documentData.name = 'idGenerate' + Math.random().toString(16).substr(2,12)
    }
}, false);

參考連接:RxDB-Middlewaregithub


sort排序

sort只能夠針對擁有index的字段,或是建立了複合索引compoundIndex才能夠進行排序。dom

// 這也是一個Schema
{
  "title": "hero schema",
  "version": 0,
  "description": "describes a simple hero",
  "type": "object",
  "properties": {
      "name": {
          "type": "string",
          "index": true
      },
      "age": {
          "type": number
      },
      "create_time": {
          "type": number
      }
  },
  "compoundIndex": [
      ["age", "create_time"]
  ]
}

先這樣吧,想到什麼再寫咯函數

圖片描述

相關文章
相關標籤/搜索