Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Map finds as well as updates #23

Merged
merged 4 commits into from

2 participants

@robholland

Avoids casting to ObjectId manually all the time.

Rob Holland added some commits
Rob Holland Correct typo. d910a81
Rob Holland Automatically map array fields. 4041c0b
Rob Holland Map finds as well as updates.
Avoids having to cast things to ObjectId all the time by hand.
553051a
Rob Holland Map the find criteria in updates as well as the update criteria. c9c0efd
@masylum
Owner

not sure 100% about this, but the commit is good and tested so I will release it and see how works on production :)

@masylum
Owner

can you explain this? I don't understand

@robholland

It stops me having to pass ObjectID around inside my applications all the time, because I can use maps to cast it inside the model logic.

I then no longer need to do:

MyModel.mongo('find', { _id: ObjectID(req.params.id) }, ...)

I can just pass req.params.id directly.

@masylum masylum merged commit 4b9724b into from
@masylum
Owner

I will push this to npm once I have some tests written.

Looks good, but not sure if any side effect may occur.
We could also provide a default _id -> ObjectId mapping.

@robholland

The default mapping would be good, that's the main reason I did it. It would need to catch fields named _id at all levels though. It would also have to be optional, not everyone will use ObejctId for _id presumably.

@masylum
Owner

Is already pushed at npm. I didn't include the default mapping though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 7, 2011
  1. Correct typo.

    Rob Holland authored
  2. Automatically map array fields.

    Rob Holland authored
  3. Map finds as well as updates.

    Rob Holland authored
    Avoids having to cast things to ObjectId all the time by hand.
Commits on Jul 8, 2011
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 9 deletions.
  1. +10 −3 lib/helpers/mapper.js
  2. +14 −6 test/helpers/mapper_test.js
View
13 lib/helpers/mapper.js
@@ -52,13 +52,19 @@ MAPPER.filterUpdate = function (maps, arg) {
} else {
is_matching = isMatching(maps, attr, level);
if (is_matching === 2) {
- el[attr] = getObject(maps, attr)(el[attr]);
+ if (Array.isArray(el[attr])) {
+ el[attr].forEach(function(value, i) {
+ el[attr][i] = getObject(maps, attr)(el[attr][i]);
+ });
+ } else {
+ el[attr] = getObject(maps, attr)(el[attr]);
+ }
} else {
if (is_matching === 1 && typeof el[attr] === 'object') {
filter(maps[attr], el[attr], level + 1);
}
}
- }
+ }
}
}
}
@@ -75,9 +81,10 @@ MAPPER.filterUpdate = function (maps, arg) {
};
MAPPER.map = function (maps, fn, args) {
- if (fn === 'insert') {
+ if (fn === 'insert' || fn.match(/^find/)) {
MAPPER.filterUpdate(maps, args[0]);
} else if (fn === 'update') {
+ MAPPER.filterUpdate(maps, args[0]);
MAPPER.filterUpdate(maps, args[1]);
}
};
View
20 test/helpers/mapper_test.js
@@ -1,34 +1,35 @@
var testosterone = require('testosterone')({post: 3000, sync: true, title: 'mongolia/helpers/mapper_test.js'}),
assert = testosterone.assert,
- Namespacer = require('./../../lib/helpers/mapper');
+ Mapper = require('./../../lib/helpers/mapper');
testosterone
.before(function () {
})
.add('`filterUpdate` should filter documents before being inserted or updated', function () {
- var test = Namespacer.filterUpdate
+ var test = Mapper.filterUpdate
, arg
, toUpper = function (val) {
return val.toUpperCase();
}
- , update = {zemba: 'FOO', nested: {id: 123, name: '300'}, foo: true}
+ , update = {zemba: 'FOO', nested: {id: 123, name: '300'}, list: [1,2,3], foo: true}
, maps = { zemba: toUpper
, nested: { id: Number
, name: String
}
+ , list: Number
, foo: Boolean
};
// document
- arg = {zemba: 'foo', nested: {id: '123', name: 300}, foo: 'true'};
+ arg = {zemba: 'foo', nested: {id: '123', name: 300}, list: [1,2,3], foo: 'true'};
test(maps, arg);
assert.deepEqual(arg, update);
assert.equal(typeof arg.nested.id, typeof update.nested.id);
assert.equal(typeof arg.nested.name, typeof update.nested.name);
// array
- arg = [{zemba: 'fleiba'}, {zemba: 'foo', nested: {id: '123', name: 300}, foo: 'true'}];
+ arg = [{zemba: 'fleiba'}, {zemba: 'foo', nested: {id: '123', name: 300}, list: [1,2,3], foo: 'true'}];
test(maps, arg);
assert.deepEqual(arg[0], {zemba: 'FLEIBA'});
assert.deepEqual(arg[1], update);
@@ -36,7 +37,7 @@ testosterone
assert.equal(typeof arg[1].nested.name, typeof update.nested.name);
// with sepcial ops
- arg = {'$set': {zemba: 'foo', nested: {id: '123', name: 300}, foo: 'true'}};
+ arg = {'$set': {zemba: 'foo', nested: {id: '123', name: 300}, list: [1,2,3], foo: 'true'}};
test(maps, arg);
assert.deepEqual(arg, {'$set': update});
assert.equal(typeof arg.$set.nested.id, typeof update.nested.id);
@@ -47,6 +48,13 @@ testosterone
test(maps, arg);
assert.equal(typeof arg.$set['nested.id'], typeof update.nested.id);
assert.equal(typeof arg.$set['nested.name'], typeof update.nested.name);
+
+ // array values
+ arg = {list: ['1', '2', 3]};
+ test(maps, arg);
+ assert.equal(typeof arg.list[0], typeof update.list[0]);
+ assert.equal(typeof arg.list[1], typeof update.list[1]);
+ assert.equal(typeof arg.list[2], typeof update.list[2]);
})
.run();
Something went wrong with that request. Please try again.