获取分层结构数据nodejs - sequelizejs - mysql

时间:2018-05-18 13:24:36

标签: mysql node.js sequelize.js hierarchical-data

这是表文件夹

sequelize.define('Folders', {
        id: {
            type: Sequelize.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: Sequelize.STRING(100),
            allowNull: false,
            validate: {
                len: [1, 100]
            }
        },
        isSub: {
            type: Sequelize.ENUM(),
            values: ['0', '1'],
            allowNull: false,
            defaultValue: '0'
        },
        parentid: {
            type: Sequelize.INTEGER,
            allowNull: true,
            validate: {
                isNumeric: true
            }
        },
        status: {
            type: Sequelize.ENUM(),
            values: ['0', '1', '2'],
            allowNull: false,
            defaultValue: '1'
        },
    },
    {
        tableName: 'folders'
    }
);

Folders.associate = function(models) {
    models.Folders.hasMany(models.Folders, {
      foreignKey: 'parentid',
      as: 'children'
    });
};

这是我用于获取分层结构的表。我正在使用 nodejs - sequelizejs 。 这是我的模特

models.Folders.findAll({
        where: {
            isSub : 1,
            status : '1',
        },
        include: [{
          model: models.Folders,
          as: 'children',
          where: {
              status: '1'
          },
            required: false
        }],
        required: false
    }).then((data) => {

    data.map((foldData) => {
        return Object.assign({}, {
            fid: foldData['id'],
            fName: foldData['name'],
            fisSub: (foldData['isSub'] === '1')? 'Yes': 'No',
            subFolders:
            foldData['children'].map((sfoldData, index) => {

                return Object.assign({}, {
                    sfid: sfoldData['id'],
                    sfName: sfoldData['name'],
                    sisSubFold: (sfoldData['isSub']) === '1'? 'Yes': 'No',
                })
            }),
        });
    });
});

我的控制器功能可以获取列表

[
    {
        "fid": "2",
        "fName": "Main Folder2",
        "fisSub": "No",
        "subCategory": [
            {
                "sfid": "3",
                "sfName": "Sub Main Fol2",
                "sisSubFold": "Yes"
            }
        ]
    },
    {
        "fid": "1",
        "fName": "Main Folder1",
        "fisSub": "No",
        "subCategory": [
            {
                "sfid": "5",
                "sfName": "Sub Main Fol1",
                "sisSubFold": "Yes"
            },
            {
                "sfid": "6",
                "sfName": "Sub Main Fol1",
                "sisSubFold": "Yes"
            },
            {
                "sfid": "8",
                "sfName": "Sub Main Fol1",
                "sisSubFold": "Yes"
            }
        ]
    },
    {
        "fid": "7",
        "fName": "Main Folder3",
        "fisSub": "No",
        "subCategory": []
    }
]

通过上面的代码我得到结果在API调用中,如下所示

Glide.with(context)
    .load(imageUrl or resourceId)
    .asGif()
    .fitCenter() //or other scaling options as you like
    .into(imageView);

这里的 “fid”:“2” 在表格中有两个级别的层次结构,但我只是第一个级别。

请更正我的代码以获得分层结构。

注意:会有很多层次结构,不仅仅是第2 /第3 /很快级别。 我搜索了很多东西,但得不到任何正确的东西。请帮帮我。

提前致谢

1 个答案:

答案 0 :(得分:0)

你想要做的是递归查询,而Sequelize尚不支持它,这里有一个提案https://github.com/sequelize/sequelize/issues/4890,解决方法是使用PKG_CXXFLAGS = -g -O3 -std=c++11 函数编写自己的CTE请求。