I am making small standalone html 5 application using deskshell, I want to use MySQL database. How can I connect from JavaScript to MySQL without using PHP or server side language?
-
1Have you read the deskshell demos/examples? JavaScript IS server side language (Node.js, actually) in this case. – Passerby Jan 10 '14 at 07:05
-
to persist data, there is an example to run on same port. I tried that its working. It's using websql to store and retrieve data, while making a folder for data persistant but if we delete the data folder it will be vanished. So need to connect to mysql. so anyway to connect to server mysql or mysql runing on local machine directly from javascript using any js or anything? – ronvinet Jan 10 '14 at 09:51
-
https://npmjs.org/search?q=mysql Again, in this case JavaScript _IS_ server side language. By default it's named `app.js` in your app folder, which is a [Node.js](http://nodejs.org/) script. – Passerby Jan 10 '14 at 09:52
-
You'll have to `npm install mysql` before you can `require("mysql")` -- it seems you're very unfamiliar with Node.js. I suggest you to learn about it first, then proceed to DeskShell, since DeskShell is built upon Node.js. – Passerby Jan 13 '14 at 03:14
-
@ronvinet i know it is over a year now, but could you point me to the example??? i don't see it under the DeskShell examples? In short: I am trying to copy online registrations to be check offline and i have to update users which are present. My curent working setup i a huge xampp+wordpress setup. – alex Mar 16 '15 at 11:18
1 Answers
This is definitely possible using deskshell. It can be a little confusing as to what is the server and what is the client etc. Generally it is much simpler than you at first think.
I suggest the following approach. Deskshell uses an unmodified nodejs binary. So to make life simpler begin by first installing node (so it sets up everything like paths etc for you). Then use npm - the node package manager to install a module that will allow you to connect to mysql. You can go to the npm website and search for mysql. When you find a package you can npm install packagename. You can remove then afterwards with npm remove packagename. (Check website for exact syntax).
So using this approach and copying and modifying the examples from the modules homepage you should be able to get nodejs to connect to mysql and run some queries.
The next step is then to add this "database ability" to your application. Here you should realise you have 2 javascript engines running. You have one "sandboxed" v8 js engine running in the chrome browser and you then have a second "unsandboxed" v8 nodejs that will run your app.js file. I find the simplest and most pleasing way to connect the two is to use a websocket to send messages from one v8 to the other. Then you can decide the level of abstraction to write your little api at. So you could write all logic in the browser scripts, send the sql ocer the socket, nodejs app.js script then recieves message, queries mysql and returns results as json. An alternative would be you ask the api for 20 user objects and it turns the request into sql and executes it for you and returns it to the browser.
You will need to copy the node_modules/packagename folder to your application directory as node_modules/packagename. In addition you may need to change your deskshell code from require ("packagename") to be require ("./path/to/package/folder")
I would suggest you look at using sqlite3 as your database engine if a separate server is not used for your application. This is because sqlite3 is a file based in process database so you do not have to run a server process on the end users machine. However if you are connecting to an existing server database then mysql is the better option.

- 416
- 2
- 3
-
I dont mind using sqlite3. I didn't get this part ---n addition you may need to change your deskshell code from require ("packagename") to be require ("./path/to/package/folder")--- in .desk file what changes i need to make. Could you write the parameter i need to change the path. And do i need to make socket connection if i use sqlite3? @sihorton. – ronvinet Jan 11 '14 at 11:53
-
It is probably worth checking out https://electronjs.org/ and using that instead. – sihorton Apr 27 '18 at 18:24