5

I would like to connect with React Native to an external database, like PostgreSQL, MySQL or what ever. Is this possible?

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 all my devices. The server (or in fact its database) should store tasks and each device should access to these tasks (read and write) locally.

Server Client Schema

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 local solution. This brought me to provide a database which is accessible from the local network.

I would be very grateful if someone has a note how to solve this problem.

Thank you!

Akshar Patel
  • 8,998
  • 6
  • 35
  • 50
CapCa
  • 556
  • 1
  • 6
  • 15
  • This sounds like a good case for developing a REST API and exposing your data and any remote procedure calls to the client devices through that. Typically that is how this would be done, is there reason to rule it out? You can use react-native's network stack for sending and fetching data, https://facebook.github.io/react-native/docs/network.html – mmccaff Jan 25 '16 at 15:40

1 Answers1

3

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.

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

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.

d-vine
  • 5,317
  • 3
  • 27
  • 25
  • Thank you very much for your advice. The point is that I will connect to software which provides the data. It stores it in its database (PostgreSQL). The software is installed on the computers of our (human-)clients. Therefore I it is very wasteful to install for each client an API and a direct access to the database would be more conformable. Would you recommend writing an own React Native Component for this or do you know another, better solution? I am still opened for other solutions than I follow at the moment. Thank you – CapCa Jan 25 '16 at 16:32
  • 1
    Just for me to understand. You have a DBs running in a customers local network for each of your customers. And each customer connects only to his/her own local DB via the app? – d-vine Jan 25 '16 at 17:40
  • No, no directly. The software provides the database (given). The apps should access to this data. Roughly said, the apps should present the data and the software maintains the data. For example: In the software you can enter translations and manage the languages. The app simply shows the translations and when a translation is out of date - it can access to the database and set a flag "may be outdated". Therefore the server needs read and write rights to the db and the apps also read (simple present) and write rights (e.g. for this flag setting). PS: Example fictional but helps 2 understand – CapCa Jan 25 '16 at 20:15
  • And "the software" is a web application or a native application? And the DB server runs on it's own machine? I really think you won't get around shipping your software with an application server as well as an DB server. Node.js for example is really quite lightweight. Another idea is to bridge react-native to use native database driver for iOS or Android. On Android you could actually use the regular ODBC drivers. But I found several threads warning against that (e.g. http://stackoverflow.com/questions/10435609/driver-jdbc-postgresql-with-android) – d-vine Jan 26 '16 at 06:29