@darlean/core
Version:
Darlean core functionality for creating applications that define, expose and host actors
90 lines (89 loc) • 3.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DistributedPersistence = void 0;
const base_1 = require("@darlean/base");
const various_1 = require("./various");
/**
* For internal use. Helper class for {@link DistributedPersistence}.
*/
class DistributedPersistable extends base_1.CustomPersistable {
constructor(persistence, partitionKey, sortKey, value) {
super(value);
this.persistence = persistence;
this.partitionKey = partitionKey;
this.sortKey = sortKey;
}
async _load() {
const result = await this.persistence.loadImpl(this.partitionKey, this.sortKey);
return { value: result[0], version: result[1] };
}
async _persist(value, version) {
await this.persistence.storeImpl(this.partitionKey, this.sortKey, value, version);
}
}
/**
* Implementation of {@link IPersistence} that uses a distributed persistence service (like the one defined
* in {@link @darlean/fs-persistence-suite}) to provide persistency.
*/
class DistributedPersistence {
constructor(service, deser, specifier) {
this.service = service;
this.deser = deser;
this.specifier = specifier;
}
async query(options) {
const intermediate = this.deser.deserializeTyped(await this.service.queryBuffer({
specifier: this.specifier,
partitionKey: options.partitionKey,
sortKeyFrom: options.sortKeyFrom,
sortKeyTo: options.sortKeyTo,
sortKeyToMatch: options.sortKeyToMatch,
maxItems: options.maxItems,
continuationToken: options.continuationToken,
sortKeyOrder: options.sortKeyOrder
}));
const results = {
continuationToken: intermediate.continuationToken,
items: []
};
for (const item of intermediate.items) {
const item2 = {
sortKey: item.sortKey,
value: item.value ? this.deser.deserialize(item.value) : undefined
};
results.items.push(item2);
}
return results;
}
persistable(partitionKey, sortKey, value) {
return new DistributedPersistable(this, partitionKey, sortKey, value);
}
async load(partitionKey, sortKey) {
const result = this.persistable(partitionKey, sortKey, undefined);
await result.load();
return result;
}
async loadImpl(partitionKey, sortKey) {
const result = await this.service.load({
specifier: this.specifier,
partitionKey: partitionKey ?? [],
sortKey: sortKey ?? []
});
const value = result.value ? this.deser.deserialize(result.value) : undefined;
return [value, result.version];
}
async storeImpl(partitionKey, sortKey, value, version) {
const v = value === undefined ? undefined : this.deser.serialize(value);
await this.service.store({
specifier: this.specifier,
partitionKey: partitionKey ?? [],
sortKey: sortKey ?? [],
value: v,
version: version
});
}
sub(partitionKey) {
return new various_1.SubPersistence(this, partitionKey);
}
}
exports.DistributedPersistence = DistributedPersistence;