@varasto/query
Version:
Query and bulk operation utilities for Varasto storage
76 lines (75 loc) • 4.12 kB
JavaScript
;
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateAllEntries = exports.updateAll = void 0;
const find_all_1 = require("./find-all");
/**
* Performs an bulk update where all entries from given namespace that match
* the given schema are partially updated with given value. Returns updated
* values or empty array if no entry matched the given schema.
*/
function updateAll(storage, namespace, schema, value) {
return __asyncGenerator(this, arguments, function* updateAll_1() {
var _a, e_1, _b, _c;
try {
for (var _d = true, _e = __asyncValues((0, find_all_1.findAllKeys)(storage, namespace, schema)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const key = _c;
yield yield __await(yield __await(storage.update(namespace, key, value)));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
}
finally { if (e_1) throw e_1.error; }
}
});
}
exports.updateAll = updateAll;
/**
* Performs an bulk update where all entries from given namespace that match
* the given schema are partially updated with given value. Returns updated
* entries (key and value) or empty array if no entry matched the given schema.
*/
function updateAllEntries(storage, namespace, schema, value) {
return __asyncGenerator(this, arguments, function* updateAllEntries_1() {
var _a, e_2, _b, _c;
try {
for (var _d = true, _e = __asyncValues((0, find_all_1.findAllKeys)(storage, namespace, schema)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) {
_c = _f.value;
_d = false;
const key = _c;
yield yield __await([key, yield __await(storage.update(namespace, key, value))]);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e));
}
finally { if (e_2) throw e_2.error; }
}
});
}
exports.updateAllEntries = updateAllEntries;