Skip to content

Commit

Permalink
Optimize firstUserShouldBeAdmin
Browse files Browse the repository at this point in the history
As count always does full table scan, while select on primary key does not.
  • Loading branch information
sverhoeven committed Mar 5, 2024
1 parent 6808840 commit 0acfd8a
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions app/models/user.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import bcryptjs from "bcryptjs";
import { count, eq, sql } from "drizzle-orm";
import { eq, sql } from "drizzle-orm";
import postgres from "postgres";

import { generatePhoto } from "./generatePhoto";
Expand Down Expand Up @@ -65,22 +65,20 @@ export async function register(email: string, password: string) {
}
}

let USER_COUNT_CACHE = 0;
let IS_FIRST_USER = true;

async function firstUserShouldBeAdmin() {
if (USER_COUNT_CACHE > 0) {
if (!IS_FIRST_USER) {
return false;
}

const result = await db
.select({
count: count(users.id),
id: users.id,
})
.from(users);
const userCount = result[0].count;
if (userCount > 0) {
USER_COUNT_CACHE = 1;
}
return userCount === 0;
.from(users).limit(1);
IS_FIRST_USER = result.length === 0;
return IS_FIRST_USER;
}

export class UserNotFoundError extends Error {
Expand Down

0 comments on commit 0acfd8a

Please sign in to comment.