UNPKG

@thisisagile/easy

Version:

Straightforward library for building domain-driven microservice architectures

83 lines (82 loc) 2.66 kB
import { Repository } from "../chunk-FBZHPHXB.mjs"; import { toJson } from "../chunk-A7KUHZ3A.mjs"; import { Exception, asList, reject, resolve, when } from "../chunk-AVHYDITZ.mjs"; import { isValidatable } from "../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 "../chunk-AAND4MKF.mjs"; import "../chunk-4N72FQFX.mjs"; // src/domain/Repo.ts var Repo = class extends Repository { constructor(ctor, gateway) { super(); this.ctor = ctor; this.gateway = gateway; } create = (item) => isValidatable(item) ? item : new this.ctor(item); all(options) { return this.gateway.all(options).then((js) => js.map((i) => new this.ctor(i))); } byId(id) { return this.gateway.byId(id).then((j) => when(j).not.isDefined.reject(Exception.DoesNotExist)).then((j) => new this.ctor(j)); } byIds(...ids) { return this.gateway.byIds(...ids).then((j) => asList(this.ctor, j)); } byKey(key, options) { return this.gateway.by("key", key, options).then((js) => js.map((i) => new this.ctor(i))); } by(key, value, options) { return this.gateway.by(key.toString(), value, options).then((js) => js.map((i) => new this.ctor(i))); } search(q, options) { return this.gateway.search(q, options).then((js) => js.map((i) => new this.ctor(i))); } filter(options) { return this.gateway.filter(options).then((js) => js.map((i) => new this.ctor(i))); } exists(id) { return this.gateway.exists(id); } add(t) { return this.extend(this.create(t), "add").then((i) => when(i).not.isValid.reject()).then((i) => this.validate(i, "add")).then((i) => this.gateway.add(toJson(i))).then((j) => new this.ctor(j)); } update(id, json) { return this.gateway.byId(id).then((j) => when(j).not.isDefined.reject(Exception.DoesNotExist)).then((j) => new this.ctor(j).update(json)).then((i) => this.extend(i, "update")).then((i) => when(i).not.isValid.reject()).then((i) => this.validate(i, "update")).then((i) => this.gateway.update(toJson(i))).then((j) => new this.ctor(j)); } remove(id) { return this.gateway.remove(id); } extend(item, _action) { return resolve(item); } validate(item, _action) { return resolve(item); } upsert(id, item) { return this.update(id, item).catch((e) => Exception.DoesNotExist.equals(e) ? this.add(item) : reject(e)); } }; export { Repo }; //# sourceMappingURL=Repo.mjs.map