Nodejs and Mongoose

| Comments

Onto the next step on my journey learning Node.

I’m going to create registration system for informale care volunteers. Volunteers can sign in and provide all sort of details about themselves. If you are a volunteer and you want to go on vacation or just need some rest, you can browse the directory and find a replacement volounteer.

We basically need an addressbook of volunteers.

The first functional specs are:
* we need a volunteer
* the volunteer can have a status like available or unavailable
* the volunteer has all sorts of charecteristics which he or she may edit herself.
* the volunteer should be able to logon and may only chanqe her or his own details.

We also have some technical requirements
* we want to write one language for the whole stack
* the app should be platform indepent, lightweight and open source

We can meet the tech reqs by using a webserver running on node.js. For the database we choose MongoDb and Mongoose for the ORM, because these seem to be the most popular.

Designing the API

/volunteers/1234:
GET: show volunteer 1234
PUT: if exists updates volunteer 1234
DELETE: delete volunteer 1234
POST: will not work

/volunteers:
GET: read
PUT: bulk update volunteers
DELETE: deletes all volunteers
POST: creates new volunteer

The first tests

1
2
3
4
5
6
7
8
mkdir iVolunteer
cd iVolunteer
npm install mocha
npm install should
npm install mongoose

mkdir test
vim volunteertest.js

I studied the Mongoose and Mocha sites thoroughly to get this. For me it is better to read the documentation from the code maintainer, because then I know I’m using the right syntax.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var should = require('should');
var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var volunteerSchema = new Schema({
  firstname:  String,
  lastname: String
});


var Volunteer = mongoose.model('Volunteer', volunteerSchema);
mongoose.connect('mongodb://localhost:27017/betadb');

describe('Volunteer', function(){

  beforeEach(function(done){
    //clean the database:
    Volunteer.remove(done);
  });

  describe('#save()', function() {
    it('should save', function(done) {
      var volunteer = new Volunteer({firstname: 'Joe' })
        volunteer.save(function(err) {
          if (err) return done(err);
          //assert.equal(volunteer.firstname,'Joe');
          volunteer.should.have.property('firstname','Joe');
          done();
        });
      });
   });
});

Comments