Skip to content

Instantly share code, notes, and snippets.

@wereHamster
Created March 14, 2011 19:16

Revisions

  1. wereHamster created this gist Mar 14, 2011.
    34 changes: 34 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@

    var mongoose = require('../lib/mongoose')
    mongoose.connect('mongodb://localhost/test');

    var Document = null, DocumentSchema = new mongoose.Schema({
    data: { type: String },
    });

    DocumentSchema.pre('init', function(next) {
    console.log('pre-init: ' + JSON.stringify(this));
    next();
    });

    DocumentSchema.post('init', function() {
    console.log('post-init: ' + JSON.stringify(this));
    });

    DocumentSchema.method({
    fubar: function(hint) {
    console.log(hint + ': ' + JSON.stringify(this));
    },
    });

    mongoose.model('Document', DocumentSchema);
    Document = mongoose.model('Document');

    var doc = new Document({ data: 'this is a document' });
    doc.fubar('fresh document');

    doc.save(function(err) {
    Document.findById(doc._id, function(err, doc) {
    doc.fubar('from database');
    });
    })