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"]
  ]
}

先这样吧,想到什么再写咯函数

图片描述

相关文章
相关标签/搜索