RxDB:indexedDB的踩坑之路

RxDB:indexedDB的踩坑之路

目前國內社區關於RxDB的材料較少,這篇文章是為了紀錄本身運用中碰到的一些問題解決總結,不會涉及到基本知識的科普,假如有同硯有興緻,再別的開一篇文章吧。

《RxDB:indexedDB的踩坑之路》

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,一直能夠剖析與字符串化,所以並不支撐函數,然則如許的好處多多,比方……

《RxDB:indexedDB的踩坑之路》

那假如我們願望完成相似上方 這類默認值生成器,該怎麼做呢?

《RxDB:indexedDB的踩坑之路》

那就是!運用Middleware-hooks增加鈎子的體式格局來操縱,比方 :

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

參考鏈接:RxDB-Middleware

sort排序

sort只能夠針對具有index的字段,或是創建了複合索引compoundIndex才能夠舉行排序。

// 這也是一個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"]
  ]
}

先如許吧,想到什麼再寫咯

《RxDB:indexedDB的踩坑之路》

    原文作者:大王饒命丶
    原文地址: https://segmentfault.com/a/1190000014618308
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