@niledatabase/server 5.0.0-alpha.32 → 5.0.0-alpha.33
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/index.d.mts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +52 -34
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +52 -34
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -2110,17 +2110,17 @@ var updateHeaders = (val) => {
|
|
|
2110
2110
|
var watchHeaders = (cb) => eventer.subscribe("headers" /* Headers */, cb);
|
|
2111
2111
|
|
|
2112
2112
|
// src/db/PoolProxy.ts
|
|
2113
|
-
function createProxyForPool(pool, config) {
|
|
2114
|
-
const { info, error } =
|
|
2113
|
+
function createProxyForPool(pool, config, logger, context) {
|
|
2114
|
+
const { info, error } = logger("[pool]");
|
|
2115
2115
|
return new Proxy(pool, {
|
|
2116
2116
|
get(target, property) {
|
|
2117
2117
|
if (property === "query") {
|
|
2118
|
-
if (!config.
|
|
2119
|
-
if (!config.
|
|
2118
|
+
if (!config.connectionString) {
|
|
2119
|
+
if (!config.user || !config.password) {
|
|
2120
2120
|
error(
|
|
2121
2121
|
"Cannot connect to the database. User and/or password are missing. Generate them at https://console.thenile.dev"
|
|
2122
2122
|
);
|
|
2123
|
-
} else if (!config.
|
|
2123
|
+
} else if (!config.database) {
|
|
2124
2124
|
error(
|
|
2125
2125
|
"Unable to obtain database name. Is process.env.NILEDB_POSTGRES_URL set?"
|
|
2126
2126
|
);
|
|
@@ -2129,7 +2129,7 @@ function createProxyForPool(pool, config) {
|
|
|
2129
2129
|
const caller = target[property];
|
|
2130
2130
|
return function query(...args) {
|
|
2131
2131
|
let log = "[QUERY]";
|
|
2132
|
-
const
|
|
2132
|
+
const [tenantId, userId] = context;
|
|
2133
2133
|
if (tenantId) {
|
|
2134
2134
|
log = `${log}[TENANT:${tenantId}]`;
|
|
2135
2135
|
}
|
|
@@ -2152,39 +2152,44 @@ var NileDatabase = class {
|
|
|
2152
2152
|
tenantId;
|
|
2153
2153
|
userId;
|
|
2154
2154
|
id;
|
|
2155
|
-
|
|
2155
|
+
logger;
|
|
2156
2156
|
timer;
|
|
2157
|
-
|
|
2158
|
-
|
|
2157
|
+
config;
|
|
2158
|
+
constructor(config, logger, id) {
|
|
2159
|
+
this.logger = logger("[NileInstance]");
|
|
2159
2160
|
this.id = id;
|
|
2160
2161
|
const poolConfig = {
|
|
2161
2162
|
min: 0,
|
|
2162
2163
|
max: 10,
|
|
2163
2164
|
idleTimeoutMillis: 3e4,
|
|
2164
|
-
...config
|
|
2165
|
+
...config
|
|
2165
2166
|
};
|
|
2166
2167
|
const { afterCreate, ...remaining } = poolConfig;
|
|
2167
|
-
config
|
|
2168
|
-
|
|
2169
|
-
const cloned = { ...this.config.db };
|
|
2168
|
+
this.config = remaining;
|
|
2169
|
+
const cloned = { ...config };
|
|
2170
2170
|
cloned.password = "***";
|
|
2171
|
-
debug(`Connection pool config ${JSON.stringify(cloned)}`);
|
|
2172
|
-
this.pool = createProxyForPool(
|
|
2171
|
+
this.logger.debug(`Connection pool config ${JSON.stringify(cloned)}`);
|
|
2172
|
+
this.pool = createProxyForPool(
|
|
2173
|
+
new pg.Pool(remaining),
|
|
2174
|
+
this.config,
|
|
2175
|
+
logger,
|
|
2176
|
+
id === "base" ? [] : id.split(":")
|
|
2177
|
+
);
|
|
2173
2178
|
if (typeof afterCreate === "function") {
|
|
2174
|
-
|
|
2179
|
+
this.logger.warn(
|
|
2175
2180
|
"Providing an pool configuration will stop automatic tenant context setting."
|
|
2176
2181
|
);
|
|
2177
2182
|
}
|
|
2178
2183
|
this.startTimeout();
|
|
2179
2184
|
this.pool.on("connect", async (client) => {
|
|
2180
|
-
debug(`pool connected ${this.id}`);
|
|
2185
|
+
this.logger.debug(`pool connected ${this.id}`);
|
|
2181
2186
|
this.startTimeout();
|
|
2182
2187
|
const afterCreate2 = makeAfterCreate(
|
|
2183
|
-
|
|
2184
|
-
`${this.id}
|
|
2188
|
+
logger,
|
|
2189
|
+
`${this.id}|${this.timer}`
|
|
2185
2190
|
);
|
|
2186
2191
|
afterCreate2(client, (err) => {
|
|
2187
|
-
const { error } =
|
|
2192
|
+
const { error } = logger("[after create callback]");
|
|
2188
2193
|
if (err) {
|
|
2189
2194
|
clearTimeout(this.timer);
|
|
2190
2195
|
error("after create failed", {
|
|
@@ -2197,7 +2202,7 @@ var NileDatabase = class {
|
|
|
2197
2202
|
});
|
|
2198
2203
|
this.pool.on("error", (err) => {
|
|
2199
2204
|
clearTimeout(this.timer);
|
|
2200
|
-
info(`pool ${this.id} failed`, {
|
|
2205
|
+
this.logger.info(`pool ${this.id} failed`, {
|
|
2201
2206
|
message: err.message,
|
|
2202
2207
|
stack: err.stack
|
|
2203
2208
|
});
|
|
@@ -2207,27 +2212,27 @@ var NileDatabase = class {
|
|
|
2207
2212
|
if (destroy) {
|
|
2208
2213
|
clearTimeout(this.timer);
|
|
2209
2214
|
evictPool(this.id);
|
|
2210
|
-
debug(`destroying pool ${this.id}`);
|
|
2215
|
+
this.logger.debug(`destroying pool ${this.id}`);
|
|
2211
2216
|
}
|
|
2212
2217
|
});
|
|
2213
2218
|
}
|
|
2214
2219
|
startTimeout() {
|
|
2215
|
-
const { debug } = this.
|
|
2220
|
+
const { debug } = this.logger;
|
|
2216
2221
|
if (this.timer) {
|
|
2217
2222
|
clearTimeout(this.timer);
|
|
2218
2223
|
}
|
|
2219
2224
|
this.timer = setTimeout(() => {
|
|
2220
2225
|
debug(
|
|
2221
|
-
`Pool reached idleTimeoutMillis. ${this.id} evicted after ${Number(this.config.
|
|
2226
|
+
`Pool reached idleTimeoutMillis. ${this.id} evicted after ${Number(this.config.idleTimeoutMillis) ?? 3e4}ms`
|
|
2222
2227
|
);
|
|
2223
2228
|
this.pool.end(() => {
|
|
2224
2229
|
clearTimeout(this.timer);
|
|
2225
2230
|
evictPool(this.id);
|
|
2226
2231
|
});
|
|
2227
|
-
}, Number(this.config.
|
|
2232
|
+
}, Number(this.config.idleTimeoutMillis) ?? 3e4);
|
|
2228
2233
|
}
|
|
2229
2234
|
shutdown() {
|
|
2230
|
-
const { debug } = this.
|
|
2235
|
+
const { debug } = this.logger;
|
|
2231
2236
|
debug(`attempting to shut down ${this.id}`);
|
|
2232
2237
|
clearTimeout(this.timer);
|
|
2233
2238
|
this.pool.end(() => {
|
|
@@ -2236,8 +2241,8 @@ var NileDatabase = class {
|
|
|
2236
2241
|
}
|
|
2237
2242
|
};
|
|
2238
2243
|
var NileInstance_default = NileDatabase;
|
|
2239
|
-
function makeAfterCreate(
|
|
2240
|
-
const { error, warn: warn2, debug } =
|
|
2244
|
+
function makeAfterCreate(logger, id) {
|
|
2245
|
+
const { error, warn: warn2, debug } = logger("[afterCreate]");
|
|
2241
2246
|
return (conn, done) => {
|
|
2242
2247
|
conn.on("error", function errorHandler(e) {
|
|
2243
2248
|
error(`Connection ${id} was terminated by server`, {
|
|
@@ -2246,8 +2251,9 @@ function makeAfterCreate(config, id) {
|
|
|
2246
2251
|
});
|
|
2247
2252
|
done(e, conn);
|
|
2248
2253
|
});
|
|
2249
|
-
const
|
|
2250
|
-
|
|
2254
|
+
const [context] = id.split("|");
|
|
2255
|
+
const [tenantId, userId] = context.split(":");
|
|
2256
|
+
if (tenantId !== "base") {
|
|
2251
2257
|
const query = [`SET nile.tenant_id = '${tenantId}'`];
|
|
2252
2258
|
if (userId) {
|
|
2253
2259
|
if (!tenantId) {
|
|
@@ -2312,9 +2318,9 @@ var DBManager = class {
|
|
|
2312
2318
|
warn2(`missed eviction of ${id}`);
|
|
2313
2319
|
}
|
|
2314
2320
|
};
|
|
2315
|
-
getConnection = (config) => {
|
|
2321
|
+
getConnection = (config, noContext = false) => {
|
|
2316
2322
|
const { info } = Logger(config)("[DBManager]");
|
|
2317
|
-
const { tenantId, userId } = ctx.getLastUsed();
|
|
2323
|
+
const { tenantId, userId } = noContext ? {} : ctx.getLastUsed();
|
|
2318
2324
|
const id = this.makeId(tenantId, userId);
|
|
2319
2325
|
const existing = this.connections.get(id);
|
|
2320
2326
|
info(`# of instances: ${this.connections.size}`);
|
|
@@ -2323,7 +2329,7 @@ var DBManager = class {
|
|
|
2323
2329
|
existing.startTimeout();
|
|
2324
2330
|
return existing.pool;
|
|
2325
2331
|
}
|
|
2326
|
-
const newOne = new NileInstance_default(config, id);
|
|
2332
|
+
const newOne = new NileInstance_default(config.db, config.logger, id);
|
|
2327
2333
|
this.connections.set(id, newOne);
|
|
2328
2334
|
info(`created new ${id}`);
|
|
2329
2335
|
info(`# of instances: ${this.connections.size}`);
|
|
@@ -3471,9 +3477,21 @@ var Server = class {
|
|
|
3471
3477
|
}
|
|
3472
3478
|
}
|
|
3473
3479
|
}
|
|
3474
|
-
|
|
3480
|
+
/**
|
|
3481
|
+
* Query the database with the current context
|
|
3482
|
+
*/
|
|
3483
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3484
|
+
query = (queryStream, values) => {
|
|
3475
3485
|
this.#config.context = { ...this.getContext() };
|
|
3476
3486
|
const pool = this.#manager.getConnection(this.#config);
|
|
3487
|
+
return pool.query(queryStream, values);
|
|
3488
|
+
};
|
|
3489
|
+
/**
|
|
3490
|
+
* Return a db object that can be used to talk to the database
|
|
3491
|
+
* Does not have a context by default
|
|
3492
|
+
*/
|
|
3493
|
+
get db() {
|
|
3494
|
+
const pool = this.#manager.getConnection(this.#config, true);
|
|
3477
3495
|
return Object.assign(pool, {
|
|
3478
3496
|
clearConnections: () => {
|
|
3479
3497
|
this.#manager.clear(this.#config);
|