Search code examples
javascripthtmlindexeddbjaydata

IndexedDb - Check table exists and contains data


Using IndexedDB when my app first runs I populate it with some data, I want to ensure that when the database and tables are created that they do not already exists.

Can I query the length of a table to see if it contains and data in JavaScript?


Solution

  • We can use the objectStoreNames property of IndexDB to check the existence of an ObjectStore.

    var db_object, object_store;
    if( !db.objectStoreNames.contains(currentobjectstore) )
    {
      db_object =  i_db.createObjectStore(currentobjectstore, {autoIncrement:true} );
     object_store = db.transaction(currentobjectstore, 'readwrite').objectStore(currentobjectstore);
    } else {
     object_store = db.transaction(currentobjectstore, 'readwrite').objectStore(currentobjectstore);
    }