Forked from suissa/mongoose-connection-best-practices
Last active
August 29, 2015 14:14
-
-
Save fguisso/0ad28cd6ca9d1eb82240 to your computer and use it in GitHub Desktop.
mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
, mongoose = require('mongoose') | |
, dbURI = 'mongodb://localhost/boilerplate' | |
, db = mongoose.connection | |
; | |
mongoose.connect(dbURI); | |
// CONNECTION EVENTS | |
// Quando a conexão der certo | |
db.on('connected', function () { | |
console.log('Conexão mongoose aberta com ' + dbURI); | |
}); | |
// Se a conexão apresentar erros | |
db.on('error',function (err) { | |
console.log('Error: ' + err); | |
}); | |
// Quando for desconectar | |
db.on('disconnected', function () { | |
console.log('Conexão mongoose desconectada'); | |
}); | |
// Quando a conexão abrir | |
db.on('open', function () { | |
console.log('Conexão mongoose aberta'); | |
}); | |
// Se o processo Node finalizar, fechar a conexão do Mongosse | |
process.on('SIGINT', function() { | |
db.close(function () { | |
console.log('Conexão mongoose fechada porque o processo node foi terminado'); | |
process.exit(0); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bring Mongoose into the app | |
var mongoose = require( 'mongoose' ); | |
// Build the connection string | |
var dbURI = 'mongodb://localhost/mongoose-best-practices'; | |
// Create the database connection | |
mongoose.connect(dbURI); | |
// CONNECTION EVENTS | |
// When successfully connected | |
mongoose.connection.on('connected', function () { | |
console.log('Mongoose default connection open to ' + dbURI); | |
}); | |
// If the connection throws an error | |
mongoose.connection.on('error',function (err) { | |
console.log('Mongoose default connection error: ' + err); | |
}); | |
// When the connection is disconnected | |
mongoose.connection.on('disconnected', function () { | |
console.log('Mongoose default connection disconnected'); | |
}); | |
// When the connection is open | |
mongoose.connection.on('open', function () { | |
console.log('Mongoose default connection is open'); | |
}); | |
// If the Node process ends, close the Mongoose connection | |
process.on('SIGINT', function() { | |
mongoose.connection.close(function () { | |
console.log('Mongoose default connection disconnected through app termination'); | |
process.exit(0); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Schema | |
var Schema = mongoose.Schema | |
, registerSchema = new Schema({ | |
name: { type: String, default: '', required: true } | |
, date: { type: String, default: '', required: true } | |
, email: { type: String, default: '', required: true} | |
, created_at: { type: Date, default: Date.now } | |
}); | |
// | |
var regUser = mongoose.model('Users', registerSchema); | |
// | |
var dados = { | |
name: req.param('name') | |
, date : req.param('date') | |
, email : req.param('email') | |
}; | |
var newDate = new regUser(dados) | |
, msg = '' | |
; | |
newDate.save(function(err, data){ | |
if(err){ | |
console.log('Erro: ', err); | |
msg = 'Erro: ' + err; | |
}else{ | |
console.log('Usuario cadastrado: ', data); | |
msg = 'Usuario cadastrado: ' + JSON.stringify(data); | |
} | |
}); | |
res.end(msg); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment