@niledatabase/server 2.2.0-alpha.3 → 2.2.1-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.
@@ -0,0 +1,3 @@
1
+ import { Pool } from 'pg';
2
+ import { Config } from '../utils/Config';
3
+ export declare function createProxyForPool(pool: Pool, config: Config): Pool;
@@ -456,7 +456,7 @@ function Logger(config) {
456
456
  }
457
457
  },
458
458
  warn: function warn() {
459
- {
459
+ if (config != null && config.debug) {
460
460
  var _console2;
461
461
  for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
462
462
  args[_key3] = arguments[_key3];
@@ -466,14 +466,12 @@ function Logger(config) {
466
466
  }
467
467
  },
468
468
  error: function error() {
469
- if (config != null && config.debug) {
470
- var _console3;
471
- for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
472
- args[_key4] = arguments[_key4];
473
- }
474
- // eslint-disable-next-line no-console
475
- (_console3 = console).error.apply(_console3, ['[niledb]', '[ERROR]'].concat(params, args));
469
+ var _console3;
470
+ for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
471
+ args[_key4] = arguments[_key4];
476
472
  }
473
+ // eslint-disable-next-line no-console
474
+ (_console3 = console).error.apply(_console3, ['[niledb]', '[ERROR]'].concat(params, args));
477
475
  }
478
476
  };
479
477
  }
