How to have complete offline functionality in a web app with PostgreSQL database?

后端 未结 2 1254
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 04:53

I would like to give a web app with a PostgreSQL database 100% offline functionality. In an ideal case the database should be completely replicated in the browser per user,

相关标签:
2条回答
  • 2020-12-15 05:34

    Without installing PostgreSQL on the client? No. Obviously you can cache data for offline use, but an entire RDBMS+procedural languages in Javscript, no.

    0 讨论(0)
  • 2020-12-15 05:41

    Short answer: I don't know of anything like this that currently exists.

    However, in theory, this could be made to work...(long answer:)

    1. Write a PostgreSQL backend for levelup (one exists for MySQL: https://github.com/kesla/mysqldown)
    2. Wire up pouch-server to read/write from your PostgreSQL db using pouchdb's existing leveldb adapter (which in turn will have to be configured to use your postgres backend). Congrats, you can now sync data using PouchDB!

    Whether an approach like this is practical in reality for your application is a different question you'll have to answer.

    You may be wondering, for example, "will I be able to sync an existing complex schema with multiple tables to the client with this approach?" The answer is probably not - the mysqldown implementation of leveldown uses a single MySQL table with three fields: id, key, and value (source), and I imagine any general-purpose PostgreSQL adapter would be similar (nothing says you can't do a special-purpose adapter just for your app though!).

    On the other hand, if you were to implement a couchdb-compatible API (or a subset- you may not need attachments, for example) over your existing database schema, there's nothing stopping you from using PouchDB on the client to talk directly to that as if it were an actual CouchDB - just pop in the URL and call replicate()! Implementing the replication protocol might be a fair bit of work, since you'd need to track revisions and so on somewhere - but again, technically not impossible!

    There are also implementations of levelup's backend storage that are designed for browsers. See level.js, which could be another way to sync between a server-side Postgres levelup backend and the browser.


    TL;DR: There's tons of work being done around Javascript databases right now. Is syncing with Postgres impossible? probably not. Would it be a lot of work? Definitely. Worth it? Who knows, but it would be cool.

    0 讨论(0)
提交回复
热议问题