ku4node-persistence

kodmunki persistence

#ku4jQuery-persistence

kodmunki™ utilities for Node persistence.

This project defines a persistence architecture for Node projects. The persistence architecture defines abstract repositories and entities. These abstract classes will be sub-subclassed in dependent projects. This project simplifies Node persistence and defines a logical and rational architecture upon which developers can build their project's persistence layers.


#Setup To import ku4node-persistence into your Node project simply:

  1. ku4node-persistence is currently dependent upon MongoDB. Include the latest mongo in your project. Read more on including dependencies in your package.json here.
  2. Include the latest ku4node-persistence in your project. Read more on including dependencies in your package.json here.
  3. Add a baseRepository to your project or subclass abstractRepository
  4. Implement the appropriate Unit Tests.
  5. Implement your subclass so that all of your Unit Tests pass.

#Documentation The following is the documentation is for the classes included in the ku4node-persistence project.

##entity Entity class is an abstract class. Subclasses will have access to the following protected methods:

  • $bsonId(value): Returns a new BSON ID using value as a seed if it is passed. The seed value must adhere to the requirements set forth by MongoDB ObjectId
  • toDto(): Retrieves the entity as a DTO object. Useful for passing the entity back to the client.
  • toObject(): Retrieves entity as a JavaScript object. Useful for passing the entity to a ku4node-persistence repository.
var $kernel = require("ku4node-kernel"),
    $persistence = require("ku4node-persistence"),
    $ = $kernel.asserters,
    $class = $kernel.class,
    $entity = $persistence.entity;
 
function person(idfirstNamelastName) {
    person.base.call(this, id, {
        "firstName": firstName,
        "lastName": lastName
    });
}
$class.extend(person, $entity);
 
module.exports = function(idfirstNamelastName){
    return new account(id, firstName, lastName);
};
module.exports.parse = function (dto) {
    var obj = ($.exists(dto.toObject)) ? dto.toObject() : dto;
    return new account(obj._id, obj.firstName, obj.lastName);
};

It is helpful to copy the skeleton subclass below to begin your subclass until you have become familiar with the class skeleton.

var $kernel = require("ku4node-kernel"),
    $persistence = require("ku4node-persistence"),
    $ = $kernel.asserters,
    $class = $kernel.class,
    $entity = $persistence.entity;
 
function NAME(id/*yourotherargumentshere*/) {
    NAME.base.call(this, id, {
        //your other properties here 
    });
}
$class.extend(NAME, $entity);
 
module.exports = function(id/*yourotherargumentshere*/){
    return new NAME(id/*, your, other, arguments, here*/);
};
module.exports.parse = function (dto) {
    var obj = ($.exists(dto.toObject)) ? dto.toObject() : dto;
    return new NAME(obj._id/*, obj.your, obj.other, obj.arguments, obj.here*/);
};

##abstractRepository AbstractRepository is an abstract class. Subclasses have access to the following protected methods:

  • $bsonId(value): Returns a new BSON ID using value as a seed if it is passed. The seed value must adhere to the requirements set forth by MongoDB ObjectId
  • $findAll(callback, options) Calls back with all records in the repository
  • $findByQuery(query, projection, callback, options) Calls back with all records in the repository
  • $create(document, callback, options) Creates a new document and calls back with the document creates
  • $update(document, newDocument, callback, options) updates the the document with properties defined in newDocument argument
  • $remove(document, callback, options) removes all documents that contain the properties defined in document argument
var $kernel = require("ku4node-kernel"),
    $persistence = require("ku4node-persistence"),
    $ = $kernel.asserters,
    $class= $kernel.class,
    $hash = $kernel.collections.hash,
    $abstractRepository = require("./abstractRepository");
 
function myRepository(databasecollection) {
    myRepository.base.call(this, database, collection);
}
myRepository.prototype = {
    createfunction(itemfunc) {
        this.$create(item, function (errcollection) {
            var item = ($.exists(collection)) ? collection[0] : null;
            func(err, item);
        }, {safe:true});
    },
    findfunction(criteriafunc) {
        this.$findByQuery(criteria, null, func);
    },
    findByIdfunction(idfunc) {
        this.find({"_id": id}, function (errcollection) {
            var item = ($.exists(collection)) ? collection[0] : null;
            func(err, item);
        });
    },
    updatefunction(itemfunc) {
        var id = item._id,
            update = $hash(item).remove("_id").toObject();
        this.$update({ "_id": id }, update, function (err) {
            func(err, $hash(item).add("_id", id).toObject());
        });
    },
    removefunction(itemfunc) {
        this.$remove(item, func);
    }
}
$class.extend(myRepository, $abstractRepository);
modeule.exports = function(databasecollection) {
    return new myRepository(database, collection);
}

##baseRepository

BaseRepository is a basic subclass of abstractRepository. It has access to the following protected methods (See abstractRepository

  • create(item, func)
  • find(criteria, func)
  • findById(id, func)
  • update(item, func)
  • remove(item, func)
//Examples can be found in the Unit Tests for this project 

#Dependencies

This project requires the following dependencies:

##Gotchas!

  1. ku4node-persistence projects depend upon a working mongoDB. When running the Unit Tests for a ku4node-persistence project you will have to start your relevant mongodb instance and ensure that your database configurations are correct. For more information see the databaseStub.js and associated Unit Tests found in this project at /tests/stubs/databaseStub.js