UNPKG

@thisisagile/easy

Version:

Straightforward library for building domain-driven microservice architectures

59 lines (58 loc) 1.75 kB
import { Gateway } from "../chunk-66DVBO4C.mjs"; import { toPageList } from "../chunk-GYJHZJKL.mjs"; import { Exception, when } from "../chunk-AVHYDITZ.mjs"; import "../chunk-EBIF6AMC.mjs"; import "../chunk-KCY4RPFR.mjs"; import "../chunk-SSROJBD5.mjs"; import "../chunk-S3NSPQ7M.mjs"; import "../chunk-NNA77YYC.mjs"; import "../chunk-SJGQU3OG.mjs"; import "../chunk-D5IYAIMK.mjs"; import "../chunk-PF7HDF6B.mjs"; import "../chunk-IXK47WKF.mjs"; import "../chunk-PIRWVOO2.mjs"; import { isDefined } from "../chunk-AAND4MKF.mjs"; import "../chunk-4N72FQFX.mjs"; // src/data/InMemoryGateway.ts var InMemoryGateway = class extends Gateway { constructor(data) { super(); this.data = data; } all(options) { return this.data.then((d) => toPageList(d, d)); } byId(id) { return this.data.then((d) => d.byId(id)).then((d) => d ? { ...d } : void 0); } by = (key, value) => { return this.data.then((d) => d.filter((i) => i[key] === value)); }; exists(id) { return this.byId(id).then((d) => isDefined(d)); } add(item) { return when(item).not.contains((i) => i.id).reject(Exception.IsMissingId).then((i) => this.exists(i.id)).then((ex) => when(ex).isTrue.reject(Exception.AlreadyExists)).then(() => this.data.then((d) => d.add(item))).then(() => ({ ...item })); } remove(id) { return this.data.then( (d) => when(d.findIndex((i) => i.id === id)).with((i) => i < 0).reject(Exception.DoesNotExist).then((i) => d.splice(i, 1)) ).then(() => true); } update(item) { return when(item).not.contains((i) => i.id).reject(Exception.IsMissingId).then((i) => this.remove(i.id).then(() => this.add(i))); } }; export { InMemoryGateway }; //# sourceMappingURL=InMemoryGateway.mjs.map