Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to create multiple dataStore in same db using angular-localForage? #51

Closed
bobsilon opened this issue Dec 18, 2014 · 16 comments
Labels

Comments

@bobsilon
Copy link

Hi there. Is it possible to have multiple dataStore in IndexedDB using angular-localForage?
I tried this:

var $tblStage = $localForage.createInstance({
    storeName   : 'tblStage', // name of the table
});

but it triggered an error which says:

Error: A localForage instance with the name dbName is already defined.

I've read this Question before but, is the only way is working with Raw IndexedDB? Or I missed something in my code? Any Idea?

@ocombe
Copy link
Collaborator

ocombe commented Dec 18, 2014

Hmm it is the correct way to do it, and it should work, can I get your config ?
If it's a bug I need to fix this.

@bobsilon bobsilon changed the title Is it possible to create multiple dataStore in angular-localForage? Is it possible to create multiple dataStore in same db using angular-localForage? Dec 18, 2014
@bobsilon
Copy link
Author

app.config(['$localForageProvider', function($localForageProvider){
    $localForageProvider.config({
        driver      : localforage.INDEXEDDB, // if you want to force a driver
        name        : 'dbName', // name of the database and prefix for your data, it is "lf" by default
        version     : 1.0, // version of the database, you shouldn't have to use this
        storeName   : 'tblGlobal', // name of the table
        description : 'some description'
    });
}]);

@ocombe
Copy link
Collaborator

ocombe commented Dec 18, 2014

Thanks, I'll check this tomorrow and I'll let you know.

@bobsilon
Copy link
Author

Thanks man :)

@ocombe
Copy link
Collaborator

ocombe commented Jan 6, 2015

Wow is it tomorrow already ? :P
Sorry I forgot to follow up on this :(

The problem is that when you create a new instance it uses the parameter "name" to keep track of the instances, and the name is also the name of the db. So if you define a new instance, you need to define a different name and it will create a new database...
I really did not think to this use case when I implemented the instances. I need to change the way I store instances to make this work...

Thanks for the report, I'll let you know when it's fixed.

@ocombe ocombe added the bug label Jan 7, 2015
@nathanbrock
Copy link

Hi Olivier. Firstly, thanks for the great lib!

I've also come across the use case highlighted by @bobsilon, the desire to use two separate dataStores, in particular with IndexedDB/WebSQL. In my own case, I'm putting together some simple diary functionality with the need to store entry meta data in one table, and the body content in another.

For the moment I've made a couple of minor changes to the createInstance method as shown below, allowing the config property 'instanceName' to be used when provided, or to fall back to the 'name’ property as the key for the instances array.

src/angular-localForage.js

LocalForageInstance.prototype.createInstance = function createInstance(config) {
    var instanceName = angular.isDefined(config.instanceName) ? config.instanceName : config.name;

    if (angular.isObject(config)) { // create new instance
        config = angular.extend({}, defaultConfig, config);
        if (angular.isDefined(lfInstances[instanceName])) {
            throw new Error('A localForage instance with the name ' + instanceName + ' is already defined.');
        }

        lfInstances[instanceName] = new LocalForageInstance(config);
        return lfInstances[instanceName];
    } else {
        throw new Error('The parameter should be a config object.')
    }
};

Simple enough, but currently only works when using the WebSQL driver. If you use IndexedDB the new instance will be created but the data won’t save as the following error is thrown.

"Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found."

When creating the second instance, I think because the same database name is being used the onupgradeneeded function in the IndexSQL driver isn’t fired and the second object store isn’t created. I’ve yet to get it working with IndexDB with the original suggestion made by @bobsilon, regarding incrementing the database version.

Additionally, with localstorage the key doesn't seem to namespace the dataStore eg 'name\key' instead of 'name\dataStore\key', but I could just be missing something!

It would be great if we could get this working. WebSQL works well for my current project platforms (Cordova for iOS / Android) but it would be great to get it working for IndexDB and LocalStorage.

Cheers!

@ocombe
Copy link
Collaborator

ocombe commented Jan 15, 2015

Thanks for your investigation.
I'll probably have to do a breaking change to fix this problem. I'm not sure that instances are really used by a lot of people right now, so this shouldn't be too serious.
I'll let you know when I have the time to work on this (I don't have a lot of free time this month).

@saniyusuf
Copy link

ANy news on this ?

@ocombe
Copy link
Collaborator

ocombe commented Feb 16, 2015

I haven't had the time to work on it sorry, I had a busy beginning of 2015 :)
It's still on my todo list though.

@psalaets
Copy link
Contributor

@nathanbrock

Your error could be related to localForage/localForage#342

Relevant discussion also in localForage/localForage#367

@saniyusuf
Copy link

Any News?

@shardulP
Copy link

shardulP commented Oct 5, 2015

Any updates on this ?

@ocombe
Copy link
Collaborator

ocombe commented Oct 5, 2015

No sorry I kinda let this project down :(
If you know anyone interested in taking over that would be cool because I don't have much time these days.

@shardulP
Copy link

shardulP commented Oct 5, 2015

If we pass storeName as an Array ? Though getItem will going to change. But is it a good way to do it ?

@ansorg
Copy link

ansorg commented Dec 11, 2015

Since localForage/localForage#342 seems now fixed and actually allows multiple stores/tables per database/instance - could this be adapted here as well?

I wonder whether the approach by @nathanbrock above would just work now? Ok, going to try that ..

@ocombe ocombe closed this as completed Mar 26, 2016
@hozkok
Copy link

hozkok commented Jul 1, 2016

hi, fixed the issue there.

created a pull request

#119

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants