UNPKG

@varasto/query

Version:

Query and bulk operation utilities for Varasto storage

107 lines (106 loc) 4.78 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.partitionEntries = exports.partitionKeys = exports.partition = void 0; const simple_json_match_1 = __importDefault(require("simple-json-match")); /** * Splits values of entries from given namespace into two arrays depending on * whether they match the given schema. Entries that match the schema will be * returned first element of returned array, while the remaining entries will * be second. */ const partition = (storage, namespace, schema) => __awaiter(void 0, void 0, void 0, function* () { var _a, e_1, _b, _c; const t = []; const f = []; 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; ((0, simple_json_match_1.default)(value, schema) ? t : f).push(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; } } return [t, f]; }); exports.partition = partition; /** * Splits keys of entries from given namespace into two arrays depending on * whether they match the given schema. Entries that match the schema will be * returned first element of returned array, while the remaining entries will * be second. */ const partitionKeys = (storage, namespace, schema) => __awaiter(void 0, void 0, void 0, function* () { var _g, e_2, _h, _j; const t = []; const f = []; try { for (var _k = true, _l = __asyncValues(storage.entries(namespace)), _m; _m = yield _l.next(), _g = _m.done, !_g; _k = true) { _j = _m.value; _k = false; const entry = _j; ((0, simple_json_match_1.default)(entry[1], schema) ? t : f).push(entry[0]); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (!_k && !_g && (_h = _l.return)) yield _h.call(_l); } finally { if (e_2) throw e_2.error; } } return [t, f]; }); exports.partitionKeys = partitionKeys; /** * Splits entries from given namespace into two arrays depending on whether * they match the given schema. Entries that match the schema will be returned * first element of returned array, while the remaining entries will be second. */ const partitionEntries = (storage, namespace, schema) => __awaiter(void 0, void 0, void 0, function* () { var _o, e_3, _p, _q; const t = []; const f = []; try { for (var _r = true, _s = __asyncValues(storage.entries(namespace)), _t; _t = yield _s.next(), _o = _t.done, !_o; _r = true) { _q = _t.value; _r = false; const entry = _q; ((0, simple_json_match_1.default)(entry[1], schema) ? t : f).push(entry); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (!_r && !_o && (_p = _s.return)) yield _p.call(_s); } finally { if (e_3) throw e_3.error; } } return [t, f]; }); exports.partitionEntries = partitionEntries;