无法运行RxDb迁移策略

时间:2019-04-01 11:23:25

标签: reactjs redux pouchdb rxdb

我正在尝试实施一个简单的migrationStrategy,如此处的文档所述:https://rxdb.info/data-migration.html

我想知道是否有人成功实现了migrationStrategies,因为我无法在线找到任何示例,还是我只是在浪费时间。

当我尝试传递一个对象(如文档规定)时,出现以下错误:

  

RxError:RxError:migrationStrategies必须是一个未捕获的函数(承诺)> RxError:RxError:migrationStrategy必须是一个函数

当我尝试将策略作为函数传递时,出现以下错误:

  

RxError:RxError:migrationStrategies必须是一个对象construct.js:30未捕获(承诺)RxError:RxError:migrationStrategies必须是一个对象

    import * as RxDB from "rxdb"
    import { Component } from "react"
    import React from "react"
    RxDB.plugin(require("pouchdb-adapter-idb"))
    RxDB.plugin(require("pouchdb-adapter-http"))
    const dbName = "master"

    export default class App extends Component {
      constructor(props) {
        super(props)
        this.state = {
          newCookie: "",
          cookies: [],
        }
        this.subs = []
        this.addCookie = this.addCookie.bind(this)
        this.handleCookieChange = this.handleCookieChange.bind(this)
      }

      async createDatabase() {
        const db = await RxDB.create({
          name: dbName,
          adapter: "idb",
          password: "12345678",
        })
        db.waitForLeadership().then(() => {
          document.title = "â™› " + document.title
        })
        await db.collection({
          name: "cookies",
          schema: {
            title: "Cookie",
            description: "Database schema for a cookie",
            version: 6,
            type: "object",
            properties: {
              id: {
                type: "string",
                primary: true,
              },
              flavour: {
                type: "string",
              },
              someNewProperty: {
                type: "string",
              },
              flavor: {
                type: "string",
              },
            },
            required: ["someNewProperty", "flavour"],
          },
          autoMigrate: true,
          migrationStrategies: {
            0: function(oldDoc) {
              return oldDoc
            },
            // 1 means, this transforms data from version 0 to version 1
            1: function(oldDoc) {
              return oldDoc
            },
            2: function(oldDoc) {
              return oldDoc
            },
            3: function(oldDoc) {
              return oldDoc
            },
            4: function(oldDoc) {
              return oldDoc
            },
            5: function(oldDoc) {
              oldDoc.someNewProperty = oldDoc.flavor
              oldDoc.flavour = oldDoc.someNewProperty
            },
          },
          // 6: function(oldDoc) {
          //   oldDoc.someNewProperty = oldDoc.flavor
          //   return oldDoc
          // },
          // 7: function(oldDoc) {
          //   oldDoc.flavour = oldDoc.someNewProperty
          //   return oldDoc
          // },
        })

        return db
      }

      async componentDidMount() {
        this.db = await this.createDatabase()
        debugger
        const sub = this.db.cookies
          .find()
          .sort({ id: 1 })
          .$.subscribe(cookies => {
            if (!cookies) return
            this.setState({ cookies: cookies })
          })
        this.subs.push(sub)
      }
      componentWillUnmount() {
        this.subs.forEach(sub => sub.unsubscribe())
      }
      render() {
        return (
          <div className="App">
            <div className="App-header">
              <h2>Migration Strategies test</h2>
            </div>

            <div>{this.renderCookies()}</div>

            <div id="add-message-div">
              <h3>Add Cookie</h3>
              <input
                type="text"
                placeholder="Cookie Flavor"
                value={this.state.newCookie}
                onChange={this.handleCookieChange}
              />
              <button onClick={this.addCookie}>Add cookie</button>
            </div>
          </div>
        )
      }
      renderCookies() {
        console.log("test", this.state)
        return this.state.cookies.map(
          ({ id, flavor, flavour, someNewProperty }) => {
            return (
              <div key={id}>
                {/* <p>{date}</p> */}
                <p>flavor: {flavor}</p>
                <p>flavour:{flavour}</p>
                <p>new prop: {someNewProperty}</p>
                <hr />
              </div>
            )
          }
        )
      }
      handleCookieChange(event) {
        this.setState({ newCookie: event.target.value })
      }
      async addCookie() {
        const id = Date.now().toString()

        const newCookie = { id, flavor: this.state.newCookie }

        await this.db.cookies.insert(newCookie)

        this.setState({ newCookie: "" })
      }
    }

期望的输出:

