1
0
mirror of synced 2026-02-03 18:01:02 -05:00

Compare commits

...

1 Commits
v0.39.0 ... db

Author SHA1 Message Date
Brandon Bayer
767926b34a change db in auth example 2021-01-01 17:30:19 -05:00
6 changed files with 10 additions and 18 deletions

View File

@@ -18,7 +18,7 @@ export const verifyPassword = async (hashedPassword: string, password: string) =
}
export const authenticateUser = async (email: string, password: string) => {
const user = await db.user.findFirst({where: {email}})
const user = await db().user.findFirst({where: {email}})
if (!user || !user.hashedPassword) throw new AuthenticationError()
@@ -28,7 +28,7 @@ export const authenticateUser = async (email: string, password: string) => {
case SecurePassword.VALID_NEEDS_REHASH:
// Upgrade hashed password with a more secure hash
const improvedHash = await hashPassword(password)
await db.user.update({where: {id: user.id}, data: {hashedPassword: improvedHash}})
await db().user.update({where: {id: user.id}, data: {hashedPassword: improvedHash}})
break
default:
throw new AuthenticationError()

View File

@@ -14,7 +14,7 @@ export default async function signup(input: SignupInputType, {session}: Ctx) {
const {email, password} = SignupInput.parse(input)
const hashedPassword = await hashPassword(password)
const user = await db.user.create({
const user = await db().user.create({
data: {email, hashedPassword, role: "user"},
select: {id: true, name: true, email: true, role: true},
})

View File

@@ -4,7 +4,7 @@ import db from "db"
export default async function getCurrentUser(_ = null, ctx: Ctx) {
if (!ctx.session.userId) return null
const user = await db.user.findFirst({
const user = await db().user.findFirst({
where: {id: ctx.session.userId},
select: {id: true, name: true, email: true, role: true},
})

View File

@@ -8,7 +8,7 @@ type GetUserInput = {
export default async function getUser({where}: GetUserInput, ctx: Ctx) {
ctx.session.authorize()
const user = await db.user.findFirst({where})
const user = await db().user.findFirst({where})
if (!user) throw new NotFoundError(`User with id ${where.id} does not exist`)

View File

@@ -6,14 +6,14 @@ type GetUsersInput = Pick<FindManyUserArgs, "where" | "orderBy" | "skip" | "take
export default async function getUsers({where, orderBy, skip = 0, take}: GetUsersInput, ctx: Ctx) {
ctx.session.authorize()
const users = await db.user.findMany({
const users = await db().user.findMany({
where,
orderBy,
take,
skip,
})
const count = await db.user.count()
const count = await db().user.count()
const hasMore = typeof take === "number" ? skip + take < count : false
const nextPage = hasMore ? {take, skip: skip + take!} : null

View File

@@ -1,15 +1,7 @@
import {PrismaClient} from "@prisma/client"
export * from "@prisma/client"
let prisma: PrismaClient
if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient()
} else {
// Ensure the prisma instance is re-used during hot-reloading
// Otherwise, a new client will be created on every reload
globalThis["prisma"] = globalThis["prisma"] || new PrismaClient()
prisma = globalThis["prisma"]
export default function db(): PrismaClient {
globalThis.prisma = globalThis.prisma || new PrismaClient()
return globalThis.prisma
}
export default prisma