【问题标题】:Sails js and SequelizeSails js 和 Sequelize
【发布时间】:2014-01-18 01:17:59
【问题描述】:

我正在学习 Node.js,而 Sails 是我选择的框架。 我想在一个带有 MySql db 的项目中使用它,我认为 Sequelize Orm 更完整。 如何在 Sails 中使用 Sequelize Orm 而不是 Waterline?

谢谢

【问题讨论】:

    标签: node.js sails.js sequelize.js waterline


    【解决方案1】:

    我认为你可以像风帆一样天生就有续集。

    您可以阅读Mike McNeil's answer here,也可以直接询问 Mike 他是否会重新引入续集支持

    【讨论】:

      【解决方案2】:

      最近出现了两个项目,我一直在努力重新引入对续集的全面支持,包括蓝图。

      帆钩续集

      sails-hook-sequelize-blueprints

      See my answer

      【讨论】:

        【解决方案3】:
        $ npm install sails-hook-sequelize
        $ npm install sails-hook-sequelize-blueprints
        $ npm install sequelize
        $ npm install pg pg-hstore
        $ npm install continuation-local-storage
        

        .sailsrc

        "hooks": {
            "blueprints": false,
            "orm": false,
            "pubsub": false
        }
        

        在connections.js中

        somePostgresqlServer: {
            user: 'postgres',
            password: '',
            database: 'database',
            options: {
                host   : 'localhost',
                port   : 5432,
                logging: true
           }
        }
        

        在模型文件夹中

        // 例如让用户表 ->users.js

        module.exports = {
          attributes: {
            firstname: {
              type: Sequelize.STRING,
              allowNull: false
            },
            secondname: {
              type: Sequelize.STRING,
              allowNull: false
            },
          }
        
          }
        };
        

        或其他连接方法创建单独的文件 db.js

           module.exports = {
         dbPath: function () {
          return ("postgres://postgres:(user)@localhost:5432/(databasename)");
         }
        }
        

        【讨论】:

          【解决方案4】:

          首先你必须安装包sails-hook-sequelize:

          npm install sails-hook-sequelize --save
          

          秒编辑文件.sailsrc

           "hooks": {
            "orm": false,
            "pubsub": false
          }
          

          文件 ./config/models.js

          module.exports.models = {
              schema: true,
              connection: 'mysql',
              migrate: 'safe'
          };
          

          文件 ./config/connections.js

          module.exports.connections = {
             mysql: {
                  adapter: 'sails-mysql',
                  port: 3306,
                  user: 'root',
                  password: '123456',
                  database: 'TestDataBase',
                  charset: 'utf8',
                  collation: 'utf8-general_ci',
                  options: {
                      host: 'localhost'
                  }
              }
          };
          

          在 ./api/models/UserAccount.js 中定义模型

          module.exports = {
              attributes: {
                  ID: {
                      type: Sequelize.BIGINT(20),
                      autoIncrement: true,
                      allowNull: false,
                      primaryKey: true
                  },
                  UID: {
                      type: Sequelize.STRING(255),
                      allowNull: false,
                      defaultValue: Sequelize.UUIDV4,
                  },
                  UserName: {
                      type: Sequelize.STRING(50),
                      allowNull: true
                  }
              },
              associations: function() {},
              options: {
                  tableName: 'UserAccount',
                  createdAt: 'CreatedDate',
                  updatedAt: 'ModifiedDate',
                  hooks: {}
          }
          };
          

          最后,使用模型:

          UserAccount.findAll({}).then(function(success){}, function(err){
          })
          

          祝你好运^^。

          【讨论】:

          • 我们的项目是一年前开始的,但现在我们需要继续进行续集。在重构完成之前我可以同时使用它们吗?
          猜你喜欢
          • 2016-01-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-13
          • 1970-01-01
          相关资源
          最近更新 更多