Comparing version 0.0.7 to 0.0.8
@@ -52,2 +52,4 @@ 'use strict'; | ||
crit = this.transform(crit); | ||
var fn = this.collection.count.bind(this.collection), | ||
@@ -79,5 +81,23 @@ args = [], | ||
this.insert = function (docs) { | ||
var fn = this.collection.insert.bind(this.collection), | ||
p = nodeFn.call(fn, docs, { w: 1 }); | ||
var fn = this.collection.insert.bind(this.collection); | ||
if (Array.isArray(docs)) { | ||
if (!docs.length) { | ||
return when(docs); | ||
} | ||
docs = docs.map(function (doc) { | ||
return this.transform(doc); | ||
}, this); | ||
} else { | ||
if (!docs) { | ||
return when(docs); | ||
} | ||
docs = this.transform(docs); | ||
} | ||
var p = nodeFn.call(fn, docs, { w: 1 }); | ||
if (!Array.isArray(docs)) { | ||
@@ -95,2 +115,4 @@ p = p.then(function (docs) { | ||
crit = this.transform(crit); | ||
var fn = this.collection.update.bind(this.collection), | ||
@@ -118,2 +140,3 @@ opts = { w: 1 }; | ||
this.save = function (doc) { | ||
doc = this.transform(doc); | ||
var fn = this.collection.save.bind(this.collection); | ||
@@ -126,2 +149,4 @@ return nodeFn.call(fn, doc, { w: 1 }); | ||
crit = this.transform(crit); | ||
var collection = this.collection, | ||
@@ -128,0 +153,0 @@ args = [], |
'use strict'; | ||
var inflection = require('inflection'), | ||
clone = require('clone'), | ||
extend = require('extend'), | ||
shien = require('shien'), | ||
when = require('when'), | ||
@@ -13,4 +17,6 @@ | ||
BaseModel = require('./base').BaseModel; | ||
BaseModel = require('./base').BaseModel, | ||
toString = Object.prototype.toString; | ||
var ModelCreator = exports.ModelCreator = function (name, db, modelize, opts) { | ||
@@ -245,2 +251,131 @@ var o = opts || {}; | ||
function isString(value) { | ||
return toString.call(value) === '[object String]'; | ||
} | ||
function isObject(value) { | ||
return toString.call(value) === '[object Object]'; | ||
} | ||
function compileMapping(mapping, prefix, compiledPrefix) { | ||
prefix = prefix || ''; | ||
compiledPrefix = compiledPrefix || ''; | ||
var compiled = { _: mapping._ || {} }; | ||
delete mapping._; | ||
delete mapping._$; | ||
for (var key in mapping) { | ||
if (!mapping.hasOwnProperty(key)) { | ||
continue; | ||
} | ||
var value = mapping[key]; | ||
var fullKey = prefix + key; | ||
if (isString(value) && key !== '$') { | ||
if (key === '$') { | ||
compiled[prefix.slice(0, -1)] = compiledPrefix.slice(0, -1); | ||
} | ||
else { | ||
compiled[fullKey] = compiledPrefix + value; | ||
} | ||
} | ||
else if (isObject(value)) { | ||
compiled[fullKey] = compiledPrefix + (value.$ || key); | ||
extend( | ||
compiled, | ||
compileMapping( | ||
value, | ||
fullKey + '.', | ||
compiled[fullKey] + '.' | ||
) | ||
); | ||
} | ||
} | ||
return compiled; | ||
} | ||
function compileInverseMapping(mapping) { | ||
var ret = { _: mapping._$ || {} }; | ||
delete mapping._; | ||
delete mapping._$; | ||
var compiled = compileMapping(mapping); | ||
for (var key in compiled) { | ||
if (compiled.hasOwnProperty(key) && key !== '_') { | ||
ret[compiled[key]] = key; | ||
} | ||
} | ||
return ret; | ||
} | ||
this.map = function (mapping) { | ||
var m1 = clone(mapping); | ||
var m2 = clone(mapping); | ||
shien.assign(this.model, { | ||
mapping: compileMapping(m1), | ||
inverseMapping: compileInverseMapping(m2) | ||
}); | ||
}; | ||
function transform(object, mapping, prefix) { | ||
prefix = prefix || ''; | ||
var transformed = {}; | ||
for (var key in object) { | ||
if (!object.hasOwnProperty(key)) { | ||
continue; | ||
} | ||
var value = object[key]; | ||
var isSystemKey = !key.indexOf('$'); | ||
var fullKey; | ||
if (isSystemKey) { | ||
fullKey = key; | ||
} | ||
else { | ||
fullKey = prefix + key; | ||
if (mapping[fullKey]) { | ||
fullKey = mapping[fullKey]; | ||
} | ||
} | ||
if (!~key.indexOf('.')) { | ||
fullKey = fullKey.split('.').pop(); | ||
} | ||
if (isObject(value) && | ||
!(value instanceof type.ObjectId)) { | ||
value = transform( | ||
value, | ||
mapping, | ||
prefix + (isSystemKey ? '' : key + '.') | ||
); | ||
} | ||
var fn = (mapping._ && mapping._[fullKey]); | ||
if (typeof fn === 'function') { | ||
value = fn(value); | ||
} | ||
transformed[fullKey] = value; | ||
} | ||
return transformed; | ||
} | ||
function defineIdProperty(field, opts) { | ||
@@ -569,2 +704,10 @@ /* jshint validthis: true */ | ||
this.model.transform = function (obj) { | ||
return transform(obj, this.mapping || {}); | ||
}; | ||
this.model.inverseTransform = function (obj) { | ||
return transform(obj, this.inverseMapping || {}); | ||
}; | ||
this.model.prototype.normalize = function () { | ||
@@ -571,0 +714,0 @@ return self.model.normalize(this); |
@@ -42,2 +42,6 @@ 'use strict'; | ||
fields = fields.map(function (field) { | ||
return (this.mapping && this.mapping[field]) || field; | ||
}, this); | ||
this.fields = {}; | ||
@@ -141,3 +145,3 @@ | ||
var args = [ this.criteria ]; | ||
var args = [ this.model.transform(this.criteria) ]; | ||
@@ -217,2 +221,6 @@ if (this.fields) { | ||
if (doc) { | ||
doc = self.model.inverseTransform(doc); | ||
} | ||
if (self.raw || !doc) { | ||
@@ -236,2 +244,11 @@ return resolve(doc); | ||
.then(function (docs) { | ||
if (Array.isArray(docs)) { | ||
docs = docs.map(function (doc) { | ||
return (doc ? self.model.inverseTransform(doc) : doc); | ||
}); | ||
} else if (docs) { | ||
docs = self.model.inverseTransform(docs); | ||
} | ||
if (!Array.isArray(docs) || self.raw) { | ||
@@ -238,0 +255,0 @@ return docs; |
{ | ||
"name": "yari", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Simple MongoDB object modeling library", | ||
@@ -29,6 +29,8 @@ "author": "Meo <i@meo.guru> (http://meo.guru)", | ||
"dependencies": { | ||
"clone": "^0.1.18", | ||
"extend": "^1.3.0", | ||
"inflection": "^1.3.5", | ||
"shien": "^0.0.8", | ||
"when": "^3.1.0", | ||
"mongodb": "^1.4.0" | ||
"mongodb": "^1.4.0", | ||
"shien": "^0.0.9", | ||
"when": "^3.1.0" | ||
}, | ||
@@ -35,0 +37,0 @@ "devDependencies": { |
43676
1205
6
+ Addedclone@^0.1.18
+ Addedextend@^1.3.0
+ Addedclone@0.1.19(transitive)
+ Addedextend@1.3.0(transitive)
+ Addedshien@0.0.9(transitive)
- Removedshien@0.0.8(transitive)
Updatedshien@^0.0.9