@niledatabase/server 2.2.0 → 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.
@@ -452,7 +452,7 @@ function Logger(config) {
452
452
  }
453
453
  },
454
454
  warn: function warn() {
455
- if (process.env.NODE_ENV !== 'test') {
455
+ if (config != null && config.debug) {
456
456
  var _console2;
457
457
  for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
458
458
  args[_key3] = arguments[_key3];
@@ -462,14 +462,12 @@ function Logger(config) {
462
462
  }
463
463
  },
464
464
  error: function error() {
465
- if (config != null && config.debug) {
466
- var _console3;
467
- for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
468
- args[_key4] = arguments[_key4];
469
- }
470
- // eslint-disable-next-line no-console
471
- (_console3 = console).error.apply(_console3, ['[niledb]', '[ERROR]'].concat(params, args));
465
+ var _console3;
466
+ for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
467
+ args[_key4] = arguments[_key4];
472
468
  }
469
+ // eslint-disable-next-line no-console
470
+ (_console3 = console).error.apply(_console3, ['[niledb]', '[ERROR]'].concat(params, args));
473
471
  }
474
472
  };
475
473
  }
@@ -535,7 +533,6 @@ var getDatabaseName = function getDatabaseName(cfg) {
535
533
  var config = cfg.config,
536
534
  logger = cfg.logger;
537
535
  var _Logger5 = Logger(config, '[databaseName]'),
538
- warn = _Logger5.warn,
539
536
  info = _Logger5.info;
540
537
  if (config != null && config.databaseName) {
541
538
  logger && info(logger, 'config', config.databaseName);
@@ -545,7 +542,6 @@ var getDatabaseName = function getDatabaseName(cfg) {
545
542
  logger && info(logger, 'config', process.env.NILEDB_NAME);
546
543
  return process.env.NILEDB_NAME;
547
544
  }
548
- warn(logger, 'config', 'No database has been set. Set NILEDB_NAME in .env or call `nile.init()`');
549
545
  return null;
550
546
  };
551
547
  var getTenantId = function getTenantId(cfg) {
@@ -660,16 +656,23 @@ var Config = /*#__PURE__*/function () {
660
656
  this.configure = /*#__PURE__*/function () {
661
657
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
662
658
  var _config$api$cookieKey, _config$api;
663
- var _Logger, info, error, envVarConfig, basePath, host, port, databaseName, url, res, database, possibleError, json, message, _database, apiHost, dbHost, name, id, dburl, apiurl;
659
+ var envVarConfig, host, _Logger, info, error, basePath, port, databaseName, url, res, database, possibleError, json, message, _database, apiHost, dbHost, name, id, dburl, apiurl;
664
660
  return _regeneratorRuntime().wrap(function _callee$(_context) {
665
661
  while (1) switch (_context.prev = _context.next) {
666
662
  case 0:
667
- _Logger = Logger(config, '[init]'), info = _Logger.info, error = _Logger.error;
668
663
  envVarConfig = {
669
664
  config: config
670
665
  };
671
- basePath = getBasePath(envVarConfig);
672
666
  host = getDbHost(envVarConfig);
667
+ _Logger = Logger(config, '[init]'), info = _Logger.info, error = _Logger.error;
668
+ if (!(host && _this.databaseName && _this.databaseId)) {
669
+ _context.next = 6;
670
+ break;
671
+ }
672
+ info('Alreaady configured, aborting fetch');
673
+ return _context.abrupt("return", _this);
674
+ case 6:
675
+ basePath = getBasePath(envVarConfig);
673
676
  port = getDbPort(envVarConfig);
674
677
  databaseName = getDatabaseName({
675
678
  config: config,
@@ -680,7 +683,7 @@ var Config = /*#__PURE__*/function () {
680
683
  url.searchParams.set('databaseName', databaseName);
681
684
  }
682
685
  info(url.href);
683
- _context.next = 11;
686
+ _context.next = 14;
684
687
  return fetch(url, {
685
688
  headers: {
686
689
  Authorization: "Bearer " + getInfoBearer({
@@ -688,13 +691,13 @@ var Config = /*#__PURE__*/function () {
688
691
  })
689
692
  }
690
693
  });
691
- case 11:
694
+ case 14:
692
695
  res = _context.sent;
693
696
  possibleError = res.clone();
694
- _context.prev = 13;
695
- _context.next = 16;
697
+ _context.prev = 16;
698
+ _context.next = 19;
696
699
  return res.json();
697
- case 16:
700
+ case 19:
698
701
  json = _context.sent;
699
702
  if (res.status === 404) {
700
703
  info('is the configured databaseName correct?');
@@ -706,53 +709,48 @@ var Config = /*#__PURE__*/function () {
706
709
  } else {
707
710
  database = json;
708
711
  }
709
- _context.next = 28;
712
+ _context.next = 31;
710
713
  break;
711
- case 21:
712
- _context.prev = 21;
713
- _context.t0 = _context["catch"](13);
714
- _context.next = 25;
714
+ case 24:
715
+ _context.prev = 24;
716
+ _context.t0 = _context["catch"](16);
717
+ _context.next = 28;
715
718
  return possibleError.text();
716
- case 25:
719
+ case 28:
717
720
  message = _context.sent;
718
721
  error(message);
719
722
  database = {
720
723
  message: message
721
724
  };
722
- case 28:
723
- if (!(!host || !_this.databaseName || !_this.databaseId)) {
724
- _context.next = 39;
725
- break;
726
- }
725
+ case 31:
727
726
  info('[fetched database]', database);
728
727
  if (!(process.env.NODE_ENV !== 'TEST')) {
729
- _context.next = 39;
728
+ _context.next = 41;
730
729
  break;
731
730
  }
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 Pool(remaining);
1842
+ this.pool = createProxyForPool(new 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
  export { Server as default };
2120
2150
  //# sourceMappingURL=server.esm.js.map