Node.js - Sequelize ORM - 简单CRUD(创建,读取,更新和删除)示例

时间:2017-10-23 10:53:32

标签: node.js sequelize.js crud

在使用最新的Sequelize ORM版本4.15搜索实现CRUD代码的简单示例后,在单个问题中找不到答案。不得不分别搜索每个CRUD操作。

1 个答案:

答案 0 :(得分:0)

使用Node.js和Sequelize ORM的简单CRUD:

使用带有" id"的简单组织实体的示例和"名称"字段。

  1. app.js

    ... var organizations = require('./routes/organizations'); ...


    1. 路由/组织
    2. 
      
      module.exports = function (sequelize, DataTypes) {
          return sequelize.define('organization', {
              id: {
                  type: DataTypes.INTEGER(11),
                  allowNull: false,
                  primaryKey: true,
                  autoIncrement: true
              },
              name: {
                  type: DataTypes.STRING(200),
                  allowNull: false
              }
          }, {
                  tableName: 'organization'
              });
      };
      
      
      


      1. 路由/组织
      2. 
        
        var express = require('express');
        var router = express.Router();
        
        const sequelize = require('path/to/sequelize');
        const Model = sequelize.import('../models/organization');
        
        /* GET Models. */
        router.get('/', function (req, res, next) {
            var code = 500;
            var message = 'Internal Server Error';
            var result = '';
        
            var page = req.query.page || 1;
            var limit = req.query.limit || 5;
            var offset = (page - 1) * limit;
        
            Model
                .findAndCountAll({
                    offset: +offset,
                    limit: +limit
                })
                .then(result => {
                    code = 200;
                    message = 'OK';
        
                    res.json({
                        code: code,
                        message: message,
                        response: {
                            data: { models: result }
                        }
                    });
                });
        });
        
        /* GET Model. */
        router.get('/:id', function (req, res, next) {
            var code = 500;
            var message = 'Internal Server Error';
            var result = '';
        
            var id = req.params.id | 0;
        
            Model
                .find({
                    where: {
                        id: id
                    }
                })
                .then(result => {
                    code = 200;
                    message = 'OK';
        
                    res.json({
                        code: code,
                        message: message,
                        response: {
                            data: { model: result }
                        }
                    });
                });
        });
        
        /* POST new Model. */
        router.post('/', function (req, res, next) {
            var code = 500;
            var message = 'Internal Server Error';
            var response = '';
        
            var postData = {
                name: req.body.name
            };
        
            Model.create(postData)
                .then(function (model) {
                    code = 200;
                    message = 'OK';
                    response = 'Record is successfully added.';
        
                    res.json({
                        code: code,
                        message: message,
                        response: {
                            msg: response
                        }
                    });
                })
                .catch(function (err) {
                    code = 500;
                    response = message;
        
                    res.json({
                        code: code,
                        message: message,
                        response: {
                            msg: response
                        }
                    });
                });
        });
        
        /* PUT old Model. */
        router.put('/:id', function (req, res, next) {
            var code = 500;
            var message = 'Internal Server Error';
            var response = '';
        
            var id = req.params.id;
            var putData = {
                name: req.body.name
            };
        
            Model.update(putData,
                {
                    where: {
                        id: id
                    }
                }
            )
                .then(function (model) {
                    code = 200;
                    message = 'OK';
                    response = 'Record is successfully updated.';
        
                    res.json({
                        code: code,
                        message: message,
                        response: {
                            msg: response
                        }
                    });
                })
                .catch(function (err) {
                    code = 500;
                    response = message;
        
                    res.json({
                        code: code,
                        message: message,
                        response: {
                            msg: response
                        }
                    });
                });
        });
        
        /* DELETE Model. */
        router.delete('/:id', function (req, res, next) {
            var code = 500;
            var message = 'Internal Server Error';
            var response = '';
        
            var id = req.params.id;
        
            Model.destroy(
                {
                    where: {
                        id: id
                    }
                }
            )
                .then(function (deletedRecord) {
                    if (deletedRecord === 1) {
                        code = 200;
                        message = 'OK';
                        response = 'Record is successfully deleted.';
                    } else {
                        code = 404;
                        message = 'OK';
                        response = 'Record not found.';
                    }
                    res.json({
                        code: code,
                        message: message,
                        response: {
                            msg: response
                        }
                    });
                })
                .catch(function (err) {
                    code = 500;
                    response = message;
        
                    res.json({
                        code: code,
                        message: message,
                        response: {
                            msg: response
                        }
                    });
                });
        });
        
        module.exports = router;
        
        
        

        非常感谢知道比上述更好的代码。它可以帮助某人。