UNPKG

ravendb

Version:
97 lines 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MetadataDictionary = exports.MetadataInternal = void 0; exports.createMetadataDictionary = createMetadataDictionary; const index_js_1 = require("../Exceptions/index.js"); const metadataDirtiness = new WeakMap(); const metadataParents = new WeakMap(); const metadataProxyTargets = new WeakMap(); class MetadataInternal { constructor(obj) { if (obj) { for (const key of Object.keys(obj)) { this[key] = this._metadataConvertValue(key, obj[key]); } } } isDirty() { return metadataDirtiness.get(metadataProxyTargets.get(this)); } getParent() { const parentData = metadataParents.get(metadataProxyTargets.get(this)); return parentData ? parentData.parent : null; } getParentKey() { const parentData = metadataParents.get(metadataProxyTargets.get(this)); return parentData ? parentData.parentKey : null; } _metadataConvertValue(key, val) { if (val == null) { return null; } if (typeof val !== "object") { return val; } if (Array.isArray(val)) { return val.map((e) => this._metadataConvertValue(key, e)); } return createMetadataDictionary({ raw: val, parentInfo: { parent: this, parentKey: key } }); } static getChangeTrackingProxy(instance) { const proxy = new Proxy(instance, { get(obj, prop, value) { return Reflect.get(obj, prop, value); }, set(obj, prop, value) { metadataDirtiness.set(obj, true); return Reflect.set(obj, prop, value); }, deleteProperty(obj, prop) { metadataDirtiness.set(obj, true); return Reflect.deleteProperty(obj, prop); }, ownKeys(target) { return Reflect.ownKeys(target) .filter(x => x !== "getParentKey" && x !== "getParent" && x !== "isDirty" && x !== "_metadataConvertValue"); } }); metadataProxyTargets.set(proxy, instance); return proxy; } } exports.MetadataInternal = MetadataInternal; class MetadataDictionary { static create(raw) { return createMetadataDictionary({ raw: raw || {} }); } static materializeFromJson(metadata) { return MetadataDictionary.create(metadata); } } exports.MetadataDictionary = MetadataDictionary; function createMetadataDictionary(metadataParams) { const parentInfo = metadataParams.parentInfo; const metadata = new MetadataInternal(metadataParams.raw); const proxy = MetadataInternal.getChangeTrackingProxy(metadata); if (parentInfo) { if (!parentInfo.parent) { return (0, index_js_1.throwError)("InvalidArgumentException", "Parent cannot be null"); } if (!parentInfo.parentKey) { return (0, index_js_1.throwError)("InvalidArgumentException", "Parent key cannot be null"); } metadataParents.set(metadata, parentInfo); } metadataDirtiness.set(metadata, false); return proxy; } //# sourceMappingURL=MetadataAsDictionary.js.map