@thisisagile/easy
Version:
Straightforward library for building domain-driven microservice architectures
96 lines (95 loc) • 3 kB
JavaScript
import {
toJson
} from "../chunk-KPEGHJSZ.mjs";
import {
Repository
} from "../chunk-MKW5GQPP.mjs";
import {
when
} from "../chunk-XAIHCZT4.mjs";
import {
isValidatable
} from "../chunk-OFGI5FLG.mjs";
import "../chunk-NCEWAKOZ.mjs";
import "../chunk-Q3WMGUO2.mjs";
import {
reject,
resolve
} from "../chunk-5HENJTR6.mjs";
import {
Exception
} from "../chunk-FKZ4MOTX.mjs";
import "../chunk-WSBULPUZ.mjs";
import "../chunk-JVDKV5HE.mjs";
import "../chunk-WTFW6DLP.mjs";
import "../chunk-NDCIZJXB.mjs";
import "../chunk-MCCIBDEH.mjs";
import "../chunk-H2762RTS.mjs";
import {
asList
} from "../chunk-A7C3XND3.mjs";
import "../chunk-JSON7A4X.mjs";
import "../chunk-CO2AFYVD.mjs";
import "../chunk-BDA5LB4S.mjs";
import "../chunk-SJGQU3OG.mjs";
import "../chunk-ZHXKBOK2.mjs";
import "../chunk-ZPNFXK7Y.mjs";
import "../chunk-DXQSIBC7.mjs";
import "../chunk-PIRWVOO2.mjs";
import "../chunk-DEJ7A5PY.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