UNPKG

aspire_core

Version:

Core api

92 lines (80 loc) 3.02 kB
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); Object.defineProperty(exports, "__esModule", { value: true }); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /* @flow */ var BaseRepository = function () { function BaseRepository(pg, dburl) { _classCallCheck(this, BaseRepository); this.pg = pg; this.dburl = dburl; } _createClass(BaseRepository, [{ key: "getConnect", value: function getConnect() { var _this = this; return new Promise(function (resolve, reject) { _this.pg.connect(_this.dburl, function (err, client, done) { if (err) reject({ type: "DB", message: "Failed to get connection " + err }); _this._client = client; _this._done = done; resolve(); }); }); } }, { key: "doQuery", value: function doQuery(query, AError) { var _this = this; return new Promise(function (resolve, reject) { _this.getConnect().then(function () { try { _this._client.query(query, function (err, result) { _this.done(); if (err) { reject(new AError(err.message + " Query:(" + query + ")")); } else { result.rows.result = result; resolve(result.rows); } }); } catch (err) { _this.done(); if (AError) reject(new AError(err));else reject(err); } }).catch(function (err) { _this.done(); if (AError) reject(new AError(err));else reject(err); }); }); } }, { key: "doParamQuery", value: function doParamQuery(query, params, AError) { var _this = this; return new Promise(function (resolve, reject) { _this.getConnect().then(function () { _this._client.query(query, params, function (err, result) { _this.done(); if (err) reject(err);else { result.rows.result = result; resolve(result.rows); } }); }).catch(function (err) { _this.done(); if (AError) reject(new AError(err));else reject(err); }); }); } }, { key: "done", value: function done() { if (this._done) this._done(); } }]); return BaseRepository; }(); exports.default = BaseRepository;