Mino
Mino

Reputation: 635

Sails js and Sequelize

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

Upvotes: 7

Views: 5295

Answers (4)

thanh1101681
thanh1101681

Reputation: 302

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 ^^.

Upvotes: 0

Nitheesh K P
Nitheesh K P

Reputation: 1110

$ 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)");
 }
}

Upvotes: 1

FXCesinha
FXCesinha

Reputation: 403

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

See my answer

Upvotes: 2

CB Stilborg
CB Stilborg

Reputation: 136

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

Upvotes: 2

Related Questions