memory-card
Version:
ES6 Map like Async API, with Swagger API Backend Support.
52 lines • 1.41 kB
JavaScript
import { Etcd3 } from 'etcd3';
import { log, } from '../config.js';
import { StorageBackend, } from './backend.js';
class StorageEtcd extends StorageBackend {
etcd;
constructor(name, options) {
log.verbose('StorageEtcd', 'constructor()');
options.type = 'etcd';
super(name, options);
options = options;
this.etcd = new Etcd3({
hosts: options.hosts,
});
}
toString() {
const text = [
this.constructor.name,
'<',
this.name,
'>',
].join('');
return text;
}
async save(payload) {
log.verbose('StorageEtcd', 'save()');
await this.etcd
.put(this.name)
.value(JSON.stringify(payload));
}
async load() {
log.verbose('StorageEtcd', 'load()');
const result = await this.etcd.get(this.name).string();
if (!result) {
return {};
}
try {
return JSON.parse(result);
}
catch (e) {
log.warn('StorageEtcd', 'load() rejection: %s', e && e.message);
console.error(e);
return {};
}
}
async destroy() {
log.verbose('StorageEtcd', 'destroy()');
await this.etcd.delete().key(this.name);
}
}
export default StorageEtcd;
export { StorageEtcd, };
//# sourceMappingURL=etcd.js.map