Skip to content

Bug in localStorage when editing table.data directly #774

Open

Description

@michaellee8

I created a database with alasql and applied my desired testing data with it by mydb.tables.mytable.data perfectly. However, something strange exists when I try to store it with localStorage

providing the javascript here :

alasql('CREATE localStorage DATABASE IF NOT EXISTS lsdb');
alasql('ATTACH localStorage DATABASE lsdb AS db');
var db = alasql.Database('db');
db.exec('CREATE TABLE IF NOT EXISTS db_01 (foo STRING)');
db.tables.db_01.data = [{
  foo: "text_foo"
}];
document.getElementById('output').innerHTML += JSON.stringify(db.tables.db_01.data) + '<br/>';
document.getElementById('output').innerHTML += localStorage.getItem('lsdb.db_01')+ '<br/>';
document.getElementById('output').innerHTML += JSON.stringify(db.exec('SELECT * FROM db_01')) + '<br/>';
document.getElementById('output').innerHTML += JSON.stringify(db.tables.db_01.data) + '<br/>';

fiddle : alasql_localStorage_bug

Attached is a full HTML page to reproduce the problem, but with a extension .txt since github don't support HTML uploading
alasql_localStorage_bug.txt

EDIT: PS: currently I can only do things properly by creating a normal DATABASE and then save it to localStorage by JSON.stringify(db.tables.db_01.data)

Activity

  1. mathiasrw commented on Oct 24, 2016

    @mathiasrw
    Member

    Thanks for taking the time to identify this...

  2. michaellee8 commented on Nov 2, 2016

    @michaellee8
    Author

    Any following action on this issue?

  3. michaellee8 commented on Nov 2, 2016

    @michaellee8
    Author

    I have checked out codes in 92localstorage and found that the problem may be caused by the fact that the localStorage engine try to put and get data directly into and from localStorage, hence the traditional way to access all data in the table cannot be used. May be we have to keep tracking the data array by using the javascript proxy but it is not yet widely supported, or try to provide a setData(data) and getData(data) for all tables, and specify its destination array for tables using localStorage to the localStorage entries directly, like below:

    function setData(ItemKey,columns,data,identities){
      try {
        var obj = JSON.parse(localStorage.getItem(ItemKey));
        obj.columns = columns || obj.columns;
        obj.data =data || obj.data;
        obj.identities = identities || obj.identities;
        localStorage.setItem(ItemKey,JSON.stringify(obj));
    } catch (err){
          return false;
    }
      return true;
    }
    function getData(ItemKey){
      return JSON.parse(localStorage.getItem(ItemKey));
    } 
    // access tables[table].data by getData(ItemKey).data

    What do you think? I don't know much about alasql so I am not going to touch the underlying codes in it, but I really hope these sample codes can help you in solving this bug out.

  4. mathiasrw commented on Nov 3, 2016

    @mathiasrw
    Member

    This is awesome - thanks ! I will digg down into this next time I get some time :)

  5. self-assigned this
    on Nov 3, 2016
  6. michaellee8 commented on Nov 7, 2016

    @michaellee8
    Author

    Well, sir, may I get your solved code then? I just found the last commit on develop branch is 12 days ago, did you push it?

  7. mathiasrw commented on Nov 8, 2016

    @mathiasrw
    Member

    Nope - its not working yet...

    try to provide a setData(data) and getData(data) for all tables, and specify its destination array for tables using localStorage to the localStorage entries directly

    I like the idea of setData but dont really feel confident about the getData that basically replaces "select * from XXXX" - inputs?

  8. removed their assignment
    on Nov 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions