UNPKG

hydrate-mongodb

Version:
205 lines (204 loc) 8.5 kB
"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 classMapping_1 = require("./classMapping"); var mappingModel_1 = require("./mappingModel"); var reference_1 = require("../reference"); var persistenceError_1 = require("../persistenceError"); var EntityMapping = (function (_super) { __extends(EntityMapping, _super); function EntityMapping(baseClass) { var _this = _super.call(this, baseClass) || this; _this.flushPriority = 50; _this.flags &= ~2048; _this.flags |= 1024; return _this; } EntityMapping.prototype.validateProperty = function (property) { if (property && property.name) { if (property.name == "_id") { return "The '_id' property on an entity class is automatically populated with the primary key and cannot be explicitly mapped."; } if (property.name == "id") { return "The 'id' property on an entity class is automatically populated with the string representation of the primary key " + "and cannot be explicitly mapped."; } } return _super.prototype.validateProperty.call(this, property); }; EntityMapping.prototype.setDocumentVersion = function (obj, version) { this.setDocumentVersion = (new Function("o", "v", "o['" + this.inheritanceRoot.versionField + "'] = v")); obj[this.inheritanceRoot.versionField] = version; }; EntityMapping.prototype.getDocumentVersion = function (obj) { this.getDocumentVersion = (new Function("o", "return o['" + this.inheritanceRoot.versionField + "']")); return obj[this.inheritanceRoot.versionField]; }; EntityMapping.prototype.addIndex = function (index) { if (!this.indexes) { this.indexes = []; } this.indexes.push(index); }; EntityMapping.prototype.refresh = function (context, entity, document) { var mapping = this.getMapping(context, document); if (mapping) { if (mapping != this) { context.addError("Refresh does not support changing instantiated class of entity."); return; } return this.readObject(context, entity, document, true); } }; EntityMapping.prototype.read = function (context, value) { if (value == null) return null; var id; if (context.path) { id = value; } else { id = value["_id"]; if (!id) { context.addError("Missing identifier.", "_id"); return; } } if (!this.inheritanceRoot.identity.validate(id)) { context.addError("'" + id.toString() + "' is not a valid identifier.", (context.path ? context.path + "." : "") + "_id"); return; } if (context.path) { return context.session.getReferenceInternal(this, id); } var obj = _super.prototype.read.call(this, context, value); if (obj) { obj["_id"] = id; obj["id"] = id.toString(); } return obj; }; EntityMapping.prototype.write = function (context, value) { if (value == null) return null; var id; if (!(id = value["_id"])) { context.addError("Missing identifier.", (context.path ? context.path + "." : "") + "_id"); return; } if (!this.inheritanceRoot.identity.validate(id)) { context.addError("'" + id.toString() + "' is not a valid identifier.", (context.path ? context.path + "." : "") + "_id"); return; } if (context.path) { return id; } var document = _super.prototype.write.call(this, context, value); if (document) { document["_id"] = id; } return document; }; EntityMapping.prototype.watchEntity = function (entity, observer) { _super.prototype.watch.call(this, entity, observer, []); }; EntityMapping.prototype.watch = function (value, observer, visited) { }; EntityMapping.prototype.areDocumentsEqual = function (document1, document2) { return _super.prototype.areEqual.call(this, document1, document2); }; EntityMapping.prototype.areEqual = function (documentValue1, documentValue2) { if (documentValue1 === documentValue2) return true; if (documentValue1 == null || documentValue2 == null) return false; var id1 = documentValue1["_id"] || documentValue1, id2 = documentValue2["_id"] || documentValue2; if (id1 == null || id2 == null) { return false; } return this.inheritanceRoot.identity.areEqual(id1, id2); }; EntityMapping.prototype.walk = function (session, value, flags, entities, embedded, references) { if (!value || typeof value !== "object") return; if (reference_1.Reference.isReference(value)) { var entity = session.getObject(value.id); if (entity) { value = entity; } else { if (flags & 16384) { references.push(value); } return; } } if (entities.indexOf(value) !== -1) return; entities.push(value); if ((flags & 8192) == 0 && entities.length > 1) return; _super.prototype.walk.call(this, session, value, flags, entities, embedded, references); }; EntityMapping.prototype.fetch = function (session, parentEntity, value, path, depth, callback) { var _this = this; if (!value || typeof value !== "object") { process.nextTick(function () { return callback(null, value); }); return; } if (reference_1.Reference.isReference(value)) { value.fetch(session, function (err, entity) { if (err) return callback(err); _super.prototype.fetch.call(_this, session, entity, entity, path, depth, callback); }); return; } _super.prototype.fetch.call(this, session, value, value, path, depth, callback); }; EntityMapping.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.")); } session.getPersister(this).findOneInverseOf(parentEntity, propertyName, function (err, value) { if (err) return callback(err); _super.prototype.fetch.call(_this, session, _this, value, path, depth, callback); }); }; EntityMapping.prototype.fetchPropertyValue = function (session, value, property, callback) { session.getPersister(this).fetchPropertyValue(value, property, callback); }; EntityMapping.prototype.getDefaultFields = function () { var fields = {}; if (this._defaultFields === undefined) { for (var i = 0; i < this.properties.length; i++) { var property = this.properties[i]; if ((property.flags & 2048) != 0) { property.setFieldValue(fields, 0); } } this._defaultFields = fields; } return this._defaultFields; }; EntityMapping.prototype.resolveCore = function (context) { if (!context.isFirst) { context.setError("Unable to resolve entity mapping. The dot notation can only be used for embedded objects."); return; } _super.prototype.resolveCore.call(this, context); }; return EntityMapping; }(classMapping_1.ClassMapping)); exports.EntityMapping = EntityMapping;