More and more e-learning platform, code interview platform, and online IDEs are using online code executing system. Such as leetcode, HackRank, Codility, w3school, etc.
Some platforms even support estimating time and space complexity. These platforms contains at least two parts: the front end support code hight, and the backend support submit code , compile and run. There are lots of very popular & easy to use front end solutions, such as Prism.js, Highlight.js, etc. And in the backend, Judge0 is quite popular solution.
Today, I will introduce another solution, which does not need to run backend server, because it run the whole database on the frontend — sql.js.
At first, let’s get a basic feeling of this frontend database: https://sql.js.org/examples/GUI/
sql.js uses emscripten to compile SQLite to webassembly (or to javascript code for compatibility with older browsers). It includes contributed math and string extension functions.
sql.js depends on wasm, to host it, you need to download sql-wasm.js & sql-wasm.wasm, they can be copy from node_modules/sql.js/, after you npm install sql.js. Example code can be found here.
Basically, it just run your SQL code in the db.run(“<SQL CODE>”) API, then you can get results into javascript objects by: stmt.getAsObject(). Because it purely runs in the frontend, refresh page or close browser will lost every thing, but it also support load & save db into sqlite db file.