modeler-mongodb
Advanced tools
Comparing version 0.0.2 to 0.0.3
24
index.js
@@ -7,2 +7,4 @@ var modeler = require('modeler'); | ||
if (!api.options.db) throw new Error('must pass a node-mongodb-native db with options.db'); | ||
api.options.sort = api.options.sort || '__idx'; | ||
var db = api.options.db | ||
@@ -23,9 +25,11 @@ , collection = db.collection(api.options.name) | ||
(function next () { | ||
var cur = collection.find().skip( skip ? skip : 0); | ||
var sort = {}; | ||
sort[api.options.sort] = reverse ? -1 : 1; | ||
var cur = collection.find({}, { _id: 0, __idx: 0 }).skip( skip ? skip : 0); | ||
limit && cur.limit(limit); | ||
cur.sort({ "__idx": reverse ? -1 : 1 }); | ||
cur.sort(sort); | ||
cur.toArray(function (err, results) { | ||
if (err) return cb(err); | ||
skip += results.length; | ||
cb(null, results.map(api._prepare), next); | ||
cb(null, results, next); | ||
}); | ||
@@ -35,6 +39,2 @@ })(); | ||
api._prepare = function (entity) { | ||
entity && (entity.id = entity._id) && delete entity._id; | ||
return entity; | ||
}; | ||
api._head = function (skip, limit, cb) { | ||
@@ -58,5 +58,7 @@ continuable(skip, limit, false, cb); | ||
var saveEntity = api.copy(entity); | ||
// We need to map `id` to `_id` and remove `id` | ||
// We need `_id` to be the same as `id` because MongoDB and Modeler | ||
// each have their own unique, completely inflexible requirements | ||
saveEntity._id = entity.id; | ||
delete saveEntity.id; | ||
// @todo consider performing a findAndModify using both | ||
// id and rev in the query part where rev > 1 | ||
collection.save(saveEntity, function (err) { | ||
@@ -79,5 +81,5 @@ cb(err); | ||
api._load = function (id, cb) { | ||
collection.findOne({ _id: id }, function (err, entity) { | ||
collection.findOne({ _id: id }, { _id: 0 }, function (err, entity) { | ||
if (err) return cb(err); | ||
cb(err, api._prepare(entity)); | ||
cb(err, entity); | ||
}); | ||
@@ -84,0 +86,0 @@ }; |
{ | ||
"name": "modeler-mongodb", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "MongoDB-powered functional entity system", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
6484
96