初始提交:DSH 多租户平台(dshs)

This commit is contained in:
admin committed 2026-09-13 16:18:10 +08:00
commit 43976fea6a
167 files changed
+24456

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
/**
* Prepared-statement cache. better-sqlite3 does not cache `db.prepare`, so
* re-preparing the same SQL on every call re-parses it. This memoizes prepared
* statements per connection (WeakMap), which matters for hot paths such as
* authentication and the reverse proxy.
* @module dshs/db/prepared
*/
import type Database from 'better-sqlite3'
import type { Database as Db } from './connection.js'
type Statement = Database.Statement<unknown[], unknown>
const caches = new WeakMap<Db, Map<string, Statement>>()
/** Prepare (and cache) a statement for a connection. */
export function prepare(db: Db, sql: string): Statement {
let cache = caches.get(db)
if (cache === undefined) {
cache = new Map()
caches.set(db, cache)
}
let statement = cache.get(sql)
if (statement === undefined) {
statement = db.prepare(sql)
cache.set(sql, statement)
}
return statement
}