@getanthill/datastore
Version:
Event-Sourced Datastore
122 lines • 4.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
class Model {
constructor(datastore, modelConfig) {
this.modelConfig = modelConfig;
this.datastore = datastore;
}
get name() {
return this.modelConfig.name;
}
async request(handler, ...args) {
const { data } = await handler.call(this.datastore, ...args);
return data;
}
heartbeat() {
return this.request(this.datastore.heartbeat);
}
createModel() {
return this.request(this.datastore.createModel, this.modelConfig);
}
updateModel() {
return this.request(this.datastore.updateModel, this.modelConfig);
}
createModelIndexes() {
return this.request(this.datastore.createModelIndexes, this.modelConfig);
}
getSchema() {
return this.request(this.datastore.getSchema, this.name);
}
async encryptOne(payload, fields = []) {
const [decrypted] = await this.request(this.datastore.encrypt, this.name, [payload], fields);
return decrypted;
}
encryptMany(payloads, fields = []) {
return this.request(this.datastore.encrypt, this.name, payloads, fields);
}
async decryptOne(payload, fields = []) {
const [decrypted] = await this.request(this.datastore.decrypt, this.name, [payload], fields);
return decrypted;
}
decryptMany(payloads, fields = []) {
return this.request(this.datastore.decrypt, this.name, payloads, fields);
}
createOne(payload, headers) {
return this.request(this.datastore.create, this.name, payload, headers);
}
createMany(payloads) {
return Promise.all(payloads.map((p) => this.createOne(p)));
}
/**
* @deprecated in favor of createOne + createMany
*/
create(payload) {
if (Array.isArray(payload)) {
return Promise.all(payload.map((p) => this.request(this.datastore.create, this.name, p)));
}
return this.request(this.datastore.create, this.name, payload);
}
updateOne(payload, headers) {
return this.request(this.datastore.update, this.name,
/* @ts-ignore */
payload[this.modelConfig.correlation_field], (0, lodash_1.omit)(payload, this.modelConfig.correlation_field), headers);
}
updateMany(payloads) {
return Promise.all(payloads.map((p) => this.updateOne(p)));
}
/**
* @deprecated
*/
update(payload) {
if (Array.isArray(payload)) {
return Promise.all(payload.map((p) => this.request(this.datastore.update, this.name, p[this.modelConfig.correlation_field], (0, lodash_1.omit)(p, this.modelConfig.correlation_field))));
}
return this.request(this.datastore.update, this.name,
/* @ts-ignore */
payload[this.modelConfig.correlation_field], (0, lodash_1.omit)(payload, this.modelConfig.correlation_field));
}
apply(eventType, payload, headers, eventVersion = '0_0_0') {
return this.request(this.datastore.apply, this.name,
/* @ts-ignore */
payload[this.modelConfig.correlation_field], eventType, eventVersion, (0, lodash_1.omit)(payload, this.modelConfig.correlation_field), headers);
}
get(correlationId, headers) {
return this.request(this.datastore.get, this.name, correlationId, headers);
}
count(query) {
return this.datastore.count.call(this.datastore, this.name, query);
}
find(query, page, pageSize, headers) {
return this.request(this.datastore.find, this.name, query, page, pageSize, headers);
}
events(correlationId, page, pageSize) {
return this.request(this.datastore.events, this.name, correlationId, page, pageSize);
}
version(correlationId, version) {
return this.request(this.datastore.version, this.name, correlationId, version);
}
at(correlationId, date) {
return this.request(this.datastore.at, this.name, correlationId, date);
}
restore(correlationId, version) {
return this.request(this.datastore.restore, this.name, correlationId, version);
}
snapshot(correlationId) {
return this.request(this.datastore.snapshot, this.name, correlationId);
}
archive(correlationId, deep) {
return this.request(this.datastore.archive, this.name, correlationId, deep);
}
unarchive(correlationId, deep) {
return this.request(this.datastore.unarchive, this.name, correlationId, deep);
}
delete(correlationId, deep) {
return this.request(this.datastore.delete, this.name, correlationId, deep);
}
async updateOverwhelmingly(query, handler, progress, pageSize) {
return this.datastore.updateOverwhelmingly(this.name, query, handler, progress, pageSize);
}
}
exports.default = Model;
//# sourceMappingURL=Model.js.map