我正在研究一个IndexedDB 支持的 JS 神经网络实现,并遇到了这个问题。
我们在 IndexedDB 中没有连接,因此您至少要查看两个对象存储命中,除非您正在进行某种记忆/缓存。
根据经验,我发现面向文档的样式最适合 IndexedDB 对象(将所有内容存储在同一个存储中),但需要辅助存储来容纳关系。
这就是我正在做的事情。
假设您想拥有一家本地的演员和电影商店——比如 IMDB。这种和大多数任何多对多关系都可以使用 IndexedDB 使用两个表进行建模:对象和关系。
这是两张表。您需要对几乎所有内容进行键查找*。任何不能说独特的东西都可以是非独特的。
对象对象存储:
type_id*
whatever*..
关系对象存储:
id* (unique, auto-incrementing)
from_type*
to_id*
演员/电影示例将是 Objects 表中的两条记录和关系表中的一条记录:
var actor1 = {
id: 'actor_jonah_goldberg',
display: 'Jonah Goldberg',
};
var actor2 = {
id: 'actor_michael_cera',
display: 'Michael Cera'
};
var movie1 = {
id: 'movie_superbad',
display: 'Superbad',
year: 2007
};
var movie2 = {
id: 'movie_juno',
display: 'Juno',
year: 2007
};
//relationship primary key ids are auto-inc
var relationship1 = {
from_id: 'actor_jonah_goldberg',
to_id: 'movie_superbad'
}
var relationship2 = {
from_id: 'actor_michael_cera',
to_id: 'movie_superbad'
}
var relationship3 = {
from_id: 'actor_michael_cera',
to_id: 'movie_juno'
}
获取 Michael Cera 电影的伪代码:
IndexedDBApp( { 'store': 'relationships', 'index': 'from_id', 'key': 'actor_michael_cera', 'on_success': function( row ) {...} );
// Would return movie_superbad and movie_juno rows on_success
从给定年份获取所有电影的伪代码:
IndexedDBApp( { 'store': 'objects', 'index': 'year', 'key': 2007, 'on_success': function( row ) {...} );
// Would return movie_superbad and movie_juno rows on_success
获取电影演员的伪代码:
IndexedDBApp( { 'store': 'relationships', 'index': 'to_id', 'key': 'movie_superbad', 'on_success': function( row ) {...} );
// Would return actor_jonah_goldberg and actor_michael_cera on_success
获取所有演员的伪代码:
IndexedDBApp( { 'store': 'relationships', 'index': 'id', 'cursor_begin': 'actor_a', 'cursor_end': 'actor_z', 'on_success': function( row ) {...} );
// Would return actor_jonah_goldberg and actor_michael_cera on_success