我正在学习 Node.js,而 Sails 是我选择的框架。我想在一个带有 MySql db 的项目中使用它,我认为 Sequelize Orm 更完整。如何在 Sails 中使用 Sequelize Orm 而不是 Waterline?
谢谢
我正在学习 Node.js,而 Sails 是我选择的框架。我想在一个带有 MySql db 的项目中使用它,我认为 Sequelize Orm 更完整。如何在 Sails 中使用 Sequelize Orm 而不是 Waterline?
谢谢
我想你可以像风帆一样天生就有续集。
您可以在此处阅读Mike McNeil 的回答,也可以直接询问 Mike 他是否会重新引入续集支持
$ 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)");
}
}
首先你必须安装包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){
})
祝你好运^^。