UNPKG

@hiki9/rich-domain

Version:

Rich Domain is a library that provides a set of tools to help you build complex business logic in NodeJS using Domain Driven Design principles.

81 lines (80 loc) 2.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.proxyHandler = exports.mutationArrayMethods = void 0; exports.mutationArrayMethods = [ 'push', 'pop', 'shift', 'unshift', 'slice', 'fill', 'copyWithin', 'splice', 'reverse' ]; const proxyHandler = function (self, keyProp = []) { return { get: function (target, prop, receiver) { if (Array.isArray(target[prop])) { return new Proxy(target[prop], (0, exports.proxyHandler)(self, [...keyProp, prop])); } const accessor = Reflect.get(target, prop, receiver); if (typeof accessor === 'function' && exports.mutationArrayMethods.includes(prop)) { return function (...args) { const result = Array.prototype[prop].apply(target, args); self.history?.addSnapshot({ props: self['props'], trace: { instanceId: self.id.value, instanceKey: self.constructor.name, fieldKey: keyProp?.join("."), updatedAt: new Date(), update: keyProp?.join("."), action: prop, }, }); return result; }; } return accessor; }, set: function (target, prop, value, receiver) { const oldValue = Reflect.get(target, prop, receiver); Reflect.set(target, prop, value, receiver); if (!Array.isArray(receiver)) { self?.history?.addSnapshot({ props: self['props'], trace: { instanceId: self.id.value, instanceKey: self.constructor.name, fieldKey: prop?.toString?.() ?? '', updatedAt: new Date(), update: prop?.toString?.(), from: oldValue, to: value, } }); } /** * else { let prefix = keyProp?.join?.("."); if (prefix) prefix += "."; console.log('position', prop) self?.history?.addSnapshot({ props: self['props'], trace: { updatedAt: new Date(), update: prefix + prop?.toString(), from: oldValue, to: value, position: Number(prop) } }); } */ return true; }, }; }; exports.proxyHandler = proxyHandler; //# sourceMappingURL=proxy.js.map