using html5 standalone app using deskshell to connect to mysql

597 Views Asked by At

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?

1

There are 1 best solutions below

2
On

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.