我正在学习Node.js和Sails是我的首选框架。 我想在MySql db的项目中使用它,我认为Sequelize Orm更完整。 如何在Sails而不是Waterline中使用Sequelize Orm?
由于
答案 0 :(得分:2)
我认为你可以像风帆一样出生于sequelize。
您可以阅读Mike McNeil's answer here,也许可以直接询问Mike是否会重新推出续集支持
答案 1 :(得分:2)
答案 2 :(得分: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)");
}
}
答案 3 :(得分:0)
首先你必须进行pakage 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){
})
祝你好运^^。