0.0.3 • Published 5 years ago

rxdb-plugin-proxy v0.0.3

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

rxdb-plugin-proxy

proxy data plugin for rxdb

way

see https://github.com/pubkey/rxdb/issues/1269

example

const RxDB = require("rxdb");
RxDB.plugin(require("pouchdb-adapter-memory"));
RxDB.plugin(require("rxdb-plugin-proxy"));


(async () => {
  const db = await RxDB.create({
    name: "db1",
    adapter: "memory"
  });

  const docSchema = {
    version: 0,
    type: "object",
    properties: {
      name: {
        type: "string"
      }
    }
  };

  const Doc = await db.collection({
    name: "doc",
    schema: docSchema,
    methods: {
      // add proxy's methods.
      change(name) {
        this.name = name; // RXDocument can't call
      }        
    }

  });

  const doc = await Doc.insert({
    name: "first"
  });

  console.log(doc.name);

  const pdoc = doc.proxy();
  pdoc.change("leo");
  console.log(pdoc.name, doc.name); // real-time out: "leo first"
  pdoc.change("leo2");
  console.log(pdoc.name, doc.name); // real-time out: "leo2 first"
  pdoc.change("leo3");
  console.log(pdoc.name, doc.name); // real-time out: "leo3 first"

  await pdoc.store(); // save doc
  console.log(pdoc.name, doc.name); // out: "leo3 leo3"
})();