UNPKG

@mee4dy/crud

Version:

Create a backend and frontend in 5 minutes! With our powerful full stack crud system, customize it to suit you.

3 lines (2 loc) 1.42 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("lodash"),c=require("deep-object-diff");function o(s){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(s){for(const e in s)if(e!=="default"){const i=Object.getOwnPropertyDescriptor(s,e);Object.defineProperty(t,e,i.get?i:{enumerable:!0,get:()=>s[e]})}}return t.default=s,Object.freeze(t)}const r=o(n);class a{constructor(t,e="id",i){this.setPK(e),this.setActions(i),this.setItem(t)}setItem(t){this.item=t,this.itemOriginal=r.cloneDeep(t),Object.keys(t).forEach(e=>{Object.defineProperty(this,e,{get(){return this.get(e)},set(i){return this.set(e,i)}})})}setActions(t){return this.actions=t,this}getActions(){return this.actions}setPK(t){return this.pk=t,this}getPK(){return this.pk}getItem(){return this.item}getItemOriginal(){return this.itemOriginal}callAction(t,e){return this.actions[t](e)}get(t){return this.item[t]}set(t,e){this.item[t]=e}diff(){const t=this.getItem(),e=this.getItemOriginal();let i=c.diff(e,t);return i=r.omit(i,["sources"]),i}commit(){const t=this.getItem(),e=this.diff();return this.callAction("commit",{pk:t.pk,data:e})}save(){const t=this.getItem(),e=this.diff(),i=this.getPK();return this.callAction("update",{pk:t[i],data:e})}delete(){const t=this.getItem(),e=this.getPK();return this.callAction("delete",{pk:t[e]})}}exports.ORMItem=a; //# sourceMappingURL=orm-item.cjs.map