Skip to content

require-sql-access-class

Keep SQL reads and writes inside a class that receives its database dependency.

Why

An injected repository class is the preferred ownership boundary for database access under this architectural policy; free functions can also express explicit dependencies.

Fix

Move the query into a repository or store class and inject the pool, connection, transaction, or typed database binding through its constructor.

Examples

Before — flagged Do not execute SQL in a free function
src/users.ts
export function find(database: Database.Database, id: string) {
return database.prepare("SELECT id FROM user WHERE id = ?").get(id);
}
After — preferred Own queries in an injected repository
src/user-repository.ts
export class UserRepository {
constructor(private readonly db: Database.Database) {}
find(id: string) {
return this.db.prepare("SELECT id FROM user WHERE id = ?").get(id);
}
}