Loki.js嵌套数组与单独的集合

时间:2018-09-26 08:54:15

标签: lokijs

我有一个非常简单的应用程序,其中包含实体“ Post”和“ Comment”。评论严格属于一个帖子。两个实体都应支持CRUD操作。 为该模型设计LokiJS存储的一种“惯用”方法是:具有两个单独的集合(评论,帖子),并从评论链接到父帖子,或者将评论作为帖子中的嵌套数组。

第二种方法可能根本不可行,因为嵌套的集合元素(注释)没有ID(是否有某种方法可以在Loki中生成它们?)。没有ID,您将无法真正对项目执行CRUD操作。

// Approach 1 - separate collection
const posts = db.addCollection('posts');
const comments = db.addCollection('comments');

const post = posts.insert({ title: 'post' });
const comment = comments.insert({ 
    text: 'nice post', parent: post.$loki 
});


// Approach 2 - nested array
const posts2 = db.addCollection('posts2');
posts2.insert({
    title: 'post', 
    comments: [
        // nested items don't have IDs, how to identify them?
        { text: 'nice post' }
    ]
});

为此简单的任务设计模型的最惯用的方法是什么?

0 个答案:

没有答案
相关问题