Where to put data access code?

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.

  • 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;
  • 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.
  • Where do you think database access code for Node.js MVC web apps?

    Come back ActiveRecord, all is forgiven.


    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.

    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.

    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

    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

    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

    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.

    链接地址: http://www.djcxy.com/p/11502.html

    上一篇: 参数对象是否泄漏?

    下一篇: 数据访问代码放在哪里?