AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.
http://alasql.org
MIT License
7.04k stars 659 forks source link

alasql USE without callback parameter gives error #857

Open arnemorken opened 7 years ago

arnemorken commented 7 years ago

Using alasql v0.3.9. This works (in browser):

alasql('CREATE INDEXEDDB DATABASE IF NOT EXISTS geo;\
        ATTACH INDEXEDDB DATABASE geo; \
        USE geo;', function(){} );

This gives "Error: Database 'geo' does not exist" in the console:

alasql('CREATE INDEXEDDB DATABASE IF NOT EXISTS geo;\
        ATTACH INDEXEDDB DATABASE geo; \
        USE geo;');
mathiasrw commented 7 years ago

I agree that it is not really awesome - but the reason is that you are trying to do a sync call to each statement in the second one. At the moment this assumes each statement is sync, but attatching the indexxed DB is async, so the geo table is not created yet.

I suggest using the lazy promise notation as follows:


alasql(['CREATE INDEXEDDB DATABASE IF NOT EXISTS geo',
           'ATTACH INDEXEDDB DATABASE geo',
           'USE geo'].then(function(){
                 // now geo is ready to be used
          }).catch(console.log)
´´´
arnemorken commented 7 years ago

Thank you for your reply. I can see that asynchronous execution might be the problem in my original code. However, this code, which I believe should be synchronous, also fails and prints "Error: Database 'geo2' does not exist" to the console:

alasql(['CREATE INDEXEDDB DATABASE IF NOT EXISTS geo2',
        'ATTACH INDEXEDDB DATABASE geo2',
        'USE geo2'])
       .then( function() { console.log("ok"); })
       .catch( function(err) { console.log(err); })
mathiasrw commented 7 years ago

hmmmmm...

Can you do me a favour and test if it works if you do not have the IF NOT EXISTS geo2 part?

arnemorken commented 7 years ago

If I remove "IF NOT EXISTS" I get the same error message "Error: Database 'geo2' does not exist", but also:

Error: IndexedDB: Cannot create new database "geo2" because it already exists alasql.js:16314:11

So it does not exist and it already exists... seems like alasql is having an existential crisis! :)