简体   繁体   English

从phonegap应用程序连接到Postgres数据库?

[英]Connecting to postgres database from a phonegap app?

I'm trying to build a phonegap app for ios and android. 我正在尝试为ios和android构建一个phonegap应用程序。 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. 我需要连接到远程Postgres数据库。 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? 是否有人对此有任何经验/提示,或是否知道任何包含相关信息的资源?

From client-side javascript, you can't. 从客户端javascript,您不能这样做。 Unless phonegap has done something very odd with permissions or provided a PostgreSQL interface (which presumably you'd know about if they had). 除非phonegap在权限方面做得很奇怪,或者没有提供PostgreSQL接口(大概您会知道它们是否有)。

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. 您要做的是为PostgreSQL提供一个小的服务器端包装程序,该包装程序将接收请求,将请求转换为查询并返回(大概)json格式的结果。 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. 如果您的要求很简单,那么在Perl / Python / Ruby等中就可以轻松完成,甚至在有node.js时也可以使用javascript。 With Perl you'd wrap DBIx::Class in a Dancer app - similar modules exist for all the above scripting languages. 使用Perl,您可以将DBIx :: Class包装在Dancer应用程序中-上述所有脚本语言都存在类似的模块。

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. 请考虑是否要通过https运行整个过程(让apache为您处理)-它将避免通过无线连接嗅探密码/私有数据的问题。

For example, your app would issue an ajax request to: http://myserver/projects/123/messages?limit=20&sort=date 例如,您的应用将向以下网址发出Ajax请求: 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). 它将转换为对项目消息表的查询,以查询按日期排序的最后20条消息,并将结果包装为JSON对象数组(大概)。

You would need to create an API for your data. 您需要为数据创建一个API。 Then access that API using promises from your js app. 然后使用您的js应用中的promise访问该API。

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. 它带有一个泊坞窗,您可以在其中打包所需的任何服务,从而轻松快速地进行试用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM