0

我正在尝试创建一个领域数据库,其中包含嵌套在容器中的项目。这将像标准文件系统一样运行,其中容器可以同时包含项目和其他容器。如何创建这种类型的结构?我已经设置了这些模式:

const ItemSchema = {
    name: 'Item',
    primaryKey: 'id',
    properties: {
        id:         {type: 'string', indexed: true},
        title:      'string',
        createdAt:  'date',
        updatedAt:  'date',
        picture:    {type: 'data', optional: true},
        parent:     {type: 'Container', optional: true},
    }
};
const ContainerSchema = {
    name: 'Container',
    primaryKey: 'id',
    properties: {
        id:         {type:'string', indexed:true},
        title:      'string',
        createdAt:  'date',
        updatedAt:  'date',
        parent:     {type: 'Container', optional: true},
        childContainers: {type: 'list', objectType: 'Container'},
        childItems: {type: 'list', objectType: 'Item'},
    }
};

我还为 Items 设置了这个模型,但还没有为 Containers 创建一个模型:

class ItemModel {
    constructor(title, children) {
        this.id = Utils.guid();
        this.title = title;
        this.children = children;
        this.createdAt = new Date();
        this.updatedAt = new Date();
    }
}

现在我如何实际填充数据库并将父母和孩子分配给现有项目?我知道我必须这样做才能创建一个项目:

let item = new ItemModel('testItem')
db.write(() => {
            item.updatedAt = new Date();
            db.create('Item', item);
        })

但我不知道在那之后我要去哪里。领域文档给出了这个例子:

carList.push({make: 'Honda', model: 'Accord', miles: 100});

但是一旦我用 . 创建了一个容器db.create,我该如何添加一个现有项目作为它的子项(而不是像文档显示的那样声明一个新项目)?

4

1 回答 1

1

创建项目后,您可以使用push()它来将其添加到容器中的列表中。

假设您已经拥有容器,代码可能如下所示:

let item = new ItemModel('testItem')
db.write(() => {
        item.updatedAt = new Date();
        var item = db.create('Item', item);
        container.childItems.push(item);
    });
于 2017-01-26T06:01:35.173 回答