4

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?

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220

3 Answers3

2

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).

Richard Huxton
  • 21,516
  • 3
  • 39
  • 51
  • This is probably *awesome* advice, but to me at my current level of experience, that's a terrifying four paragraphs. "a small server-side wrapper"....I don't really know what that entails. I have never done server-side programming. It's a separate team of people who manage that side of things. If I understand correctly, you're saying I can connect to the server via javascript and forward it a query, which it then sends to the database *for* me? Is that right? *Assuming* the server has this script wrapper set up? – temporary_user_name Mar 30 '12 at 08:06
  • 1
    See added example. Server-side programming is probably simpler than client-side. If javascript is all you know, then check out node.js, otherwise Perl or Python are both popular. Perl's syntax is less clean than Python, but it's got more modules on its CPAN network. If you're learning perl perhaps start here: http://www.onyxneon.com/books/modern_perl/index.html - pay for a paper version or download for free. – Richard Huxton Mar 30 '12 at 08:17
  • The example link gives me a "Server Not Found" error; does it load for you? – temporary_user_name Mar 30 '12 at 08:20
  • What the "myserver" thing - no, that's just a made-up URL. You'll need to actually write a wrapper for a real database. – Richard Huxton Mar 30 '12 at 08:41
0

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

0

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.

woodz
  • 737
  • 6
  • 13