@mee4dy/crud
Version:
Create a backend and frontend in 5 minutes! With our powerful full stack crud system, customize it to suit you.
76 lines (75 loc) • 1.5 kB
JavaScript
import * as s from "lodash";
import { diff as r } from "deep-object-diff";
class o {
constructor(t, e = "id", i) {
this.setPK(e), this.setActions(i), this.setItem(t);
}
setItem(t) {
this.item = t, this.itemOriginal = s.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 = r(e, t);
return i = s.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]
});
}
}
export {
o as ORMItem
};
//# sourceMappingURL=orm-item.js.map