如何在realm-react-native中创建嵌套的类似于文件夹的结构?

Talor A

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

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,我该如何添加一个现有项作为其子项(而不是在文档显示中声明一个新项)?

AST

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Flask 中创建类似于 Codeigniter 的 MVC 文件夹结构

React Native:如何在Realm DB中保存日期

如何在 React 中创建类似于 onChange 的事件

如何将具有绝对文件路径的字典转换为类似于文件夹结构的嵌套Dict?

在Octopress中,如何创建类似于博客文件夹的文件夹并列出存档文件?

如何使用Finder在嵌套文件夹层次结构中创建新文件夹?

如何在React Native中访问其他文件夹中的资产?

Realm React Native:如何在一定条件下从对象的嵌套数组中删除元素

如何在React Native Fs中重命名/移动文件夹?

如何在iOS上处理Realm React Native迁移和schemaVersion?

如何在React with Typescript中的类组件中创建类似于setState的自定义钩子

如何在AppData中创建嵌套文件夹

如何在SQL Server中创建类似于结构的队列

如何从react-native中的Assets文件夹中读取文件?

如何从React(创建React App)的公共文件夹中设置背景图像

如何在React Native项目版本0.61.x中重新生成Android和IOS文件夹?

如何使Downloads文件夹的行为类似于临时目录

如何在React Native中创建和保存CSV文件?

如何在react-native中创建pdf文件?

如何从react native中的主题文件夹导入可重用组件?

如何在React Native项目中重新生成ios文件夹?

如何直接在当前文件夹中创建React应用

如何在 .Net Core+React 项目中创建 wwwroot 文件夹?

React Native 错误:RNHTMLtoPDF 错误:无法创建文件夹结构

如何使用Realm项目文件组织React Native?

Realm React-Native中的嵌套对象

Swift如何在临时文件夹中创建复杂的文件夹结构

如何创建类似于发送到桌面快捷方式的“发送到“文件夹””?

如何在React应用程序中从公用文件夹导入文件?