2014-01-18 85 views
6

我在學習Node.js和Sails是我的首選框架。 我想在MySql數據庫的項目中使用它,我認爲Sequelize Orm更完整。 如何在風帆中使用Sequelize Orm而不是水線?Sails js和Sequelize

謝謝

回答

2

我覺得你可以隨着風帆出生而產生續集。

您可以閱讀Mike McNeil's answer here也許問麥克直接他是否會重新推出sequelize支持

2

有兩個項目,近來紛紛站出來,我已經工作過,爲恢復到sequelize全面支持,包括藍圖。

帆鉤形件sequelize

帆-鉤sequelize-藍圖

See my answer

1
$ 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 
    } 
} 

在模型文件夾

//例如讓採取userstable - > 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)"); 
} 
} 
0

首先你必須填寫包帆 - 鉤 - 續集:

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){ 
}) 

好運^^。

+0

我們的項目是在一年前開始的,但現在我們需要繼續進行續集。我可以在重構之前並行使用它們兩個嗎? – Crusader