UNPKG

synapse-react-client

Version:

[![npm version](https://badge.fury.io/js/synapse-react-client.svg)](https://badge.fury.io/js/synapse-react-client) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettie

59 lines (58 loc) 1.53 kB
import { cloneDeep as n, isEqual as r, uniqueId as s } from "lodash-es"; class h { data; idField; autoGenerateId; constructor(e) { this.data = n(e?.initialData) || [], this.idField = e?.idField, this.autoGenerateId = e?.autoGenerateId; } getAll() { return [...this.data]; } getOneById(e) { const t = this.idField; if (!t) throw new Error("idField is not defined"); return this.data.find((i) => r(i[t], e)); } getOneByPredicate(e) { return this.data.find(e); } getOneByField(e, t) { return this.data.find((i) => r(i[e], t)); } getMany(e) { return this.data.filter(e); } create(e) { const t = n(e), i = this.idField; if (i && this.autoGenerateId) t[i] = s(); else if (i && !t[i]) throw new Error( `autoGenerateId is not true, but attempted to create new object with idField: ${String( i )} and id: ${t[i]}` ); return this.data.push(t), t; } update(e, t, i = "replace") { const a = this.idField; if (!a) throw new Error("idField is not defined"); return this.data = this.data.map((d) => (r(d[a], e) && (i === "merge" ? d = { ...d, ...t } : i === "replace" && (d = t), d[a] = e), d)), this.getOneById(e); } delete(e) { const t = this.idField; if (!t) throw new Error("idField is not defined"); this.data = this.data.filter((i) => !r(i[t], e)); } clear() { this.data = []; } } export { h as default }; //# sourceMappingURL=BasicMockedCrudService.js.map