Created
April 16, 2015 01:34
-
-
Save karlbright/73518708af1f0d54e1bc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import mongoose from 'mongoose' | |
let CitySchema = new mongoose.Schema({ | |
name: { type: String, required: true }, | |
country: { type: String, required: true }, | |
province: { type: String, required: true }, | |
location: { type: [Number], required: true, index: '2d' } | |
}) | |
CitySchema.pre('validate', next => { | |
console.log(Date.now(), 'validate pre-hook', this) | |
next() | |
}) | |
export default mongoose.model('City', CitySchema) | |
This file contains hidden or 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
import mongoose from 'mongoose' | |
import {mongodb} from '../config' | |
import logger from './logger' | |
var uri = `${mongodb.host}:${mongodb.port}/${mongodb.database}` | |
if(mongodb.username && mongodb.password) { | |
uri = `${mongodb.username}:${mondob.password}@uri` | |
} | |
mongoose.connect('mongodb://' + uri) | |
var database = mongoose.connection | |
database.once('open', logger.info.bind(null, 'Database successfully connected')) | |
database.on('error', (e) => { | |
logger.error('Could not establish connection to database', { | |
uri: uri, | |
error: e | |
}) | |
}) | |
export default database |
This file contains hidden or 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
> var db = require('./lib/support/database') | |
2015-04-16T01:32:50.789Z - info: Config loaded from development.js | |
undefined | |
> 2015-04-16T01:32:50.904Z - info: Database successfully connected | |
> var City = require('./lib/models/city') | |
undefined | |
> var c = new City({name: "Perth", country: "Australia", province: "Western Australia" }) | |
undefined | |
> c.save() | |
1429148012090 'validate pre-hook' undefined | |
{ emitter: | |
{ domain: null, | |
_events: { reject: [Function] }, | |
_maxListeners: 10 }, | |
emitted: {}, | |
ended: true } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment