better-auth/packages/database/prisma/queries/users.ts
 Илья Шалонин 8538ab969a first
2025-10-29 13:43:43 +05:00

113 lines
1.7 KiB
TypeScript

import type { z } from "zod";
import { db } from "../client";
import type { UserUncheckedUpdateInputSchema } from "../zod";
export async function getUsers({
limit,
offset,
query,
}: {
limit: number;
offset: number;
query?: string;
}) {
return await db.user.findMany({
where: query
? {
name: {
contains: query,
},
}
: undefined,
take: limit,
skip: offset,
});
}
export async function countAllUsers() {
return await db.user.count();
}
export async function getUserById(id: string) {
return await db.user.findUnique({
where: {
id,
},
});
}
export async function getUserByEmail(email: string) {
return await db.user.findUnique({
where: {
email,
},
});
}
export async function createUser({
email,
name,
role,
emailVerified,
onboardingComplete,
}: {
email: string;
name: string;
role: "admin" | "user";
emailVerified: boolean;
onboardingComplete: boolean;
}) {
return await db.user.create({
data: {
email,
name,
role,
emailVerified,
onboardingComplete,
createdAt: new Date(),
updatedAt: new Date(),
},
});
}
export async function getAccountById(id: string) {
return await db.account.findUnique({
where: {
id,
},
});
}
export async function createUserAccount({
userId,
providerId,
accountId,
hashedPassword,
}: {
userId: string;
providerId: string;
accountId: string;
hashedPassword?: string;
}) {
return await db.account.create({
data: {
userId,
accountId,
providerId,
password: hashedPassword,
createdAt: new Date(),
updatedAt: new Date(),
},
});
}
export async function updateUser(
user: z.infer<typeof UserUncheckedUpdateInputSchema> & { id: string },
) {
return await db.user.update({
where: {
id: user.id,
},
data: user,
});
}