UNPKG

seamless-cloud

Version:

JavaScript client for Seamless.cloud (web and node)

111 lines 4.92 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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SeamlessClient = void 0; const fetch_1 = require("../shared/fetch"); class SeamlessClient { constructor(cfg, fetchFn) { this.cfg = cfg; this.fetchFn = fetchFn; this.cfg = cfg; this.fetchFn = fetchFn; this.debug = cfg.debug || false; if (!this.cfg.region) { this.cfg.region = 'us-east-2'; } } getBaseUrl() { return `https://${this.cfg.region}.sqlapi.seamless.cloud/sqlapi`; } /** * Find and return a single row. No result throws Error with optional custom message. */ findOneOrThrow(queryKey, query, errorMsg) { var _a; return __awaiter(this, void 0, void 0, function* () { const response = yield this.query(queryKey, query); const rows = (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.results; if (!rows || rows.length === 0) { throw new Error(errorMsg || 'Record not found'); } return rows[0]; }); } /** * Find and return a single row. No result returns boolean false. */ findOne(queryKey, query) { var _a; return __awaiter(this, void 0, void 0, function* () { const response = yield this.query(queryKey, query); const rows = (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.results; if (!rows || rows.length === 0) { return false; } return rows[0]; }); } /** * Find many rows (Always returns an array. No results = empty array) */ findMany(queryKey, query) { var _a; return __awaiter(this, void 0, void 0, function* () { const response = yield this.query(queryKey, query); const rows = (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.results; return rows || []; }); } /** * Run SQL Query */ query(queryKey, queryObject) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const { querySql, queryVars } = queryObject; const seamlessUrl = this.getBaseUrl(); const queryParams = { account: this.cfg.account, apiKey: this.cfg.apiKey, project: this.cfg.project, queryKey, querySql, queryVars, }; if (this.debug) { const { apiKey } = queryParams, logParams = __rest(queryParams, ["apiKey"]); // Don't log out apiKey console.log('[seamless-cloud query]: ', seamlessUrl, logParams); } const beforeQueryMs = Date.now(); const results = yield (0, fetch_1.fetchJSON)(this.fetchFn, 'POST', seamlessUrl, queryParams); const afterQueryMs = Date.now(); if (this.debug) { console.log('[seamless-cloud timeMs]: ', (_a = results === null || results === void 0 ? void 0 : results.data) === null || _a === void 0 ? void 0 : _a.timeMs); console.log('[seamless-cloud rowCount]: ', (_b = results === null || results === void 0 ? void 0 : results.data) === null || _b === void 0 ? void 0 : _b.rowCount); console.log('[seamless-cloud responseTimeMs]: ', afterQueryMs - beforeQueryMs); } return results; }); } } exports.SeamlessClient = SeamlessClient; //# sourceMappingURL=SeamlessClient.js.map