UNPKG

@varasto/query

Version:

Query and bulk operation utilities for Varasto storage

111 lines (110 loc) 5.25 kB
"use strict"; 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]); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.findAllEntries = exports.findAllKeys = exports.findAll = void 0; const simple_json_match_1 = __importDefault(require("simple-json-match")); /** * Searches for entries from given namespace that match given schema and * returns their values. If no entries match the given schema, empty array * is returned instead. */ function findAll(storage, namespace, schema) { return __asyncGenerator(this, arguments, function* findAll_1() { var _a, e_1, _b, _c; try { for (var _d = true, _e = __asyncValues(storage.values(namespace)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const value = _c; if ((0, simple_json_match_1.default)(value, schema)) { yield yield __await(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.findAll = findAll; /** * Searches for entries from given namespace that match given schema and * returns their keys. If no entries match the given schema, empty array is * returned instead. */ function findAllKeys(storage, namespace, schema) { return __asyncGenerator(this, arguments, function* findAllKeys_1() { var _a, e_2, _b, _c; try { for (var _d = true, _e = __asyncValues(storage.entries(namespace)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const entry = _c; if ((0, simple_json_match_1.default)(entry[1], schema)) { yield yield __await(entry[0]); } } } 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.findAllKeys = findAllKeys; /** * Searches for entries from given namespace that match given schema and * returns them in an array. If no entries match the given schema, empty * array is returned instead. */ function findAllEntries(storage, namespace, schema) { return __asyncGenerator(this, arguments, function* findAllEntries_1() { var _a, e_3, _b, _c; try { for (var _d = true, _e = __asyncValues(storage.entries(namespace)), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const entry = _c; if ((0, simple_json_match_1.default)(entry[1], schema)) { yield yield __await(entry); } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield __await(_b.call(_e)); } finally { if (e_3) throw e_3.error; } } }); } exports.findAllEntries = findAllEntries;