我可以使用jsstore在indexeddb中拥有多个数据库

时间:2017-11-22 09:48:46

标签: angular typescript indexeddb

我可以使用angular2中的jsstore在indexeddb中拥有多个数据库。

请指导我们如何实现这一目标。请提供相应的语法。

这会降低性能吗?

下面是我的服务代码

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);
    });
}

已创建数据库。但插入时我需要提一下db,否则就会出现这样的错误 -

{   名称:“table_not_exist”,   消息:“表'ArticleNew'不存在” }

我可以看到db就在那里。

1 个答案:

答案 0 :(得分:1)

您可以创建多个数据库,但一次只能运行一个。因此,如果您想在应用程序中使用两个数据库 - 通过调用'openDb'api来更改数据库。

因此,如果您的数据库是 - DTSubscription,DTSubscriptionNew

当您想要执行db-DTSubscription的查询时。查询将是这样的 -

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

如果要执行db的查询 - 'DTSubscriptionNew'

Connection.openDb('DTSubscriptionNew');

由于您使用的是angular2,我建议为两个数据库定义两个服务并保持JsStore连接的通用性。这样,特定服务将执行特定数据库的查询。

有关如何使用它的更多信息,请查看在jsstore中使用两个db的演示项目 -

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