@niledatabase/server 2.4.1-alpha.0 → 2.4.2-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,10 @@ import { ServerConfig } from '../types';
3
3
  import NileDatabase from './NileInstance';
4
4
  export default class DBManager {
5
5
  connections: Map<string, NileDatabase>;
6
+ cleared: boolean;
6
7
  private makeId;
7
8
  constructor(config: ServerConfig);
8
- getConnection(config: ServerConfig): Pool;
9
+ poolWatcher: (config: ServerConfig) => (id: undefined | null | string) => void;
10
+ getConnection: (config: ServerConfig) => Pool;
11
+ clear: (config: ServerConfig) => void;
9
12
  }
@@ -764,7 +764,7 @@ var Config = /*#__PURE__*/function () {
764
764
  error(database);
765
765
  throw new Error('HTTP error has occured');
766
766
  case 41:
767
- throw new Error('Unable to auto-configure. Please set or remove NILEDB_API, NILEDB_NAME, and NILEDB_HOST in your .env file.');
767
+ throw new Error('Unable to auto-configure. Please remove NILEDB_NAME, NILEDB_API_URL, NILEDB_POSTGRES_URL, and/or NILEDB_HOST from your environment variables.');
768
768
  case 42:
769
769
  if (typeof database === 'object') {
770
770
  _database = database, apiHost = _database.apiHost, dbHost = _database.dbHost, name = _database.name, id = _database.id;
@@ -806,9 +806,17 @@ var Config = /*#__PURE__*/function () {
806
806
  config: _config,
807
807
  logger: logger
808
808
  };
809
- this.databaseId = getDatbaseId(_envVarConfig);
810
809
  this.user = getUsername(_envVarConfig);
811
810
  this.password = getPassword(_envVarConfig);
811
+ {
812
+ if (!this.user) {
813
+ throw new Error('User is required. Set NILEDB_USER as an environment variable or set `user` in the config options.');
814
+ }
815
+ if (!this.password) {
816
+ throw new Error('Password is required. Set NILEDB_PASSWORD as an environment variable or set `password` in the config options.');
817
+ }
818
+ }
819
+ this.databaseId = getDatbaseId(_envVarConfig);
812
820
  this.databaseName = getDatabaseName(_envVarConfig);
813
821
  this._tenantId = getTenantId(_envVarConfig);
814
822
  this.debug = Boolean(_config == null ? void 0 : _config.debug);
@@ -895,6 +903,14 @@ var Eventer = /*#__PURE__*/function () {
895
903
  // store the callback function of the subscriber
896
904
  this.events[eventName].push(callback);
897
905
  };
906
+ _proto.unsubscribe = function unsubscribe(eventName, callback) {
907
+ var toRemove = this.events[eventName].findIndex(function (cb) {
908
+ return cb === callback;
909
+ });
910
+ if (toRemove !== -1) {
911
+ this.events[eventName].splice(toRemove, 1);
912
+ }
913
+ };
898
914
  return Eventer;
899
915
  }(); // tenantId manager
900
916
  var eventer = /*#__PURE__*/new Eventer();
@@ -919,6 +935,9 @@ var watchToken = function watchToken(cb) {
919
935
  var watchEvictPool = function watchEvictPool(cb) {
920
936
  return eventer.subscribe(Events.EvictPool, cb);
921
937
  };
938
+ var closeEvictPool = function closeEvictPool(cb) {
939
+ return eventer.unsubscribe(Events.EvictPool, cb);
940
+ };
922
941
  var evictPool = function evictPool(val) {
923
942
  eventer.publish(Events.EvictPool, val);
924
943
  };
@@ -1908,7 +1927,11 @@ var NileDatabase = /*#__PURE__*/function () {
1908
1927
  while (1) switch (_context2.prev = _context2.next) {
1909
1928
  case 0:
1910
1929
  info('pool failed', e);
1911
- case 1:
1930
+ if (_this.timer) {
1931
+ clearTimeout(_this.timer);
1932
+ }
1933
+ evictPool(_this.id);
1934
+ case 3:
1912
1935
  case "end":
1913
1936
  return _context2.stop();
1914
1937
  }
@@ -1922,6 +1945,8 @@ var NileDatabase = /*#__PURE__*/function () {
1922
1945
  var _proto = NileDatabase.prototype;
1923
1946
  _proto.startTimeout = function startTimeout() {
1924
1947
  var _this2 = this;
1948
+ var _Logger2 = Logger(this.config, '[NileInstance]'),
1949
+ info = _Logger2.info;
1925
1950
  if (this.timer) {
1926
1951
  clearTimeout(this.timer);
1927
1952
  }
@@ -1929,11 +1954,14 @@ var NileDatabase = /*#__PURE__*/function () {
1929
1954
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1930
1955
  while (1) switch (_context3.prev = _context3.next) {
1931
1956
  case 0:
1932
- _context3.next = 2;
1933
- return _this2.pool.end();
1934
- case 2:
1935
- evictPool(_this2.id);
1957
+ info('Pool reached idleTimeoutMillis.', _this2.id, 'evicted after', _this2.config.db.idleTimeoutMillis, 'ms');
1958
+ _context3.next = 3;
1959
+ return _this2.pool.end(function () {
1960
+ // something odd going on here. Without the callback, pool.end() is flakey
1961
+ });
1936
1962
  case 3:
1963
+ evictPool(_this2.id);
1964
+ case 4:
1937
1965
  case "end":
1938
1966
  return _context3.stop();
1939
1967
  }
@@ -1943,9 +1971,9 @@ var NileDatabase = /*#__PURE__*/function () {
1943
1971
  return NileDatabase;
1944
1972
  }();
1945
1973
  function makeAfterCreate(config) {
1946
- var _Logger2 = Logger(config, '[afterCreate]'),
1947
- warn = _Logger2.warn,
1948
- info = _Logger2.info;
1974
+ var _Logger3 = Logger(config, '[afterCreate]'),
1975
+ warn = _Logger3.warn,
1976
+ info = _Logger3.info;
1949
1977
  return function (conn, done) {
1950
1978
  conn.on('error', function errorHandler(error) {
1951
1979
  warn('Connection was terminated by server', error);
@@ -1975,21 +2003,59 @@ function makeAfterCreate(config) {
1975
2003
  }
1976
2004
 
1977
2005
  var DBManager = /*#__PURE__*/function () {
1978
- function DBManager(config) {
2006
+ function DBManager(_config) {
1979
2007
  var _this = this;
1980
2008
  this.connections = void 0;
1981
- var _Logger = Logger(config, '[DBManager]'),
1982
- info = _Logger.info;
2009
+ this.cleared = void 0;
2010
+ this.poolWatcher = function (config) {
2011
+ return function (id) {
2012
+ var _Logger = Logger(config, '[DBManager]'),
2013
+ info = _Logger.info;
2014
+ if (id && _this.connections.has(id)) {
2015
+ info('Removing', id, 'from db connection pool.');
2016
+ _this.connections["delete"](id);
2017
+ }
2018
+ };
2019
+ };
2020
+ this.getConnection = function (config) {
2021
+ var _Logger2 = Logger(config, '[DBManager]'),
2022
+ info = _Logger2.info;
2023
+ var id = _this.makeId(config.tenantId, config.userId);
2024
+ var existing = _this.connections.get(id);
2025
+ info('# of instances:', _this.connections.size);
2026
+ if (existing) {
2027
+ info('returning existing', id);
2028
+ existing.startTimeout();
2029
+ return existing.pool;
2030
+ }
2031
+ var newOne = new NileDatabase(new Config(config), id);
2032
+ _this.connections.set(id, newOne);
2033
+ info('created new', id);
2034
+ info('# of instances:', _this.connections.size);
2035
+ // resume listening to the evict pool if a connection is requested.
2036
+ if (_this.cleared) {
2037
+ _this.cleared = false;
2038
+ watchEvictPool(_this.poolWatcher(config));
2039
+ }
2040
+ return newOne.pool;
2041
+ };
2042
+ this.clear = function (config) {
2043
+ var _Logger3 = Logger(config, '[DBManager]'),
2044
+ info = _Logger3.info;
2045
+ info('Clearing all connections', _this.connections.size);
2046
+ closeEvictPool(_this.poolWatcher(config));
2047
+ _this.cleared = true;
2048
+ _this.connections.clear();
2049
+ };
2050
+ var _Logger4 = Logger(_config, '[DBManager]'),
2051
+ _info = _Logger4.info;
2052
+ this.cleared = false;
1983
2053
  this.connections = new Map();
1984
2054
  // add the base one, so you can at least query
1985
- var id = this.makeId();
1986
- info('constructor', id);
1987
- this.connections.set(id, new NileDatabase(new Config(config), id));
1988
- watchEvictPool(function (id) {
1989
- if (id && _this.connections.has(id)) {
1990
- _this.connections["delete"](id);
1991
- }
1992
- });
2055
+ var _id = this.makeId();
2056
+ _info('constructor', _id);
2057
+ this.connections.set(_id, new NileDatabase(new Config(_config), _id));
2058
+ watchEvictPool(this.poolWatcher(_config));
1993
2059
  }
1994
2060
  var _proto = DBManager.prototype;
1995
2061
  _proto.makeId = function makeId(tenantId, userId) {
@@ -2001,22 +2067,6 @@ var DBManager = /*#__PURE__*/function () {
2001
2067
  }
2002
2068
  return 'base';
2003
2069
  };
2004
- _proto.getConnection = function getConnection(config) {
2005
- var _Logger2 = Logger(config, '[DBManager]'),
2006
- info = _Logger2.info;
2007
- var id = this.makeId(config.tenantId, config.userId);
2008
- var existing = this.connections.get(id);
2009
- info('# of instances:', this.connections.size);
2010
- if (existing) {
2011
- info('returning existing', id);
2012
- return existing.pool;
2013
- }
2014
- var newOne = new NileDatabase(new Config(config), id);
2015
- this.connections.set(id, newOne);
2016
- info('created new', id);
2017
- info('# of instances:', this.connections.size);
2018
- return newOne.pool;
2019
- };
2020
2070
  return DBManager;
2021
2071
  }();
2022
2072
 
@@ -2074,10 +2124,11 @@ var Server = /*#__PURE__*/function () {
2074
2124
  case 2:
2075
2125
  updatedConfig = _context.sent;
2076
2126
  this.setConfig(updatedConfig);
2127
+ this.manager.clear(this.config);
2077
2128
  this.manager = new DBManager(this.config);
2078
- this.api = _init(updatedConfig);
2129
+ this.api = _init(this.config);
2079
2130
  return _context.abrupt("return", this);
2080
- case 7:
2131
+ case 8:
2081
2132
  case "end":
2082
2133
  return _context.stop();
2083
2134
  }