我正在学习Node.js,我的框架选择是Sails。
我想在一个使用MySql数据库的项目中使用它,我认为Sequelize ORM更完整。
我应该如何在Sails中使用Sequelize ORM而不是Waterline?
谢谢
谢谢
最近出现了两个项目,我已经努力重新引入对Sequelize的完全支持,包括蓝图。
sails-hook-sequelize
sails-hook-sequelize-blueprints
我认为你可以使用Sequelize,因为Sails与Sequelize共同诞生。
你可以阅读Mike McNeil在这里的回答,并且也许可以直接向Mike询问他是否会重新引入对Sequelize的支持。
$ npm install sails-hook-sequelize
$ npm install sails-hook-sequelize-blueprints
$ npm install sequelize
$ npm install pg pg-hstore
$ npm install continuation-local-storage
"hooks": {
"blueprints": false,
"orm": false,
"pubsub": false
}
somePostgresqlServer: {
user: 'postgres',
password: '',
database: 'database',
options: {
host : 'localhost',
port : 5432,
logging: true
}
}
module.exports = {
attributes: {
firstname: {
type: Sequelize.STRING,
allowNull: false
},
secondname: {
type: Sequelize.STRING,
allowNull: false
},
}
}
};
module.exports = {
dbPath: function () {
return ("postgres://postgres:(user)@localhost:5432/(databasename)");
}
}
首先你必须安装 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){
})
祝你好运 ^^。