我正在尝试创建一个嵌套在容器内的项目的领域数据库。这将像标准文件系统一样操作,其中容器中可以同时包含项目和其他容器。如何创建这种类型的结构?我已经建立了以下模式:
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'},
}
};
我还为商品设置了此模型,但尚未为容器创建一个模型:
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
,我该如何添加一个现有项作为其子项(而不是在文档显示中声明一个新项)?
创建项目后,可以将push()
其添加到容器中的列表中。
假设您已经有了该容器,则代码可能如下所示:
let item = new ItemModel('testItem')
db.write(() => {
item.updatedAt = new Date();
var item = db.create('Item', item);
container.childItems.push(item);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句