IndexedDB和关系 [英] IndexedDB and Relationships

查看:80
本文介绍了IndexedDB和关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在IndexedDB中的对象库之间创建关系吗?



例如,我有两个对象库: artist 专辑艺术家专辑有一对多的关系。 album.artistId 将相册与 artist.id 相关联。



<我在这里想着Hibernate。我想对艺术家进行查询,并将属于该艺术家的专辑作为艺术家的数组返回专辑对象。

  artist.albums = []; 



跟进(4。5年后,2017)



下面有一些很好的答案可以很好地回答这个问题。我想补充一点,我最初试图将IndexedDB用作关系存储,并在其上构建类似ORM的解决方案,但它并不适合。 IndexedDB是一个NoSQL数据库,因为我开始以这种方式处理它,它更有意义,我的项目更容易管理。我希望这为那些不断遇到它的人增加了原始问题的一些价值。

解决方案

indexedDB提供了低级方法创建,读取,更新和删除(CRUD)对象的记录。但该标准没有定义关系操作的具体方法。目标是保持标准简单,并允许开发人员以他们认为合适的任何方式实现更高级别的功能。但是,通过您的少量开发,您所要求的仍然是可以实现的。



为了解决问题,Parashuram Narasimhan为indexedDB写了一个Jquery实现,它将为你完成这项工作。



Jquery indexedDB示例1



Jquery indexedDB示例2



考虑以下一般概括:

  $ .indexeddb(MyDatabase)
.objectStore(Artist)
.openCursor()
.each(function (){
// Got Artist
//枚举专辑
$ .indexeddb(MyDatabase)
.objectStore(Artist)
.openCursor( )
.each(function(){
//检查匹配的artist.Id
//如果此专辑的艺术家匹配艺术家
//则d o
}
;
}
;

答案有点迟,但我希望它有助于润滑齿轮。


Can I create relationships between my object stores in IndexedDB?

For example, I have two object stores: artist and album. An artist has a one-to-many relationship with an album. album.artistId relates the album to artist.id.

I'm thinking along the lines of Hibernate here. I would like to do a query for artists and have the albums belonging to that artist returned as an array called artists on the album object.

artist.albums = [];

Follow Up (4.5 years later, 2017)

There are some great answers below that answer the question very well. I'd like to add that I was originally trying to use IndexedDB as a relational store and build an ORM-like solution on top of it, which it is not suited for. IndexedDB is a NoSQL database and since I've started treating it that way it's made more sense and my projects easier to manage. I hope this adds some value to the original question for those who continually come across it.

解决方案

The indexedDB provides low level methods to create, read, update, and delete (CRUD) "records" of objects. But the standard does not define specific methods for relational operations. The goal was to keep the standard simple and allow developers to implement the higher-level functions in any way they saw fit. However, what you're asking for is still attainable through a small amount of development on your part.

To help things out, Parashuram Narasimhan wrote a Jquery implementation for indexedDB that will finish the job for you.

Jquery indexedDB Example 1

Jquery indexedDB Example 2

Consider the following generalized possibility:

$.indexeddb("MyDatabase")
  .objectStore("Artist")
  .openCursor()
  .each(function(){
    //Got Artist
    //Enumerate for Albums
    $.indexeddb("MyDatabase")
      .objectStore("Artist")
      .openCursor()
      .each(function(){
        //Check for matching artist.Id
        //if this albums's artist matches artist
        //then do something
        }
      ;
    }
  ;

A little late for an answer, but I hope it helps grease the gears a bit.

这篇关于IndexedDB和关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