Node.JS MVC - 在哪里放置数据访问代码?

时间:2021-11-16 14:07:15

I have a few small Node.js / Express applications up and running now, but I've never been satisfied with any of the solutions for database access.

我现在已经启动并运行了一些小型Node.js / Express应用程序,但我从未对任何数据库访问解决方案感到满意。

  • I started with 'fat controllers' that included database access;
  • 我从'胖控制器'开始,包括数据库访问;

  • I tried 'fat models' using Mongoose, but this heavily pollutes the models with database access stuff;
  • 我尝试使用Mongoose的“胖模型”,但这严重污染了具有数据库访问功能的模型;

  • I tried a kind of DAO pattern that I would implement with Java and turned my models back into value objects to move data around, but that doesn't feel very idiomatic.
  • 我尝试了一种DAO模式,我将用Java实现,并将我的模型转换回值对象来移动数据,但这并不是非常惯用。

Where do you think database access code for Node.js MVC web apps?

您认为Node.js MVC Web应用程序的数据库访问代码在哪里?

Come back ActiveRecord, all is forgiven.

回到ActiveRecord,一切都被原谅了。

1 个解决方案

#1


2  

In my opinion it really depends on what kind of datbase arch you are working with. When using MySQL I usually use them in the controller, while when using MongoDB I put them into the models as it feels more natural. To be perfectly honest, as NodeJS is more of a upgraded javascript, MVC is all about the definition.

在我看来,这实际上取决于您正在使用的数据库拱。当使用MySQL时,我通常在控制器中使用它们,而在使用MongoDB时,我将它们放入模型中,因为感觉更自然。说实话,由于NodeJS更像是一个升级的javascript,MVC就是定义。

When thinking about MVC and the structure of NodeJS via NPM once could easily think about shifting a database access into a new module. I also am not totally satisfied with the way you implement your database access in nodeJS.

当通过NPM思考MVC和NodeJS的结构时,很容易想到将数据库访问转移到新模块中。我对nodeJS中实现数据库访问的方式也不是很满意。

When using Express though we usually apply a RESTful interface and use routes to populate the CRUD operations. My applications have never been that big, that such a behaviour would have been to overwhelmingly large, but for big projects one might want to structure routes likes this

虽然我们通常应用RESTful接口并使用路由来填充CRUD操作,但在使用Express时。我的应用程序从来没有那么大,这样的行为会非常大,但对于大型项目,人们可能想要构建像这样的路由

app.get('/api/item', function(req, res){
 //access to your API and do database business
});

to a more suitable and organized form by splitting them into a datbase access file called datbase.js with that can be loaded in your main app via

通过将它们分成一个名为datbase.js的数据库访问文件,可以将其加载到主应用程序中

require('./datbase.js').setupDatabase('localhost', port);

For example such a database organizer could look like

例如,这样的数据库组织器可能看起来像

function setupDatabase(address, port) {
  //connect to your datbase
  //access to your API and do database business
 });
}

module.exports.setupDatabase = setupDatabase;

I have personally never tried this in such a way, but in my opinion this could help you structure the way you access your database even better. Hope this helped!

我个人从未尝试过这种方式,但在我看来,这可以帮助您构建更好地访问数据库的方式。希望这有帮助!

Edit As an implementation of a CRUD was requested Im posting one with Mongoose. You can exchange that with every other DB that you are working with

编辑作为CRUD的实现被请求我发布一个与Mongoose。您可以与正在使用的其他每个数据库进行交换

var application_root = __dirname,
 express = require("express"),
 path = require("path"),
 mongoose = require('mongoose');

var app = express();

mongoose.connect('mongodb://localhost/my_database');

var Item = mongoose.model('Item', new mongoose.Schema({
  text: String
}));

app.configure(function(){
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(application_root, "public")));
});

app.get('/', function(req, res){
  res.send('Hello World');
});

app.get('/item', function(req, res){
  res.render('item', {title: "Your App"});
});

app.get('/api/items', function(req, res){
  return Item.find(function(err, items) {
    return res.send(items);
  });
});

app.get('/api/items/:id', function(req, res){
  return Item.findById(req.params.id, function(err, item) {
    if (!err) {
      return res.send(item);
    }
  });
});

