最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Node.js用mysql编写api代码(sequelize)

IT培训 admin 9浏览 0评论

Node.js用mysql编写api代码(sequelize)

我的mysql表模型:

//mysql
//table structures start

var Contact = sequelize.define('contact', {
    gsm: {
        type: Sequelize.STRING,
        unique: true,
        required: true
    },
    firstName: Sequelize.STRING,
    lastName: Sequelize.STRING,
    street: Sequelize.STRING,
    city: Sequelize.STRING,
})

var Group = sequelize.define('group', {
    groupname: Sequelize.STRING

})

var ContactGroup = sequelize.define('contactgroup', {
    /* Empty */
})

ContactGroup.belongsTo(Contact, {
    onDelete: 'CASCADE'
});

Contact.hasMany(ContactGroup, {
    onDelete: 'CASCADE'
});

ContactGroup.belongsTo(Group, {
    onDelete: 'CASCADE'
});
Group.hasMany(ContactGroup, {
    onDelete: 'CASCADE'
});

这是我的第一个api代码:

exports.getNewGroup = (req, res) => {
    Group.findAll({
        attributes: ['id', 'groupname', [sequelize.fn('COUNT', sequelize.col('contactgroups.groupId')), 'contactsCount']],
        include: [{
            model: ContactGroup,
            attributes: ['id']
        }],
        group: ['id']
    }).then(data => {
        return res.status(200).send(data);
    }).catch(err => {
        return res.status(400).send(err.message);
    });
};

我得到了第一个这样的api响应:

