kamalav
kamalav

Reputation: 1200

Can I have multiple database in indexeddb using jsstore

can i have multiple databases in indexeddb using jsstore in angular2.

please guide me how can we achieve this. please provide syntax for that.

will this degrade the performance?

below is my service code

constructor(private _http:Http)
{
    this._connection=new JsStore.Instance();
    let that=this, dbName='DTSubscription';
    JsStore.isDbExist(dbName,function(isExist)
    {
        if(isExist)
        {
            that._connection.openDb(dbName);
        }
        else
        {
            const Database=that.GetDatabase();
            that._connection.createDb(Database);
        }
    },function(err)
    {
        alert(err.Message);
    });

    this._connection=new JsStore.Instance();
    let t=this, db2='DTSubscriptionNew';
    JsStore.isDbExist(db2,function(isExist)
    {
        if(isExist)
        {
            t._connection.openDb(db2);
        }
        else{
            const Database2=t.GetDatabase2();
            t._connection.createDb(Database2);
        }
    },function(err)
    {
    alert(err.Message);
    });
}

The db has been created. But while inserting I need to mention db otherwise it gives error like this -

{ Name: "table_not_exist", Message: "Table 'ArticleNew' does not exist" }

I can see db is there.

Upvotes: 1

Views: 848

Answers (1)

Ujjwal Kumar Gupta
Ujjwal Kumar Gupta

Reputation: 2376

You can create multiple database but only one will work at a time. So if you want to use two database in a app - change the database by calling 'openDb' api.

So if your database is - DTSubscription, DTSubscriptionNew

When you want to execute query for the db - DTSubscription. The query will be like this -

var Connection = new JsStore.Instance();
Connection.openDb('DTSubscription');

When you want to execute query for db - 'DTSubscriptionNew'

Connection.openDb('DTSubscriptionNew');

Since you are using angular2, i will recommended to define two service for two database and keep the JsStore connection common. This way the particular service will execute the query for particular database.

For more info about how to use this, take a look at demo projects for using two db in jsstore -

https://github.com/ujjwalguptaofficial/multipledb-in-jsstore

Update

Starting from JsStore v4 , you can use multiple connection for multiple database and all will work parallely.

Upvotes: 1

Related Questions