Search code examples
javascriptdatabasenode.jschromiumweb-sql

Nwjs and WebSql - can't delete existing databases?


I am creating a desktop application with Nwjs. So far everything works like a charm, I really like this whole concept.

But I need database functionalities and in the Nwjs docs, WebSql is the first database on the suggested list. I have managed to write my methods to create, insert, etc., but I can't find a way to delete databases which I have created accidently.

This makes me wonder if WebSql is a suitable option for creating a production desktop app. Or should I switch to another database module?

sample code:

var db = openDatabase('mydb', '1.0', 'my first database', 2 * 1024 * 1024);

    if(!db){
        alert('no table!');
    }

    console.log(db);

    /**** DATABASE FUNCTIONS ****/

   // Create table if not exists
    db.transaction(function (tx) {
        tx.executeSql( 'CREATE TABLE IF NOT EXISTS companies (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), street VARCHAR(128), nr VARCHAR(12), city VARCHAR(128))' );
    });


    function insertCompanyInDB(name, street, nr, city) {
        db.transaction(function (tx) {
            tx.executeSql('INSERT INTO companies (name, street, nr, city) VALUES (?, ?, ?, ?)', [name, street, nr, city]);
        });
    }

Solution

  • for future users, as mentioned in https://www.w3.org/TR/webdatabase/#databases

    There is no way to enumerate or delete the databases available for an origin from this API.

    but since nwjs app works as chrome extension there is away where you can delete all webSql databases (clear websql storage), like this:

    1-in your package.json add:

    "name": "myapp",
    "permissions": [
        "browsingData"
      ]
    

    2- from javascript call this command

    chrome.browsingData.removeWebSQL({since:0, originTypes:{extension:true}});
    

    Note After extensive test i personally did i recommended using LocalStorage with JSON.stringify for complex objects or SQlite for relational data structure