hydrate-mongodb
Version:
An Object Document Mapper (ODM) for MongoDB.
144 lines (143 loc) • 6 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var async = require("async");
var mappingBase_1 = require("./mappingBase");
var mappingModel_1 = require("./mappingModel");
var persistenceError_1 = require("../persistenceError");
var ArrayMapping = (function (_super) {
__extends(ArrayMapping, _super);
function ArrayMapping(elementMapping) {
var _this = _super.call(this, 1) || this;
_this.elementMapping = elementMapping;
return _this;
}
ArrayMapping.prototype.read = function (context, value) {
if (value == null)
return null;
if (!Array.isArray(value)) {
context.addError("Expected array.");
return;
}
var result = new Array(value.length), mapping = this.elementMapping;
for (var i = 0, l = value.length; i < l; i++) {
result[i] = mapping.read(context, value[i]);
}
if (context.observer) {
context.observer.watch(result);
}
return result;
};
ArrayMapping.prototype.write = function (context, value) {
if (value == null)
return null;
if (!Array.isArray(value)) {
context.addError("Expected array.");
return;
}
var result = new Array(value.length), mapping = this.elementMapping;
for (var i = 0, l = value.length; i < l; i++) {
result[i] = mapping.write(context, value[i]);
}
return result;
};
ArrayMapping.prototype.watch = function (value, observer, visited) {
if (!value || !Array.isArray(value))
return;
observer.watch(value);
for (var i = 0, l = value.length; i < l; i++) {
this.elementMapping.watch(value[i], observer, visited);
}
};
ArrayMapping.prototype.areEqual = function (documentValue1, documentValue2) {
if (documentValue1 === documentValue2)
return true;
if (documentValue1 == null || documentValue2 == null)
return false;
if (!Array.isArray(documentValue1) || !Array.isArray(documentValue2) || documentValue1.length != documentValue2.length) {
return false;
}
var mapping = this.elementMapping;
for (var i = 0, l = documentValue1.length; i < l; i++) {
var fieldValue1 = documentValue1[i];
var fieldValue2 = documentValue2[i];
if (fieldValue1 !== fieldValue2 && !mapping.areEqual(fieldValue1, fieldValue2)) {
return false;
}
}
return true;
};
ArrayMapping.prototype.walk = function (session, value, flags, entities, embedded, references) {
if (!Array.isArray(value)) {
return;
}
var mapping = this.elementMapping;
for (var i = 0, l = value.length; i < l; i++) {
mapping.walk(session, value[i], flags, entities, embedded, references);
}
};
ArrayMapping.prototype.fetch = function (session, parentEntity, value, path, depth, callback) {
if (!Array.isArray(value) || value.length == 0) {
process.nextTick(function () { return callback(null, value); });
return;
}
var mapping = this.elementMapping;
async.map(value, function (item, done) { return mapping.fetch(session, parentEntity, item, path, depth, done); }, callback);
};
ArrayMapping.prototype.fetchInverse = function (session, parentEntity, propertyName, path, depth, callback) {
var _this = this;
if (!parentEntity) {
return callback(new persistenceError_1.PersistenceError("Parent entity required to resolve inverse relationship."));
}
if (!(this.elementMapping.flags & 1024)) {
return callback(new persistenceError_1.PersistenceError("Element mapping must be an entity to resolve inverse relationship."));
}
session.getPersister(this.elementMapping).findInverseOf(parentEntity, propertyName, function (err, value) {
if (err)
return callback(err);
_this.fetch(session, _this, value, path, depth, callback);
});
};
ArrayMapping.prototype.resolveCore = function (context) {
var property = context.currentProperty, index;
if (property == "$" || ((index = parseInt(property)) === index)) {
if (context.resolveProperty(this.elementMapping, property)) {
return;
}
}
this.elementMapping.resolve(context);
};
Object.defineProperty(ArrayMapping.prototype, "nestedDepth", {
get: function () {
if (this._nestedDepth !== undefined) {
this._nestedDepth = this._findNestedDepth(0, this);
}
return this._nestedDepth;
},
enumerable: true,
configurable: true
});
ArrayMapping.prototype._findNestedDepth = function (depth, mapping) {
if (mapping.flags & 1) {
return this._findNestedDepth(depth + 1, mapping.elementMapping);
}
else if (mapping.flags & 512) {
var elementMappings = mapping.elementMappings;
for (var i = 0, l = elementMappings.length; i < l; i++) {
depth = Math.max(depth, this._findNestedDepth(depth + 1, elementMappings[i]));
}
}
return depth;
};
return ArrayMapping;
}(mappingBase_1.MappingBase));
exports.ArrayMapping = ArrayMapping;