http://mongoosejs.com (GitHub)에 대하여 알아보자. (mongoose의 light-weight ORM version으로 몽고리안-mongolian-도 존재한다)
1) mongoose의 schema는 extension = controller 이다 : Node.js 소스
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/mydb');
var UserSchema = new Schema({ // MVC controller
age : Number,
name: String,
sex: Boolean
});
var UserModel = mongoose.model('users', UserSchema); // Model ---> collection
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.param('name', function (req, res, next, name) {
UserModel.find({ name: name }, function (err, docs) {
req.user = docs[0];
next();
});
});
app.get('/users/:name', function (req, res) {
// res.json({ user: req.user });
res.render('./show',{ title: 'Express User',user: req.user });
});
app.get('/users/:name/edit', function (req, res) {
res.render('./edit',{ title: 'Express User',user: req.user });
});
app.get('/users', function (req, res) {
// res.json({ user: req.user });
UserModel.find({},function(err,docs) {
res.render('./users', { title: 'Express User', users: docs });
});
});
app.get('/user/new', function (req, res) {
res.render('new',{layout:false}); //,user:{name:'',age:0,sex:false}}); //,{ title: 'Express User'});
});
app.del('/users/:name', function (req, res) {
UserModel.remove({ name: req.params.name }, function (err) {
res.redirect('/users');
});
});
app.post('/users', function (req, res) {
var b = req.body;
new UserModel({
name: b.name,
age: b.age,
sex:b.sex
}).save(function (err, user) {
if (err) res.json(err);
res.redirect('/users/' + user.name);
});
});
app.put('/users/:name/edit', function (req, res) {
var b = req.body;
UserModel.update(
{ name: req.params.name },
{ name: b.name, age: b.age, sex: b.sex },
function (err) {
res.redirect('/users/' + b.name);
}
);
});
app.listen(process.env.port || 3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
- Jade 소스
h1 Edit User
form(method='POST', action('/users/#{user.name}'))
input(type='hidden',name='_method',value='PUT')
p Name:
input#name(type='text', name='name', value='#{user.name}')
p Age:
input#age(type='number', name='age', value='#{user.age}')
p Sex:
input#sex(type='boolean', name='sex', value='#{user.sex}')
p
button(type='submit') Update
h1 New User
form(method='POST', action= '/users')
p Name:
input#name(type='text',name='name')
p Age:
input#age(type='number',name='age')
p Sex:
input#sex(type='boolean',name='sex')
p
button(type='submit') Create
h1 #{user.name}
ul
li Age: #{user.age}
li Sex: #{user.sex}
ul
li: a(href='/users/#{user.name}/edit') Edit
li
form(action='/users/#{user.name}' , method='POST')
input(type='hidden',name='_method',value='DELETE')
button(type='submit') Delete
h1 All Users
ul
each user in users
li: a(href='/users/#{user.name}') #{user.name} #{user.age} #{user.sex}
2) v3에서 변경된 것들
< 참조 >
- Mongoose 2.3.9 온라인 매뉴얼
http://nodejs-kr.org/insidejs/archives/536
- Mongoose : Elegant MongoDB object modeling for Node.js (GitHub)
- Node.js
http://www.mongodb.org/display/DOCS/Node.js
- masylum / mongolia
'MongoDB' 카테고리의 다른 글
[MongoDB] Ubuntu 에 설치하기 (0) | 2012.11.14 |
---|