Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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, and synchronized when online. So that the same code can be used to talk to both the offline and online database. I know this is possible with PouchDB and CouchDB, but have not found a solution that works with PostgreSQL. Is this at all possible?

like image 685
Blix Avatar asked Nov 13 '13 12:11

Blix


People also ask

Can PostgreSQL be used offline?

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

Is PostgreSQL good for web development?

When it comes to complex queries and large amounts of data, PostgreSQL has the advantage. It is designed with flexibility and compatibility in mind which makes it a good choice for complex systems that store their information in several locations.

Does Postgres have a web interface?

We are very happy to announce the release of TeamPostgreSQL, a web interface for the PostgreSQL database, completely free for the community. TeamPostgreSQL is a web application that can be used to access an organization's databases over the intranet (or internet).


1 Answers

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.

like image 146
jches Avatar answered Sep 28 '22 08:09

jches