运行迁移并设置新属性(flavor和someNewProperty)

以migrationStrategies作为对象传递时的实际结果:

    Error in console:  RxError: RxError:
    migrationStrategies must be a function

    Uncaught (in promise) RxError: RxError:
    migrationStrategy must be a function
    Given parameters: {
    version:5
    type:"object"
    schema:{
      "jsonID": {
        "title": "Cookie",
        "description": "Database schema for a cookie",
        "version": 6,
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "primary": true
          },
          "flavour": {
            "type": "string"
          },
          "someNewProperty": {
            "type": "string"
          },
          "flavor": {
            "type": "string"
          },
          "_rev": {
            "type": "string",
            "minLength": 1
          },
          "_attachments": {
            "type": "object"
          }
        },
        "required": [
          "someNewProperty",
          "flavour",
          "id"
        ],
        "additionalProperties": false,
        "keyCompression": false,
        "compoundIndexes": []
      },
      "compoundIndexes": [],
      "indexes": [],
      "primaryPath": "id",
      "finalFields": [
        "id"
      ],
      "_normalized": {
        "additionalProperties": false,
        "compoundIndexes": [],
        "description": "Database schema for a cookie",
        "keyCompression": false,
        "properties": {
          "_attachments": {
            "type": "object"
          },
          "_rev": {
            "minLength": 1,
            "type": "string"
          },
          "flavor": {
            "type": "string"
          },
          "flavour": {
            "type": "string"
          },
          "id": {
            "primary": true,
            "type": "string"
          },
          "someNewProperty": {
            "type": "string"
          }
        },
        "required": [
          "flavour",
          "id",
          "someNewProperty"
        ],
        "title": "Cookie",
        "type": "object",
        "version": 6
      },
      "_hash": "2be159496392dec24c2a70e26094c600"
    }}


      migrationStrategies: () => {
            return {
              0: function(oldDoc) {
                return oldDoc
              },
              // 1 means, this transforms data from version 0 to version 1
              1: function(oldDoc) {
                return oldDoc
              },
              2: function(oldDoc) {
                return oldDoc
              },
              3: function(oldDoc) {
                return oldDoc
              },
              4: function(oldDoc) {
                return oldDoc
              },
              5: function(oldDoc) {
                oldDoc.someNewProperty = oldDoc.flavor
                oldDoc.flavour = oldDoc.someNewProperty
              },
            }
            // 6: function(oldDoc) {
            //   oldDoc.someNewProperty = oldDoc.flavor
            //   return oldDoc
            // },
            // 7: function(oldDoc) {
            //   oldDoc.flavour = oldDoc.someNewProperty
            //   return oldDoc
            // },
          },

实际结果:当将迁移策略作为上述函数传递时,我收到以下错误消息:

RxError: RxError: migrationStrategies must be an object
construct.js:30 Uncaught (in promise) RxError: RxError:
migrationStrategies must be an object
Given parameters: {
schema:{
  "jsonID": {
    "title": "Cookie",
    "description": "Database schema for a cookie",
    "version": 6,
    "type": "object",
    "properties": {
      "id": {
        "type": "string",
        "primary": true
      },
      "flavour": {
        "type": "string"
      },
      "someNewProperty": {
        "type": "string"
      },
      "flavor": {
        "type": "string"
      },
      "_rev": {
        "type": "string",
        "minLength": 1
      },
      "_attachments": {
        "type": "object"
      }
    },
    "required": [
      "someNewProperty",
      "flavour",
      "id"
    ],
    "additionalProperties": false,
    "keyCompression": false,
    "compoundIndexes": []
  },
  "compoundIndexes": [],
  "indexes": [],
  "primaryPath": "id",
  "finalFields": [
    "id"
  ],
  "_normalized": {
    "additionalProperties": false,
    "compoundIndexes": [],
    "description": "Database schema for a cookie",
    "keyCompression": false,
    "properties": {
      "_attachments": {
        "type": "object"
      },
      "_rev": {
        "minLength": 1,
        "type": "string"
      },
      "flavor": {
        "type": "string"
      },
      "flavour": {
        "type": "string"
      },
      "id": {
        "primary": true,
        "type": "string"
      },
      "someNewProperty": {
        "type": "string"
      }
    },
    "required": [
      "flavour",
      "id",
      "someNewProperty"
    ],
    "title": "Cookie",
    "type": "object",
    "version": 6
  },
  "_hash": "2be159496392dec24c2a70e26094c600"
}}

0 个答案:

没有答案