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
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