I'm learning Node.js and Sails is my framework of choice. I want to use it in a project with MySql db and I think that Sequelize Orm is more complete. How can I use Sequelize Orm in Sails instead of Waterline?
Thanks
I'm learning Node.js and Sails is my framework of choice. I want to use it in a project with MySql db and I think that Sequelize Orm is more complete. How can I use Sequelize Orm in Sails instead of Waterline?
Thanks
I think you can as sails was born with sequelize.
You can read Mike McNeil's answer here and maybe ask Mike directly if he will reintroduce sequelize support
There are two project that come out recently that i've have worked to reintroduce full support to sequelize, including blueprints.
sails-hook-sequelize
sails-hook-sequelize-blueprints
$ 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
}
In connections.js
somePostgresqlServer: {
user: 'postgres',
password: '',
database: 'database',
options: {
host : 'localhost',
port : 5432,
logging: true
}
}
In model folder
// for example let take userstable ->users.js
module.exports = {
attributes: {
firstname: {
type: Sequelize.STRING,
allowNull: false
},
secondname: {
type: Sequelize.STRING,
allowNull: false
},
}
}
};
or another method for connection create seperate file db.js
module.exports = {
dbPath: function () {
return ("postgres://postgres:(user)@localhost:5432/(databasename)");
}
}
First you must intsall pakage sails-hook-sequelize:
npm install sails-hook-sequelize --save
Seconds edit file .sailsrc
"hooks": {
"orm": false,
"pubsub": false
}
file ./config/models.js
module.exports.models = {
schema: true,
connection: 'mysql',
migrate: 'safe'
};
file ./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'
}
}
};
define models in ./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: {}
}
};
Final, use model:
UserAccount.findAll({}).then(function(success){}, function(err){
})
Good luck ^^.
© 2022 - 2024 — McMap. All rights reserved.