Sails js and Sequelize
Asked Answered
F

4

7

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

Fadge answered 18/1, 2014 at 1:17 Comment(0)
L
2

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

Limbourg answered 30/1, 2014 at 10:27 Comment(0)
A
2

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

Aromatize answered 2/8, 2015 at 3:13 Comment(0)
S
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
}

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)");
 }
}
Salleysalli answered 25/1, 2016 at 4:46 Comment(0)
C
0

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

Caitlyncaitrin answered 26/10, 2016 at 6:5 Comment(1)
Our project was started year ago, but now we need to move on sequelize. Can I use both of them in parallel before refactoring be done?Martelli

© 2022 - 2024 — McMap. All rights reserved.