Connecting to postgres database from a phonegap app?

I'm trying to build a phonegap app for ios and android. It's been going well so far but now I hit a major obstacle and I need some help.

I need to connect to a remote Postgres database. I haven't done anything like that before.

Does anyone have any experience/tips for this, or know of any resources which contain relevant information?

Answers:

Answer

From client-side javascript, you can't. Unless phonegap has done something very odd with permissions or provided a PostgreSQL interface (which presumably you'd know about if they had).

What you'll want to do is provide a small server-side wrapper to PostgreSQL that will take requests, convert them to queries and return (presumably) json-formatted results. Oh - and you'll need to think about security too - who can connect, what can they do, does it all need to be encrypted?

If your requirements are simple, this can be easy enough to do in Perl/Python/Ruby etc. or even javascript if you have node.js to hand. With Perl you'd wrap DBIx::Class in a Dancer app - similar modules exist for all the above scripting languages.

Do consider whether you want to run the whole thing over https (let apache handle this for you) - it will avoid issues with passwords/private data being sniffed over wireless connections.

For example, your app would issue an ajax request to: http://myserver/projects/123/messages?limit=20&sort=date

That would be translated into a query into the project-messages table for the last 20 messages sorted by date and wrap the results up as an array of JSON objects (presumably).

Answer

You would need to create an API for your data. Then access that API using promises from your js app.

Answer

To let the security issues where they belong to (in existing experienced and tested parts of server / client software) and to have a minimum effort of development, I suggest to use some existing lightweight middle ware:

It comes with a docker, where any service you require is packed in, thus making it easy to try it out quickly.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.