An object-modeling framework for mongodb and redis.
This software is actively under development and should not yet be used in production environments!
Originally created by Max Seiden.
Currently maintained by Jamie Pitts.
First install node.js, mongodb 2.x, and redis 2.x.
npm install rapt-modelrizerly
First, an application context must be defined. It contains a winston logger instance and configuration data:
var Winston = require('winston'),
logger = new (Winston.Logger) ({ });
var app_context = {
logger: logger,
log: function (message, attr) {
logger.log(attr.level, message, attr);
},
config: {
mongodb: {
host: '127.0.0.1',
port: 27017,
db: 'rapt_modelrizerly_demo'
},
redis: {
host: '127.0.0.1',
port: 6379,
options: {}
}
}
};
Initialization with the app_context occurs after the models are loaded:
var Modelrizerly = require("modelrizerly"),
ModelLoader = require("./models/bootloader"),
Models = Modelrizerly.init({ context: app_context });
After initialization, you can then interact with the database:
Models.Lyric().find({}, {}, function(err, lyrics) {
for (var i=0; i<lyrics.length; i++) {
console.log(lyrics[i].title);
}
});
The basics:
var Modelrizerly = require("modelrizerly");
var Lyric = Modelrizerly.define_model("lyric", function() {
var model = this;
model.db_driver("mongodb");
// identity
model.pk_name("_id");
// attributes
model.string('title');
model.string('author');
model.string('words');
model.number('word_count');
model.number('line_count');
model.boolean('is_public');
});
Query with mongodb operators:
var query = { created_at: { $gt: since_epoch } };
var query_options = { sort: {title: 1} };
Models.Lyric().find(query, query_options, function(err, lyrics) {
for (var i=0; i<lyrics.length; i++) {
console.log(lyrics[i].title);
}
});
Models.Lyric({ _id: mongoid_as_string }).read(null, function(err, lyric) {
console.log(lyric.title);
});
var new_lyric = {title:'My Lyric', author:'The Source', words:'My words are few.'};
Models.Lyric(new_lyric).create(function(err, created_lyric) {
console.log(created_lyric.pk_value());
});
var lyric_update = {title: 'My Lyricism'};
Models.Lyric({ _id: mongoid_as_string }).read(null, function(err, lyric) {
lyric.update({ query: '$set': lyric_update }, function (err, updated_lyric) {
console.log(updated_lyric.title);
});
});
Models.Lyric({ _id: mongoid_as_string }).destroy(function(err) {
console.log('deleted!');
});
See the mongodb example web application.