Skip to content

Dexie.getDatabaseNames()

David Fahlander edited this page Jun 11, 2014 · 9 revisions

Syntax

Dexie.getDatabaseNames(function callback(names){})

Return Value

Promise

Parameters

  • callback - callback to be called with the result.

Callback Arguments

names: Array

Description

Returns an array of database names at current host.

Sample

Dexie.getDatabaseNames(function (databases) {
    for (var i=0; i<databases.length; ++i) {
        console.log(databases[i]);
    }
});

Browser Specific Info

This method is an extension to the native indexedDB API which does not support listing of database names. Chrome and Opera has implemented webkitGetDatabaseNames() but neither IE or Mozilla supports it. This method will check if webkitGetDatabaseNames() is present and use it if so. Otherwise it will return a list maintained by Dexie by using the localStorage API. The key in localStorage used for this, is "Dexie.DatabaseNames" pointing out a JSON representation of a string array.

This means that on Opera and Chrome, the method will work no matter if the database was created using Dexie or not, but on IE and Firefox, the method will only see databases created using Dexie.

Detecting When Databases Are Added Or Deleted

To detect when a database is added or deleted using Dexie, it is possible to subscribe to the storage event:

window.addEventListener('storage', function (event) {
    if (event.key === "Dexie.DatabaseNames") {
        console.log("A database was added or removed");
        console.log("Old list: " + event.oldValue);
        console.log("New list: " + event.newValue);
    }
});

Note that this sample will work also on Opera and Chrome since the localStorage key "Dexie.DatabaseNames" is maintained also there even though not used by Dexie.getDatabaseNames().

Clone this wiki locally