//etc.

All of those CRUD operations are there for your model that you introducted in your Backbone app and connect it with the MongoDB. Instead of the general paths to your MongoDB you could also use other packages to create a mysql query by using NPM and install nodejs-mysql-native which I found to be very useful at times.

所有这些CRUD操作都适用于您在Backbone应用程序中引入并将其与MongoDB连接的模型。除了MongoDB的一般路径,您还可以使用其他包来创建一个mysql查询,使用NPM并安装nodejs-mysql-native,我发现它有时非常有用。

#1


2  

In my opinion it really depends on what kind of datbase arch you are working with. When using MySQL I usually use them in the controller, while when using MongoDB I put them into the models as it feels more natural. To be perfectly honest, as NodeJS is more of a upgraded javascript, MVC is all about the definition.

在我看来,这实际上取决于您正在使用的数据库拱。当使用MySQL时,我通常在控制器中使用它们,而在使用MongoDB时,我将它们放入模型中,因为感觉更自然。说实话,由于NodeJS更像是一个升级的javascript,MVC就是定义。

When thinking about MVC and the structure of NodeJS via NPM once could easily think about shifting a database access into a new module. I also am not totally satisfied with the way you implement your database access in nodeJS.

当通过NPM思考MVC和NodeJS的结构时,很容易想到将数据库访问转移到新模块中。我对nodeJS中实现数据库访问的方式也不是很满意。

When using Express though we usually apply a RESTful interface and use routes to populate the CRUD operations. My applications have never been that big, that such a behaviour would have been to overwhelmingly large, but for big projects one might want to structure routes likes this

虽然我们通常应用RESTful接口并使用路由来填充CRUD操作,但在使用Express时。我的应用程序从来没有那么大,这样的行为会非常大,但对于大型项目,人们可能想要构建像这样的路由

app.get('/api/item', function(req, res){
 //access to your API and do database business
});

to a more suitable and organized form by splitting them into a datbase access file called datbase.js with that can be loaded in your main app via

通过将它们分成一个名为datbase.js的数据库访问文件,可以将其加载到主应用程序中

require('./datbase.js').setupDatabase('localhost', port);

For example such a database organizer could look like

例如,这样的数据库组织器可能看起来像

function setupDatabase(address, port) {
  //connect to your datbase
  //access to your API and do database business
 });
}

module.exports.setupDatabase = setupDatabase;

I have personally never tried this in such a way, but in my opinion this could help you structure the way you access your database even better. Hope this helped!

我个人从未尝试过这种方式,但在我看来,这可以帮助您构建更好地访问数据库的方式。希望这有帮助!

Edit As an implementation of a CRUD was requested Im posting one with Mongoose. You can exchange that with every other DB that you are working with

编辑作为CRUD的实现被请求我发布一个与Mongoose。您可以与正在使用的其他每个数据库进行交换

var application_root = __dirname,
 express = require("express"),
 path = require("path"),
 mongoose = require('mongoose');

var app = express();

mongoose.connect('mongodb://localhost/my_database');

var Item = mongoose.model('Item', new mongoose.Schema({
  text: String
}));

app.configure(function(){
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(application_root, "public")));
});

app.get('/', function(req, res){
  res.send('Hello World');
});

app.get('/item', function(req, res){
  res.render('item', {title: "Your App"});
});

app.get('/api/items', function(req, res){
  return Item.find(function(err, items) {
    return res.send(items);
  });
});

app.get('/api/items/:id', function(req, res){
  return Item.findById(req.params.id, function(err, item) {
    if (!err) {
      return res.send(item);
    }
  });
});

//etc.

All of those CRUD operations are there for your model that you introducted in your Backbone app and connect it with the MongoDB. Instead of the general paths to your MongoDB you could also use other packages to create a mysql query by using NPM and install nodejs-mysql-native which I found to be very useful at times.

所有这些CRUD操作都适用于您在Backbone应用程序中引入并将其与MongoDB连接的模型。除了MongoDB的一般路径,您还可以使用其他包来创建一个mysql查询,使用NPM并安装nodejs-mysql-native,我发现它有时非常有用。