@@ -539,7 +537,6 @@ var getDatabaseName = function getDatabaseName(cfg) {
539
537
  var config = cfg.config,
540
538
  logger = cfg.logger;
541
539
  var _Logger5 = Logger(config, '[databaseName]'),
542
- warn = _Logger5.warn,
543
540
  info = _Logger5.info;
544
541
  if (config != null && config.databaseName) {
545
542
  logger && info(logger, 'config', config.databaseName);
@@ -549,7 +546,6 @@ var getDatabaseName = function getDatabaseName(cfg) {
549
546
  logger && info(logger, 'config', process.env.NILEDB_NAME);
550
547
  return process.env.NILEDB_NAME;
551
548
  }
552
- warn(logger, 'config', 'No database has been set. Set NILEDB_NAME in .env or call `nile.init()`');
553
549
  return null;
554
550
  };
555
551
  var getTenantId = function getTenantId(cfg) {
@@ -664,16 +660,23 @@ var Config = /*#__PURE__*/function () {
664
660
  this.configure = /*#__PURE__*/function () {
665
661
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
666
662
  var _config$api$cookieKey, _config$api;
667
- var _Logger, info, error, envVarConfig, basePath, host, port, databaseName, url, res, database, possibleError, json, message, _database, apiHost, dbHost, name, id, dburl, apiurl;
663
+ var envVarConfig, host, _Logger, info, error, basePath, port, databaseName, url, res, database, possibleError, json, message, _database, apiHost, dbHost, name, id, dburl, apiurl;
668
664
  return _regeneratorRuntime().wrap(function _callee$(_context) {
669
665
  while (1) switch (_context.prev = _context.next) {
670
666
  case 0:
671
- _Logger = Logger(config, '[init]'), info = _Logger.info, error = _Logger.error;
672
667
  envVarConfig = {
673
668
  config: config
674
669
  };
675
- basePath = getBasePath(envVarConfig);
676
670
  host = getDbHost(envVarConfig);
671
+ _Logger = Logger(config, '[init]'), info = _Logger.info, error = _Logger.error;
672
+ if (!(host && _this.databaseName && _this.databaseId)) {
673
+ _context.next = 6;
674
+ break;
675
+ }
676
+ info('Alreaady configured, aborting fetch');
677
+ return _context.abrupt("return", _this);
678
+ case 6:
679
+ basePath = getBasePath(envVarConfig);
677
680
  port = getDbPort(envVarConfig);
678
681
  databaseName = getDatabaseName({
679
682
  config: config,
@@ -684,7 +687,7 @@ var Config = /*#__PURE__*/function () {
684
687
  url.searchParams.set('databaseName', databaseName);
685
688
  }
686
689
  info(url.href);
687
- _context.next = 11;
690
+ _context.next = 14;
688
691
  return fetch(url, {
689
692
  headers: {
690
693
  Authorization: "Bearer " + getInfoBearer({
@@ -692,13 +695,13 @@ var Config = /*#__PURE__*/function () {
692
695
  })
693
696
  }
694
697
  });
695
- case 11:
698
+ case 14:
696
699
  res = _context.sent;
697
700
  possibleError = res.clone();
698
- _context.prev = 13;
699
- _context.next = 16;
701
+ _context.prev = 16;
702
+ _context.next = 19;
700
703
  return res.json();
701
- case 16:
704
+ case 19:
702
705
  json = _context.sent;
703
706
  if (res.status === 404) {
704
707
  info('is the configured databaseName correct?');
@@ -710,49 +713,44 @@ var Config = /*#__PURE__*/function () {
710
713
  } else {
711
714
  database = json;
712
715
  }
713
- _context.next = 28;
716
+ _context.next = 31;
714
717
  break;
715
- case 21:
716
- _context.prev = 21;
717
- _context.t0 = _context["catch"](13);
718
- _context.next = 25;
718
+ case 24:
719
+ _context.prev = 24;
720
+ _context.t0 = _context["catch"](16);
721
+ _context.next = 28;
719
722
  return possibleError.text();
720
- case 25:
723
+ case 28:
721
724
  message = _context.sent;
722
725
  error(message);
723
726
  database = {
724
727
  message: message
725
728
  };
726
- case 28:
727
- if (!(!host || !_this.databaseName || !_this.databaseId)) {
728
- _context.next = 39;
729
- break;
730
- }
729
+ case 31:
731
730
  info('[fetched database]', database);
732
731
  if (!('message' in database)) {
733
- _context.next = 38;
732
+ _context.next = 40;
734
733
  break;
735
734
  }
736
735
  if (!('statusCode' in database)) {
737
- _context.next = 37;
736
+ _context.next = 39;
738
737
  break;
739
738
  }
740
739
  error(database);
741
740
  throw new Error('HTTP error has occured');
742
- case 37:
741
+ case 39:
743
742
  throw new Error('Unable to auto-configure. Please set or remove NILEDB_API, NILEDB_NAME, and NILEDB_HOST in your .env file.');
744
- case 38:
743
+ case 40:
745
744
  if (typeof database === 'object') {
746
745
  _database = database, apiHost = _database.apiHost, dbHost = _database.dbHost, name = _database.name, id = _database.id;
747
746
  _this.databaseId = id;
748
747
  _this.databaseName = name;
749
- // gotta do something dumb here
750
748
  dburl = new URL(dbHost);
751
749
  apiurl = new URL(apiHost);
752
750
  host = dburl.host;
753
751
  basePath = apiurl.origin;
754
752
  }
755
- case 39:
753
+ case 41:
756
754
  _this.api = new ApiConfig({
757
755
  basePath: basePath,
758
756
  cookieKey: (_config$api$cookieKey = config == null || (_config$api = config.api) == null ? void 0 : _config$api.cookieKey) != null ? _config$api$cookieKey : 'token',
@@ -769,11 +767,11 @@ var Config = /*#__PURE__*/function () {
769
767
  }, typeof (config == null ? void 0 : config.db) === 'object' ? config.db : {});
770
768
  info('[config set]', _this);
771
769
  return _context.abrupt("return", _this);
772
- case 43:
770
+ case 45:
773
771
  case "end":
774
772
  return _context.stop();
775
773
  }
776
- }, _callee, null, [[13, 21]]);
774
+ }, _callee, null, [[16, 24]]);
777
775
  }));
778
776
  return function (_x) {
779
777
  return _ref2.apply(this, arguments);
@@ -803,9 +801,11 @@ var Config = /*#__PURE__*/function () {
803
801
  user: this.user,
804
802
  password: this.password,
805
803
  host: _host,
806
- port: _port,
807
- database: this.databaseName
804
+ port: _port
808
805
  }, typeof (_config == null ? void 0 : _config.db) === 'object' ? _config.db : {});
806
+ if (this.databaseName) {
807
+ this.db.database = this.databaseName;
808
+ }
809
809
  }
810
810
  return _createClass(Config, [{
811
811
  key: "tenantId",
@@ -1787,6 +1787,34 @@ var Tenants = /*#__PURE__*/function (_Config) {
1787
1787
  }]);
1788
1788
  }(Config);
1789
1789
 
1790
+ function createProxyForPool(pool, config) {
1791
+ var _Logger = Logger(config, '[pool]'),
1792
+ info = _Logger.info,
1793
+ error = _Logger.error;
1794
+ return new Proxy(pool, {
1795
+ get: function get(target, property) {
1796
+ if (property === 'query') {
1797
+ if (!config.user || !config.password) {
1798
+ error('Cannot connect to the database. User and/or password are missing. Generate them at https://console.thenile.dev');
1799
+ } else if (!config.db.database) {
1800
+ error('Database id is missing from the config. Either call `nile.init()` when you create the NileDB server or set NILEDB_ID in your .env');
1801
+ }
1802
+ var caller = target[property];
1803
+ return function query() {
1804
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1805
+ args[_key] = arguments[_key];
1806
+ }
1807
+ info.apply(void 0, args);
1808
+ // @ts-expect-error - not mine
1809
+ var called = caller.apply(this, args);
1810
+ return called;
1811
+ };
1812
+ }
1813
+ return target[property];
1814
+ }
1815
+ });
1816
+ }
1817
+
1790
1818
  var _excluded = ["afterCreate"];
1791
1819
  var NileDatabase = /*#__PURE__*/function () {
1792
1820
  function NileDatabase(config, id) {
@@ -1811,7 +1839,7 @@ var NileDatabase = /*#__PURE__*/function () {
1811
1839
  config.db = poolConfig;
1812
1840
  this.config = config;
1813
1841
  info(this.config);
1814
- this.pool = new pg.Pool(remaining);
1842
+ this.pool = createProxyForPool(new pg.Pool(remaining), this.config);
1815
1843
  if (typeof afterCreate === 'function') {
1816
1844
  warn('Providing an pool configuration will stop automatic tenant context setting.');
1817
1845
  }
@@ -2116,5 +2144,7 @@ var Server = /*#__PURE__*/function () {
2116
2144
  }]);
2117
2145
  }();
2118
2146
 
2147
+ module.exports = Server;
2148
+
2119
2149
  exports.default = Server;
2120
2150
  //# sourceMappingURL=server.cjs.development.js.map