UNPKG

@varasto/query

Version:

Query and bulk operation utilities for Varasto storage

108 lines (107 loc) 4.76 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.findEntry = exports.findKey = exports.find = void 0; const simple_json_match_1 = __importDefault(require("simple-json-match")); /** * Searches for an entry from given namespace that matches given schema and * returns value of first matching result. If no entry in the namespace matches * the schema, `undefined` is returned instead. */ function find(storage, namespace, schema) { var _a, e_1, _b, _c; return __awaiter(this, void 0, void 0, function* () { try { for (var _d = true, _e = __asyncValues(storage.values(namespace)), _f; _f = yield _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)) { return value; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } }); } exports.find = find; /** * Searches for an entry from given namespace that matches given schema and * returns key of first matching result. If no entry in the namespace matches * the schema, `undefined` is returned instead. */ function findKey(storage, namespace, schema) { var _a, e_2, _b, _c; return __awaiter(this, void 0, void 0, function* () { try { for (var _d = true, _e = __asyncValues(storage.entries(namespace)), _f; _f = yield _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)) { return entry[0]; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_2) throw e_2.error; } } }); } exports.findKey = findKey; /** * Searches for an entry from given namespace that matches given schema and * returns both key and value of first matching result. If no entry in the * namespace matches the schema, `undefined` is returned instead. */ function findEntry(storage, namespace, schema) { var _a, e_3, _b, _c; return __awaiter(this, void 0, void 0, function* () { try { for (var _d = true, _e = __asyncValues(storage.entries(namespace)), _f; _f = yield _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)) { return entry; } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_3) throw e_3.error; } } }); } exports.findEntry = findEntry;