[
    {
        "id": 14,
        "groupname": "Angular",
        "contactsCount": 2,
        "contactgroups": [
            {
                "id": 1
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "contactsCount": 1,
        "contactgroups": [
            {
                "id": 3
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "contactsCount": 0,
        "contactgroups": []
    }
]

这是我的第二个api代码:

exports.getNewGroupForProfsms = (req, res) => {
    Group.findAll({
        include: [{
            model: ContactGroup,
            attributes: ['id'],
            include: [{
                model: Contact,
                attributes: ['id', 'gsm']
            }]
        }],
    }).then(data => {
        return res.status(200).send(data);
    }).catch(err => {
        return res.status(400).send(err.message);
    });
};

这是我的第二个api回复:

[
    {
        "id": 14,
        "groupname": "Angular",
        "createdAt": "2017-12-15T15:06:39.000Z",
        "updatedAt": "2017-12-15T15:06:39.000Z",
        "contactgroups": [
            {
                "id": 1,
                "contact": {
                    "id": 20,
                    "gsm": "987654321"
                }
            },
            {
                "id": 2,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "createdAt": "2017-12-15T15:06:45.000Z",
        "updatedAt": "2017-12-15T15:06:45.000Z",
        "contactgroups": [
            {
                "id": 3,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "createdAt": "2017-12-15T15:06:51.000Z",
        "updatedAt": "2017-12-15T15:06:51.000Z",
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "createdAt": "2017-12-17T16:07:38.000Z",
        "updatedAt": "2017-12-17T16:07:38.000Z",
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "createdAt": "2017-12-21T05:50:50.000Z",
        "updatedAt": "2017-12-21T05:50:50.000Z",
        "contactgroups": []
    }
]

在上面的代码中,我写了两个api,现在我想写单个api,

我需要这种类型的响应:(我在第一次api响应中有contactsCount,我希望在第二次api响应中)

最后,我希望使用单个api代码进行此类响应:

[
    {
        "id": 14,
        "groupname": "Angular",
        "contactsCount": 2,
        "createdAt": "2017-12-15T15:06:39.000Z",
        "updatedAt": "2017-12-15T15:06:39.000Z",
        "contactgroups": [
            {
                "id": 1,
                "contact": {
                    "id": 20,
                    "gsm": "987654321"
                }
            },
            {
                "id": 2,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "contactsCount": 1,
        "createdAt": "2017-12-15T15:06:45.000Z",
        "updatedAt": "2017-12-15T15:06:45.000Z",
        "contactgroups": [
            {
                "id": 3,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "contactsCount": 0,
        "createdAt": "2017-12-15T15:06:51.000Z",
        "updatedAt": "2017-12-15T15:06:51.000Z",
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "contactsCount": 0,
        "createdAt": "2017-12-17T16:07:38.000Z",
        "updatedAt": "2017-12-17T16:07:38.000Z",
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "contactsCount": 0,
        "createdAt": "2017-12-21T05:50:50.000Z",
        "updatedAt": "2017-12-21T05:50:50.000Z",
        "contactgroups": []
    }
]

有人会帮助我吗?我需要单个api代码。

我尝试了嵌套查询,并修改了我的第一个api代码,如下所示:

exports.getNewGroup = (req, res) => {
    Group.findAll({
        attributes: ['id', 'groupname', [sequelize.fn('COUNT', sequelize.col('contactgroups.groupId')), 'contactsCount']],
        include: [{
            model: ContactGroup,
            attributes: ['id'],
            include: [{
                model: Contact,
                attributes: ['id', 'gsm']
            }]
        }],
        group: ['id']
    }).then(data => {
        return res.status(200).send(data);
    }).catch(err => {
        return res.status(400).send(err.message);
    });
};

但我得到的这种反应不是我的要求

[
    {
        "id": 14,
        "groupname": "Angular",
        "contactsCount": 2,
        "contactgroups": [
            {
                "id": 1,
                "contact": {
                    "id": 20,
                    "gsm": "987654321"
                }
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "contactsCount": 1,
        "contactgroups": [
            {
                "id": 3,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "contactsCount": 0,
        "contactgroups": []
    }
]
回答如下:

试试这段代码

exports.getNewGroupForProfsms = (req, res) => {
  Group.findAll({
    include: [{
      model: ContactGroup,
      attributes: ['id'],
      include: [{
        model: Contact,
        attributes: ['id', 'gsm']
      }]
    }],
  }).then(data => {
    // change happen here
    return res.status(200).send(data.map((x) => {
      // assign contactsCount to each row
      return Object.assign({
        contactsCount: x.contactgroups.length,
      }, x.toJSON()) // update toJSON have a try
    }));
  }).catch(err => {
    return res.status(400).send(err.message);
  });
};

Node.js用mysql编写api代码(sequelize)

我的mysql表模型:

//mysql
//table structures start

var Contact = sequelize.define('contact', {
    gsm: {
        type: Sequelize.STRING,
        unique: true,
        required: true
    },
    firstName: Sequelize.STRING,
    lastName: Sequelize.STRING,
    street: Sequelize.STRING,
    city: Sequelize.STRING,
})

var Group = sequelize.define('group', {
    groupname: Sequelize.STRING

})

var ContactGroup = sequelize.define('contactgroup', {
    /* Empty */
})

ContactGroup.belongsTo(Contact, {
    onDelete: 'CASCADE'
});

Contact.hasMany(ContactGroup, {
    onDelete: 'CASCADE'
});

ContactGroup.belongsTo(Group, {
    onDelete: 'CASCADE'
});
Group.hasMany(ContactGroup, {
    onDelete: 'CASCADE'
});

这是我的第一个api代码:

exports.getNewGroup = (req, res) => {
    Group.findAll({
        attributes: ['id', 'groupname', [sequelize.fn('COUNT', sequelize.col('contactgroups.groupId')), 'contactsCount']],
        include: [{
            model: ContactGroup,
            attributes: ['id']
        }],
        group: ['id']
    }).then(data => {
        return res.status(200).send(data);
    }).catch(err => {
        return res.status(400).send(err.message);
    });
};

我得到了第一个这样的api响应:

[
    {
        "id": 14,
        "groupname": "Angular",
        "contactsCount": 2,
        "contactgroups": [
            {
                "id": 1
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "contactsCount": 1,
        "contactgroups": [
            {
                "id": 3
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "contactsCount": 0,
        "contactgroups": []
    }
]

这是我的第二个api代码:

exports.getNewGroupForProfsms = (req, res) => {
    Group.findAll({
        include: [{
            model: ContactGroup,
            attributes: ['id'],
            include: [{
                model: Contact,
                attributes: ['id', 'gsm']
            }]
        }],
    }).then(data => {
        return res.status(200).send(data);
    }).catch(err => {
        return res.status(400).send(err.message);
    });
};

这是我的第二个api回复:

[
    {
        "id": 14,
        "groupname": "Angular",
        "createdAt": "2017-12-15T15:06:39.000Z",
        "updatedAt": "2017-12-15T15:06:39.000Z",
        "contactgroups": [
            {
                "id": 1,
                "contact": {
                    "id": 20,
                    "gsm": "987654321"
                }
            },
            {
                "id": 2,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "createdAt": "2017-12-15T15:06:45.000Z",
        "updatedAt": "2017-12-15T15:06:45.000Z",
        "contactgroups": [
            {
                "id": 3,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "createdAt": "2017-12-15T15:06:51.000Z",
        "updatedAt": "2017-12-15T15:06:51.000Z",
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "createdAt": "2017-12-17T16:07:38.000Z",
        "updatedAt": "2017-12-17T16:07:38.000Z",
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "createdAt": "2017-12-21T05:50:50.000Z",
        "updatedAt": "2017-12-21T05:50:50.000Z",
        "contactgroups": []
    }
]

在上面的代码中,我写了两个api,现在我想写单个api,

我需要这种类型的响应:(我在第一次api响应中有contactsCount,我希望在第二次api响应中)

最后,我希望使用单个api代码进行此类响应:

[
    {
        "id": 14,
        "groupname": "Angular",
        "contactsCount": 2,
        "createdAt": "2017-12-15T15:06:39.000Z",
        "updatedAt": "2017-12-15T15:06:39.000Z",
        "contactgroups": [
            {
                "id": 1,
                "contact": {
                    "id": 20,
                    "gsm": "987654321"
                }
            },
            {
                "id": 2,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "contactsCount": 1,
        "createdAt": "2017-12-15T15:06:45.000Z",
        "updatedAt": "2017-12-15T15:06:45.000Z",
        "contactgroups": [
            {
                "id": 3,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "contactsCount": 0,
        "createdAt": "2017-12-15T15:06:51.000Z",
        "updatedAt": "2017-12-15T15:06:51.000Z",
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "contactsCount": 0,
        "createdAt": "2017-12-17T16:07:38.000Z",
        "updatedAt": "2017-12-17T16:07:38.000Z",
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "contactsCount": 0,
        "createdAt": "2017-12-21T05:50:50.000Z",
        "updatedAt": "2017-12-21T05:50:50.000Z",
        "contactgroups": []
    }
]

有人会帮助我吗?我需要单个api代码。

我尝试了嵌套查询,并修改了我的第一个api代码,如下所示:

exports.getNewGroup = (req, res) => {
    Group.findAll({
        attributes: ['id', 'groupname', [sequelize.fn('COUNT', sequelize.col('contactgroups.groupId')), 'contactsCount']],
        include: [{
            model: ContactGroup,
            attributes: ['id'],
            include: [{
                model: Contact,
                attributes: ['id', 'gsm']
            }]
        }],
        group: ['id']
    }).then(data => {
        return res.status(200).send(data);
    }).catch(err => {
        return res.status(400).send(err.message);
    });
};

但我得到的这种反应不是我的要求

[
    {
        "id": 14,
        "groupname": "Angular",
        "contactsCount": 2,
        "contactgroups": [
            {
                "id": 1,
                "contact": {
                    "id": 20,
                    "gsm": "987654321"
                }
            }
        ]
    },
    {
        "id": 15,
        "groupname": "React",
        "contactsCount": 1,
        "contactgroups": [
            {
                "id": 3,
                "contact": {
                    "id": 21,
                    "gsm": "123456789"
                }
            }
        ]
    },
    {
        "id": 16,
        "groupname": "Vue",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 17,
        "groupname": "NodeJs",
        "contactsCount": 0,
        "contactgroups": []
    },
    {
        "id": 18,
        "groupname": "RxJS",
        "contactsCount": 0,
        "contactgroups": []
    }
]
回答如下:

试试这段代码

exports.getNewGroupForProfsms = (req, res) => {
  Group.findAll({
    include: [{
      model: ContactGroup,
      attributes: ['id'],
      include: [{
        model: Contact,
        attributes: ['id', 'gsm']
      }]
    }],
  }).then(data => {
    // change happen here
    return res.status(200).send(data.map((x) => {
      // assign contactsCount to each row
      return Object.assign({
        contactsCount: x.contactgroups.length,
      }, x.toJSON()) // update toJSON have a try
    }));
  }).catch(err => {
    return res.status(400).send(err.message);
  });
};
发布评论

评论列表 (0)

  1. 暂无评论