最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Where should I write queries in model or controller (sequelize)? - Stack Overflow

programmeradmin6浏览0评论

I try to figure out where should I write queries in node js application which uses sequalize as an ORM.

For example, I have a model address, and I write data to like this:

let adr = await Address.create({street, number, city, state, country})

Should I write this code in the controller where I get the data or in the model and then just pass (for example full object address) to the method of the model? What is the best practice?

I assume that is it better to write it in models because I can use the same code in many controllers. But maybe there are some other constraints.

I try to figure out where should I write queries in node js application which uses sequalize as an ORM.

For example, I have a model address, and I write data to like this:

let adr = await Address.create({street, number, city, state, country})

Should I write this code in the controller where I get the data or in the model and then just pass (for example full object address) to the method of the model? What is the best practice?

I assume that is it better to write it in models because I can use the same code in many controllers. But maybe there are some other constraints.

Share Improve this question edited Mar 6, 2018 at 13:49 trojek asked Aug 24, 2017 at 10:54 trojektrojek 3,2283 gold badges34 silver badges54 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Actually you should use a repository. The ideea behind is to make a module that will abstract the database, just inject it in as a dependency, and then use the repository in controller a short example can be like this.

// repository.js

module.exports = (db) => {
   const findUserById = (id) => {
        // db query your user by id
   }

   const createUser = (data) {
       // db insert a new user
   }

   // ... other repository functions to deal with the database

   return Object.create({
     findUserById,
     createUser,
     // ...
   })
} 

And then in your controller you will just use the repository by giving the proper db connection.

const repository = require('./repository.js')(dbConnection);

app.get('/users/:id', (req, res) => {
   repository.findUserById(req.params.id);
   // ...
})

And in this way you can easily test your code by mocking the database or even test with good values. And you abstract your db connection and other important features when for example you will need to change your database from mysql to mongodb you will change only the repository not the entire application or controllers.

So a good practice is to use the repository for sequelize there also exist a way to do it.

var db = require('../models');

var UsersRepository = {
    findByEmail: function(email) {
        return db.User.findAll({
            where: {
                email: email
            }
        })
    }
}

module.exports = UsersRepository;
发布评论

评论列表(0)

  1. 暂无评论