@niledatabase/server 1.0.0-alpha.196 → 1.0.0-alpha.197
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.
- package/dist/Server.d.ts +1 -1
- package/dist/db/DBManager.d.ts +8 -0
- package/dist/db/NileInstance.d.ts +13 -0
- package/dist/db/index.d.ts +2 -14
- package/dist/server.cjs.development.js +82 -85
- package/dist/server.cjs.development.js.map +1 -1
- package/dist/server.cjs.production.min.js +1 -1
- package/dist/server.cjs.production.min.js.map +1 -1
- package/dist/server.esm.js +82 -85
- package/dist/server.esm.js.map +1 -1
- package/dist/utils/Event/index.d.ts +2 -0
- package/package.json +3 -3
package/dist/Server.d.ts
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Config } from '../utils/Config';
|
|
2
|
+
import NileDatabase, { NileDatabaseI } from './NileInstance';
|
|
3
|
+
export default class DBManager {
|
|
4
|
+
connections: Map<string, NileDatabase>;
|
|
5
|
+
private makeId;
|
|
6
|
+
constructor(config: Config);
|
|
7
|
+
getConnection(config: Config): NileDatabaseI;
|
|
8
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Knex } from 'knex';
|
|
2
|
+
import { Config } from '../utils/Config';
|
|
3
|
+
declare class NileDatabase {
|
|
4
|
+
knex: Knex;
|
|
5
|
+
tenantId?: undefined | null | string;
|
|
6
|
+
userId?: undefined | null | string;
|
|
7
|
+
id: string;
|
|
8
|
+
config: any;
|
|
9
|
+
constructor(config: Config, id: string);
|
|
10
|
+
startTimeout(): void;
|
|
11
|
+
}
|
|
12
|
+
export declare type NileDatabaseI = (table?: string) => Knex;
|
|
13
|
+
export default NileDatabase;
|
package/dist/db/index.d.ts
CHANGED
|
@@ -1,14 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
declare class NileDatabase {
|
|
4
|
-
knex: Knex;
|
|
5
|
-
db: Knex;
|
|
6
|
-
tenantId?: undefined | null | string;
|
|
7
|
-
userId?: undefined | null | string;
|
|
8
|
-
config: any;
|
|
9
|
-
constructor(config: Config);
|
|
10
|
-
ensureUpToDate(): void;
|
|
11
|
-
setConfig(newConfig: Config): void;
|
|
12
|
-
}
|
|
13
|
-
export declare type NileDatabaseI = (table?: string) => Knex;
|
|
14
|
-
export default NileDatabase;
|
|
1
|
+
export { default } from './DBManager';
|
|
2
|
+
export { NileDatabaseI } from './NileInstance';
|
|
@@ -512,6 +512,7 @@ var Events;
|
|
|
512
512
|
Events["User"] = "userId";
|
|
513
513
|
Events["Tenant"] = "tenantId";
|
|
514
514
|
Events["Token"] = "token";
|
|
515
|
+
Events["EvictPool"] = "EvictPool";
|
|
515
516
|
})(Events || (Events = {}));
|
|
516
517
|
var Eventer = /*#__PURE__*/function () {
|
|
517
518
|
function Eventer() {
|
|
@@ -560,6 +561,12 @@ var updateToken = function updateToken(val) {
|
|
|
560
561
|
var watchToken = function watchToken(cb) {
|
|
561
562
|
return eventer.subscribe(Events.Token, cb);
|
|
562
563
|
};
|
|
564
|
+
var watchEvictPool = function watchEvictPool(cb) {
|
|
565
|
+
return eventer.subscribe(Events.EvictPool, cb);
|
|
566
|
+
};
|
|
567
|
+
var evictPool = function evictPool(val) {
|
|
568
|
+
eventer.publish(Events.EvictPool, val);
|
|
569
|
+
};
|
|
563
570
|
|
|
564
571
|
var X_NILE_TENANT = 'x-nile-tenantId';
|
|
565
572
|
var X_NILE_USER_ID = 'x-nile-userId';
|
|
@@ -1364,117 +1371,119 @@ var Tenants = /*#__PURE__*/function (_Config) {
|
|
|
1364
1371
|
|
|
1365
1372
|
// doing this now, to provide flexibility later
|
|
1366
1373
|
var NileDatabase = /*#__PURE__*/function () {
|
|
1367
|
-
function NileDatabase(config) {
|
|
1368
|
-
var
|
|
1369
|
-
this.
|
|
1370
|
-
client: 'pg'
|
|
1371
|
-
});
|
|
1372
|
-
this.knex = knex(this.config);
|
|
1373
|
-
// Create a proxy to intercept method calls
|
|
1374
|
-
// @ts-expect-error - proxy, but knex
|
|
1375
|
-
this.db = new Proxy(this, {
|
|
1376
|
-
get: function get(target, method) {
|
|
1377
|
-
if (method === 'tenantId') {
|
|
1378
|
-
return _this.tenantId;
|
|
1379
|
-
}
|
|
1380
|
-
if (method === 'userId') {
|
|
1381
|
-
return _this.userId;
|
|
1382
|
-
}
|
|
1383
|
-
if (method === 'db') {
|
|
1384
|
-
return function () {
|
|
1385
|
-
var _target$knex;
|
|
1386
|
-
//@ts-expect-error - its a string
|
|
1387
|
-
return (_target$knex = target.knex).table.apply(_target$knex, arguments);
|
|
1388
|
-
};
|
|
1389
|
-
}
|
|
1390
|
-
//@ts-expect-error - its a string
|
|
1391
|
-
if (typeof target.knex[method] === 'function') {
|
|
1392
|
-
return function () {
|
|
1393
|
-
var _target$knex2;
|
|
1394
|
-
//@ts-expect-error - its a string
|
|
1395
|
-
return (_target$knex2 = target.knex)[method].apply(_target$knex2, arguments);
|
|
1396
|
-
};
|
|
1397
|
-
} else {
|
|
1398
|
-
return target.knex;
|
|
1399
|
-
}
|
|
1400
|
-
}
|
|
1401
|
-
});
|
|
1402
|
-
}
|
|
1403
|
-
var _proto = NileDatabase.prototype;
|
|
1404
|
-
_proto.ensureUpToDate = function ensureUpToDate() {
|
|
1405
|
-
// Close the existing pool connections and update the Knex instance with the latest config
|
|
1406
|
-
this.knex.destroy();
|
|
1407
|
-
this.knex = knex(_extends({}, this.config.db, {
|
|
1408
|
-
client: 'pg'
|
|
1409
|
-
}));
|
|
1410
|
-
};
|
|
1411
|
-
_proto.setConfig = function setConfig(newConfig) {
|
|
1412
|
-
var _this2 = this;
|
|
1413
|
-
var tenantId = newConfig.tenantId,
|
|
1414
|
-
userId = newConfig.userId;
|
|
1415
|
-
this.tenantId = tenantId;
|
|
1416
|
-
this.userId = userId;
|
|
1374
|
+
function NileDatabase(config, id) {
|
|
1375
|
+
var _config$db$connection;
|
|
1376
|
+
this.id = id;
|
|
1417
1377
|
var poolConfig = {};
|
|
1418
1378
|
var afterCreate = function afterCreate(conn, done) {
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
if (!_this2.tenantId) {
|
|
1379
|
+
var query = ["SET nile.tenant_id = '" + config.tenantId + "'"];
|
|
1380
|
+
if (config.userId) {
|
|
1381
|
+
if (!config.tenantId) {
|
|
1423
1382
|
// eslint-disable-next-line no-console
|
|
1424
1383
|
console.warn('A user id cannot be set in context without a tenant id');
|
|
1425
1384
|
}
|
|
1426
|
-
query.push("SET nile.user_id = '" +
|
|
1385
|
+
query.push("SET nile.user_id = '" + config.userId + "'");
|
|
1427
1386
|
}
|
|
1428
1387
|
// in this example we use pg driver's connection API
|
|
1429
1388
|
conn.query(query.join(';'), function (err) {
|
|
1430
1389
|
done(err, conn);
|
|
1431
1390
|
});
|
|
1432
1391
|
};
|
|
1433
|
-
if (
|
|
1434
|
-
var
|
|
1435
|
-
if ((
|
|
1392
|
+
if (config.tenantId) {
|
|
1393
|
+
var _config$db$pool;
|
|
1394
|
+
if ((_config$db$pool = config.db.pool) != null && _config$db$pool.afterCreate) {
|
|
1436
1395
|
// eslint-disable-next-line no-console
|
|
1437
|
-
console.log('Providing an
|
|
1438
|
-
} else if (
|
|
1439
|
-
poolConfig = _extends({},
|
|
1396
|
+
console.log('Providing an pool configuration will stop automatic tenant context setting.');
|
|
1397
|
+
} else if (config.db.pool) {
|
|
1398
|
+
poolConfig = _extends({}, config.db.pool, {
|
|
1440
1399
|
afterCreate: afterCreate
|
|
1441
1400
|
});
|
|
1442
|
-
} else if (!
|
|
1401
|
+
} else if (!config.db.pool) {
|
|
1443
1402
|
poolConfig = {
|
|
1444
1403
|
afterCreate: afterCreate
|
|
1445
1404
|
};
|
|
1446
1405
|
}
|
|
1447
1406
|
}
|
|
1448
|
-
this.config = _extends({},
|
|
1449
|
-
db: _extends({},
|
|
1407
|
+
this.config = _extends({}, config, {
|
|
1408
|
+
db: _extends({}, config.db, {
|
|
1409
|
+
connection: _extends({}, config.db.connection, {
|
|
1410
|
+
database: (_config$db$connection = config.db.connection.database) != null ? _config$db$connection : config.database
|
|
1411
|
+
}),
|
|
1450
1412
|
pool: poolConfig
|
|
1451
1413
|
})
|
|
1452
1414
|
});
|
|
1453
|
-
this.
|
|
1415
|
+
var knexConfig = _extends({}, this.config.db, {
|
|
1416
|
+
client: 'pg'
|
|
1417
|
+
});
|
|
1418
|
+
// start the timer for cleanup
|
|
1419
|
+
this.startTimeout();
|
|
1420
|
+
this.knex = knex(knexConfig);
|
|
1421
|
+
}
|
|
1422
|
+
var _proto = NileDatabase.prototype;
|
|
1423
|
+
_proto.startTimeout = function startTimeout() {
|
|
1424
|
+
var _this = this,
|
|
1425
|
+
_this$config$db$pool$;
|
|
1426
|
+
setTimeout(function () {
|
|
1427
|
+
_this.knex.destroy();
|
|
1428
|
+
evictPool(_this.id);
|
|
1429
|
+
}, (_this$config$db$pool$ = this.config.db.pool.idleTimeoutMillis) != null ? _this$config$db$pool$ : 30000);
|
|
1454
1430
|
};
|
|
1455
1431
|
return NileDatabase;
|
|
1456
1432
|
}();
|
|
1457
1433
|
|
|
1434
|
+
var DBManager = /*#__PURE__*/function () {
|
|
1435
|
+
function DBManager(config) {
|
|
1436
|
+
var _this = this;
|
|
1437
|
+
this.connections = new Map();
|
|
1438
|
+
// add the base one, so you can at least query
|
|
1439
|
+
var id = this.makeId();
|
|
1440
|
+
this.connections.set(id, new NileDatabase(new Config(config), id));
|
|
1441
|
+
watchEvictPool(function (id) {
|
|
1442
|
+
if (id && _this.connections.has(id)) {
|
|
1443
|
+
_this.connections["delete"](id);
|
|
1444
|
+
}
|
|
1445
|
+
});
|
|
1446
|
+
}
|
|
1447
|
+
var _proto = DBManager.prototype;
|
|
1448
|
+
_proto.makeId = function makeId(tenantId, userId) {
|
|
1449
|
+
if (tenantId && userId) {
|
|
1450
|
+
return tenantId + ":" + userId;
|
|
1451
|
+
}
|
|
1452
|
+
if (tenantId) {
|
|
1453
|
+
return "" + tenantId;
|
|
1454
|
+
}
|
|
1455
|
+
return 'base';
|
|
1456
|
+
};
|
|
1457
|
+
_proto.getConnection = function getConnection(config) {
|
|
1458
|
+
var id = this.makeId(config.tenantId, config.userId);
|
|
1459
|
+
var existing = this.connections.get(id);
|
|
1460
|
+
if (existing) {
|
|
1461
|
+
return existing;
|
|
1462
|
+
}
|
|
1463
|
+
this.connections.set(id, new NileDatabase(new Config(config), id));
|
|
1464
|
+
return this.connections.get(id);
|
|
1465
|
+
};
|
|
1466
|
+
return DBManager;
|
|
1467
|
+
}();
|
|
1468
|
+
|
|
1458
1469
|
var init = function init(config) {
|
|
1459
1470
|
var auth = new Auth(config);
|
|
1460
1471
|
var users = new Users(config);
|
|
1461
1472
|
var tenants = new Tenants(config);
|
|
1462
|
-
var db = new NileDatabase(config);
|
|
1463
1473
|
return [{
|
|
1464
1474
|
auth: auth,
|
|
1465
1475
|
users: users,
|
|
1466
1476
|
tenants: tenants
|
|
1467
|
-
}
|
|
1477
|
+
}];
|
|
1468
1478
|
};
|
|
1469
1479
|
var Server = /*#__PURE__*/function () {
|
|
1470
1480
|
function Server(config) {
|
|
1471
1481
|
var _this = this;
|
|
1472
1482
|
this.config = new Config(config);
|
|
1473
1483
|
var _init = init(this.config),
|
|
1474
|
-
api = _init[0]
|
|
1475
|
-
knex = _init[1];
|
|
1484
|
+
api = _init[0];
|
|
1476
1485
|
this.api = api;
|
|
1477
|
-
this.
|
|
1486
|
+
this.manager = new DBManager(this.config);
|
|
1478
1487
|
watchTenantId(function (tenantId) {
|
|
1479
1488
|
_this.tenantId = tenantId;
|
|
1480
1489
|
});
|
|
@@ -1487,15 +1496,7 @@ var Server = /*#__PURE__*/function () {
|
|
|
1487
1496
|
}
|
|
1488
1497
|
var _proto = Server.prototype;
|
|
1489
1498
|
_proto.setConfig = function setConfig(cfg) {
|
|
1490
|
-
|
|
1491
|
-
this.config.db.connection = cfg.db.connection;
|
|
1492
|
-
}
|
|
1493
|
-
if (cfg.database && this.database !== cfg.workspace) {
|
|
1494
|
-
this.database = cfg.database;
|
|
1495
|
-
}
|
|
1496
|
-
if (cfg.workspace && this.workspace !== cfg.workspace) {
|
|
1497
|
-
this.workspace = cfg.workspace;
|
|
1498
|
-
}
|
|
1499
|
+
this.config = new Config(cfg);
|
|
1499
1500
|
};
|
|
1500
1501
|
_createClass(Server, [{
|
|
1501
1502
|
key: "database",
|
|
@@ -1526,8 +1527,6 @@ var Server = /*#__PURE__*/function () {
|
|
|
1526
1527
|
set: function set(userId) {
|
|
1527
1528
|
this.database = this.config.database;
|
|
1528
1529
|
this.config.userId = userId;
|
|
1529
|
-
// update the db with config values
|
|
1530
|
-
this._db.setConfig(this.config);
|
|
1531
1530
|
if (this.api) {
|
|
1532
1531
|
this.api.auth.userId = this.config.userId;
|
|
1533
1532
|
this.api.users.userId = this.config.userId;
|
|
@@ -1542,8 +1541,6 @@ var Server = /*#__PURE__*/function () {
|
|
|
1542
1541
|
set: function set(tenantId) {
|
|
1543
1542
|
this.database = this.config.database;
|
|
1544
1543
|
this.config.tenantId = tenantId;
|
|
1545
|
-
// update the db with config values
|
|
1546
|
-
this._db.setConfig(this.config);
|
|
1547
1544
|
if (this.api) {
|
|
1548
1545
|
this.api.auth.tenantId = tenantId;
|
|
1549
1546
|
this.api.users.tenantId = tenantId;
|
|
@@ -1571,13 +1568,13 @@ var Server = /*#__PURE__*/function () {
|
|
|
1571
1568
|
get: function get() {
|
|
1572
1569
|
// only need to interact with the knex object
|
|
1573
1570
|
//@ts-expect-error - because that's where it is in the proxy
|
|
1574
|
-
return this.
|
|
1571
|
+
return this.manager.getConnection(this.config).knex;
|
|
1575
1572
|
}
|
|
1576
1573
|
}]);
|
|
1577
1574
|
return Server;
|
|
1578
1575
|
}(); // export default Server;
|
|
1579
1576
|
function Nile(config) {
|
|
1580
|
-
var server = new Server();
|
|
1577
|
+
var server = new Server(config);
|
|
1581
1578
|
server.setConfig(new Config(config));
|
|
1582
1579
|
return server;
|
|
1583
1580
|
}
|