Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2014/01/30

You Had ONE Job, Mongoose.js Documentation

Source
I'm trying to learn Node.js, MongoDB and Mongoose.js as an ORM connecting the two. I don't know what I'm doing yet, going through the early example code. I come to this as a reasonably experienced hand with Perl and MySQL. The first thing people write is a "Hello World" application, where you give it an input, commonly your name, and it gives you an output. In data, the four actions you want are Create, Read, Update and Delete, often used as CRUD. When trying out a data-storage system, you want to be able to Hello World your data.

The following is code adapted from the front page of Mongoosejs.com and the first link to documentation.

  1. #!/usr/bin/js  
  2.   
  3. /* 
  4.  * test_mongo.js 
  5.  *  adapted from mongoosejs.com 
  6.  *   
  7.  * Getting up to speed with MongoDB, Mongoose as an ORM, ORMs in general, 
  8.  * and Node.js.  
  9. */  
  10.   
  11. // Testing direct from http://mongoosejs.com/  
  12.   
  13. // initiallizing connection to MongoDB   
  14. var mongoose = require('mongoose');  
  15. mongoose.connect('mongodb://localhost/cats');  
  16. var db = mongoose.connection;  
  17. db.on('error', console.error.bind(console, 'connection error:'));  
  18. db.once('open'function callback () {  
  19.     console.log( "Yay" );  
  20.     });  
  21.   
  22. // Schema - what comes from DB  
  23. var kittySchema = mongoose.Schema({ name: String }) ;   
  24. kittySchema.methods.speak = function () {  
  25.     var greeting = this.name  
  26.         ? "Meow name is " + this.name  
  27.         : "I don't have a name" ;  
  28.     console.log(greeting);  
  29.     } ;  
  30. kittySchema.methods.save = function ( err , cat ) {  
  31.     console.log( cat ) ;  
  32.     if ( err ) {  
  33.         console.log(err) ;  
  34.         }  
  35.     cat.speak() ;  
  36.     } ;  
  37.   
  38. // Model - what we work with  
  39. var Kitten = mongoose.model('Kitten', kittySchema) ;  
  40. Kitten.find(function (err, kittens) {  
  41.     if (err) // TODO handle err  
  42.     console.log(kittens)  
  43.     })  
  44.   
  45. // Objects - instances of models  
  46. var silence = new Kitten({ name: 'Silence' }) ;  
  47. var morris = new Kitten({ name: 'Morris' }) ;  
  48. var fluffy = new Kitten({ name: 'Fluffy' });  
  49. var blank  = new Kitten({ });  
  50.   
  51. console.log( '------------------------' ) ;  
  52. silence.speak() ;  
  53. morris.speak() ;  
  54. fluffy.speak() ;  
  55. blank.speak() ;  
  56. console.log( '------------------------' ) ;  
  57. silence.save() ;  
  58. morris.save() ;  
  59. fluffy.save() ;  
  60. blank.save() ;  
  61. console.log( '------------------------' ) ;  
  62.   
  63. process.exit(0) ;  

save() doesn't seem to save, or do anything, really. speak() works, but why shouldn't it? The reason I'm doing this is so that Mongoose can stand between me and MongoDB. Mongoose has one job. The documentation has one job: to show people me how to do just that.

One Job!

No comments:

Post a Comment