I would like to create a database file locally and write all the data into it, but would like to do it without node.js and Coffeescript. I would like to do it through Javascript just to run in the browser, as I am developing an application for storing some data and that app has to be shared with my colleagues and I don't have permission to install NodeJs in the PCs.

var sql = window.SQL;
        var db = new SQL.Database();
        db.run("CREATE TABLE test (col1, col2);");
        db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,111]);
        db.run("INSERT INTO test VALUES (?,?), (?,?)", [3,333,4,444]);
        var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
        stmt.getAsObject({$start:1, $end:1});
        stmt.bind({$start:1, $end:4});
                    while(stmt.step()) { 
            var row = stmt.getAsObject();
            console.log(row.col1)
        }

        var data = db.export();
        //As per the official documentation, the data is converted into
        //buffer and written into file using writeFileSync();
        var buffer = new Buffer(data);
        fs.writeFileSync("filename.sqlite", buffer);

But as Buffer() and writeFileSync() are NodeJs functions, I can't use them in my code. Is there any other way I could write my data into the database and then export it into a file?

1

There are 1 best solutions below

3
On

It's not good idea to execute a requests to database from your client code. It's not secure. And you basically need for node on your PC because it's a runtime environment of JS and Buffer with writeFileSync() from fs module is a part of this runtime.