This commit is contained in:
@@ -645,21 +645,15 @@ export function db(): Database.Database {
|
||||
throw err;
|
||||
}
|
||||
|
||||
// Cache only runtime queries, after migrations have finished. Statements
|
||||
// belong to this connection, so reopening the DB starts with an empty cache.
|
||||
// Callers share statement options (e.g. raw()); use consistent options for
|
||||
// identical SQL and pass values to run/get/all rather than binding permanently.
|
||||
const statements = new Map<string, Database.Statement>();
|
||||
// Instrument runtime queries after migrations. Each prepare must return a
|
||||
// fresh statement: modes, permanent bindings, and active iterators are mutable
|
||||
// state owned by the caller, even when another caller uses identical SQL.
|
||||
const slowMs = Number(process.env.EC_SLOW_SQL_MS ?? 0);
|
||||
const prepare = handle.prepare.bind(handle);
|
||||
handle.prepare = ((sql: string) => {
|
||||
let statement = statements.get(sql);
|
||||
if (!statement) {
|
||||
statement = timeStatement(prepare(sql), sql, slowMs);
|
||||
statements.set(sql, statement);
|
||||
}
|
||||
return statement;
|
||||
}) as typeof handle.prepare;
|
||||
if (Number.isFinite(slowMs) && slowMs > 0) {
|
||||
const prepare = handle.prepare.bind(handle);
|
||||
handle.prepare = ((sql: string) =>
|
||||
timeStatement(prepare(sql), sql, slowMs)) as typeof handle.prepare;
|
||||
}
|
||||
|
||||
_db = handle;
|
||||
optimizeTimer = setInterval(() => {
|
||||
|
||||
Reference in New Issue
Block a user