我正在使用 Phonegap 应用程序中 http://brian.io/lawnchair/的 Lawnchair JS 来管理我的数据库记录。我是文档存储的新手,但对传统的关系数据库有经验。如果我想在另一条记录上引用一条记录,我应该怎么做。例如:我有多个食品配料记录,我有多个食谱记录,在食谱 JSON 中,有没有办法引用成分记录?
Lawnchair 这样的 json 文档存储来实现您想要的,您只需要将引用文档的键名存储在文档中。
例如,您将拥有这些食品成分文件:
{key:'fi_1', name: 'rice', id: 1}
{key:'fi_2', name: 'corn', id: 2}
和那些食谱文件:
{key:'r_332', ingredients: ['fi_1', 'fi_54'], name: 'risotto', id: 332}
{key:'r_333', ingredient: ['fi_12'], name:'cookie', id: 333}
您可以将所有食谱的列表存储在一个文档中:
{key:'cookbook', recipes: ['r_1', 'r_2', .... 'r_332', 'r_333', .... ] }
然后,您可以检索说明书文档:
store.get('cookbook', function(data) {
var recipes = data.recipes;
// do something with the list of all recipes
});
并寻找食谱来检索其成分:
store.get('r_332', function(data) {
var ingredients = data.ingredients;
for (ingredient_key in ingredients) {
store.get(ingredient_key, function(ingredient_data) {
var name = ingredient_data.name;
var id = ingredient_data.id;
// do something with each ingredient
alert('ingredient: ' + name + ' - id: ' + id);
}
}
});
在上面的列表中,您可以只存储它们的 id,而不是存储引用文档的完整键名,因为您应该知道它们的类型以及如何从中重新创建键名(食品成分:"fi_"前缀后跟 id,食谱:"r_"后跟 id..)。