UNPKG

ts-comply

Version:

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

406 lines 24.6 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; 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 __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { var useValue = arguments.length > 2; for (var i = 0; i < initializers.length; i++) { value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); } return useValue ? value : void 0; }; var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); var _, done = false; for (var i = decorators.length - 1; i >= 0; i--) { var context = {}; for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; for (var p in contextIn.access) context.access[p] = contextIn.access[p]; context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); if (kind === "accessor") { if (result === void 0) continue; if (result === null || typeof result !== "object") throw new TypeError("Object expected"); if (_ = accept(result.get)) descriptor.get = _; if (_ = accept(result.set)) descriptor.set = _; if (_ = accept(result.init)) initializers.push(_); } else if (_ = accept(result)) { if (kind === "field") initializers.push(_); else descriptor[key] = _; } } if (target) Object.defineProperty(target, contextIn.name, descriptor); done = true; }; 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 }; } }; var _this = this; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpstreamDatastoreMongoOverHttp = void 0; var mongodb_1 = require("./mongodb"); var globals_ix_1 = require("../../../src/common/globals.ix"); var http_shim_1 = require("../../http/http.shim"); var env_profile_1 = require("../../../src/common/env/env.profile"); var MongoDB = __importStar(require("mongodb")); var security_common_1 = require("../../../src/common/security/security.common"); var scopeName = "mongo-over-http-server;pid=".concat(process.pid); var roles = { ADMIN: 99, NO_AUTH: 0, }; var UpstreamDatastoreMongoOverHttp = exports.UpstreamDatastoreMongoOverHttp = function () { var _a; var _instanceExtraInitializers = []; var _upstreamRead_decorators; var _upstreamCreate_decorators; var _upstreamUpdate_decorators; var _upstreamDelete_decorators; var _upstreamFind_decorators; var _upstreamList_decorators; var _upstreamCollectionDrop_decorators; var _upstreamIndexCreate_decorators; var _upstreamIndexList_decorators; var _upstreamIndexEnsure_decorators; return _a = (function (_super) { __extends(UpstreamDatastoreMongoOverHttp, _super); function UpstreamDatastoreMongoOverHttp() { var _this = this; var appProfile = (0, env_profile_1.envVar)('UPSTREAM_MONGO_HTTP_SERVER_APP_PROFILE', env_profile_1.APP.PROFILE); var mongoDbLocalEndpoint = (0, env_profile_1.envVar)('UPSTREAM_MONGO_HTTP_SERVER_LOCAL_MONGODB_ENDPOINT', 'localhost:27017'); var serverKey = (0, env_profile_1.envVar)('UPSTREAM_MONGO_HTTP_SERVER_KEY', 'upstreamMongoOverHttp'); _this = _super.call(this, { name: "mongodb-over-http-".concat(serverKey), env: appProfile, type: http_shim_1.HttpBaseLib.EXPRESS, scopeName: scopeName, security: { accessor: { required: false, baseToken: "<secret.".concat(serverKey, ".token>"), }, secureChannel: { enabled: true, required: false, encryption: security_common_1.SecureChannelTypes.ECC_4Q, signingKey: "<secret.".concat(serverKey, ".signingKey>") }, }, startOptions: { port: (0, globals_ix_1.toBe)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["<secret.", ".port ?: number:", ">"], ["<secret.", ".port ?: number:", ">"])), serverKey, UpstreamDatastoreMongoOverHttp.defaultPort) }, }) || this; _this.ds = (__runInitializers(_this, _instanceExtraInitializers), void 0); _this.mongoDbLocalEndpoint = mongoDbLocalEndpoint; if (!serverKey) { throw new Error("serverKey (UPSTREAM_MONGO_HTTP_SERVER_KEY) cannot be null"); } _this.apiVersion = 'v1'; _this.apiPath = _this.configGlobal.api.basePath; _this.addDefaultProcessor(http_shim_1.ReqProcessor.BASIC); _this.initialize(); return _this; } UpstreamDatastoreMongoOverHttp.prototype.initialize = function () { this.ds = new mongodb_1.UpstreamDatastoreMongo({ path: 'local', endpoint: { type: 'mongo', endpoint: 'localhost', credentials: { endpoint: this.mongoDbLocalEndpoint } }, }); }; UpstreamDatastoreMongoOverHttp.prototype.upstreamRead = function (op) { return __awaiter(this, void 0, void 0, function () { var typename, _gid, res; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: typename = (_a = op.params, _a.typename), _gid = _a._gid; return [4, this.ds.read(typename, _gid)]; case 1: res = _b.sent(); return [2, res.bad ? op.raise(res) : op.returnJson(res.data)]; } }); }); }; UpstreamDatastoreMongoOverHttp.prototype.upstreamCreate = function (op) { return __awaiter(this, void 0, void 0, function () { var typename, target, typeVersion, res; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: typename = (_a = op.params, _a.typename), target = _a.target, typeVersion = _a.typeVersion; target._id = new MongoDB.ObjectId(); return [4, this.ds.create(typename, target, typeVersion)]; case 1: res = _b.sent(); return [2, res.bad ? op.raise(res) : op.returnJson(res.data)]; } }); }); }; UpstreamDatastoreMongoOverHttp.prototype.upstreamUpdate = function (op) { return __awaiter(this, void 0, void 0, function () { var typename, _gid, updater, res; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: typename = (_a = op.params, _a.typename), _gid = _a._gid, updater = _a.updater; return [4, this.ds.update(typename, _gid, updater)]; case 1: res = _b.sent(); return [2, res.bad ? op.raise(res) : op.returnJson(res.data)]; } }); }); }; UpstreamDatastoreMongoOverHttp.prototype.upstreamDelete = function (op) { return __awaiter(this, void 0, void 0, function () { var typename, _gid, res; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: typename = (_a = op.params, _a.typename), _gid = _a._gid; return [4, this.ds.delete(typename, _gid)]; case 1: res = _b.sent(); return [2, res.bad ? op.raise(res) : op.returnJson(res.data)]; } }); }); }; UpstreamDatastoreMongoOverHttp.prototype.upstreamFind = function (op) { return __awaiter(this, void 0, void 0, function () { var typename, limit, matcher, indexName, collection, parsedMatcher, result, resultArray, e_1; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: typename = (_a = op.params, _a.typename), limit = _a.limit, matcher = _a.matcher, indexName = _a.indexName; _b.label = 1; case 1: _b.trys.push([1, 4, , 5]); return [4, this.ds.ensureCollection(typename)]; case 2: collection = _b.sent(); parsedMatcher = this.ds.getMatcherRubric(matcher); result = indexName ? collection.find(parsedMatcher).hint(indexName) : collection.find(parsedMatcher); if (limit) { result = result.limit(limit); } return [4, result.toArray()]; case 3: resultArray = (_b.sent()).map(function (a) { a._id = a._id.toString(); return a; }); return [2, op.returnJson(resultArray)]; case 4: e_1 = _b.sent(); return [2, op.raise(e_1)]; case 5: return [2]; } }); }); }; UpstreamDatastoreMongoOverHttp.prototype.upstreamList = function (op) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2]; }); }); }; UpstreamDatastoreMongoOverHttp.prototype.upstreamCollectionDrop = function (op) { return __awaiter(this, void 0, void 0, function () { var typename, res, e_2; return __generator(this, function (_a) { switch (_a.label) { case 0: typename = op.params.typename; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4, this.ds.admin.dropCollection(typename)]; case 2: res = _a.sent(); return [2, op.returnJson(res.data)]; case 3: e_2 = _a.sent(); return [2, op.raise(e_2)]; case 4: return [2]; } }); }); }; UpstreamDatastoreMongoOverHttp.prototype.upstreamIndexCreate = function (op) { return __awaiter(this, void 0, void 0, function () { var typename, columns, options, collection, e_3; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: typename = (_a = op.params, _a.typename), columns = _a.columns, options = _a.options; return [4, this.ds.ensureCollection(typename)]; case 1: collection = _b.sent(); _b.label = 2; case 2: _b.trys.push([2, 4, , 5]); return [4, collection.createIndex(columns, options)]; case 3: _b.sent(); return [2, op.returnJson(true)]; case 4: e_3 = _b.sent(); return [2, op.raise(e_3)]; case 5: return [2]; } }); }); }; UpstreamDatastoreMongoOverHttp.prototype.upstreamIndexList = function (op) { return __awaiter(this, void 0, void 0, function () { var typename, collection, indexes, e_4; return __generator(this, function (_a) { switch (_a.label) { case 0: typename = op.params.typename; return [4, this.ds.ensureCollection(typename)]; case 1: collection = _a.sent(); _a.label = 2; case 2: _a.trys.push([2, 4, , 5]); return [4, collection.indexes()]; case 3: indexes = _a.sent(); return [2, op.returnJson(indexes)]; case 4: e_4 = _a.sent(); return [2, op.raise(e_4)]; case 5: return [2]; } }); }); }; UpstreamDatastoreMongoOverHttp.prototype.upstreamIndexEnsure = function (op) { return __awaiter(this, void 0, void 0, function () { var typename, indexDefinitions, forceRecheck; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: typename = (_a = op.params, _a.typename), indexDefinitions = _a.indexDefinitions, forceRecheck = _a.forceRecheck; return [4, this.ds.ensureCollection(typename)]; case 1: _b.sent(); return [4, this.ds.index.ensure(typename, indexDefinitions, forceRecheck)]; case 2: _b.sent(); return [2, op.returnJson(true)]; } }); }); }; return UpstreamDatastoreMongoOverHttp; }(http_shim_1.HttpServerShim)), (function () { _upstreamRead_decorators = [http_shim_1.HTTP.GET("/read")]; _upstreamCreate_decorators = [http_shim_1.HTTP.POST("/create")]; _upstreamUpdate_decorators = [http_shim_1.HTTP.POST("/update")]; _upstreamDelete_decorators = [http_shim_1.HTTP.POST("/delete")]; _upstreamFind_decorators = [http_shim_1.HTTP.GET("/find")]; _upstreamList_decorators = [http_shim_1.HTTP.GET("/list")]; _upstreamCollectionDrop_decorators = [http_shim_1.HTTP.POST("/collection/drop")]; _upstreamIndexCreate_decorators = [http_shim_1.HTTP.POST("/index/create")]; _upstreamIndexList_decorators = [http_shim_1.HTTP.GET("/index/list")]; _upstreamIndexEnsure_decorators = [http_shim_1.HTTP.POST("/index/ensure")]; __esDecorate(_a, null, _upstreamRead_decorators, { kind: "method", name: "upstreamRead", static: false, private: false, access: { has: function (obj) { return "upstreamRead" in obj; }, get: function (obj) { return obj.upstreamRead; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _upstreamCreate_decorators, { kind: "method", name: "upstreamCreate", static: false, private: false, access: { has: function (obj) { return "upstreamCreate" in obj; }, get: function (obj) { return obj.upstreamCreate; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _upstreamUpdate_decorators, { kind: "method", name: "upstreamUpdate", static: false, private: false, access: { has: function (obj) { return "upstreamUpdate" in obj; }, get: function (obj) { return obj.upstreamUpdate; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _upstreamDelete_decorators, { kind: "method", name: "upstreamDelete", static: false, private: false, access: { has: function (obj) { return "upstreamDelete" in obj; }, get: function (obj) { return obj.upstreamDelete; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _upstreamFind_decorators, { kind: "method", name: "upstreamFind", static: false, private: false, access: { has: function (obj) { return "upstreamFind" in obj; }, get: function (obj) { return obj.upstreamFind; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _upstreamList_decorators, { kind: "method", name: "upstreamList", static: false, private: false, access: { has: function (obj) { return "upstreamList" in obj; }, get: function (obj) { return obj.upstreamList; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _upstreamCollectionDrop_decorators, { kind: "method", name: "upstreamCollectionDrop", static: false, private: false, access: { has: function (obj) { return "upstreamCollectionDrop" in obj; }, get: function (obj) { return obj.upstreamCollectionDrop; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _upstreamIndexCreate_decorators, { kind: "method", name: "upstreamIndexCreate", static: false, private: false, access: { has: function (obj) { return "upstreamIndexCreate" in obj; }, get: function (obj) { return obj.upstreamIndexCreate; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _upstreamIndexList_decorators, { kind: "method", name: "upstreamIndexList", static: false, private: false, access: { has: function (obj) { return "upstreamIndexList" in obj; }, get: function (obj) { return obj.upstreamIndexList; } } }, null, _instanceExtraInitializers); __esDecorate(_a, null, _upstreamIndexEnsure_decorators, { kind: "method", name: "upstreamIndexEnsure", static: false, private: false, access: { has: function (obj) { return "upstreamIndexEnsure" in obj; }, get: function (obj) { return obj.upstreamIndexEnsure; } } }, null, _instanceExtraInitializers); })(), _a.defaultPort = 28000, _a; }(); var templateObject_1; //# sourceMappingURL=mongodb-over-http.js.map