Is it possible to connect to a SQL database, e.g. a SQL Server database, from javascript (client-side). Preferably using an ODBC or OLEDB connection.
I know this normally isn't recommended, and you should always connect from the server-side, but I want people to connect to their own SQL databases on their own local machines.
I have found examples to do this using ActiveX objects, but this only works in Internet Explorer, and I don't want to be restricted by this.
I don't know of any browser-side database drivers, but perhaps check out PostgREST:
https://github.com/begriffs/postgrest
It wraps PostgreSQL in a simple REST API, so you don't need any additional server software.
Related
I have a Oracle database server hosting data, and I want to build a small app on my local network for people to do light data entry.
Normally we can't query a database from the browser because the browser is client-side and the database is on the server.
But what if the server and website are both hosted on the local network? Would it then be possible? The problem is I don't have a server I'm just making a small website as a file on a shared drive.
But what if the server and website are both hosted on the local network?
This does not change almost anything, maybe it makes things a bit easier when it comes to application security.
Normally we can't query a database from the browser because the browser is client-side and the database is on the server.
That's not true. You can query a database from a browser/client-side. The problem behind such a solution is security of course, because in this case the client would have to know the database credentials, which is why such operations are performed on the server side, but it does not mean that querying database from client-side is not possible.
The conclusion is that if you know the database credentials anyway, it is possible to write a simple application (which will query database from client-side) for yourself. For this you can use OracleDB module.
---EDIT---
I will only emphasize once again that you should not make such an application available anywhere outside for security reasons (availability of database credentials).
However, you can use JavaScript fullstack frameworks like nextjs or sveltekit - they are able to perform some server-side operations and then pass this data to the client. This approach would be much safer.
I'd use Oracle Application Express.
It is installed in Oracle database; lets you develop applications rather quickly (if you know some SQL; even if you don't - use wizards). The only "tool" you need is a web browser. Your users would also need it (the browser), accessing your application via local network and enjoy beautiful application you'll create.
I understand this is not best practice but I am operating within a limited realm and, as far as I can tell, this is the only solution to this problem.
I am trying to connect to an MS-SQL database both to get data and to put data onto the database. I cannot seem to do it through JavaScript.
ActiveXObject is, from my understanding, now depreciated so that is not working, which eliminates every functional solution that I could find recommended in the past.
Please note: I am not a web developer and I have no access to any of the standard web development tools for this task.
This is the question has been asked multiple times in various forums - Can my client-side Javascript (running in a browser) connect to a database server and fetch data?
The short answer is - not recommended in general, not feasible without breaching security and without using outdated technologies. Let us dig into it.
It is possible to connect to remote database from a browser using outdated technologies
There are two pieces of technologies from Java and .Net worlds - Applet and ActiveX that run on the browser and can communicate to a remote database. The Java Applet is hardly used by anyone nowadays and browsers are stopping to support it. ActiveX is discontinued by Microsoft in their newer browser Edge. So, you have to enforce your target users to use old insecure browsers if you want to go with these options.
Do not use this.
Use databases embedded in the browser and sync with a remote database
You may use the database locally available in the browser and perform all read/write operations. Periodically sync this database with a remote one. Here are the options:
MongoDB and use change stream to sync with a remote MongoDB
PouchDB and sync with a remote CouchDB or even a MySQL database
Use this only for offline storage temporarily in the browser.
The traditional and secure approach to connect to a remote Database
Use a server-side technology to develop an app that your client-side code (Javascript, HTML) talks to. The app may provide RESTful APIs to interact from the client-side. Then the app that is running in a web server connects and interacts with the database. There are plenty of server-side technologies such as Java, PHP, Python, NodeJS (Javascript based), .Net, etc. to develop your server-side app.
Go with this option.
Well javascript is a client side scripting where as your database runs on a server. So firstly you cannot connect to a database for executing any query from client side i.e javascript and also you need to setup a server side service which can connect to the database and execute the query and give you the result at the client side. You can refer any client-server architecture for this on the web.
I'm developing a Chrome extension and I'm looking for a way to get client JavaScript, without any back-end layer on my side. Is it possible?
Maybe it's possible to make an AJAX request to the Google Cloud with a SQL query as a parameter? I'm using Google OAuth.
Thanks!
As Cloud SQL uses the MySQL wire protocol and there is no javascript support for this (or even for the underlying socket requirements) it is not possible to directly connect Javascript to Cloud SQL.
You probably shouldn't do this even if you could, as its very difficult to make a system like that secure. As you would have to embed the username/password for the database in your extension, you cannot enforce any separation of data between your users.
A backend is usually needed to enforce ACLs, and also makes future development (an app, website, desktop app, API etc) much easier. This backend does not have to be a complex beast - Firebase provides a good backend out of the box with no coding required (although it isn't backed by MySQL).
Weather javascript is a database enabled language?
Can javascript be used as a language to connect and query the database? If yes why and if no why? Please explain in detail?
Theoretically any programming language can be used to connect to a database, as long as it has the proper db libraries that the developer can use to connect to the DBMS.
In particularly javascript has some limitations (like servers it can connect to, resources from PC that can access), when run inside a web browser container, limitations that are not part of the language itself. Node.js on the other hand doesn't have these limitations.
Most of the times you'll need a proxy between your javascript ajax calls and the actual database, because:
on one hand to not publicly expose the db credentials and also as secured databases don't allow remote connections,
and on the other hand as many DBMS require a binary communication protocol, which is not feasible to be implemented in javascript (there are DBMS which allow a json-based communication though)
My recommendation: implement the DB connect and query logic on the server-side part, and build a (RESTful) API to indirectly access the database this way. This also gives you control over what a logged in user can do with the database.
I know that for connecting to the database from JavaScript I need to mention the database credentials in plain JavaScript code. Therefore for a online application that would be a huge security risk. But in my case I want to write a small JavaScript application which is stored locally. So the credentials won't be shown to the world but just to the user I give the application, which is acceptable for me.
The motivation behind this is that I want to connect to an online database without a running PHP server, just from a JavaScript embedded in the local page. My goal is to provide an application that can be run by the user without the need for PHP and a server, except the database server. It's similar to a desktop application but running in the browser.
How can I connect from JavaScript to an online MySQL database? All other similar questions I found on Stack Overflow advices the thread starter against this usage for good reason but hadn't answered the question.
I heard that connecting to MySQL from JavaScript would be impossible. But how do, say, Windows 8 Metro Apps written in JavaScript handle that issue?
A backend repeater is always needed. For this issue you can set up a light-weight server that forwards your database accessing request to mysql server using, say node.js.
If you are focussing on a specific web browser, maybe you'll find a workaround. But if you're thinking on a local application independent from the user agent, you should follow the standards to reach a predictable behavior (or at least the best approach). In the W3C standards you have two options for storage:
Web Storage API: you're limited to key-value storage, but is very well supported.
Indexed Database API. I've no experience with it, but it's supported.
If you're not to limit the user context to a restricted machine and user agent, you can start with standard storage solutions as mentioned above and then enhance your app for more advanced browsers (perhaps even with MySQL!), as recommended in Progressive Enhancement