@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.
@@ -111,7 +111,7 @@ function _regeneratorRuntime() {
111
111
  function makeInvokeMethod(e, r, n) {
112
112
  var o = h;
113
113
  return function (i, a) {
114
- if (o === f) throw new Error("Generator is already running");
114
+ if (o === f) throw Error("Generator is already running");
115
115
  if (o === s) {
116
116
  if ("throw" === i) throw a;
117
117
  return {
@@ -253,7 +253,7 @@ function _regeneratorRuntime() {
253
253
  } else if (c) {
254
254
  if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
255
255
  } else {
256
- if (!u) throw new Error("try statement without catch or finally");
256
+ if (!u) throw Error("try statement without catch or finally");
257
257
  if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
258
258
  }
259
259
  }
@@ -293,7 +293,7 @@ function _regeneratorRuntime() {
293
293
  return o;
294
294
  }
295
295
  }
296
- throw new Error("illegal catch attempt");
296
+ throw Error("illegal catch attempt");
297
297
  },
298
298
  delegateYield: function (e, r, n) {
299
299
  return this.delegate = {
@@ -304,6 +304,20 @@ function _regeneratorRuntime() {
304
304
  }
305
305
  }, e;
306
306
  }
307
+ function _toPrimitive(t, r) {
308
+ if ("object" != typeof t || !t) return t;
309
+ var e = t[Symbol.toPrimitive];
310
+ if (void 0 !== e) {
311
+ var i = e.call(t, r || "default");
312
+ if ("object" != typeof i) return i;
313
+ throw new TypeError("@@toPrimitive must return a primitive value.");
314
+ }
315
+ return ("string" === r ? String : Number)(t);
316
+ }
317
+ function _toPropertyKey(t) {
318
+ var i = _toPrimitive(t, "string");
319
+ return "symbol" == typeof i ? i : i + "";
320
+ }
307
321
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
308
322
  try {
309
323
  var info = gen[key](arg);
@@ -377,12 +391,6 @@ function _setPrototypeOf(o, p) {
377
391
  };
378
392
  return _setPrototypeOf(o, p);
379
393
  }
380
- function _assertThisInitialized(self) {
381
- if (self === void 0) {
382
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
383
- }
384
- return self;
385
- }
386
394
  function _unsupportedIterableToArray(o, minLen) {
387
395
  if (!o) return;
388
396
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -414,31 +422,20 @@ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
414
422
  }
415
423
  throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
416
424
  }
417
- function _toPrimitive(input, hint) {
418
- if (typeof input !== "object" || input === null) return input;
419
- var prim = input[Symbol.toPrimitive];
420
- if (prim !== undefined) {
421
- var res = prim.call(input, hint || "default");
422
- if (typeof res !== "object") return res;
423
- throw new TypeError("@@toPrimitive must return a primitive value.");
424
- }
425
- return (hint === "string" ? String : Number)(input);
426
- }
427
- function _toPropertyKey(arg) {
428
- var key = _toPrimitive(arg, "string");
429
- return typeof key === "symbol" ? key : String(key);
430
- }
431
425
 
432
426
  var ApiConfig = /*#__PURE__*/function () {
433
427
  function ApiConfig(_ref) {
434
428
  var basePath = _ref.basePath,
435
429
  cookieKey = _ref.cookieKey,
436
430
  token = _ref.token;
431
+ this.cookieKey = void 0;
432
+ this.basePath = void 0;
433
+ this._token = void 0;
437
434
  this.basePath = basePath;
438
435
  this.cookieKey = cookieKey;
439
436
  this._token = token;
440
437
  }
441
- _createClass(ApiConfig, [{
438
+ return _createClass(ApiConfig, [{
442
439
  key: "token",
443
440
  get: function get() {
444
441
  return this._token;
@@ -447,12 +444,17 @@ var ApiConfig = /*#__PURE__*/function () {
447
444
  this._token = value;
448
445
  }
449
446
  }]);
450
- return ApiConfig;
451
447
  }();
452
448
  var niledatabase_url = 'thenile.dev';
453
449
  var Config = /*#__PURE__*/function () {
454
450
  function Config(_config) {
455
451
  var _config$api$basePath, _config$api, _config$api$cookieKey, _config$api2, _config$api3, _config$db, _config$db2, _config$db3, _config$db4, _config$db5;
452
+ this.database = void 0;
453
+ this.workspace = void 0;
454
+ this.db = void 0;
455
+ this.api = void 0;
456
+ this._tenantId = void 0;
457
+ this._userId = void 0;
456
458
  // always provided
457
459
  this.database = String(_config == null ? void 0 : _config.database);
458
460
  this.workspace = String(_config == null ? void 0 : _config.workspace);
@@ -477,7 +479,7 @@ var Config = /*#__PURE__*/function () {
477
479
  connection: connection
478
480
  });
479
481
  }
480
- _createClass(Config, [{
482
+ return _createClass(Config, [{
481
483
  key: "tenantId",
482
484
  get: function get() {
483
485
  return this._tenantId;
@@ -494,10 +496,10 @@ var Config = /*#__PURE__*/function () {
494
496
  this._userId = value;
495
497
  }
496
498
  }]);
497
- return Config;
498
499
  }();
499
500
 
500
501
  var ResponseError = function ResponseError(body, init) {
502
+ this.response = void 0;
501
503
  this.response = new Response(body, init);
502
504
  };
503
505
 
@@ -510,6 +512,7 @@ var Events;
510
512
  })(Events || (Events = {}));
511
513
  var Eventer = /*#__PURE__*/function () {
512
514
  function Eventer() {
515
+ this.events = void 0;
513
516
  this.events = {};
514
517
  }
515
518
  var _proto = Eventer.prototype;
@@ -699,7 +702,6 @@ function _fetch2() {
699
702
  }
700
703
 
701
704
  var Requester = /*#__PURE__*/function (_Config) {
702
- _inheritsLoose(Requester, _Config);
703
705
  function Requester(config) {
704
706
  var _this;
705
707
  _this = _Config.call(this, config) || this;
@@ -762,6 +764,7 @@ var Requester = /*#__PURE__*/function (_Config) {
762
764
  }();
763
765
  return _this;
764
766
  }
767
+ _inheritsLoose(Requester, _Config);
765
768
  var _proto = Requester.prototype;
766
769
  _proto.rawRequest = /*#__PURE__*/function () {
767
770
  var _rawRequest = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(method, url, init, body) {
@@ -795,16 +798,16 @@ var Requester = /*#__PURE__*/function (_Config) {
795
798
  }
796
799
  return rawRequest;
797
800
  }()
798
- /**
799
- * three optios here
800
- * 1) pass in headers for a server side request
801
- * 2) pass in the payload that matches the api
802
- * 3) pass in the request object sent by a browser
803
- * @param method
804
- * @param url
805
- * @param req
806
- * @param init
807
- * @returns
801
+ /**
802
+ * three optios here
803
+ * 1) pass in headers for a server side request
804
+ * 2) pass in the payload that matches the api
805
+ * 3) pass in the request object sent by a browser
806
+ * @param method
807
+ * @param url
808
+ * @param req
809
+ * @param init
810
+ * @returns
808
811
  */
809
812
  ;
810
813
  _proto.request =
@@ -887,7 +890,6 @@ var Requester = /*#__PURE__*/function (_Config) {
887
890
  }(Config);
888
891
 
889
892
  var Auth = /*#__PURE__*/function (_Config) {
890
- _inheritsLoose(Auth, _Config);
891
893
  function Auth(config) {
892
894
  var _this;
893
895
  _this = _Config.call(this, config) || this;
@@ -900,7 +902,7 @@ var Auth = /*#__PURE__*/function (_Config) {
900
902
  headers = new Headers({
901
903
  'content-type': 'application/json'
902
904
  });
903
- _requester = new Requester(_assertThisInitialized(_this));
905
+ _requester = new Requester(_this);
904
906
  params = req instanceof Request ? new URL(req.url).searchParams : new URLSearchParams();
905
907
  sso = params.get('sso');
906
908
  if (!(sso === 'true')) {
@@ -1047,7 +1049,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1047
1049
  while (1) switch (_context3.prev = _context3.next) {
1048
1050
  case 0:
1049
1051
  headers = new Headers();
1050
- _requester = new Requester(_assertThisInitialized(_this));
1052
+ _requester = new Requester(_this);
1051
1053
  _context3.next = 4;
1052
1054
  return _requester.post(req, _this.signUpUrl, init)["catch"](function (e) {
1053
1055
  // eslint-disable-next-line no-console
@@ -1105,7 +1107,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1105
1107
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1106
1108
  while (1) switch (_context4.prev = _context4.next) {
1107
1109
  case 0:
1108
- _requester = new Requester(_assertThisInitialized(_this));
1110
+ _requester = new Requester(_this);
1109
1111
  return _context4.abrupt("return", _requester.get(req, _this.listTenantProvidersUrl, init));
1110
1112
  case 2:
1111
1113
  case "end":
@@ -1123,7 +1125,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1123
1125
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1124
1126
  while (1) switch (_context5.prev = _context5.next) {
1125
1127
  case 0:
1126
- _requester = new Requester(_assertThisInitialized(_this));
1128
+ _requester = new Requester(_this);
1127
1129
  providerName = 'okta';
1128
1130
  return _context5.abrupt("return", _requester.post(req, _this.updateProviderUrl(providerName), init));
1129
1131
  case 3:
@@ -1142,7 +1144,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1142
1144
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
1143
1145
  while (1) switch (_context6.prev = _context6.next) {
1144
1146
  case 0:
1145
- _requester = new Requester(_assertThisInitialized(_this));
1147
+ _requester = new Requester(_this);
1146
1148
  providerName = 'okta';
1147
1149
  return _context6.abrupt("return", _requester.put(req, _this.updateProviderUrl(providerName), init));
1148
1150
  case 3:
@@ -1162,7 +1164,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1162
1164
  return _regeneratorRuntime().wrap(function _callee7$(_context7) {
1163
1165
  while (1) switch (_context7.prev = _context7.next) {
1164
1166
  case 0:
1165
- _requester = new Requester(_assertThisInitialized(_this));
1167
+ _requester = new Requester(_this);
1166
1168
  if (!(req && 'body' in req)) {
1167
1169
  _context7.next = 5;
1168
1170
  break;
@@ -1188,12 +1190,13 @@ var Auth = /*#__PURE__*/function (_Config) {
1188
1190
  if (typeof tenantId === 'string') {
1189
1191
  tenantId = param;
1190
1192
  } else if (param instanceof Headers) {
1191
- tenantId = getTenantFromHttp(param, _assertThisInitialized(_this));
1193
+ tenantId = getTenantFromHttp(param, _this);
1192
1194
  }
1193
1195
  return _this.api.basePath + "/workspaces/" + _this.workspace + "/databases/" + _this.database + "/tenants/" + tenantId + "/auth/oidc/callback";
1194
1196
  };
1195
1197
  return _this;
1196
1198
  }
1199
+ _inheritsLoose(Auth, _Config);
1197
1200
  var _proto = Auth.prototype;
1198
1201
  _proto.updateProviderUrl = function updateProviderUrl(providerName) {
1199
1202
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + (this.tenantId ? encodeURIComponent(this.tenantId) : '{tenantId}') + "/auth/oidc/providers/" + encodeURIComponent(providerName);
@@ -1201,7 +1204,7 @@ var Auth = /*#__PURE__*/function (_Config) {
1201
1204
  _proto.providerUrl = function providerUrl(email) {
1202
1205
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/auth/oidc/providers" + (email ? "?email=" + encodeURIComponent(email) : '');
1203
1206
  };
1204
- _createClass(Auth, [{
1207
+ return _createClass(Auth, [{
1205
1208
  key: "loginUrl",
1206
1209
  get: function get() {
1207
1210
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/users/login";
@@ -1217,11 +1220,9 @@ var Auth = /*#__PURE__*/function (_Config) {
1217
1220
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + (this.tenantId ? encodeURIComponent(this.tenantId) : '{tenantId}') + "/auth/oidc/providers";
1218
1221
  }
1219
1222
  }]);
1220
- return Auth;
1221
1223
  }(Config);
1222
1224
 
1223
1225
  var Users = /*#__PURE__*/function (_Config) {
1224
- _inheritsLoose(Users, _Config);
1225
1226
  function Users(config) {
1226
1227
  var _this;
1227
1228
  _this = _Config.call(this, config) || this;
@@ -1231,7 +1232,7 @@ var Users = /*#__PURE__*/function (_Config) {
1231
1232
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1232
1233
  while (1) switch (_context.prev = _context.next) {
1233
1234
  case 0:
1234
- _requester = new Requester(_assertThisInitialized(_this));
1235
+ _requester = new Requester(_this);
1235
1236
  _context.next = 3;
1236
1237
  return _requester.post(req, _this.tenantUsersUrl, init);
1237
1238
  case 3:
@@ -1246,15 +1247,15 @@ var Users = /*#__PURE__*/function (_Config) {
1246
1247
  return _ref.apply(this, arguments);
1247
1248
  };
1248
1249
  }();
1249
- _this.listTenantUsers = /*#__PURE__*/function () {
1250
+ _this.listUsers = /*#__PURE__*/function () {
1250
1251
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, init) {
1251
1252
  var _requester;
1252
1253
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1253
1254
  while (1) switch (_context2.prev = _context2.next) {
1254
1255
  case 0:
1255
- _requester = new Requester(_assertThisInitialized(_this));
1256
+ _requester = new Requester(_this);
1256
1257
  _context2.next = 3;
1257
- return _requester.get(req, _this.tenantUsersUrl, init);
1258
+ return _requester.get(req, _this.usersUrl, init);
1258
1259
  case 3:
1259
1260
  return _context2.abrupt("return", _context2.sent);
1260
1261
  case 4:
@@ -1267,15 +1268,15 @@ var Users = /*#__PURE__*/function (_Config) {
1267
1268
  return _ref2.apply(this, arguments);
1268
1269
  };
1269
1270
  }();
1270
- _this.me = /*#__PURE__*/function () {
1271
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
1271
+ _this.updateUser = /*#__PURE__*/function () {
1272
+ var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(userId, req, init) {
1272
1273
  var _requester;
1273
1274
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1274
1275
  while (1) switch (_context3.prev = _context3.next) {
1275
1276
  case 0:
1276
- _requester = new Requester(_assertThisInitialized(_this));
1277
+ _requester = new Requester(_this);
1277
1278
  _context3.next = 3;
1278
- return _requester.get(req, _this.meUrl, init);
1279
+ return _requester.put(req, _this.usersUrl + "/" + userId, init);
1279
1280
  case 3:
1280
1281
  return _context3.abrupt("return", _context3.sent);
1281
1282
  case 4:
@@ -1284,17 +1285,70 @@ var Users = /*#__PURE__*/function (_Config) {
1284
1285
  }
1285
1286
  }, _callee3);
1286
1287
  }));
1287
- return function (_x5, _x6) {
1288
+ return function (_x5, _x6, _x7) {
1288
1289
  return _ref3.apply(this, arguments);
1289
1290
  };
1290
1291
  }();
1292
+ _this.listTenantUsers = /*#__PURE__*/function () {
1293
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, init) {
1294
+ var _requester;
1295
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1296
+ while (1) switch (_context4.prev = _context4.next) {
1297
+ case 0:
1298
+ _requester = new Requester(_this);
1299
+ _context4.next = 3;
1300
+ return _requester.get(req, _this.tenantUsersUrl, init);
1301
+ case 3:
1302
+ return _context4.abrupt("return", _context4.sent);
1303
+ case 4:
1304
+ case "end":
1305
+ return _context4.stop();
1306
+ }
1307
+ }, _callee4);
1308
+ }));
1309
+ return function (_x8, _x9) {
1310
+ return _ref4.apply(this, arguments);
1311
+ };
1312
+ }();
1313
+ _this.me = /*#__PURE__*/function () {
1314
+ var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req, init) {
1315
+ var _requester;
1316
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
1317
+ while (1) switch (_context5.prev = _context5.next) {
1318
+ case 0:
1319
+ _requester = new Requester(_this);
1320
+ _context5.next = 3;
1321
+ return _requester.get(req, _this.meUrl, init);
1322
+ case 3:
1323
+ return _context5.abrupt("return", _context5.sent);
1324
+ case 4:
1325
+ case "end":
1326
+ return _context5.stop();
1327
+ }
1328
+ }, _callee5);
1329
+ }));
1330
+ return function (_x10, _x11) {
1331
+ return _ref5.apply(this, arguments);
1332
+ };
1333
+ }();
1291
1334
  return _this;
1292
1335
  }
1293
- _createClass(Users, [{
1336
+ _inheritsLoose(Users, _Config);
1337
+ return _createClass(Users, [{
1338
+ key: "baseUrl",
1339
+ get: function get() {
1340
+ return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database);
1341
+ }
1342
+ }, {
1343
+ key: "usersUrl",
1344
+ get: function get() {
1345
+ return this.baseUrl + "/users";
1346
+ }
1347
+ }, {
1294
1348
  key: "tenantUsersUrl",
1295
1349
  get: function get() {
1296
1350
  var _this$tenantId;
1297
- return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + ((_this$tenantId = this.tenantId) != null ? _this$tenantId : '{tenantId}') + "/users";
1351
+ return this.baseUrl + "/tenants/" + ((_this$tenantId = this.tenantId) != null ? _this$tenantId : '{tenantId}') + "/users";
1298
1352
  }
1299
1353
  }, {
1300
1354
  key: "meUrl",
@@ -1302,11 +1356,9 @@ var Users = /*#__PURE__*/function (_Config) {
1302
1356
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/users/me";
1303
1357
  }
1304
1358
  }]);
1305
- return Users;
1306
1359
  }(Config);
1307
1360
 
1308
1361
  var Tenants = /*#__PURE__*/function (_Config) {
1309
- _inheritsLoose(Tenants, _Config);
1310
1362
  function Tenants(config) {
1311
1363
  var _this;
1312
1364
  _this = _Config.call(this, config) || this;
@@ -1316,7 +1368,7 @@ var Tenants = /*#__PURE__*/function (_Config) {
1316
1368
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1317
1369
  while (1) switch (_context.prev = _context.next) {
1318
1370
  case 0:
1319
- _requester = new Requester(_assertThisInitialized(_this));
1371
+ _requester = new Requester(_this);
1320
1372
  return _context.abrupt("return", _requester.post(req, _this.tenantsUrl, init));
1321
1373
  case 2:
1322
1374
  case "end":
@@ -1334,7 +1386,7 @@ var Tenants = /*#__PURE__*/function (_Config) {
1334
1386
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1335
1387
  while (1) switch (_context2.prev = _context2.next) {
1336
1388
  case 0:
1337
- _requester = new Requester(_assertThisInitialized(_this));
1389
+ _requester = new Requester(_this);
1338
1390
  return _context2.abrupt("return", _requester.get(req, _this.tenantUrl, init));
1339
1391
  case 2:
1340
1392
  case "end":
@@ -1348,7 +1400,8 @@ var Tenants = /*#__PURE__*/function (_Config) {
1348
1400
  }();
1349
1401
  return _this;
1350
1402
  }
1351
- _createClass(Tenants, [{
1403
+ _inheritsLoose(Tenants, _Config);
1404
+ return _createClass(Tenants, [{
1352
1405
  key: "tenantsUrl",
1353
1406
  get: function get() {
1354
1407
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants";
@@ -1360,94 +1413,112 @@ var Tenants = /*#__PURE__*/function (_Config) {
1360
1413
  return "/workspaces/" + encodeURIComponent(this.workspace) + "/databases/" + encodeURIComponent(this.database) + "/tenants/" + ((_this$tenantId = this.tenantId) != null ? _this$tenantId : '{tenantId}');
1361
1414
  }
1362
1415
  }]);
1363
- return Tenants;
1364
1416
  }(Config);
1365
1417
 
1366
- // doing this now, to provide flexibility later
1367
1418
  var NileDatabase = /*#__PURE__*/function () {
1368
1419
  function NileDatabase(config, id) {
1369
1420
  var _config$db$connection,
1370
1421
  _config$db$connection2,
1422
+ _config$db$pool,
1371
1423
  _this = this;
1424
+ this.knex = void 0;
1425
+ this.tenantId = void 0;
1426
+ this.userId = void 0;
1427
+ this.id = void 0;
1428
+ this.config = void 0;
1429
+ this.timer = void 0;
1372
1430
  this.id = id;
1373
1431
  var poolConfig = _extends({
1374
- min: 0
1432
+ min: 0,
1433
+ max: 10,
1434
+ idleTimeoutMillis: 30000
1375
1435
  }, config.db.pool);
1376
- var afterCreate = function afterCreate(conn, done) {
1377
- var query = ["SET nile.tenant_id = '" + config.tenantId + "'"];
1378
- if (config.userId) {
1379
- if (!config.tenantId) {
1380
- // eslint-disable-next-line no-console
1381
- console.warn('A user id cannot be set in context without a tenant id');
1382
- }
1383
- query.push("SET nile.user_id = '" + config.userId + "'");
1384
- }
1385
- // in this example we use pg driver's connection API
1386
- conn.query(query.join(';'), function (err) {
1387
- done(err, conn);
1388
- });
1389
- };
1390
- if (config.tenantId) {
1391
- var _config$db$pool;
1392
- if ((_config$db$pool = config.db.pool) != null && _config$db$pool.afterCreate) {
1393
- // eslint-disable-next-line no-console
1394
- console.log('Providing an pool configuration will stop automatic tenant context setting.');
1395
- } else if (config.db.pool) {
1396
- poolConfig = _extends({}, config.db.pool, {
1397
- afterCreate: afterCreate
1398
- });
1399
- } else if (!config.db.pool) {
1400
- poolConfig = {
1401
- afterCreate: afterCreate
1402
- };
1403
- }
1404
- }
1436
+ var database = (_config$db$connection = (_config$db$connection2 = config.db.connection) == null ? void 0 : _config$db$connection2.database) != null ? _config$db$connection : config.database;
1405
1437
  this.config = _extends({}, config, {
1406
1438
  db: _extends({}, config.db, {
1407
1439
  connection: _extends({}, config.db.connection, {
1408
- database: (_config$db$connection = (_config$db$connection2 = config.db.connection) == null ? void 0 : _config$db$connection2.database) != null ? _config$db$connection : config.database
1440
+ database: database
1409
1441
  }),
1410
1442
  pool: poolConfig
1411
1443
  })
1412
1444
  });
1413
- var knexConfig = _extends({}, this.config.db, {
1445
+ this.knex = knex(_extends({}, this.config.db, {
1414
1446
  client: 'pg'
1415
- });
1447
+ }));
1448
+ if ((_config$db$pool = config.db.pool) != null && _config$db$pool.afterCreate) {
1449
+ console.log('Providing an pool configuration will stop automatic tenant context setting.');
1450
+ } else {
1451
+ var afterCreate = makeAfterCreate(config);
1452
+ poolConfig.afterCreate = afterCreate;
1453
+ }
1416
1454
  // start the timer for cleanup
1417
1455
  this.startTimeout();
1418
- this.knex = knex(knexConfig);
1419
- this.knex.on('query', function () {
1420
- _this.startTimeout();
1421
- });
1456
+ this.knex.on('query', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
1457
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1458
+ while (1) switch (_context.prev = _context.next) {
1459
+ case 0:
1460
+ _this.startTimeout();
1461
+ case 1:
1462
+ case "end":
1463
+ return _context.stop();
1464
+ }
1465
+ }, _callee);
1466
+ })));
1422
1467
  }
1423
1468
  var _proto = NileDatabase.prototype;
1424
1469
  _proto.startTimeout = function startTimeout() {
1425
- var _this2 = this,
1426
- _this$config$db$pool$;
1470
+ var _this2 = this;
1427
1471
  if (this.timer) {
1428
1472
  clearTimeout(this.timer);
1429
1473
  }
1430
- this.timer = setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
1431
- return _regeneratorRuntime().wrap(function _callee$(_context) {
1432
- while (1) switch (_context.prev = _context.next) {
1474
+ this.timer = setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
1475
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1476
+ while (1) switch (_context2.prev = _context2.next) {
1433
1477
  case 0:
1434
- _context.next = 2;
1435
- return _this2.knex.destroy();
1478
+ _context2.next = 2;
1479
+ return _this2.knex.client.pool.destroy();
1436
1480
  case 2:
1481
+ _context2.next = 4;
1482
+ return _this2.knex.destroy();
1483
+ case 4:
1437
1484
  evictPool(_this2.id);
1438
- case 3:
1485
+ case 5:
1439
1486
  case "end":
1440
- return _context.stop();
1487
+ return _context2.stop();
1441
1488
  }
1442
- }, _callee);
1443
- })), (_this$config$db$pool$ = this.config.db.pool.idleTimeoutMillis) != null ? _this$config$db$pool$ : 30000);
1489
+ }, _callee2);
1490
+ })), this.config.db.pool.idleTimeoutMillis);
1444
1491
  };
1445
1492
  return NileDatabase;
1446
1493
  }();
1494
+ function makeAfterCreate(config) {
1495
+ return function (conn, done) {
1496
+ conn.on('error', function errorHandler(error) {
1497
+ console.log('Connection was terminated by server', error);
1498
+ done(error, conn);
1499
+ });
1500
+ if (config.tenantId) {
1501
+ var query = ["SET nile.tenant_id = '" + config.tenantId + "'"];
1502
+ if (config.userId) {
1503
+ if (!config.tenantId) {
1504
+ console.warn('A user id cannot be set in context without a tenant id');
1505
+ }
1506
+ query.push("SET nile.user_id = '" + config.userId + "'");
1507
+ }
1508
+ // in this example we use pg driver's connection API
1509
+ conn.query(query.join(';'), function (err) {
1510
+ console.log('tenant id and user id set', config.userId, config.tenantId);
1511
+ done(err, conn);
1512
+ });
1513
+ }
1514
+ done(null, conn);
1515
+ };
1516
+ }
1447
1517
 
1448
1518
  var DBManager = /*#__PURE__*/function () {
1449
1519
  function DBManager(config) {
1450
1520
  var _this = this;
1521
+ this.connections = void 0;
1451
1522
  this.connections = new Map();
1452
1523
  // add the base one, so you can at least query
1453
1524
  var id = this.makeId();
@@ -1501,6 +1572,10 @@ var init = function init(config) {
1501
1572
  var Server = /*#__PURE__*/function () {
1502
1573
  function Server(config) {
1503
1574
  var _this = this;
1575
+ this.config = void 0;
1576
+ this.api = void 0;
1577
+ this.manager = void 0;
1578
+ this.servers = void 0;
1504
1579
  this.config = new Config(config);
1505
1580
  this.servers = new Map();
1506
1581
  var _init = init(this.config),
@@ -1521,10 +1596,10 @@ var Server = /*#__PURE__*/function () {
1521
1596
  _proto.setConfig = function setConfig(cfg) {
1522
1597
  this.config = new Config(cfg);
1523
1598
  };
1524
- /**
1525
- * A utility function if you want to manage different NileDB instances yourself
1526
- * returns the global Server object, an existing server that's already been configured,
1527
- * or a new one if the config isn't in the cache
1599
+ /**
1600
+ * A utility function if you want to manage different NileDB instances yourself
1601
+ * returns the global Server object, an existing server that's already been configured,
1602
+ * or a new one if the config isn't in the cache
1528
1603
  */
1529
1604
  _proto.getInstance = function getInstance(config) {
1530
1605
  var _config = _extends({}, this.config, config);
@@ -1549,7 +1624,7 @@ var Server = /*#__PURE__*/function () {
1549
1624
  this.servers.set(serverId, new Server(_config));
1550
1625
  return this.servers.get(serverId);
1551
1626
  };
1552
- _createClass(Server, [{
1627
+ return _createClass(Server, [{
1553
1628
  key: "database",
1554
1629
  set: function set(val) {
1555
1630
  if (val) {
@@ -1622,7 +1697,6 @@ var Server = /*#__PURE__*/function () {
1622
1697
  return this.manager.getConnection(this.config).knex;
1623
1698
  }
1624
1699
  }]);
1625
- return Server;
1626
1700
  }(); // export default Server;
1627
1701
  function Nile(config) {
1628
1702
  var server = new Server(config);
@@ -1632,5 +1706,5 @@ function Nile(config) {
1632
1706
 
1633
1707
  module.exports = Nile;
1634
1708
 
1635
- export default Nile;
1709
+ export { Nile as default };
1636
1710
  //# sourceMappingURL=server.esm.js.map