UNPKG

@jovian/type-tools

Version:

TypeTools is a Typescript library for providing extensible tooling runtime validations and type helpers.

445 lines 26.3 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpstreamHttpDatastore = exports.MongoCode = void 0; var common_iface_1 = require("./common.iface"); var globals_ix_1 = require("../common/globals.ix"); var axios = __importStar(require("axios")); var upstream_1 = require("../upstream"); var DatastoreCodeEnum; (function (DatastoreCodeEnum) { DatastoreCodeEnum[DatastoreCodeEnum["CONNECTION_ERROR"] = 0] = "CONNECTION_ERROR"; DatastoreCodeEnum[DatastoreCodeEnum["CREATE_ACK_FAIL"] = 1] = "CREATE_ACK_FAIL"; DatastoreCodeEnum[DatastoreCodeEnum["CREATE_CONTENTION_INDEX"] = 2] = "CREATE_CONTENTION_INDEX"; DatastoreCodeEnum[DatastoreCodeEnum["CREATE_ERROR"] = 3] = "CREATE_ERROR"; DatastoreCodeEnum[DatastoreCodeEnum["READ_NO_GID"] = 4] = "READ_NO_GID"; DatastoreCodeEnum[DatastoreCodeEnum["READ_ERROR"] = 5] = "READ_ERROR"; DatastoreCodeEnum[DatastoreCodeEnum["UPDATE_NO_GID"] = 6] = "UPDATE_NO_GID"; DatastoreCodeEnum[DatastoreCodeEnum["UPDATE_INDEX_CONTENTION"] = 7] = "UPDATE_INDEX_CONTENTION"; DatastoreCodeEnum[DatastoreCodeEnum["UPDATE_ERROR"] = 8] = "UPDATE_ERROR"; DatastoreCodeEnum[DatastoreCodeEnum["COLLECTION_NOT_FOUND"] = 9] = "COLLECTION_NOT_FOUND"; DatastoreCodeEnum[DatastoreCodeEnum["COLLECTION_NOT_FOUND_CACHED"] = 10] = "COLLECTION_NOT_FOUND_CACHED"; DatastoreCodeEnum[DatastoreCodeEnum["COLLECTION_FETCH_ERROR"] = 11] = "COLLECTION_FETCH_ERROR"; DatastoreCodeEnum[DatastoreCodeEnum["COLLECTION_CREATE_ERROR"] = 12] = "COLLECTION_CREATE_ERROR"; DatastoreCodeEnum[DatastoreCodeEnum["COLLECTION_FETCH_REJECT"] = 13] = "COLLECTION_FETCH_REJECT"; })(DatastoreCodeEnum || (DatastoreCodeEnum = {})); exports.MongoCode = (0, globals_ix_1.ReturnCodeFamily)('DatastoreCode', DatastoreCodeEnum); var UpstreamHttpDatastore = (function () { function UpstreamHttpDatastore(config) { var _this = this; this.knownCollections = {}; this.indexEnsuringPromise = null; this.admin = { dropCollection: function (type) { return __awaiter(_this, void 0, void 0, function () { var typename, res, e_1, concise; var _a, _b, _c, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 2, , 3]); typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); return [4, axios.default.post("".concat(this.endpoint, "/collection/drop"), { typename: typename })]; case 1: res = _e.sent(); return [2, (0, globals_ix_1.ok)(res.data.result)]; case 2: e_1 = _e.sent(); concise = ((_b = (_a = e_1.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ? (_d = (_c = e_1.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message : e_1.code; if (upstream_1.Upstream.showOperationErrors) { console.error(concise); } return [2, (0, globals_ix_1.errorResult)(e_1)]; case 3: return [2]; } }); }); } }; this.index = { checkDefinitions: function (type) { var typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); var collectionInfo = _this.knownCollections[typename]; return { definitions: collectionInfo === null || collectionInfo === void 0 ? void 0 : collectionInfo.indexDefinitions, timeSet: collectionInfo === null || collectionInfo === void 0 ? void 0 : collectionInfo.timeIndexDefinitionSet, timeUpdated: collectionInfo === null || collectionInfo === void 0 ? void 0 : collectionInfo.timeIndexUpdated, }; }, setDefinitions: function (type, indexDefinitions) { var typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); var collectionInfo = _this.knownCollections[typename]; if (!collectionInfo) { collectionInfo = _this.knownCollections[typename] = {}; } collectionInfo.indexDefinitions = indexDefinitions; collectionInfo.timeIndexDefinitionSet = Date.now(); }, create: function (type, indexDefinition) { return __awaiter(_this, void 0, void 0, function () { var typename, columnsCopy, _i, _a, columnName, indexOptions, indexOptionsArg, res, e_2, concise; var _b, _c, _d, _e; return __generator(this, function (_f) { switch (_f.label) { case 0: _f.trys.push([0, 2, , 3]); typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); columnsCopy = (0, globals_ix_1.deepCopy)(indexDefinition.columns); for (_i = 0, _a = Object.keys(columnsCopy); _i < _a.length; _i++) { columnName = _a[_i]; if (columnsCopy[columnName]) { columnsCopy[columnName] = 1; } else { columnsCopy[columnName] = -1; } } indexOptions = { name: indexDefinition.name, sparse: true }; indexOptionsArg = indexDefinition.options ? indexDefinition.options : {}; indexOptionsArg = (0, globals_ix_1.deepCopy)(indexOptionsArg); if (indexOptionsArg.unique) { indexOptions.unique = true; } if (indexOptionsArg.nonsparse) { indexOptions.sparse = false; } return [4, axios.default.post("".concat(this.endpoint, "/index/create"), { typename: typename, columns: columnsCopy, options: indexOptions })]; case 1: res = _f.sent(); return [2, (0, globals_ix_1.ok)(res.data.result)]; case 2: e_2 = _f.sent(); concise = ((_c = (_b = e_2.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.message) ? (_e = (_d = e_2.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.message : e_2.code; if (upstream_1.Upstream.showOperationErrors) { console.error(concise); } return [2, (0, globals_ix_1.errorResult)(e_2)]; case 3: return [2]; } }); }); }, delete: function (type, indexDefinition) { return __awaiter(_this, void 0, void 0, function () { var typename, e_3, concise; var _a, _b, _c, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 2, , 3]); typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); return [4, this.index.ensure(typename)]; case 1: _e.sent(); return [2, (0, globals_ix_1.ok)(true)]; case 2: e_3 = _e.sent(); concise = ((_b = (_a = e_3.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ? (_d = (_c = e_3.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message : e_3.code; if (upstream_1.Upstream.showOperationErrors) { console.error(concise); } return [2, (0, globals_ix_1.errorResult)(e_3)]; case 3: return [2]; } }); }); }, list: function (type) { return __awaiter(_this, void 0, void 0, function () { var typename, res, e_4, concise; var _a, _b, _c, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 3, , 4]); typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); return [4, this.index.ensure(typename)]; case 1: _e.sent(); return [4, axios.default.get("".concat(this.endpoint, "/index/list"), { data: { typename: typename } })]; case 2: res = _e.sent(); return [2, (0, globals_ix_1.ok)(res.data.result)]; case 3: e_4 = _e.sent(); concise = ((_b = (_a = e_4.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ? (_d = (_c = e_4.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message : e_4.code; if (upstream_1.Upstream.showOperationErrors) { console.error(concise); } return [2, (0, globals_ix_1.errorResult)(e_4)]; case 4: return [2]; } }); }); }, ensure: function (type, indexDefinitions, forceRecheck) { return __awaiter(_this, void 0, void 0, function () { var prom; var _this = this; return __generator(this, function (_a) { if (this.indexEnsuringPromise) { return [2, this.indexEnsuringPromise]; } prom = this.indexEnsuringPromise = (0, globals_ix_1.promise)(function (resolve) { return __awaiter(_this, void 0, void 0, function () { var typename, collectionInfo, e_5, concise; var _a, _b, _c, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 2, , 3]); typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); collectionInfo = this.knownCollections[typename]; if (!collectionInfo) { collectionInfo = this.knownCollections[typename] = {}; } if (indexDefinitions) { collectionInfo.indexDefinitions = indexDefinitions; forceRecheck = true; } if (!forceRecheck && collectionInfo.timeIndexUpdated) { return [2, resolve((0, globals_ix_1.ok)(true))]; } if (!indexDefinitions) { indexDefinitions = this.knownCollections[typename].indexDefinitions; } if (!indexDefinitions || Object.keys(indexDefinitions).length === 0) { collectionInfo.timeIndexUpdated = Date.now(); return [2, resolve((0, globals_ix_1.ok)(true))]; } return [4, axios.default.post("".concat(this.endpoint, "/index/ensure"), { typename: typename, indexDefinitions: indexDefinitions, forceRecheck: forceRecheck })]; case 1: _e.sent(); collectionInfo.timeIndexUpdated = Date.now(); return [2, resolve((0, globals_ix_1.ok)(true))]; case 2: e_5 = _e.sent(); concise = ((_b = (_a = e_5.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ? (_d = (_c = e_5.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message : e_5.code; if (upstream_1.Upstream.showOperationErrors) { console.error(concise); } return [2, resolve((0, globals_ix_1.errorResult)(e_5))]; case 3: return [2]; } }); }); }).finally(function () { if (prom === _this.indexEnsuringPromise) { _this.indexEnsuringPromise = null; } }); return [2, prom]; }); }); } }; this.config = config; this.endpoint = this.config.endpoint.endpoint; } UpstreamHttpDatastore.prototype.read = function (type, _gid, _v) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var typename, res, e_6, concise; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 3, , 4]); typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); return [4, this.index.ensure(typename)]; case 1: _e.sent(); if (!_gid) { return [2, exports.MongoCode.error('READ_NO_GID')]; } return [4, axios.default.get("".concat(this.endpoint, "/read"), { data: { typename: typename, _gid: _gid } })]; case 2: res = _e.sent(); return [2, (0, globals_ix_1.ok)(res.data.result)]; case 3: e_6 = _e.sent(); concise = ((_b = (_a = e_6.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ? (_d = (_c = e_6.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message : e_6.code; if (upstream_1.Upstream.showOperationErrors) { console.error(concise); } return [2, exports.MongoCode.error('READ_ERROR', e_6)]; case 4: return [2]; } }); }); }; ; UpstreamHttpDatastore.prototype.create = function (type, target, typeVersion) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var typename, targetAny, res, e_7, concise; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 3, , 4]); typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); return [4, this.index.ensure(typename)]; case 1: _e.sent(); targetAny = target; targetAny._tfn = typename; targetAny._tv = typeVersion; targetAny._v = 1; targetAny._ct = Date.now(); targetAny._ut = Date.now(); return [4, axios.default.post("".concat(this.endpoint, "/create"), { typename: typename, target: target, typeVersion: typeVersion })]; case 2: res = _e.sent(); return [2, (0, globals_ix_1.ok)(res.data.result)]; case 3: e_7 = _e.sent(); concise = ((_b = (_a = e_7.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ? (_d = (_c = e_7.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message : e_7.code; if (upstream_1.Upstream.showOperationErrors) { console.error(concise); } return [2, exports.MongoCode.error('CREATE_ERROR', e_7)]; case 4: return [2]; } }); }); }; ; UpstreamHttpDatastore.prototype.update = function (type, _gid, updater) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var typename, res, e_8, concise; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 3, , 4]); typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); return [4, this.index.ensure(typename)]; case 1: _e.sent(); if (!_gid) { return [2, exports.MongoCode.error('UPDATE_NO_GID')]; } return [4, axios.default.post("".concat(this.endpoint, "/update"), { typename: typename, _gid: _gid, updater: updater })]; case 2: res = _e.sent(); return [2, (0, globals_ix_1.ok)(res.data.result)]; case 3: e_8 = _e.sent(); concise = ((_b = (_a = e_8.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ? (_d = (_c = e_8.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message : e_8.code; if (upstream_1.Upstream.showOperationErrors) { console.error(concise); } return [2, (0, globals_ix_1.errorResult)(e_8)]; case 4: return [2]; } }); }); }; ; UpstreamHttpDatastore.prototype.delete = function (type, id) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2, (0, globals_ix_1.ok)(false)]; }); }); }; ; UpstreamHttpDatastore.prototype.find = function (type, matcher, limit, indexName) { var _a, _b, _c, _d; if (limit === void 0) { limit = 0; } if (indexName === void 0) { indexName = null; } return __awaiter(this, void 0, void 0, function () { var typename, res, e_9, concise; return __generator(this, function (_e) { switch (_e.label) { case 0: _e.trys.push([0, 3, , 4]); typename = typeof type === 'string' ? type : (0, common_iface_1.typeFullName)(type); return [4, this.index.ensure(typename)]; case 1: _e.sent(); return [4, axios.default.get("".concat(this.endpoint, "/find"), { data: { typename: typename, matcher: matcher, limit: limit, indexName: indexName } })]; case 2: res = _e.sent(); return [2, (0, globals_ix_1.ok)(res.data.result)]; case 3: e_9 = _e.sent(); concise = ((_b = (_a = e_9.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ? (_d = (_c = e_9.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message : e_9.code; if (upstream_1.Upstream.showOperationErrors) { console.error(concise); } return [2, (0, globals_ix_1.errorResult)(e_9)]; case 4: return [2]; } }); }); }; ; UpstreamHttpDatastore.prototype.list = function (type, filter) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function () { var concise; return __generator(this, function (_e) { try { } catch (e) { concise = ((_b = (_a = e.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) ? (_d = (_c = e.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.message : e.code; if (upstream_1.Upstream.showOperationErrors) { console.error(concise); } return [2, (0, globals_ix_1.errorResult)(e)]; } return [2]; }); }); }; ; return UpstreamHttpDatastore; }()); exports.UpstreamHttpDatastore = UpstreamHttpDatastore; //# sourceMappingURL=upstream-http-connector.js.map