@niledatabase/server 2.1.2-alpha.0 → 2.2.0-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.
@@ -643,8 +643,10 @@ var ApiConfig = /*#__PURE__*/function () {
643
643
  }]);
644
644
  }();
645
645
  var Config = /*#__PURE__*/function () {
646
- function Config(config, allowPhoneHome) {
647
- var _config$api$cookieKey, _config$api;
646
+ function Config(_config) {
647
+ var _this = this,
648
+ _config$api$cookieKey2,
649
+ _config$api2;
648
650
  this.user = void 0;
649
651
  this.password = void 0;
650
652
  this.databaseId = void 0;
@@ -654,69 +656,142 @@ var Config = /*#__PURE__*/function () {
654
656
  this.api = void 0;
655
657
  this._tenantId = void 0;
656
658
  this._userId = void 0;
657
- var _Logger = Logger(config, '[config]'),
658
- info = _Logger.info,
659
- error = _Logger.error;
660
- var envVarConfig = {
661
- config: config
662
- };
663
- if (allowPhoneHome) {
664
- envVarConfig.logger = '[config]';
665
- }
666
- this.databaseId = getDatbaseId(envVarConfig);
667
- this.user = getUsername(envVarConfig);
668
- this.password = getPassword(envVarConfig);
669
- this.databaseName = getDatabaseName(envVarConfig);
670
- this._tenantId = getTenantId(envVarConfig);
671
- this.debug = Boolean(config == null ? void 0 : config.debug);
672
- var basePath = getBasePath(envVarConfig);
673
- var host = getDbHost(envVarConfig);
674
- var port = getDbPort(envVarConfig);
675
- this._userId = config == null ? void 0 : config.userId;
676
- if (allowPhoneHome && (!host || !this.databaseName || !this.databaseId)) {
677
- var database = getInfo(config);
678
- info('[fetched database]', database);
679
- if (process.env.NODE_ENV !== 'TEST') {
680
- if ('message' in database) {
681
- if ('statusCode' in database) {
682
- error(database);
683
- throw new Error('HTTP error has occured');
684
- } else {
685
- throw new Error('Unable to auto-configure. Please set or remove NILEDB_API, NILEDB_NAME, and NILEDB_HOST in your .env file.');
659
+ this.configure = /*#__PURE__*/function () {
660
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
661
+ var _config$api$cookieKey, _config$api;
662
+ var _Logger, info, error, envVarConfig, basePath, host, port, databaseName, url, res, database, possibleError, json, message, _database, apiHost, dbHost, name, id, dburl, apiurl;
663
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
664
+ while (1) switch (_context.prev = _context.next) {
665
+ case 0:
666
+ _Logger = Logger(config, '[init]'), info = _Logger.info, error = _Logger.error;
667
+ envVarConfig = {
668
+ config: config
669
+ };
670
+ basePath = getBasePath(envVarConfig);
671
+ host = getDbHost(envVarConfig);
672
+ port = getDbPort(envVarConfig);
673
+ databaseName = getDatabaseName({
674
+ config: config,
675
+ logger: 'getInfo'
676
+ });
677
+ url = new URL(basePath + "/databases/configure");
678
+ if (databaseName) {
679
+ url.searchParams.set('databaseName', databaseName);
680
+ }
681
+ info(url.href);
682
+ res = syncFetch(url, {
683
+ headers: {
684
+ Authorization: "Bearer " + getInfoBearer({
685
+ config: config
686
+ })
687
+ }
688
+ });
689
+ possibleError = res.clone();
690
+ try {
691
+ json = res.json();
692
+ if (res.status === 404) {
693
+ info('is the configured databaseName correct?');
694
+ }
695
+ if (json.status && json.status !== 'READY') {
696
+ database = {
697
+ message: 'Database is not ready yet'
698
+ };
699
+ } else {
700
+ database = json;
701
+ }
702
+ } catch (e) {
703
+ message = possibleError.text();
704
+ error(message);
705
+ database = {
706
+ message: message
707
+ };
708
+ }
709
+ if (!(!host || !_this.databaseName || !_this.databaseId)) {
710
+ _context.next = 23;
711
+ break;
712
+ }
713
+ info('[fetched database]', database);
714
+ if (!(process.env.NODE_ENV !== 'TEST')) {
715
+ _context.next = 23;
716
+ break;
717
+ }
718
+ if (!('message' in database)) {
719
+ _context.next = 22;
720
+ break;
721
+ }
722
+ if (!('statusCode' in database)) {
723
+ _context.next = 21;
724
+ break;
725
+ }
726
+ error(database);
727
+ throw new Error('HTTP error has occured');
728
+ case 21:
729
+ throw new Error('Unable to auto-configure. Please set or remove NILEDB_API, NILEDB_NAME, and NILEDB_HOST in your .env file.');
730
+ case 22:
731
+ if (typeof database === 'object') {
732
+ _database = database, apiHost = _database.apiHost, dbHost = _database.dbHost, name = _database.name, id = _database.id;
733
+ _this.databaseId = id;
734
+ _this.databaseName = name;
735
+ // gotta do something dumb here
736
+ dburl = new URL(dbHost);
737
+ apiurl = new URL(apiHost);
738
+ host = dburl.host;
739
+ basePath = apiurl.origin;
740
+ }
741
+ case 23:
742
+ _this.api = new ApiConfig({
743
+ basePath: basePath,
744
+ cookieKey: (_config$api$cookieKey = config == null || (_config$api = config.api) == null ? void 0 : _config$api.cookieKey) != null ? _config$api$cookieKey : 'token',
745
+ token: getToken({
746
+ config: config
747
+ })
748
+ });
749
+ _this.db = _extends({
750
+ user: _this.user,
751
+ password: _this.password,
752
+ host: host,
753
+ port: port,
754
+ database: _this.databaseName
755
+ }, typeof (config == null ? void 0 : config.db) === 'object' ? config.db : {});
756
+ info('[config set]', _this);
757
+ return _context.abrupt("return", _this);
758
+ case 27:
759
+ case "end":
760
+ return _context.stop();
686
761
  }
687
- }
688
- if (typeof database === 'object') {
689
- var apiHost = database.apiHost,
690
- dbHost = database.dbHost,
691
- name = database.name,
692
- id = database.id;
693
- this.databaseId = id;
694
- this.databaseName = name;
695
- // gotta do something dumb here
696
- var dburl = new URL(dbHost);
697
- var apiurl = new URL(apiHost);
698
- host = dburl.host;
699
- basePath = apiurl.origin;
700
- }
701
- }
702
- }
762
+ }, _callee);
763
+ }));
764
+ return function (_x) {
765
+ return _ref2.apply(this, arguments);
766
+ };
767
+ }();
768
+ var _envVarConfig = {
769
+ config: _config
770
+ };
771
+ this.databaseId = getDatbaseId(_envVarConfig);
772
+ this.user = getUsername(_envVarConfig);
773
+ this.password = getPassword(_envVarConfig);
774
+ this.databaseName = getDatabaseName(_envVarConfig);
775
+ this._tenantId = getTenantId(_envVarConfig);
776
+ this.debug = Boolean(_config == null ? void 0 : _config.debug);
777
+ this._userId = _config == null ? void 0 : _config.userId;
778
+ var _basePath = getBasePath(_envVarConfig);
779
+ var _host = getDbHost(_envVarConfig);
780
+ var _port = getDbPort(_envVarConfig);
703
781
  this.api = new ApiConfig({
704
- basePath: basePath,
705
- cookieKey: (_config$api$cookieKey = config == null || (_config$api = config.api) == null ? void 0 : _config$api.cookieKey) != null ? _config$api$cookieKey : 'token',
782
+ basePath: _basePath,
783
+ cookieKey: (_config$api$cookieKey2 = _config == null || (_config$api2 = _config.api) == null ? void 0 : _config$api2.cookieKey) != null ? _config$api$cookieKey2 : 'token',
706
784
  token: getToken({
707
- config: config
785
+ config: _config
708
786
  })
709
787
  });
710
788
  this.db = _extends({
711
789
  user: this.user,
712
790
  password: this.password,
713
- host: host,
714
- port: port,
791
+ host: _host,
792
+ port: _port,
715
793
  database: this.databaseName
716
- }, typeof (config == null ? void 0 : config.db) === 'object' ? config.db : {});
717
- if (allowPhoneHome) {
718
- info(this);
719
- }
794
+ }, typeof (_config == null ? void 0 : _config.db) === 'object' ? _config.db : {});
720
795
  }
721
796
  return _createClass(Config, [{
722
797
  key: "tenantId",
@@ -736,49 +811,6 @@ var Config = /*#__PURE__*/function () {
736
811
  }
737
812
  }]);
738
813
  }();
739
- function getInfo(config) {
740
- var basePath = getBasePath({
741
- config: config
742
- });
743
- var databaseName = getDatabaseName({
744
- config: config,
745
- logger: 'getInfo'
746
- });
747
- var url = new URL(basePath + "/databases/configure");
748
- if (databaseName) {
749
- url.searchParams.set('databaseName', databaseName);
750
- }
751
- var _Logger2 = Logger(config, '[getInfo]'),
752
- info = _Logger2.info,
753
- error = _Logger2.error;
754
- info(url.href);
755
- var res = syncFetch(url, {
756
- headers: {
757
- Authorization: "Bearer " + getInfoBearer({
758
- config: config
759
- })
760
- }
761
- });
762
- var possibleError = res.clone();
763
- try {
764
- var json = res.json();
765
- if (res.status === 404) {
766
- info('is the configured databaseName correct?');
767
- }
768
- if (json.status && json.status !== 'READY') {
769
- return {
770
- message: 'Database is not ready yet'
771
- };
772
- }
773
- return json;
774
- } catch (e) {
775
- var message = possibleError.text();
776
- error(message);
777
- return {
778
- message: message
779
- };
780
- }
781
- }
782
814
 
783
815
  var ResponseError = function ResponseError(body, init) {
784
816
  this.response = void 0;
@@ -1921,15 +1953,15 @@ var makeServerId = function makeServerId(config) {
1921
1953
  return Buffer.from(JSON.stringify(config), 'base64').toString();
1922
1954
  };
1923
1955
 
1924
- var init = function init(config) {
1956
+ var _init = function init(config) {
1925
1957
  var auth = new Auth(config);
1926
1958
  var users = new Users(config);
1927
1959
  var tenants = new Tenants(config);
1928
- return [{
1960
+ return {
1929
1961
  auth: auth,
1930
1962
  users: users,
1931
1963
  tenants: tenants
1932
- }];
1964
+ };
1933
1965
  };
1934
1966
  var Server = /*#__PURE__*/function () {
1935
1967
  function Server(config) {
@@ -1938,11 +1970,9 @@ var Server = /*#__PURE__*/function () {
1938
1970
  this.api = void 0;
1939
1971
  this.manager = void 0;
1940
1972
  this.servers = void 0;
1941
- this.config = new Config(config, true);
1973
+ this.config = new Config(config);
1942
1974
  this.servers = new Map();
1943
- var _init = init(this.config),
1944
- api = _init[0];
1945
- this.api = api;
1975
+ this.api = _init(this.config);
1946
1976
  this.manager = new DBManager(this.config);
1947
1977
  watchTenantId(function (tenantId) {
1948
1978
  _this.tenantId = tenantId;
@@ -1956,8 +1986,32 @@ var Server = /*#__PURE__*/function () {
1956
1986
  }
1957
1987
  var _proto = Server.prototype;
1958
1988
  _proto.setConfig = function setConfig(cfg) {
1959
- this.config = new Config(cfg, false);
1989
+ this.config = new Config(cfg);
1960
1990
  };
1991
+ _proto.init = /*#__PURE__*/function () {
1992
+ var _init2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(cfg) {
1993
+ var updatedConfig;
1994
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1995
+ while (1) switch (_context.prev = _context.next) {
1996
+ case 0:
1997
+ _context.next = 2;
1998
+ return this.config.configure(_extends({}, this.config, cfg));
1999
+ case 2:
2000
+ updatedConfig = _context.sent;
2001
+ this.setConfig(updatedConfig);
2002
+ this.manager = new DBManager(this.config);
2003
+ this.api = _init(updatedConfig);
2004
+ case 6:
2005
+ case "end":
2006
+ return _context.stop();
2007
+ }
2008
+ }, _callee, this);
2009
+ }));
2010
+ function init(_x) {
2011
+ return _init2.apply(this, arguments);
2012
+ }
2013
+ return init;
2014
+ }();
1961
2015
  /**
1962
2016
  * A utility function if you want to manage different NileDB instances yourself
1963
2017
  * returns the global Server object, an existing server that's already been configured,
@@ -1973,7 +2027,7 @@ var Server = /*#__PURE__*/function () {
1973
2027
  var existing = this.servers.get(serverId);
1974
2028
  if (existing) {
1975
2029
  // be sure the config is up to date
1976
- var updatedConfig = new Config(_config, false);
2030
+ var updatedConfig = new Config(_config);
1977
2031
  existing.setConfig(updatedConfig);
1978
2032
  // propagage special config items
1979
2033
  existing.tenantId = updatedConfig.tenantId;
@@ -2046,14 +2100,7 @@ var Server = /*#__PURE__*/function () {
2046
2100
  return this.manager.getConnection(this.config);
2047
2101
  }
2048
2102
  }]);
2049
- }(); // export default Server;
2050
- function Nile(config) {
2051
- var server = new Server(config);
2052
- // server.setConfig(new Config(config as ServerConfig, false));
2053
- return server;
2054
- }
2055
-
2056
- module.exports = Nile;
2103
+ }();
2057
2104
 
2058
- export { Nile as default };
2105
+ export { Server as default };
2059
2106
  //# sourceMappingURL=server.esm.js.map