-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MongoDB support, drones and associated states saved to DB and r…
…estarted automatically along with Ishiki, drones stdout and stderr logged to DB against app/user
- Loading branch information
Hadrien Jouet
committed
Jan 27, 2013
1 parent
4490e79
commit 0c17c1c
Showing
8 changed files
with
288 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
var Bson = require('mongodb').BSONPure; | ||
|
||
var BaseModel = exports.BaseModel = function(options) { | ||
options = options || {}; | ||
|
||
this.collection = options.collection || ''; | ||
}; | ||
|
||
//retrieve one entry if id passed, or several if {} passed | ||
BaseModel.prototype.get = function(filter, callback) { | ||
if (!callback) { | ||
callback = filter; | ||
filter = {}; | ||
} | ||
|
||
db.collection(this.collection, function(err, collection) { | ||
if (err) { | ||
callback(err); | ||
}else{ | ||
if (typeof filter == 'string') | ||
{ | ||
collection.findOne({_id: new Bson.ObjectID(filter)}, function(err, data) { | ||
if (err) | ||
callback(err); | ||
else | ||
callback(null, data); | ||
}); | ||
} | ||
else if (typeof filter == 'object') { | ||
collection.find(filter).toArray(function(err, data) { | ||
if (err) | ||
callback(err); | ||
else | ||
callback(null, data); | ||
}); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
//add entry | ||
BaseModel.prototype.add = function(data, callback) { | ||
db.collection(this.collection, function(err, collection) { | ||
if (err) { | ||
callback(err); | ||
}else{ | ||
collection.insert(data, function(err, result) { | ||
if (err) | ||
callback(err); | ||
else | ||
callback(null, result); | ||
}) | ||
} | ||
}); | ||
}; | ||
|
||
//update entry | ||
BaseModel.prototype.edit = function(id, data, callback) { | ||
db.collection(this.collection, function(err, collection) { | ||
if (err) { | ||
callback(err); | ||
}else{ | ||
collection.update({_id: new Bson.ObjectID(id)}, data, {safe: true}, function(err, result) { | ||
if (err) | ||
callback(err); | ||
else | ||
callback(null, result); | ||
}); | ||
} | ||
}) | ||
}; | ||
|
||
//delete entry | ||
BaseModel.prototype.delete = function(id, callback) { | ||
db.collection(this.collection, function(err, collection) { | ||
if (err) { | ||
callback(err); | ||
}else{ | ||
collection.remove({_id: Bson.ObjectID(id)}, {safe: true}, function(err, result) { | ||
if (err) | ||
callback(err); | ||
else | ||
callback(null, result); | ||
}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
var BaseModel = require('./_base').BaseModel, | ||
util = require('util'); | ||
|
||
//mongodb illegal key chars | ||
var illegal_chars = ['/', '\\', '.', ' ', '"', '*', '<', '>', ':', '|', '?'], | ||
escape_with = '__'; | ||
|
||
//processes mongodb illegal characters in keys | ||
//pre to true preprocesses, otherwise postprocesses | ||
BaseModel.prototype.process = function(data, pre) { | ||
var self = this; | ||
|
||
Object.keys(data).forEach(function(key) { | ||
var new_key = key; | ||
|
||
illegal_chars.forEach(function(c, i) { | ||
var from = pre ? c : escape_with + i + escape_with, | ||
to = pre ? escape_with + i + escape_with : c; | ||
|
||
if (key.indexOf(from) !== -1) { | ||
new_key = new_key.split(from).join(to); | ||
} | ||
}); | ||
|
||
if (new_key != key) { | ||
data[new_key] = data[key]; | ||
delete data[key]; | ||
key = new_key; | ||
} | ||
|
||
if (data[key] && typeof data[key] == 'object') | ||
data[key] = self.process(data[key], pre); | ||
}); | ||
|
||
return data; | ||
}; | ||
|
||
BaseModel.prototype.createOrUpdate = function(pkg, callback) { | ||
var self = this; | ||
|
||
if (pkg._id) delete pkg._id; | ||
|
||
this.get({name: pkg.name, user: pkg.user}, function(err, result) { | ||
if (err) | ||
return callback(err); | ||
|
||
pkg = self.process(pkg, true); | ||
|
||
if (result.length == 1) { | ||
self.edit(result[0]._id.toString(), {$set: pkg}, function(err, success) { | ||
if (err) | ||
return callback(err); | ||
|
||
callback(null, self.process(pkg, false)); | ||
}); | ||
}else{ | ||
self.add(pkg, function(err, result) { | ||
if (err) | ||
return callback(err); | ||
|
||
callback(null, self.process(result[0], false)); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = new BaseModel({collection: 'drone'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var BaseModel = require('./_base').BaseModel; | ||
|
||
module.exports = new BaseModel({collection: 'log'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters