generated from remix-run/indie-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80f5bfc
commit ed0e62d
Showing
7 changed files
with
68 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,33 @@ | ||
import type { User, Note } from "@prisma/client"; | ||
import { eq } from "drizzle-orm"; | ||
|
||
import { prisma } from "~/db.server"; | ||
import { db } from "drizzle/config"; | ||
import { note } from "drizzle/schema"; | ||
import { dbDate } from "~/utils"; | ||
|
||
export function getNote({ | ||
id, | ||
userId, | ||
}: Pick<Note, "id"> & { | ||
userId: User["id"]; | ||
}) { | ||
return prisma.note.findFirst({ | ||
select: { id: true, body: true, title: true }, | ||
where: { id, userId }, | ||
}); | ||
export async function getNote({ id }: { id: string }) { | ||
const [note] = await db.select().from(note).where(eq(note.id, id)).limit(1); | ||
Check failure on line 8 in app/models/note.server.ts GitHub Actions / ʦ TypeScript
Check failure on line 8 in app/models/note.server.ts GitHub Actions / ʦ TypeScript
|
||
return note; | ||
} | ||
|
||
export function getNoteListItems({ userId }: { userId: User["id"] }) { | ||
return prisma.note.findMany({ | ||
where: { userId }, | ||
select: { id: true, title: true }, | ||
orderBy: { updatedAt: "desc" }, | ||
}); | ||
export function getNoteListItems() { | ||
return db | ||
.select({ id: note.id, title: note.title }) | ||
.from(note) | ||
.orderBy(note.createdAt); | ||
} | ||
|
||
export function createNote({ | ||
body, | ||
title, | ||
userId, | ||
}: Pick<Note, "body" | "title"> & { | ||
userId: User["id"]; | ||
}: { | ||
body: string; | ||
title: string; | ||
userId: string; | ||
}) { | ||
return prisma.note.create({ | ||
data: { | ||
title, | ||
body, | ||
user: { | ||
connect: { | ||
id: userId, | ||
}, | ||
}, | ||
}, | ||
}); | ||
return db.insert(note).values([{ body, title, userId, updatedAt: dbDate() }]); | ||
} | ||
|
||
export function deleteNote({ | ||
id, | ||
userId, | ||
}: Pick<Note, "id"> & { userId: User["id"] }) { | ||
return prisma.note.deleteMany({ | ||
where: { id, userId }, | ||
}); | ||
export function deleteNote({ id }: { id: string }) { | ||
return db.delete(note).where(eq(note.id, id)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,63 @@ | ||
import type { Password, User } from "@prisma/client"; | ||
import bcrypt from "bcryptjs"; | ||
import { eq } from "drizzle-orm"; | ||
|
||
import { prisma } from "~/db.server"; | ||
import { db } from "drizzle/config"; | ||
import { password, user } from "drizzle/schema"; | ||
|
||
export type { User } from "@prisma/client"; | ||
|
||
export async function getUserById(id: User["id"]) { | ||
return prisma.user.findUnique({ where: { id } }); | ||
export async function getUserById(id: string) { | ||
const [_user] = await db.select().from(user).where(eq(user.id, id)).limit(1); | ||
return _user; | ||
} | ||
|
||
export async function getUserByEmail(email: User["email"]) { | ||
return prisma.user.findUnique({ where: { email } }); | ||
export async function getUserByEmail(email: string) { | ||
const [_user] = await db.select().from(user).where(eq(user.email, email)); | ||
return _user; | ||
} | ||
|
||
export async function createUser(email: User["email"], password: string) { | ||
const hashedPassword = await bcrypt.hash(password, 10); | ||
export async function createUser(email: string, _password: string) { | ||
const hashedPassword = await bcrypt.hash(_password, 10); | ||
|
||
return prisma.user.create({ | ||
data: { | ||
email, | ||
password: { | ||
create: { | ||
hash: hashedPassword, | ||
return await db.transaction(async (tx) => { | ||
const [createdUser] = await tx | ||
.insert(user) | ||
.values([ | ||
{ | ||
email, | ||
updatedAt: new Date().toLocaleDateString(), | ||
}, | ||
}, | ||
}, | ||
]) | ||
.returning({ id: user.id, email: user.email }); | ||
await tx.insert(password).values({ | ||
userId: createdUser.id, | ||
hash: hashedPassword, | ||
}); | ||
return createdUser; | ||
}); | ||
} | ||
|
||
export async function deleteUserByEmail(email: User["email"]) { | ||
return prisma.user.delete({ where: { email } }); | ||
export async function deleteUserByEmail(email: string) { | ||
return await db.delete(user).where(eq(user.email, email)); | ||
} | ||
|
||
export async function verifyLogin( | ||
email: User["email"], | ||
password: Password["hash"], | ||
) { | ||
const userWithPassword = await prisma.user.findUnique({ | ||
where: { email }, | ||
include: { | ||
password: true, | ||
}, | ||
}); | ||
export async function verifyLogin(email: string, _password: string) { | ||
const [userWithPassword] = await db | ||
.select() | ||
.from(user) | ||
.leftJoin(password, eq(password.userId, user.id)) | ||
.where(eq(user.email, email)); | ||
|
||
if (!userWithPassword || !userWithPassword.password) { | ||
if (!userWithPassword || !userWithPassword.Password) { | ||
return null; | ||
} | ||
|
||
const isValid = await bcrypt.compare( | ||
password, | ||
userWithPassword.password.hash, | ||
_password, | ||
userWithPassword.Password.hash, | ||
); | ||
|
||
if (!isValid) { | ||
return null; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { password: _password, ...userWithoutPassword } = userWithPassword; | ||
|
||
return userWithoutPassword; | ||
return userWithPassword.User; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters