@niledatabase/server 1.0.0-alpha.200 → 1.0.0-alpha.202

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.
@@ -2,12 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
-
7
- var isEmpty = _interopDefault(require('lodash/isEmpty'));
8
- var isObject = _interopDefault(require('lodash/isObject'));
5
+ var isEmpty = require('lodash/isEmpty');
6
+ var isObject = require('lodash/isObject');
9
7
  var jose = require('jose');
10
- var knex = _interopDefault(require('knex'));
8
+ var knex = require('knex');
11
9
 
12
10
  function _regeneratorRuntime() {
13
11
  _regeneratorRuntime = function () {
@@ -117,7 +115,7 @@ function _regeneratorRuntime() {
117
115
  function makeInvokeMethod(e, r, n) {
118
116
  var o = h;
119
117
  return function (i, a) {
120
- if (o === f) throw new Error("Generator is already running");
118
+ if (o === f) throw Error("Generator is already running");
121
119
  if (o === s) {
122
120
  if ("throw" === i) throw a;
123
121
  return {
@@ -259,7 +257,7 @@ function _regeneratorRuntime() {
259
257
  } else if (c) {
260
258
  if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
261
259
  } else {
262
- if (!u) throw new Error("try statement without catch or finally");
260
+ if (!u) throw Error("try statement without catch or finally");
263
261
  if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
264
262
  }
265
263
  }
@@ -299,7 +297,7 @@ function _regeneratorRuntime() {
299
297
  return o;
300
298
  }
301
299
  }
302
- throw new Error("illegal catch attempt");
300
+ throw Error("illegal catch attempt");
303
301
  },
304
302
  delegateYield: function (e, r, n) {
305
303
  return this.delegate = {
@@ -310,6 +308,20 @@ function _regeneratorRuntime() {
310
308
  }
311
309
  }, e;
312
310
  }
311
+ function _toPrimitive(t, r) {
312
+ if ("object" != typeof t || !t) return t;
313
+ var e = t[Symbol.toPrimitive];
314
+ if (void 0 !== e) {
315
+ var i = e.call(t, r || "default");
316
+ if ("object" != typeof i) return i;
317
+ throw new TypeError("@@toPrimitive must return a primitive value.");
318
+ }
319
+ return ("string" === r ? String : Number)(t);
320
+ }
321
+ function _toPropertyKey(t) {
322
+ var i = _toPrimitive(t, "string");
323
+ return "symbol" == typeof i ? i : i + "";
324
+ }
313
325
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
314
326
  try {
315
327
  var info = gen[key](arg);
@@ -383,12 +395,6 @@ function _setPrototypeOf(o, p) {
383
395
  };
384
396
  return _setPrototypeOf(o, p);
385
397
  }
386
- function _assertThisInitialized(self) {
387
- if (self === void 0) {
388
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
389
- }
390
- return self;
391
- }
392
398
  function _unsupportedIterableToArray(o, minLen) {
393
399
  if (!o) return;
394
400
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -420,31 +426,20 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
420
426
  }
421
427
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
422
428
  }
423
- function _toPrimitive(input, hint) {
424
- if (typeof input !== "object" || input === null) return input;
425
- var prim = input[Symbol.toPrimitive];
426
- if (prim !== undefined) {
427
- var res = prim.call(input, hint || "default");
428
- if (typeof res !== "object") return res;
429
- throw new TypeError("@@toPrimitive must return a primitive value.");
430
- }
431
- return (hint === "string" ? String : Number)(input);
432
- }
433
- function _toPropertyKey(arg) {
434
- var key = _toPrimitive(arg, "string");
435
- return typeof key === "symbol" ? key : String(key);
436
- }
437
429
 
438
430
  var ApiConfig = /*#__PURE__*/function () {
439
431
  function ApiConfig(_ref) {
440
432
  var basePath = _ref.basePath,
441
433
  cookieKey = _ref.cookieKey,
442
434
  token = _ref.token;
435
+ this.cookieKey = void 0;
436
+ this.basePath = void 0;
437
+ this._token = void 0;
443
438
  this.basePath = basePath;
444
439
  this.cookieKey = cookieKey;
445
440
  this._token = token;
446
441
  }
447
- _createClass(ApiConfig, [{
442
+ return _createClass(ApiConfig, [{
448
443
  key: "token",
449
444
  get: function get() {
450
445
  return this._token;
@@ -453,12 +448,17 @@ var ApiConfig = /*#__PURE__*/function () {
453
448
  this._token = value;
454
449
  }
455
450
  }]);
456
- return ApiConfig;
457
451
  }();
458
452
  var niledatabase_url = 'thenile.dev';
459
453
  var Config = /*#__PURE__*/function () {
460
454
  function Config(_config) {
461
455
  var _config$api$basePath, _config$api, _config$api$cookieKey, _config$api2, _config$api3, _config$db, _config$db2, _config$db3, _config$db4, _config$db5;
456
+ this.database = void 0;
457
+ this.workspace = void 0;
458
+ this.db = void 0;
459
+ this.api = void 0;
460
+ this._tenantId = void 0;
461
+ this._userId = void 0;
462
462
  // always provided
463
463
  this.database = String(_config == null ? void 0 : _config.database);
464
464
  this.workspace = String(_config == null ? void 0 : _config.workspace);
@@ -483,7 +483,7 @@ var Config = /*#__PURE__*/function () {
483
483
  connection: connection
484
484
  });
485
485
  }
486
- _createClass(Config, [{
486
+ return _createClass(Config, [{
487
487
  key: "tenantId",
488
488
  get: function get() {
489
489
  return this._tenantId;
@@ -500,10 +500,10 @@ var Config = /*#__PURE__*/function () {
500
500
  this._userId = value;
501
501
  }
502
502
  }]);
503
- return Config;
504
503
  }();
505
504
 
506
505
  var ResponseError = function ResponseError(body, init) {
506
+ this.response = void 0;
507
507
  this.response = new Response(body, init);
508
508
  };
509
509
 
@@ -516,6 +516,7 @@ var Events;
516
516
  })(Events || (Events = {}));
517
517
  var Eventer = /*#__PURE__*/function () {
518
518
  function Eventer() {
519
+ this.events = void 0;
519
520
  this.events = {};
520
521
  }
521
522
  var _proto = Eventer.prototype;
@@ -705,7 +706,6 @@ function _fetch2() {
705
706
  }
706
707
 
707
708
  var Requester = /*#__PURE__*/function (_Config) {
708
- _inheritsLoose(Requester, _Config);
709
709
  function Requester(config) {
710
710
  var _this;
711
711
  _this = _Config.call(this, config) || this;
@@ -768,6 +768,7 @@ var Requester = /*#__PURE__*/function (_Config) {
768
768
  }();
769
769
  return _this;
770
770
  }
771
+ _inheritsLoose(Requester, _Config);
771
772
  var _proto = Requester.prototype;
772
773
  _proto.rawRequest = /*#__PURE__*/function () {
773
774
  var _rawRequest = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(method, url, init, body) {
@@ -801,16 +802,16 @@ var Requester = /*#__PURE__*/function (_Config) {
801
802
  }
802
803
  return rawRequest;
803
804
  }()
804
- /**
805
- * three optios here
806
- * 1) pass in headers for a server side request
807
- * 2) pass in the payload that matches the api
808
- * 3) pass in the request object sent by a browser
809
- * @param method
810
- * @param url
811
- * @param req
812
- * @param init
813
- * @returns
805
+ /**
806
+ * three optios here
807
+ * 1) pass in headers for a server side request
808
+ * 2) pass in the payload that matches the api
809
+ * 3) pass in the request object sent by a browser
810
+ * @param method
811
+ * @param url
812
+ * @param req
813
+ * @param init
814
+ * @returns
814
815
  */
815
816
  ;
816
817
  _proto.request =
@@ -893,7 +894,6 @@ var Requester = /*#__PURE__*/function (_Config) {
893
894
  }(Config);
894
895
 
895
896
  var Auth = /*#__PURE__*/function (_Config) {
896
- _inheritsLoose(Auth, _Config);
897
897
  function Auth(config) {
898
898
  var _this;
899
899
  _this = _Config.call(this, config) || this;
@@ -906,7 +906,7 @@ var Auth = /*#__PURE__*/function (_Config) {
906
906
  headers = new Headers({
907
907
  'content-type': 'application/json'
908
908
  });
909
- _requester = new Requester(_assertThisInitialized(_this));
909
+ _requester = new Requester(_this);
910
910
  params = req instanceof Request ? new URL(req.url).searchParams : new URLSearchParams();
911
911
  sso = params.get('sso');
912
912
  if (!(sso === 'true')) {
@@ -1053,7 +1053,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1053
1053
  while (1) switch (_context3.prev = _context3.next) {
1054
1054
  case 0:
1055
1055
  headers = new Headers();
1056
- _requester = new Requester(_assertThisInitialized(_this));
1056
+ _requester = new Requester(_this);
1057
1057
  _context3.next = 4;
1058
1058
  return _requester.post(req, _this.signUpUrl, init)["catch"](function (e) {
1059
1059
  // eslint-disable-next-line no-console
@@ -1111,7 +1111,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1111
1111
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1112
1112
  while (1) switch (_context4.prev = _context4.next) {
1113
1113
  case 0:
1114
- _requester = new Requester(_assertThisInitialized(_this));
1114
+ _requester = new Requester(_this);
1115
1115
  return _context4.abrupt("return", _requester.get(req, _this.listTenantProvidersUrl, init));
1116
1116
  case 2:
1117
1117
  case "end":
@@ -1129,7 +1129,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1129
1129
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1130
1130
  while (1) switch (_context5.prev = _context5.next) {
1131
1131
  case 0:
1132
- _requester = new Requester(_assertThisInitialized(_this));
1132
+ _requester = new Requester(_this);
1133
1133
  providerName = 'okta';
1134
1134
  return _context5.abrupt("return", _requester.post(req, _this.updateProviderUrl(providerName), init));
1135
1135
  case 3:
@@ -1148,7 +1148,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1148
1148
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
1149
1149
  while (1) switch (_context6.prev = _context6.next) {
1150
1150
  case 0:
1151
- _requester = new Requester(_assertThisInitialized(_this));
1151
+ _requester = new Requester(_this);
1152
1152
  providerName = 'okta';
1153
1153
  return _context6.abrupt("return", _requester.put(req, _this.updateProviderUrl(providerName), init));
1154
1154
  case 3:
@@ -1168,7 +1168,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1168
1168
  return _regeneratorRuntime().wrap(function _callee7$(_context7) {
1169
1169
  while (1) switch (_context7.prev = _context7.next) {
1170
1170
  case 0:
1171
- _requester = new Requester(_assertThisInitialized(_this));
1171
+ _requester = new Requester(_this);
1172
1172
  if (!(req && 'body' in req)) {
1173
1173
  _context7.next = 5;
1174
1174
  break;
@@ -1194,12 +1194,13 @@ var Auth = /*#__PURE__*/function (_Config) {
1194
1194
  if (typeof tenantId === 'string') {
1195
1195
  tenantId = param;
1196
1196
  } else if (param instanceof Headers) {
1197
- tenantId = getTenantFromHttp(param, _assertThisInitialized(_this));
1197
+ tenantId = getTenantFromHttp(param, _this);
1198
1198
  }
1199
1199
  return _this.api.basePath + "/workspaces/" + _this.workspace + "/databases/" + _this.database + "/tenants/" + tenantId + "/auth/oidc/callback";
1200
1200
  };
1201
1201
  return _this;
1202
1202
  }
1203
+ _inheritsLoose(Auth, _Config);
1203
1204
  var _proto = Auth.prototype;
1204
1205
  _proto.updateProviderUrl = function updateProviderUrl(providerName) {
1205
1206
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + (this.tenantId ? encodeURIComponent(this.tenantId) : '{tenantId}') + "/auth/oidc/providers/" + encodeURIComponent(providerName);
@@ -1207,7 +1208,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1207
1208
  _proto.providerUrl = function providerUrl(email) {
1208
1209
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/auth/oidc/providers" + (email ? "?email=" + encodeURIComponent(email) : '');
1209
1210
  };
1210
- _createClass(Auth, [{
1211
+ return _createClass(Auth, [{
1211
1212
  key: "loginUrl",
1212
1213
  get: function get() {
1213
1214
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/users/login";
@@ -1223,11 +1224,9 @@ var Auth = /*#__PURE__*/function (_Config) {
1223
1224
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + (this.tenantId ? encodeURIComponent(this.tenantId) : '{tenantId}') + "/auth/oidc/providers";
1224
1225
  }
1225
1226
  }]);
1226
- return Auth;
1227
1227
  }(Config);
1228
1228
 
1229
1229
  var Users = /*#__PURE__*/function (_Config) {
1230
- _inheritsLoose(Users, _Config);
1231
1230
  function Users(config) {
1232
1231
  var _this;
1233
1232
  _this = _Config.call(this, config) || this;
@@ -1237,7 +1236,7 @@ var Users = /*#__PURE__*/function (_Config) {
1237
1236
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1238
1237
  while (1) switch (_context.prev = _context.next) {
1239
1238
  case 0:
1240
- _requester = new Requester(_assertThisInitialized(_this));
1239
+ _requester = new Requester(_this);
1241
1240
  _context.next = 3;
1242
1241
  return _requester.post(req, _this.tenantUsersUrl, init);
1243
1242
  case 3:
@@ -1252,15 +1251,15 @@ var Users = /*#__PURE__*/function (_Config) {
1252
1251
  return _ref.apply(this, arguments);
1253
1252
  };
1254
1253
  }();
1255
- _this.listTenantUsers = /*#__PURE__*/function () {
1254
+ _this.listUsers = /*#__PURE__*/function () {
1256
1255
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, init) {
1257
1256
  var _requester;
1258
1257
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1259
1258
  while (1) switch (_context2.prev = _context2.next) {
1260
1259
  case 0:
1261
- _requester = new Requester(_assertThisInitialized(_this));
1260
+ _requester = new Requester(_this);
1262
1261
  _context2.next = 3;
1263
- return _requester.get(req, _this.tenantUsersUrl, init);
1262
+ return _requester.get(req, _this.usersUrl, init);
1264
1263
  case 3:
1265
1264
  return _context2.abrupt("return", _context2.sent);
1266
1265
  case 4:
@@ -1273,15 +1272,15 @@ var Users = /*#__PURE__*/function (_Config) {
1273
1272
  return _ref2.apply(this, arguments);
1274
1273
  };
1275
1274
  }();
1276
- _this.me = /*#__PURE__*/function () {
1277
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
1275
+ _this.updateUser = /*#__PURE__*/function () {
1276
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(userId, req, init) {
1278
1277
  var _requester;
1279
1278
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1280
1279
  while (1) switch (_context3.prev = _context3.next) {
1281
1280
  case 0:
1282
- _requester = new Requester(_assertThisInitialized(_this));
1281
+ _requester = new Requester(_this);
1283
1282
  _context3.next = 3;
1284
- return _requester.get(req, _this.meUrl, init);
1283
+ return _requester.put(req, _this.usersUrl + "/" + userId, init);
1285
1284
  case 3:
1286
1285
  return _context3.abrupt("return", _context3.sent);
1287
1286
  case 4:
@@ -1290,17 +1289,70 @@ var Users = /*#__PURE__*/function (_Config) {
1290
1289
  }
1291
1290
  }, _callee3);
1292
1291
  }));
1293
- return function (_x5, _x6) {
1292
+ return function (_x5, _x6, _x7) {
1294
1293
  return _ref3.apply(this, arguments);
1295
1294
  };
1296
1295
  }();
1296
+ _this.listTenantUsers = /*#__PURE__*/function () {
1297
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, init) {
1298
+ var _requester;
1299
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1300
+ while (1) switch (_context4.prev = _context4.next) {
1301
+ case 0:
1302
+ _requester = new Requester(_this);
1303
+ _context4.next = 3;
1304
+ return _requester.get(req, _this.tenantUsersUrl, init);
1305
+ case 3:
1306
+ return _context4.abrupt("return", _context4.sent);
1307
+ case 4:
1308
+ case "end":
1309
+ return _context4.stop();
1310
+ }
1311
+ }, _callee4);
1312
+ }));
1313
+ return function (_x8, _x9) {
1314
+ return _ref4.apply(this, arguments);
1315
+ };
1316
+ }();
1317
+ _this.me = /*#__PURE__*/function () {
1318
+ var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req, init) {
1319
+ var _requester;
1320
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1321
+ while (1) switch (_context5.prev = _context5.next) {
1322
+ case 0:
1323
+ _requester = new Requester(_this);
1324
+ _context5.next = 3;
1325
+ return _requester.get(req, _this.meUrl, init);
1326
+ case 3:
1327
+ return _context5.abrupt("return", _context5.sent);
1328
+ case 4:
1329
+ case "end":
1330
+ return _context5.stop();
1331
+ }
1332
+ }, _callee5);
1333
+ }));
1334
+ return function (_x10, _x11) {
1335
+ return _ref5.apply(this, arguments);
1336
+ };
1337
+ }();
1297
1338
  return _this;
1298
1339
  }
1299
- _createClass(Users, [{
1340
+ _inheritsLoose(Users, _Config);
1341
+ return _createClass(Users, [{
1342
+ key: "baseUrl",
1343
+ get: function get() {
1344
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database);
1345
+ }
1346
+ }, {
1347
+ key: "usersUrl",
1348
+ get: function get() {
1349
+ return this.baseUrl + "/users";
1350
+ }
1351
+ }, {
1300
1352
  key: "tenantUsersUrl",
1301
1353
  get: function get() {
1302
1354
  var _this$tenantId;
1303
- return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + ((_this$tenantId = this.tenantId) != null ? _this$tenantId : '{tenantId}') + "/users";
1355
+ return this.baseUrl + "/tenants/" + ((_this$tenantId = this.tenantId) != null ? _this$tenantId : '{tenantId}') + "/users";
1304
1356
  }
1305
1357
  }, {
1306
1358
  key: "meUrl",
@@ -1308,11 +1360,9 @@ var Users = /*#__PURE__*/function (_Config) {
1308
1360
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/users/me";
1309
1361
  }
1310
1362
  }]);
1311
- return Users;
1312
1363
  }(Config);
1313
1364
 
1314
1365
  var Tenants = /*#__PURE__*/function (_Config) {
1315
- _inheritsLoose(Tenants, _Config);
1316
1366
  function Tenants(config) {
1317
1367
  var _this;
1318
1368
  _this = _Config.call(this, config) || this;
@@ -1322,7 +1372,7 @@ var Tenants = /*#__PURE__*/function (_Config) {
1322
1372
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1323
1373
  while (1) switch (_context.prev = _context.next) {
1324
1374
  case 0:
1325
- _requester = new Requester(_assertThisInitialized(_this));
1375
+ _requester = new Requester(_this);
1326
1376
  return _context.abrupt("return", _requester.post(req, _this.tenantsUrl, init));
1327
1377
  case 2:
1328
1378
  case "end":
@@ -1340,7 +1390,7 @@ var Tenants = /*#__PURE__*/function (_Config) {
1340
1390
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1341
1391
  while (1) switch (_context2.prev = _context2.next) {
1342
1392
  case 0:
1343
- _requester = new Requester(_assertThisInitialized(_this));
1393
+ _requester = new Requester(_this);
1344
1394
  return _context2.abrupt("return", _requester.get(req, _this.tenantUrl, init));
1345
1395
  case 2:
1346
1396
  case "end":
@@ -1354,7 +1404,8 @@ var Tenants = /*#__PURE__*/function (_Config) {
1354
1404
  }();
1355
1405
  return _this;
1356
1406
  }
1357
- _createClass(Tenants, [{
1407
+ _inheritsLoose(Tenants, _Config);
1408
+ return _createClass(Tenants, [{
1358
1409
  key: "tenantsUrl",
1359
1410
  get: function get() {
1360
1411
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants";
@@ -1366,94 +1417,112 @@ var Tenants = /*#__PURE__*/function (_Config) {
1366
1417
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + ((_this$tenantId = this.tenantId) != null ? _this$tenantId : '{tenantId}');
1367
1418
  }
1368
1419
  }]);
1369
- return Tenants;
1370
1420
  }(Config);
1371
1421
 
1372
- // doing this now, to provide flexibility later
1373
1422
  var NileDatabase = /*#__PURE__*/function () {
1374
1423
  function NileDatabase(config, id) {
1375
1424
  var _config$db$connection,
1376
1425
  _config$db$connection2,
1426
+ _config$db$pool,
1377
1427
  _this = this;
1428
+ this.knex = void 0;
1429
+ this.tenantId = void 0;
1430
+ this.userId = void 0;
1431
+ this.id = void 0;
1432
+ this.config = void 0;
1433
+ this.timer = void 0;
1378
1434
  this.id = id;
1379
1435
  var poolConfig = _extends({
1380
- min: 0
1436
+ min: 0,
1437
+ max: 10,
1438
+ idleTimeoutMillis: 30000
1381
1439
  }, config.db.pool);
1382
- var afterCreate = function afterCreate(conn, done) {
1383
- var query = ["SET nile.tenant_id = '" + config.tenantId + "'"];
1384
- if (config.userId) {
1385
- if (!config.tenantId) {
1386
- // eslint-disable-next-line no-console
1387
- console.warn('A user id cannot be set in context without a tenant id');
1388
- }
1389
- query.push("SET nile.user_id = '" + config.userId + "'");
1390
- }
1391
- // in this example we use pg driver's connection API
1392
- conn.query(query.join(';'), function (err) {
1393
- done(err, conn);
1394
- });
1395
- };
1396
- if (config.tenantId) {
1397
- var _config$db$pool;
1398
- if ((_config$db$pool = config.db.pool) != null && _config$db$pool.afterCreate) {
1399
- // eslint-disable-next-line no-console
1400
- console.log('Providing an pool configuration will stop automatic tenant context setting.');
1401
- } else if (config.db.pool) {
1402
- poolConfig = _extends({}, config.db.pool, {
1403
- afterCreate: afterCreate
1404
- });
1405
- } else if (!config.db.pool) {
1406
- poolConfig = {
1407
- afterCreate: afterCreate
1408
- };
1409
- }
1410
- }
1440
+ var database = (_config$db$connection = (_config$db$connection2 = config.db.connection) == null ? void 0 : _config$db$connection2.database) != null ? _config$db$connection : config.database;
1411
1441
  this.config = _extends({}, config, {
1412
1442
  db: _extends({}, config.db, {
1413
1443
  connection: _extends({}, config.db.connection, {
1414
- database: (_config$db$connection = (_config$db$connection2 = config.db.connection) == null ? void 0 : _config$db$connection2.database) != null ? _config$db$connection : config.database
1444
+ database: database
1415
1445
  }),
1416
1446
  pool: poolConfig
1417
1447
  })
1418
1448
  });
1419
- var knexConfig = _extends({}, this.config.db, {
1449
+ this.knex = knex(_extends({}, this.config.db, {
1420
1450
  client: 'pg'
1421
- });
1451
+ }));
1452
+ if ((_config$db$pool = config.db.pool) != null && _config$db$pool.afterCreate) {
1453
+ console.log('Providing an pool configuration will stop automatic tenant context setting.');
1454
+ } else {
1455
+ var afterCreate = makeAfterCreate(config);
1456
+ poolConfig.afterCreate = afterCreate;
1457
+ }
1422
1458
  // start the timer for cleanup
1423
1459
  this.startTimeout();
1424
- this.knex = knex(knexConfig);
1425
- this.knex.on('query', function () {
1426
- _this.startTimeout();
1427
- });
1460
+ this.knex.on('query', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
1461
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1462
+ while (1) switch (_context.prev = _context.next) {
1463
+ case 0:
1464
+ _this.startTimeout();
1465
+ case 1:
1466
+ case "end":
1467
+ return _context.stop();
1468
+ }
1469
+ }, _callee);
1470
+ })));
1428
1471
  }
1429
1472
  var _proto = NileDatabase.prototype;
1430
1473
  _proto.startTimeout = function startTimeout() {
1431
- var _this2 = this,
1432
- _this$config$db$pool$;
1474
+ var _this2 = this;
1433
1475
  if (this.timer) {
1434
1476
  clearTimeout(this.timer);
1435
1477
  }
1436
- this.timer = setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
1437
- return _regeneratorRuntime().wrap(function _callee$(_context) {
1438
- while (1) switch (_context.prev = _context.next) {
1478
+ this.timer = setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
1479
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1480
+ while (1) switch (_context2.prev = _context2.next) {
1439
1481
  case 0:
1440
- _context.next = 2;
1441
- return _this2.knex.destroy();
1482
+ _context2.next = 2;
1483
+ return _this2.knex.client.pool.destroy();
1442
1484
  case 2:
1485
+ _context2.next = 4;
1486
+ return _this2.knex.destroy();
1487
+ case 4:
1443
1488
  evictPool(_this2.id);
1444
- case 3:
1489
+ case 5:
1445
1490
  case "end":
1446
- return _context.stop();
1491
+ return _context2.stop();
1447
1492
  }
1448
- }, _callee);
1449
- })), (_this$config$db$pool$ = this.config.db.pool.idleTimeoutMillis) != null ? _this$config$db$pool$ : 30000);
1493
+ }, _callee2);
1494
+ })), this.config.db.pool.idleTimeoutMillis);
1450
1495
  };
1451
1496
  return NileDatabase;
1452
1497
  }();
1498
+ function makeAfterCreate(config) {
1499
+ return function (conn, done) {
1500
+ conn.on('error', function errorHandler(error) {
1501
+ console.log('Connection was terminated by server', error);
1502
+ done(error, conn);
1503
+ });
1504
+ if (config.tenantId) {
1505
+ var query = ["SET nile.tenant_id = '" + config.tenantId + "'"];
1506
+ if (config.userId) {
1507
+ if (!config.tenantId) {
1508
+ console.warn('A user id cannot be set in context without a tenant id');
1509
+ }
1510
+ query.push("SET nile.user_id = '" + config.userId + "'");
1511
+ }
1512
+ // in this example we use pg driver's connection API
1513
+ conn.query(query.join(';'), function (err) {
1514
+ console.log('tenant id and user id set', config.userId, config.tenantId);
1515
+ done(err, conn);
1516
+ });
1517
+ }
1518
+ done(null, conn);
1519
+ };
1520
+ }
1453
1521
 
1454
1522
  var DBManager = /*#__PURE__*/function () {
1455
1523
  function DBManager(config) {
1456
1524
  var _this = this;
1525
+ this.connections = void 0;
1457
1526
  this.connections = new Map();
1458
1527
  // add the base one, so you can at least query
1459
1528
  var id = this.makeId();
@@ -1507,6 +1576,10 @@ var init = function init(config) {
1507
1576
  var Server = /*#__PURE__*/function () {
1508
1577
  function Server(config) {
1509
1578
  var _this = this;
1579
+ this.config = void 0;
1580
+ this.api = void 0;
1581
+ this.manager = void 0;
1582
+ this.servers = void 0;
1510
1583
  this.config = new Config(config);
1511
1584
  this.servers = new Map();
1512
1585
  var _init = init(this.config),
@@ -1527,10 +1600,10 @@ var Server = /*#__PURE__*/function () {
1527
1600
  _proto.setConfig = function setConfig(cfg) {
1528
1601
  this.config = new Config(cfg);
1529
1602
  };
1530
- /**
1531
- * A utility function if you want to manage different NileDB instances yourself
1532
- * returns the global Server object, an existing server that's already been configured,
1533
- * or a new one if the config isn't in the cache
1603
+ /**
1604
+ * A utility function if you want to manage different NileDB instances yourself
1605
+ * returns the global Server object, an existing server that's already been configured,
1606
+ * or a new one if the config isn't in the cache
1534
1607
  */
1535
1608
  _proto.getInstance = function getInstance(config) {
1536
1609
  var _config = _extends({}, this.config, config);
@@ -1555,7 +1628,7 @@ var Server = /*#__PURE__*/function () {
1555
1628
  this.servers.set(serverId, new Server(_config));
1556
1629
  return this.servers.get(serverId);
1557
1630
  };
1558
- _createClass(Server, [{
1631
+ return _createClass(Server, [{
1559
1632
  key: "database",
1560
1633
  set: function set(val) {
1561
1634
  if (val) {
@@ -1628,7 +1701,6 @@ var Server = /*#__PURE__*/function () {
1628
1701
  return this.manager.getConnection(this.config).knex;
1629
1702
  }
1630
1703
  }]);
1631
- return Server;
1632
1704
  }(); // export default Server;
1633
1705
  function Nile(config) {
1634
1706
  var server = new Server(config);