fix(12): WR-02 fix TypeScript type annotation for execute() count result
Drizzle mysql2 execute() returns [rows, fields] for SELECT queries; the generic type parameter alone does not correctly type the result. Use unknown cast pattern consistent with admin.ts to access the count row. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
c86cff5dad
commit
ed4e64a06a
@@ -255,11 +255,14 @@ setupRouter.post('/credential', zValidator('json', credentialSchema, noEchoHook)
|
|||||||
let localUser: typeof users.$inferSelect | undefined;
|
let localUser: typeof users.$inferSelect | undefined;
|
||||||
try {
|
try {
|
||||||
localUser = await db.transaction(async (tx) => {
|
localUser = await db.transaction(async (tx) => {
|
||||||
// Serialise: at most one unclaimed admin row may exist (WR-02)
|
// Serialise: at most one unclaimed admin row may exist (WR-02).
|
||||||
const [{ count }] = await tx.execute<{ count: number }>(
|
// Cast through unknown — Drizzle mysql2 execute() returns [rows, fields] for SELECTs;
|
||||||
|
// the generic type parameter on execute() is not sufficient to type the result correctly.
|
||||||
|
const countRows = (await tx.execute(
|
||||||
sql`SELECT COUNT(*) AS count FROM users WHERE claimed = false FOR UPDATE`,
|
sql`SELECT COUNT(*) AS count FROM users WHERE claimed = false FOR UPDATE`,
|
||||||
);
|
)) as unknown as [{ count: string | number }[], unknown];
|
||||||
if (Number(count) > 0) {
|
const unclaimedCount = Number(countRows[0][0]?.count ?? 0);
|
||||||
|
if (unclaimedCount > 0) {
|
||||||
throw Object.assign(new Error('An unclaimed user already exists'), { code: 'DUPLICATE_UNCLAIMED' });
|
throw Object.assign(new Error('An unclaimed user already exists'), { code: 'DUPLICATE_UNCLAIMED' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user