0.0.2 • Published 8 years ago

mongodb-datamapper v0.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
8 years ago

MongoDb Data Mapper Build Status npm version

What ?

A data mapper is an abstraction on top of a persistence mechanism. It works as a bridge between domain entities and a data store.

Why ?

MongoDb Data Mapper is easier to test alternative to popular Active Record-based ORMs. It achieves so by exposing a minimalist (therefore trivial to mock) API and providing set-up/tear-down methods.

How ?

const coroutine = require('co');
const mongoDataMapper = require('mongodb-datamapper');

const petsDataMapper = mongoDataMapper({
  connectionUri: 'mongodb://localhost/test',
  collectionName: 'pets',
});

coroutine(function* () {
  // Always `initialize` before querying.
  yield petsDataMapper.initialize();

  const herPets = yield petsDataMapper.find({ owner: 'Sabrina' });

  herPets.forEach(p => console.log(p.name));

  // `destroy` mapper once you're done.
  yield petsDataMapper.destroy();
});

Using with your transform-s:

const coroutine = require('co');
const mongoDataMapper = require('mongodb-datamapper');

const student = require('./my-awesome-domain/student');

const studentsDataMapper = mongoDataMapper({
  transform: student,
  connectionUri: 'mongodb://localhost/test',
  collectionName: 'students',
});

coroutine(function* () {
  yield studentsDataMapper.initialize();

  const allStudents = yield studentsDataMapper.find();
  const studentsWithHighScores = allStudents.map(s => s.averageScore > 90);

  studentsWithHighScores.forEach(s => {
    s.showOffGoldenStars(); // Assume `student#showOffGoldenStars`
  });

  yield studentsDataMapper.destroy();
});