React Native - 连接到 PostgreSQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34995930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React Native - connect to PostgreSQL database
提问by CapCa
I would like to connect with React Native to an external database, like PostgreSQL, MySQL or what ever. Is this possible?
我想使用 React Native 连接到外部数据库,如 PostgreSQL、MySQL 或其他任何东西。这可能吗?
But first let me tell you something about my plan: I will develop my application for multiple installations (multiple devices - one installation on one device). All devices are connected with a local network which is provided by a Windows installation (server). On the server runs a database (PostgreSQL) and I would like to connect to this database with allmy devices. The server (or in fact its database) should store tasks and each device should access to these tasks (read and write) locally.
但首先让我告诉您一些有关我的计划的信息:我将为多个安装(多个设备 - 在一个设备上安装一个)开发我的应用程序。所有设备都连接到由 Windows 安装(服务器)提供的本地网络。在服务器上运行一个数据库 (PostgreSQL),我想用我的所有设备连接到这个数据库。服务器(或者实际上是它的数据库)应该存储任务,并且每个设备都应该在本地访问这些任务(读和写)。
I had also some other ideas like simple SMB-XML file storage (but the problem with collisions) and an SQLite database on an SMB shared file - but the website of SQLite tells that this will not avoid problems with multiple accesses. Therefore I need another localsolution. This brought me to provide a database which is accessible from the local network.
我还有一些其他想法,例如简单的 SMB-XML 文件存储(但存在冲突问题)和 SMB 共享文件上的 SQLite 数据库 - 但 SQLite 的网站告诉我们,这不会避免多次访问的问题。因此我需要另一个本地解决方案。这让我提供了一个可从本地网络访问的数据库。
I would be very grateful if someone has a note how to solve this problem.
如果有人有如何解决这个问题的说明,我将不胜感激。
Thank you!
谢谢!
回答by d-vine
In order to access a database directly from react native you would need a pure JavaScript database driver. A quick google search produced a JavaScript drivers for PostgreSQL (https://github.com/brianc/node-postgres) but it is written for Node.js and I doubt it will work in JavaScript Core or Chrome. CouchDB works via a REST API and it's actually intended for direct access by multiple clients. There are multiple JavaScript clients available for that.
为了直接从 React Native 访问数据库,您需要一个纯 JavaScript 数据库驱动程序。谷歌快速搜索为 PostgreSQL ( https://github.com/brianc/node-postgres)生成了一个 JavaScript 驱动程序,但它是为 Node.js 编写的,我怀疑它是否适用于 JavaScript Core 或 Chrome。CouchDB 通过 REST API 工作,它实际上旨在供多个客户端直接访问。有多个 JavaScript 客户端可用于此。
BUT why do you want your clients to connect to the DB directly? What you describe seems like a pretty standard application model. You'll just need a backend running on your server talking to the DB and providing an API. Good old web development. If you want to write JavaScript on the backend I suggest using Node.js
但是为什么您希望您的客户直接连接到数据库?您所描述的似乎是一个非常标准的应用程序模型。您只需要在服务器上运行的后端与数据库通信并提供 API。良好的旧网络开发。如果你想在后端编写 JavaScript,我建议使用 Node.js
If you really can't or don't want to write a backend, there are several services that provide API for data storage and even some data processing. But of course they aren't local. Have a look at parse.com or firebase.com for example.
如果你真的不会或者不想写后端,那么有几个服务可以提供用于数据存储甚至一些数据处理的 API。但当然他们不是本地人。例如,查看 parse.com 或 firebase.com。